-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(normalizer): Add normalizer for WOF which can be configured to r…
…emove accents/hyphen/spaces and do lowercase (#33)
- Loading branch information
1 parent
7f05b8f
commit 94c9b4a
Showing
6 changed files
with
116 additions
and
3 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
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 @@ | ||
const removeAccents = require('remove-accents') | ||
|
||
function normalizer (options = {}) { | ||
return (value) => { | ||
value = value.trim() | ||
if (options.lowercase) { | ||
value = value.toLowerCase() | ||
} | ||
if (options.removeAccents) { | ||
value = removeAccents(value) | ||
} | ||
if (options.removeHyphen) { | ||
value = value.replace(/-/g, ' ') | ||
} | ||
if (options.removeSpaces) { | ||
value = value.replace(/ /g, '') | ||
} | ||
return value | ||
} | ||
} | ||
|
||
module.exports = normalizer |
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,68 @@ | ||
const normalizer = require('./normalizer') | ||
|
||
module.exports.tests = {} | ||
|
||
module.exports.tests.normalizer = (test) => { | ||
test('normalizerr: hyphen', (t) => { | ||
const value = ' Value-With-Some-Hyphen ' | ||
const expected = 'Value With Some Hyphen' | ||
const normalize = normalizer({ removeHyphen: true }) | ||
|
||
t.deepEquals(normalize(value), expected) | ||
t.end() | ||
}) | ||
|
||
test('normalizer: accents', (t) => { | ||
const value = ' Vâlüé-Wìth-Sômê-Accents ' | ||
const expected = 'Value-With-Some-Accents' | ||
const normalize = normalizer({ removeAccents: true }) | ||
|
||
t.deepEquals(normalize(value), expected) | ||
t.end() | ||
}) | ||
|
||
test('normalizer: lowercase', (t) => { | ||
const value = 'Value-With-Some-UpperCases' | ||
const expected = 'value-with-some-uppercases' | ||
const normalize = normalizer({ lowercase: true }) | ||
|
||
t.deepEquals(normalize(value), expected) | ||
t.end() | ||
}) | ||
|
||
test('normalizer: spaces', (t) => { | ||
const value = 'Value With Some Spaces' | ||
const expected = 'ValueWithSomeSpaces' | ||
const normalize = normalizer({ removeSpaces: true }) | ||
|
||
t.deepEquals(normalize(value), expected) | ||
t.end() | ||
}) | ||
|
||
test('normalizer: option mix', (t) => { | ||
const value = 'Vâlüé-Mìxèd' | ||
const expected = 'value mixed' | ||
const normalize = normalizer({ lowercase: true, removeHyphen: true, removeAccents: true }) | ||
|
||
t.deepEquals(normalize(value), expected) | ||
t.end() | ||
}) | ||
|
||
test('normalizer: no options', (t) => { | ||
const value = 'Value-With-Some-Hyphen' | ||
const normalize = normalizer() | ||
|
||
t.deepEquals(normalize(value), value) | ||
t.end() | ||
}) | ||
} | ||
|
||
module.exports.all = (tape, common) => { | ||
function test (name, testFunction) { | ||
return tape(`normalizer: ${name}`, testFunction) | ||
} | ||
|
||
for (var testCase in module.exports.tests) { | ||
module.exports.tests[testCase](test, common) | ||
} | ||
} |