File tree Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Expand file tree Collapse file tree 2 files changed +18
-2
lines changed Original file line number Diff line number Diff line change @@ -8,11 +8,16 @@ module.exports = prepareSimpleTextSearch
8
8
// ```
9
9
//
10
10
// Objects in a collection get stringified to search it.
11
- // You can also define a property to search in:
11
+ // You can also define a property to search in or a pruning function to preprocess
12
+ // the collection:
12
13
// ```
13
14
// var get = simpleTextSearch([{name: 'Zürich'}, {name: 'Marc'}], 'name')
14
15
// var results = get('zurich')
15
- // // -> returns [{name: 'Marc'}]
16
+ // // -> returns [{name: 'Zürich'}]
17
+ //
18
+ // var get = simpleTextSearch([{name: 'Zürich'}, {name: 'Marc'}], (v) => v.name)
19
+ // var results = get('zurich')
20
+ // // -> returns [{name: 'Zürich'}]
16
21
// ```
17
22
function prepareSimpleTextSearch ( collection , property ) {
18
23
let cachedPrunedElements
@@ -22,6 +27,7 @@ function prepareSimpleTextSearch (collection, property) {
22
27
for ( const elem of collection ) {
23
28
let val = elem
24
29
if ( typeof property === 'string' ) val = val && val [ property ]
30
+ else if ( typeof property === 'function' ) val = val && property ( val )
25
31
if ( typeof val === 'object' ) val = JSON . stringify ( val )
26
32
else if ( typeof val !== 'string' ) continue
27
33
val = { pruned : clean ( val ) , elem }
Original file line number Diff line number Diff line change @@ -77,3 +77,13 @@ assert.strictEqual(res7a[1], 'Test 1')
77
77
const res7b = get7 ( '2' )
78
78
assert . strictEqual ( res7b . length , 1 )
79
79
assert . strictEqual ( res7b [ 0 ] , 'Hello 2' )
80
+
81
+ // Allows a specifying pruning function
82
+ const arr6 = [ { id : 1 , name : 'Test' } , { id : 2 , name : 'Marc' } ]
83
+ const get10 = search ( arr3 , ( option ) => option . name )
84
+ const res10a = get3 ( 'Marc' )
85
+ assert . strictEqual ( res3a [ 0 ] , arr3 [ 1 ] )
86
+ assert . strictEqual ( res3a . length , 1 )
87
+
88
+ const res10b = get3 ( 2 )
89
+ assert . strictEqual ( res3b . length , 0 )
You can’t perform that action at this time.
0 commit comments