generated from Nerdware-LLC/template-npm-pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge changes from v1.5.0-next.2 into main (#28)
- Loading branch information
Showing
25 changed files
with
397 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { sanitizeName } from "./name.js"; | ||
|
||
describe("sanitize: name", () => { | ||
test("removes invalid characters from the name", () => { | ||
expect(sanitizeName("Jane Doe!@#$%^&*()_+123")).toBe("Jane Doe"); | ||
}); | ||
|
||
test("keeps valid characters in the name", () => { | ||
const input = "José Dã'Hüman-persón"; | ||
expect(sanitizeName(input)).toBe(input); | ||
}); | ||
|
||
test("keeps valid punctuation marks in the name", () => { | ||
const input = "'.,-"; | ||
expect(sanitizeName(input)).toBe(input); | ||
}); | ||
|
||
test("keeps valid whitespace characters in the name", () => { | ||
const input = "John Doe"; | ||
expect(sanitizeName(input)).toBe(input); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getSanitizerFn } from "./_helpers.js"; | ||
|
||
/** | ||
* Removes characters from `str` which are generally not valid in a name. | ||
* | ||
* **Permitted Characters:** | ||
* - [Unicode Latin-script characters](https://en.wikipedia.org/w/index.php?title=Latin_script_in_Unicode&oldid=1210023145#Table_of_characters) | ||
* - Single-horizontal-space characters (`\x20`) | ||
* - Apostrophes (`'`) | ||
* - Periods (`.`) | ||
* - Commas (`,`) | ||
* - Hyphens (`-`) | ||
* | ||
* ```ts | ||
* // Non-ASCII latin-script characters are preserved | ||
* sanitizeName("José São Hüman-persón"); // "José São Hüman-përsón" | ||
* // Punctuation marks sometimes used in names are preserved | ||
* sanitizeName("Jane Da'Human, Jr."); // "Jane Da'Human, Jr." | ||
* ``` | ||
* | ||
* **Pattern Notes:** | ||
* - `\p{Script=Latin}` is used as a more i18n-friendly alternative to `[a-zA-Z]`. | ||
* - `\x20` is the ASCII hex code for a single horizontal space. | ||
* - The hyphen literal is at the end of the group to avoid unintentional range creation. | ||
*/ | ||
export const sanitizeName = getSanitizerFn(/[^\p{Script=Latin}\x20'.,-]/gu); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { sanitizeText } from "./text.js"; | ||
|
||
describe("sanitize: text", () => { | ||
test("removes characters that pose security risks", () => { | ||
expect(sanitizeText(`Hello {world}!`)).toBe("Hello world!"); | ||
expect(sanitizeText(`Hello [world]!`)).toBe("Hello world!"); | ||
expect(sanitizeText(`Hello <world>!`)).toBe("Hello world!"); | ||
}); | ||
|
||
test("keeps permitted characters", () => { | ||
const input = `123%$#@'&.,:;"!?+=()_—-`; | ||
expect(sanitizeText(input)).toBe(input); | ||
}); | ||
|
||
test("keeps Latin-script characters", () => { | ||
expect(sanitizeText(`Hello José 世界!`)).toBe(`Hello José !`); | ||
}); | ||
|
||
test("keeps whitespace characters", () => { | ||
const input1 = `Hello\nworld!\n`; | ||
const input2 = ` | ||
Hello | ||
world!`; | ||
|
||
expect(sanitizeText(input1)).toBe(input1); | ||
expect(sanitizeText(input2)).toBe(input2); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { getSanitizerFn } from "./_helpers.js"; | ||
|
||
/** | ||
* Removes characters from `str` which meet the following criteria: | ||
* | ||
* 1. The character is generally not used in everyday written text | ||
* 2. The character poses potential security risks if not properly handled | ||
* | ||
* > **Examples of characters removed by this sanitizer:** `{}`, `[]`, and `<>` | ||
* | ||
* This function is intended for arbitrary user-provided text, like comments or forum posts. | ||
* | ||
* **Permitted Characters:** | ||
* - [Unicode Latin-script characters](https://en.wikipedia.org/w/index.php?title=Latin_script_in_Unicode&oldid=1210023145#Table_of_characters) | ||
* - Numerical digits (`\d`) | ||
* - Whitespace characters (`\s`) | ||
* - Percent signs (`%`) | ||
* - Dollar signs (`$`) | ||
* - Number signs (`#`) | ||
* - At symbols (`@`) | ||
* - Apostrophes (`'`) | ||
* - Ampersands (`&`) | ||
* - Periods (`.`) | ||
* - Commas (`,`) | ||
* - Colons (`:`) | ||
* - Semicolons (`;`) | ||
* - Double quotes (`"`) | ||
* - Exclamation marks (`!`) | ||
* - Question marks (`?`) | ||
* - Plus signs (`+`) | ||
* - Equal signs (`=`) | ||
* - Parentheses (`()`) | ||
* - Underscores (`_`) | ||
* - Em dashes (`—`) | ||
* - Hyphens (`-`) | ||
* | ||
* **Pattern Notes:** | ||
* - `\p{Script=Latin}` is used as a more i18n-friendly alternative to `[a-zA-Z]`. | ||
* - The hyphen literal is at the end of the group to avoid unintentional range creation. | ||
*/ | ||
export const sanitizeText = getSanitizerFn(/[^\p{Script=Latin}\d\s%$#@'&.,:;"!?+=()_—-]/gu); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { isValidName } from "./name.js"; | ||
|
||
describe("validate: name", () => { | ||
// POSITIVE TEST CASES: | ||
|
||
test("returns true when called with a value which contains ASCII letters", () => { | ||
expect(isValidName("ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz")).toBe(true); | ||
}); | ||
test("returns true when called with a value which contains non-ASCII latin-script characters", () => { | ||
expect(isValidName("José Dã'Hüman-persón")).toBe(true); | ||
}); | ||
test("returns true when called with a value which contains permitted punctuation characters", () => { | ||
expect(isValidName("A'.,-")).toBe(true); | ||
}); | ||
test("returns true when called with a value which contains several space characters", () => { | ||
expect(isValidName("John Doe")).toBe(true); | ||
}); | ||
|
||
// NEGATIVE TEST CASES: | ||
|
||
test("returns false when called with a value which contains invalid special characters", () => { | ||
expect(isValidName("Jane Doe!@#$%^&*()_+123")).toBe(false); | ||
}); | ||
test("returns false when called with a value which contains a number", () => { | ||
expect(isValidName("Jane Doe 123")).toBe(false); | ||
}); | ||
test("returns false when called with a value which contains a symbol", () => { | ||
expect(isValidName("Jane Doe #")).toBe(false); | ||
}); | ||
test("returns false when called with a value which contains a newline character", () => { | ||
expect(isValidName("Jane\nDoe")).toBe(false); | ||
}); | ||
test("returns false when called with a value which contains a tab character", () => { | ||
expect(isValidName("Jane\tDoe")).toBe(false); | ||
}); | ||
test("returns false when called with an empty string", () => { | ||
expect(isValidName("")).toBe(false); | ||
}); | ||
test("returns false when called with a string containing only a space character", () => { | ||
expect(isValidName(" ")).toBe(false); | ||
}); | ||
test("returns false when called with a string containing only a hyphen character", () => { | ||
expect(isValidName("-")).toBe(false); | ||
}); | ||
test("returns false when called with a string containing only a period character", () => { | ||
expect(isValidName(".")).toBe(false); | ||
}); | ||
test("returns false when called with a string containing only a comma character", () => { | ||
expect(isValidName(",")).toBe(false); | ||
}); | ||
test("returns false when called with a string containing only an apostrophe character", () => { | ||
expect(isValidName("'")).toBe(false); | ||
}); | ||
}); |
Oops, something went wrong.