From afb3bf907ce048040a539ffbd6cc142f7adbc66a Mon Sep 17 00:00:00 2001 From: "Rong Sen Ng (motss)" Date: Wed, 16 Jan 2019 22:51:41 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=A9=20src:=20Minor=20fixes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixed typings in source. Added new test for normalizing empty string. Minor update in docs. --- README.md | 2 +- index.ts | 4 ++-- test/index.spec.ts | 9 +++++++-- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index cc1fc13..52564c8 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/index.ts b/index.ts index 9bae847..400b8f2 100644 --- a/index.ts +++ b/index.ts @@ -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) { diff --git a/test/index.spec.ts b/test/index.spec.ts index ab8db01..8ffbbbe 100644 --- a/test/index.spec.ts +++ b/test/index.spec.ts @@ -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'); @@ -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'); @@ -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))); /**