diff --git a/docs/formatToMonogram.md b/docs/formatToMonogram.md new file mode 100644 index 0000000..d8bb33c --- /dev/null +++ b/docs/formatToMonogram.md @@ -0,0 +1,22 @@ +# `formatToMonogram` + +Removes all non-numeric characters from a string + +## Arguments + +- `value: String`: value to be format + +## Returns + +- `string: String`: A common string. If not exist number the return default is an empty + +## Usage + +```jsx +import { formatToMonogram } from '@platformbuilders/helpers'; + +formatToMonogram('John Doe'); // return 'JD' +formatToMonogram('John Doe Due'); // return 'JDD' +formatToMonogram('John'); // return 'J' +formatToMonogram(''); // return '' +``` diff --git a/package.json b/package.json index 3bb3e6e..c3a2278 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@platformbuilders/helpers", - "version": "0.8.1", + "version": "0.8.2", "description": "Builders helpers library", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/__tests__/formatToMonogram.spec.ts b/src/__tests__/formatToMonogram.spec.ts new file mode 100644 index 0000000..f0dde80 --- /dev/null +++ b/src/__tests__/formatToMonogram.spec.ts @@ -0,0 +1,29 @@ +import { formatToMonogram } from '../shared/formatToMonogram'; + +describe('formatToMonogram tests', () => { + it('should format to monogram', () => { + const unformatted = 'John Doe'; + const formatted = formatToMonogram(unformatted); + expect(formatted).toBe('JD'); + }); + it('should format to monogram with 3 words', () => { + const unformatted = 'John Doe Due'; + const formatted = formatToMonogram(unformatted); + expect(formatted).toBe('JDD'); + }); + it('should format to monogram when there is only one word', () => { + const unformatted = 'John'; + const formatted = formatToMonogram(unformatted); + expect(formatted).toBe('J'); + }); + it('should format to monogram when there is no word', () => { + const unformatted = ''; + const formatted = formatToMonogram(unformatted); + expect(formatted).toBe(''); + }); + it('should format to monogram when there is no word', () => { + const unformatted = ''; + const formatted = formatToMonogram(unformatted); + expect(formatted).toBe(''); + }); +}); diff --git a/src/shared/formatToMonogram.ts b/src/shared/formatToMonogram.ts new file mode 100644 index 0000000..cffa7d9 --- /dev/null +++ b/src/shared/formatToMonogram.ts @@ -0,0 +1,22 @@ +/** + * Format string to monogram + * @param value The string to be formatted + * @returns The string formatted to monogram + * @example + * formatToMonogram('John') // J + * formatToMonogram('John Doe') // JD + * formatToMonogram('John Doe Due') // JDD + * formatToMonogram('') // '' + * formatToMonogram() // '' + * + */ +export const formatToMonogram = (value: string): string => { + if (!value) return ''; + + return value + .trim() + .split(' ') + .map((word) => word[0]) + .join('') + .toUpperCase(); +}; diff --git a/src/shared/index.ts b/src/shared/index.ts index 95f257d..b8d8c97 100644 --- a/src/shared/index.ts +++ b/src/shared/index.ts @@ -7,4 +7,5 @@ export * from './currencyToNumber'; export * from './addMaskToCardNumber'; export * from './formatCardNumber'; export * from './sanitizer'; +export * from './formatToMonogram'; export * from './formatToPhone';