Skip to content

Commit 08a9a43

Browse files
committed
dev: adds helpers: pluralize, titleize
1 parent 0ca06a6 commit 08a9a43

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

app/helpers/pluralize.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,23 @@
11
import { helper } from '@ember/component/helper';
2+
import { titleize } from 'dealfront/helpers/titleize';
23

3-
export default helper(function pluralize([number, word]: [
4+
interface PluralizeSignature {
5+
Args: {
6+
Positional: [number, string];
7+
Named: Record<string, never>;
8+
};
9+
Return: string;
10+
}
11+
12+
export default helper<PluralizeSignature>(function pluralize([number, word]: [
413
number,
514
string,
615
]): string {
16+
if (typeof number !== 'number' || typeof word !== 'string') {
17+
throw new Error('pluralize helper requires a number and string argument');
18+
}
19+
720
const isSingle = number.toString().endsWith('1');
821

9-
return isSingle ? word : word + 'es';
22+
return titleize(isSingle ? word : word + 'es');
1023
});

app/helpers/titleize.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { helper } from '@ember/component/helper';
2+
3+
interface TitleizeSignature {
4+
Args: {
5+
Positional: [string];
6+
Named: Record<string, never>;
7+
};
8+
Return: string;
9+
}
10+
11+
export function titleize(word: string): string {
12+
if (typeof word !== 'string') {
13+
throw new Error('titleize helper requires a string argument');
14+
}
15+
16+
return word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
17+
}
18+
19+
export default helper<TitleizeSignature>(function ([word]: [string]) {
20+
return titleize(word);
21+
});

tests/integration/helpers/pluralize-test.gts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,17 @@ import pluralize from 'dealfront/helpers/pluralize';
66
module('Integration | Helper | pluralize', function (hooks) {
77
setupRenderingTest(hooks);
88

9-
// TODO: Replace this with your real tests.
109
test('it renders', async function (assert) {
1110
const inputValue = [
12-
{ number: 1, word: 'branch' },
13-
{ number: 12, word: 'branches' },
14-
{ number: 21, word: 'branch' },
11+
{ number: 1, word: 'Branch' },
12+
{ number: 12, word: 'Branches' },
13+
{ number: 21, word: 'Branch' },
14+
{ number: 0, word: 'Branches' },
1515
];
1616

17-
// await render(<template>{{pluralize inputValue[0] "branch"}}</template>);
18-
1917
for (const { number, word } of inputValue) {
2018
await render(<template>{{pluralize number "branch"}}</template>);
2119
assert.dom().hasText(word);
2220
}
23-
// assert.dom().hasText('12 branches');
2421
});
2522
});
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { module, test } from 'qunit';
2+
import { setupRenderingTest } from 'dealfront/tests/helpers';
3+
import { render } from '@ember/test-helpers';
4+
import titleize from 'dealfront/helpers/titleize';
5+
6+
module('Integration | Helper | titleize', function (hooks) {
7+
setupRenderingTest(hooks);
8+
9+
test('it titleizes strings', async function (assert) {
10+
const strings = [
11+
{ inStr: 'branch', outStr: 'Branch' },
12+
{ inStr: 'camelCase', outStr: 'Camelcase' },
13+
{ inStr: 'CAPS', outStr: 'Caps' },
14+
];
15+
16+
for (const { inStr, outStr } of strings) {
17+
await render(<template>{{titleize inStr}}</template>);
18+
assert.dom().hasText(outStr);
19+
}
20+
});
21+
});

0 commit comments

Comments
 (0)