diff --git a/src/util/data.js b/src/util/data.js index c0ab0574..abc53ce0 100644 --- a/src/util/data.js +++ b/src/util/data.js @@ -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); +}; diff --git a/src/util/data.test.js b/src/util/data.test.js index f19e5e73..189817fa 100644 --- a/src/util/data.test.js +++ b/src/util/data.test.js @@ -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); + }); +});