Skip to content

Commit 5a8cf14

Browse files
committed
Remove support for passing an object
1 parent 4481c07 commit 5a8cf14

File tree

4 files changed

+13
-64
lines changed

4 files changed

+13
-64
lines changed

index.js

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
* @typedef {import('./lib/index.js').Handler} Handler
33
* @typedef {import('./lib/index.js').Options} Options
44
* @typedef {import('./lib/index.js').PhrasesList} PhrasesList
5-
* @typedef {import('./lib/index.js').PhrasesMap} PhrasesMap
65
*/
76

87
export {search} from './lib/index.js'

lib/index.js

+10-35
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,6 @@
3737
* Instead of a word with letters, it’s also possible to use a wildcard
3838
* symbol (`*`, an asterisk) which will match any word in a pattern
3939
* (`alpha * charlie`).
40-
*
41-
* @typedef {Record<string, unknown>} PhrasesMap
42-
* Map where the keys are phrases.
4340
*/
4441

4542
import {visit} from 'unist-util-visit'
@@ -53,7 +50,7 @@ const own = {}.hasOwnProperty
5350
*
5451
* @param {Nodes} tree
5552
* Tree to search.
56-
* @param {PhrasesList | PhrasesMap} phrases
53+
* @param {PhrasesList} phrases
5754
* Phrases to search for.
5855
* @param {Handler} handler
5956
* Handle a match
@@ -63,7 +60,6 @@ const own = {}.hasOwnProperty
6360
* Nothing.
6461
*/
6562
// To do: next major: remove boolean overload.
66-
// To do: next major: remove `PhrasesMap` support.
6763
export function search(tree, phrases, handler, options) {
6864
const config =
6965
typeof options === 'boolean' ? {allowApostrophes: options} : options || {}
@@ -79,19 +75,16 @@ export function search(tree, phrases, handler, options) {
7975
/** @type {Record<string, Array<string>>} */
8076
const byWord = {'*': []}
8177

82-
if (Array.isArray(phrases)) {
83-
let index = -1
84-
while (++index < phrases.length) {
85-
handlePhrase(phrases[index])
86-
}
87-
} else {
88-
/** @type {string} */
89-
let key
78+
let index = -1
9079

91-
for (key in phrases) {
92-
if (own.call(phrases, key)) {
93-
handlePhrase(key)
94-
}
80+
while (++index < phrases.length) {
81+
const phrase = phrases[index]
82+
const firstWord = normalize(phrase.split(' ', 1)[0], config)
83+
84+
if (own.call(byWord, firstWord)) {
85+
byWord[firstWord].push(phrase)
86+
} else {
87+
byWord[firstWord] = [phrase]
9588
}
9689
}
9790

@@ -168,22 +161,4 @@ export function search(tree, phrases, handler, options) {
168161

169162
return siblings.slice(start, position)
170163
}
171-
172-
/**
173-
* Index a phrase.
174-
*
175-
* @param {string} phrase
176-
* Raw phrase.
177-
* @returns {undefined}
178-
* Nothing.
179-
*/
180-
function handlePhrase(phrase) {
181-
const firstWord = normalize(phrase.split(' ', 1)[0], config)
182-
183-
if (own.call(byWord, firstWord)) {
184-
byWord[firstWord].push(phrase)
185-
} else {
186-
byWord[firstWord] = [phrase]
187-
}
188-
}
189164
}

readme.md

+3-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
* [`Handler`](#handler)
2222
* [`Options`](#options)
2323
* [`PhrasesList`](#phraseslist)
24-
* [`PhrasesMap`](#phrasesmap)
2524
* [Types](#types)
2625
* [Compatibility](#compatibility)
2726
* [Related](#related)
@@ -118,8 +117,7 @@ Search for phrases in a tree.
118117

119118
* `tree` ([`Node`][node])
120119
— tree to search
121-
* `phrases` ([`PhrasesList`][api-phrases-list] or
122-
[`PhrasesMap`][api-phrases-map])
120+
* `phrases` (`Array<string>`)
123121
— phrases to search for
124122
* `handler` ([`Handler`][api-handler])
125123
— handle a match
@@ -180,24 +178,13 @@ Instead of a word with letters, it’s also possible to use a wildcard symbol
180178
type PhrasesList = Array<string>
181179
```
182180
183-
### `PhrasesMap`
184-
185-
Map of phrases (TypeScript type).
186-
187-
###### Type
188-
189-
```ts
190-
type PhrasesMap = Record<string, unknown>
191-
```
192-
193181
## Types
194182
195183
This package is fully typed with [TypeScript][].
196184
It exports the additional types
197185
[`Handler`][api-handler],
198-
[`Options`][api-options],
199-
[`PhrasesList`][api-phrases-list], and
200-
[`PhrasesMap`][api-phrases-map].
186+
[`Options`][api-options], and
187+
[`PhrasesList`][api-phrases-list].
201188
202189
## Compatibility
203190
@@ -293,5 +280,3 @@ abide by its terms.
293280
[api-options]: #options
294281
295282
[api-phrases-list]: #phraseslist
296-
297-
[api-phrases-map]: #phrasesmap

test.js

-10
Original file line numberDiff line numberDiff line change
@@ -121,16 +121,6 @@ test('search', async function (t) {
121121
})
122122
})
123123

124-
await t.test('should search (object)', async function () {
125-
search(tree, {do: true}, function (nodes, index, parent, phrase) {
126-
const match = [tree.children[2]]
127-
assert.deepEqual(nodes, match, 'should pass nodes (object)')
128-
assert.equal(index, 2, 'should pass the correct index (object)')
129-
assert.equal(parent, tree, 'should pass the parent (object)')
130-
assert.equal(phrase, 'do', 'should pass the phrase (object)')
131-
})
132-
})
133-
134124
await t.test('should search (normalized, 2)', async function () {
135125
search(tree, ['blocklevel'], function (nodes, index, parent, phrase) {
136126
const match = [tree.children[4]]

0 commit comments

Comments
 (0)