Skip to content

Commit

Permalink
fix: year replacement templating in the Apache 2.0 license (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion authored May 26, 2024
1 parent 49df1c9 commit 03901a4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/getLicense.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import MIT from "@ovyerus/licenses/licenses/MIT.json";
import Apache20 from "@ovyerus/licenses/licenses/Apache-2.0.json";

import getLicense from "./getLicense";

Expand All @@ -12,6 +13,14 @@ test("replaces the author and year fields in the MIT license", () => {
);
});

test("replaces the author and year fields in the Apache-2.0 license", () => {
expect(getLicense("Apache-2.0", { author: "Ovyerus", yyyy: "2020" })).toEqual(
Apache20.licenseText
.replace(/<author>/g, "Ovyerus")
.replace(/\[(yyyy)\]/g, "2020")
);
});

test("throws on invalid license", () => {
expect(() => getLicense("bad identifier")).toThrowError(
new TypeError("license is not a valid SPDX identifier")
Expand Down
5 changes: 4 additions & 1 deletion src/getLicense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export function getLicense(
// Remove any undefined or falsey values
([, value]) => value
))
modified = modified.replace(new RegExp(`<${key}>`, "g"), value);
modified = modified
.replace(new RegExp(`<${key}>`, "g"), value)
// Some licenses, like Apache 2.0 use square bracket templating for some values.
.replace(new RegExp(`\\[${key}\\]`, "g"), value);

return modified;
}
Expand Down

0 comments on commit 03901a4

Please sign in to comment.