Add a function that humanizes line item codes

This commit is contained in:
Hannu Lyytikainen 2019-04-03 10:26:38 +03:00
parent d8c614c911
commit a15d3d15ab
2 changed files with 38 additions and 1 deletions

View file

@ -277,7 +277,7 @@ export const userDisplayNameAsString = (user, defaultUserDisplayName) => {
*/
export const userDisplayName = (user, bannedUserDisplayName) => {
console.warn(
`Function userDisplayName is deprecated!
`Function userDisplayName is deprecated!
User function userDisplayNameAsString or component UserDisplayName instead.`
);
@ -333,3 +333,21 @@ export const overrideArrays = (objValue, srcValue, key, object, source, stack) =
return srcValue;
}
};
/**
* Humanizes a line item code. Strips the "line-item/" namespace
* definition from the beginnign, replaces dashes with spaces and
* capitalizes the first character.
*
* @param {string} code a line item code
*
* @return {string} returns the line item code humanized
*/
export const humanizeLineItemCode = code => {
if (!/^line-item\/.+/.test(code)) {
throw new Error(`Invalid line item code: ${code}`);
}
const lowercase = code.replace(/^line-item\//, '').replace(/-/g, ' ');
return lowercase.charAt(0).toUpperCase() + lowercase.slice(1);
};

View file

@ -6,6 +6,7 @@ import {
denormalisedEntities,
arrayToFormValues,
formValuesToArray,
humanizeLineItemCode,
} from './data';
const { UUID } = sdkTypes;
@ -329,3 +330,21 @@ describe('data utils', () => {
});
});
});
describe('humanizeLineItemCode', () => {
it('should humanize a line item code', () => {
expect(humanizeLineItemCode('line-item/new-line-item')).toEqual('New line item');
});
it('should capitalize a one word code', () => {
expect(humanizeLineItemCode('line-item/booking')).toEqual('Booking');
});
it('should reject a code with missing namespace', () => {
expect(() => humanizeLineItemCode('new-line-item')).toThrowError(Error);
});
it('should reject a code with missing code value', () => {
expect(() => humanizeLineItemCode('line-item/')).toThrowError(Error);
});
});