Skip to content

Commit

Permalink
Add pluralization helper
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Apr 7, 2021
1 parent 382b7b3 commit dd8b8e0
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 2 deletions.
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export {
handleError,
handleErrorSync,
hideString,
pluralize,
replacer,
toArray,
truncate,
Expand Down
5 changes: 4 additions & 1 deletion lib/template.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import * as fs from "fs-extra";
import * as path from "path";

import { bytes } from "./util";
import { bytes, pluralize } from "./util";

export async function render(
name: string,
Expand Down Expand Up @@ -60,6 +60,9 @@ async function hb(): Promise<any> {
args !== undefined ? bytes(args) : undefined,
);
handlebars.registerHelper("or", (arg1, arg2) => arg1 || arg2);
handlebars.registerHelper("plural", (arg1, arg2, arg3) =>
pluralize(arg1, arg2, arg3),
);
return handlebars;
}

Expand Down
10 changes: 10 additions & 0 deletions lib/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,13 @@ export function isStaging(): boolean {
"https://automation.atomist.com/graphql"
).includes(".services");
}

export function pluralize(
text: string,
count: number | any[],
include = true,
): string {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const plu = require("pluralize");
return plu(text, typeof count === "number" ? count : count.length, include);
}
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"@types/lodash.sortby": "^4.7.6",
"@types/lodash.uniq": "^4.5.6",
"@types/node-fetch": "^2.5.9",
"@types/pluralize": "0.0.29",
"@types/semver": "^7.3.4",
"@types/stack-trace": "0.0.29",
"@types/uuid": "^8.3.0",
Expand Down Expand Up @@ -111,6 +112,7 @@
"lodash.uniq": "^4.5.0",
"node-fetch": "^2.6.1",
"p-retry": "^4.5.0",
"pluralize": "^8.0.0",
"prettier": "^2.2.1",
"semver": "^7.3.5",
"source-map-support": "^0.5.19",
Expand Down
2 changes: 2 additions & 0 deletions test/template.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ describe("template", () => {
assert.deepStrictEqual(
result,
`Hello Mickey Mouse
3 colors
* \`blue\` [test](google.com)
* \`orange\` [test](google.com)
* \`red\` [test](google.com)
Expand Down
17 changes: 16 additions & 1 deletion test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

import * as assert from "assert";

import { bytes, extractParameters, guid, truncate } from "../lib/util";
import {
bytes,
extractParameters,
guid,
pluralize,
truncate,
} from "../lib/util";

describe("util", () => {
describe("extractParameters", () => {
Expand Down Expand Up @@ -104,4 +110,13 @@ describe("util", () => {
assert.deepStrictEqual(bytes("1024"), "1.0kb");
});
});

describe("pluralize", () => {
it("should correctly pluralize", () => {
assert.deepStrictEqual(
pluralize("vulnerability", ["CVE-1234", "CVE-5678"]),
"2 vulnerabilities",
);
});
});
});
2 changes: 2 additions & 0 deletions test/views/test.hbs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{{~> greeting ~}}

{{plural "color" color}}

{{# each color}}
* {{code this}} {{link "test" "google.com"}}
{{/each ~}}

0 comments on commit dd8b8e0

Please sign in to comment.