Skip to content

Commit

Permalink
💩 src: Minor fixes
Browse files Browse the repository at this point in the history
Fixed typings in source.

Added new test for normalizing empty string.

Minor update in docs.
  • Loading branch information
motss committed Jan 16, 2019
1 parent 0792e08 commit afb3bf9
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ import { normalize } from 'https://denopkg.com/motss/normalize-diacritics@v1.0.0

### normalize([input])

- `input` <[?string][string-mdn-url]> Input string that contains accents/ diacritics.
- `input` <[?string][string-mdn-url]> Optional input string that contains accents/ diacritics.
- returns: <[Promise][promise-mdn-url]<[string][string-mdn-url]>> Promise which resolves with normalized input string.

This method normalizes any accents/ diacritics found in a given input string and output a normalized string as a result.
Expand Down
4 changes: 2 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@ function replaceDiacritics(inputChar: string) {
return Array.isArray(normalized) && normalized.length > 0 ? normalized[0].letter : inputChar;
}

export function normalizeSync(input: string) {
export function normalizeSync(input?: string) {
if (typeof input !== 'string') {
throw new TypeError(`Expected 'input' to be of type string, but received '${input}'`);
}

return !input.length ? input : input.replace(/(\S)/g, (_, p) => replaceDiacritics(p));
return !input.length ? input : input.replace(/(\S)/g, (_, p: string) => replaceDiacritics(p));
}

export async function normalize(input?: string) {
Expand Down
9 changes: 7 additions & 2 deletions test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async function normalizesStrings() {

async function normalizesStringsWithoutUsingNativeFunction() {
const cachedFn = String.prototype.normalize;
String.prototype.normalize = null;
String.prototype.normalize = null!;

try {
assertEqual(await normalize('Réunion'), 'Reunion');
Expand All @@ -59,7 +59,7 @@ async function returnsOriginalCharacterWhenNoMatchFound() {
const cachedFilter = Array.prototype.filter;
const cachedFn = String.prototype.normalize;
Array.prototype.filter = () => [];
String.prototype.normalize = null;
String.prototype.normalize = null!;

try {
assertEqual(await normalize('Réunion'), 'Réunion');
Expand All @@ -75,12 +75,17 @@ async function normalizeSingleCharacter() {
assertEqual(await normalize('ô'), 'o');
}

async function returnsEmptyStringUnTouched() {
assertEqual(await normalize(''), '');
}

Promise.all([
throwsWhenInvalidInput,
throwsWhenInputIsUndefined,
normalizesStringsWithoutUsingNativeFunction,
returnsOriginalCharacterWhenNoMatchFound,
normalizeSingleCharacter,
returnsEmptyStringUnTouched,
].map(n => test(n)));

/**
Expand Down

0 comments on commit afb3bf9

Please sign in to comment.