|
1 | | -/** |
2 | | - * @author Titus Wormer |
3 | | - * @copyright 2016 Titus Wormer |
4 | | - * @license MIT |
5 | | - * @module nlcst:normalize |
6 | | - * @fileoverview Normalize a word for easier comparison. |
7 | | - */ |
8 | | - |
9 | 1 | 'use strict'; |
10 | 2 |
|
11 | | -/* eslint-env commonjs */ |
12 | | - |
13 | | -/* |
14 | | - * Dependencies. |
15 | | - */ |
16 | | - |
17 | 3 | var toString = require('nlcst-to-string'); |
18 | 4 |
|
19 | | -/* |
20 | | - * Constants. |
21 | | - */ |
| 5 | +module.exports = normalize; |
22 | 6 |
|
23 | 7 | var ALL = /[-']/g; |
24 | 8 | var DASH = /-/g; |
25 | 9 | var APOSTROPHE = /’/g; |
26 | 10 | var QUOTE = '\''; |
27 | 11 | var EMPTY = ''; |
28 | 12 |
|
29 | | -/** |
30 | | - * Normalize `value`. |
31 | | - * |
32 | | - * @param {string} value - Value to normalize. |
33 | | - * @param {Object?} options - Control stripping |
34 | | - * apostrophes and dashes. |
35 | | - * @return {string} - Normalized `value`. |
36 | | - */ |
| 13 | +/* Normalize `value`. */ |
37 | 14 | function normalize(value, options) { |
38 | | - var settings = options || {}; |
39 | | - var allowApostrophes = settings.allowApostrophes; |
40 | | - var allowDashes = settings.allowDashes; |
41 | | - var result = (typeof value === 'string' ? value : toString(value)) |
42 | | - .toLowerCase() |
43 | | - .replace(APOSTROPHE, QUOTE); |
44 | | - |
45 | | - if (allowApostrophes && allowDashes) { |
46 | | - return result; |
47 | | - } |
48 | | - |
49 | | - if (allowApostrophes) { |
50 | | - return result.replace(DASH, EMPTY); |
51 | | - } |
52 | | - |
53 | | - if (allowDashes) { |
54 | | - return result.replace(QUOTE, EMPTY); |
55 | | - } |
56 | | - |
57 | | - return result.replace(ALL, EMPTY); |
| 15 | + var settings = options || {}; |
| 16 | + var allowApostrophes = settings.allowApostrophes; |
| 17 | + var allowDashes = settings.allowDashes; |
| 18 | + var result = (typeof value === 'string' ? value : toString(value)) |
| 19 | + .toLowerCase() |
| 20 | + .replace(APOSTROPHE, QUOTE); |
| 21 | + |
| 22 | + if (allowApostrophes && allowDashes) { |
| 23 | + return result; |
| 24 | + } |
| 25 | + |
| 26 | + if (allowApostrophes) { |
| 27 | + return result.replace(DASH, EMPTY); |
| 28 | + } |
| 29 | + |
| 30 | + if (allowDashes) { |
| 31 | + return result.replace(QUOTE, EMPTY); |
| 32 | + } |
| 33 | + |
| 34 | + return result.replace(ALL, EMPTY); |
58 | 35 | } |
59 | | - |
60 | | -/* |
61 | | - * Expose. |
62 | | - */ |
63 | | - |
64 | | -module.exports = normalize; |
0 commit comments