Skip to content

Commit 2850f9b

Browse files
committed
Add support for allowApostrophes
1 parent 26ab8cc commit 2850f9b

File tree

5 files changed

+31
-8
lines changed

5 files changed

+31
-8
lines changed

component.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
"repository": "wooorm/nlcst-search",
1414
"dependencies": {
1515
"wooorm/nlcst-is-literal": "^1.0.0",
16-
"wooorm/nlcst-normalize": "^1.0.0",
16+
"wooorm/nlcst-normalize": "^1.1.0",
1717
"wooorm/unist-util-visit": "^1.0.0"
1818
},
1919
"scripts": [

index.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,11 @@ var T_WHITE_SPACE = 'WhiteSpaceNode';
4444
* @param {Object|Array.<string>} phrases - Phrases to
4545
* search for. When `object`, searches for its keys.
4646
* @param {Function} handler - Handler.
47+
* @param {boolean} allowApostrophes - Do not strip
48+
* apostrophes, normalize them.
4749
*/
48-
function search(tree, phrases, handler) {
50+
function search(tree, phrases, handler, allowApostrophes) {
51+
var apos = allowApostrophes;
4952
var byWord = {};
5053
var length;
5154
var index;
@@ -58,7 +61,7 @@ function search(tree, phrases, handler) {
5861
* @param {string} phrase - Phrase to search for.
5962
*/
6063
function handlePhrase(phrase) {
61-
firstWord = normalize(phrase.split(C_SPACE, 1)[0]);
64+
firstWord = normalize(phrase.split(C_SPACE, 1)[0], apos);
6265

6366
if (has.call(byWord, firstWord)) {
6467
byWord[firstWord].push(phrase);
@@ -143,7 +146,7 @@ function search(tree, phrases, handler) {
143146
if (
144147
!node ||
145148
node.type !== T_WORD ||
146-
normalize(expression[index]) !== normalize(node)
149+
normalize(expression[index], apos) !== normalize(node, apos)
147150
) {
148151
return null;
149152
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"author": "Titus Wormer <[email protected]> (http://wooorm.com)",
2222
"dependencies": {
2323
"nlcst-is-literal": "^1.0.0",
24-
"nlcst-normalize": "^1.0.0",
24+
"nlcst-normalize": "^1.1.0",
2525
"unist-util-visit": "^1.0.0"
2626
},
2727
"devDependencies": {

readme.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ search(tree, ['do blocklevel'], function (nodes) {
6363

6464
## API
6565

66-
### `search(node, patterns, handler)`
66+
### `search(node, patterns, handler[, allowApostrophes])`
6767

6868
Search for patterns in an NLCST tree.
6969

@@ -83,7 +83,10 @@ Note that the algorithm ignores [literal][literal] words.
8383
* `handler` ([`Function`][fn-handler])
8484
— Patterns to search for. If an `Object`, uses its keys.
8585
Each pattern is a space-delimited list of words, where each
86-
word is normalized to remove casing, apostrophes, and dashes.
86+
word is normalized to remove casing, apostrophes, and dashes;
87+
88+
* `allowApostrophes` (`boolean`, default: `false`)
89+
— Do not strip apostrophes (but normalize them).
8790

8891
**Throws**: `Error` — When not given `node` or `patterns`.
8992

test.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ var tree = {
160160
'value': 'this'
161161
}
162162
]
163+
},
164+
{
165+
'type': 'WhiteSpaceNode',
166+
'value': ' '
167+
},
168+
{
169+
'type': 'WordNode',
170+
'children': [
171+
{
172+
'type': 'TextNode',
173+
'value': 'hell'
174+
}
175+
]
163176
}
164177
]
165178
};
@@ -169,7 +182,7 @@ var tree = {
169182
*/
170183

171184
test('search(tree, patterns, handle)', function (t) {
172-
t.plan(39);
185+
t.plan(40);
173186

174187
t.throws(
175188
function () {
@@ -261,4 +274,8 @@ test('search(tree, patterns, handle)', function (t) {
261274
t.equal(parent, match[2], 'should pass the parent (phrases)');
262275
t.equal(phrase, match[3], 'should pass the phrase (phrases)');
263276
});
277+
278+
t.doesNotThrow(function () {
279+
search(tree, ['he’ll'], null, true);
280+
}, 'should not find non-apostrophe words when `allowApostrophes` is true');
264281
});

0 commit comments

Comments
 (0)