Skip to content

Commit db881e2

Browse files
Dave Newtonwooorm
authored andcommitted
Add support for allowDashes
Add support for `allowDashes` as an option, next to `allowApostrophes`, to control whether hyphens are normalised. Closes GH-1.
1 parent 7108c5d commit db881e2

File tree

3 files changed

+805
-43
lines changed

3 files changed

+805
-43
lines changed

index.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var toString = require('nlcst-to-string');
2020
* Constants.
2121
*/
2222

23-
var ALL = /[-']/g;
23+
var ALL = /[-']/g;
2424
var DASH = /-/g;
2525
var APOSTROPHE = //g;
2626
var QUOTE = '\'';
@@ -30,18 +30,28 @@ var EMPTY = '';
3030
* Normalize `value`.
3131
*
3232
* @param {string} value - Value to normalize.
33-
* @param {boolean} allowApostrophes - Do not strip
34-
* apostrophes.
33+
* @param {Object?} options - Control stripping
34+
* apostrophes and dashes.
3535
* @return {string} - Normalized `value`.
3636
*/
37-
function normalize(value, allowApostrophes) {
37+
function normalize(value, options) {
38+
var settings = options || {};
39+
var allowApostrophes = settings.allowApostrophes;
40+
var allowDashes = settings.allowDashes;
3841
var result = (typeof value === 'string' ? value : toString(value))
39-
.toLowerCase();
42+
.toLowerCase()
43+
.replace(APOSTROPHE, QUOTE);
44+
45+
if (allowApostrophes && allowDashes) {
46+
return result;
47+
}
4048

4149
if (allowApostrophes) {
42-
return result
43-
.replace(APOSTROPHE, QUOTE)
44-
.replace(DASH, EMPTY);
50+
return result.replace(DASH, EMPTY);
51+
}
52+
53+
if (allowDashes) {
54+
return result.replace(QUOTE, EMPTY);
4555
}
4656

4757
return result.replace(ALL, EMPTY);

readme.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ normalize({
4444

4545
## API
4646

47-
### `normalize(value[, allowApostrophes])`
47+
### `normalize(value[, options])`
4848

4949
Quote a value.
5050

@@ -53,8 +53,13 @@ Quote a value.
5353
* `value` ([`Node`][nlcst-node], `Array.<Node>`, or `string`)
5454
— Value to normalize;
5555

56-
* `allowApostrophes` (`boolean`, default: `false`)
57-
— Do not strip apostrophes (but do normalize them).
56+
* `options` (`Object?`):
57+
58+
* `allowApostrophes` (`boolean`, default: `false`)
59+
— Do not strip apostrophes (`'`);
60+
61+
* `allowDashes` (`boolean`, default: `false`)
62+
— Do not strip hyphens (`-`).
5863

5964
**Returns**: `string` — Normalized value.
6065

0 commit comments

Comments
 (0)