Skip to content

Commit

Permalink
Feat/format monogram (#396)
Browse files Browse the repository at this point in the history
* feat(format-phone): adding `formatToPhone` helper, creating tests

* feat(format-phone): updating package details

* feat(format-monogram): adding `formatToMonogram` helper
  • Loading branch information
ammichael authored Jun 10, 2023
1 parent 3e8c96b commit b4adfdf
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 1 deletion.
22 changes: 22 additions & 0 deletions docs/formatToMonogram.md
Original file line number Diff line number Diff line change
@@ -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 ''
```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
29 changes: 29 additions & 0 deletions src/__tests__/formatToMonogram.spec.ts
Original file line number Diff line number Diff line change
@@ -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('');
});
});
22 changes: 22 additions & 0 deletions src/shared/formatToMonogram.ts
Original file line number Diff line number Diff line change
@@ -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();
};
1 change: 1 addition & 0 deletions src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from './currencyToNumber';
export * from './addMaskToCardNumber';
export * from './formatCardNumber';
export * from './sanitizer';
export * from './formatToMonogram';
export * from './formatToPhone';

0 comments on commit b4adfdf

Please sign in to comment.