Skip to content

Commit

Permalink
Add pluralize option
Browse files Browse the repository at this point in the history
  • Loading branch information
ShaitanLyss committed Jul 31, 2024
1 parent fcbeffe commit 0114002
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@selenite/commons",
"version": "0.18.7",
"version": "0.18.8",
"repository": "github:ShaitanLyss/selenite-commons",
"license": "MIT",
"keywords": [
Expand Down
10 changes: 10 additions & 0 deletions src/lib/utils/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,15 @@ describe('string utils', () => {
})
).toEqual('beautifulWorld');
});
it('can return pluralized result', () => {
expect(
String.getSharedString(['who let the dog out', 'beware scary dog out there'], {pluralize: true})
).toBe('dog outs');
})
it('can return pluralized camelcase result', () => {
expect(
String.getSharedString(['who let the dog out', 'beware scary dog out there'], { pluralize: true, camelcase: true })
).toBe('dogOuts');
});
});
});
9 changes: 6 additions & 3 deletions src/lib/utils/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,18 @@ export function getSharedWords(strings: string[][] | string[]): string[] {

export function getSharedString(
strings: string[] | string[][],
options: { camelcase?: boolean } = {}
options: { camelcase?: boolean, pluralize?: boolean } = {}
): string {
const words = getSharedWords(strings as string[][]);
let res: string;
if (options.camelcase) {
if (words.length === 0) return '';
const firstWord = words.splice(0, 1)[0];
return words.reduce((acc, s) => acc + upperFirst(s), firstWord);
res = words.reduce((acc, s) => acc + upperFirst(s), firstWord);
} else {
res = words.join(' ');
}
return words.join(' ');
return options.pluralize ? plural(res) : res;
}
export function getVarsFromFormatString(formatString: string): string[] {
// return all matches of the regex
Expand Down

0 comments on commit 0114002

Please sign in to comment.