@orchidjs/sifter 0.8.2 → 0.9.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/README.md +27 -5
  2. package/dist/cjs/diacritics.js +44 -31
  3. package/dist/cjs/diacritics.js.map +1 -1
  4. package/dist/cjs/sifter.js +9 -4
  5. package/dist/cjs/sifter.js.map +1 -1
  6. package/dist/cjs/utils.js +1 -0
  7. package/dist/cjs/utils.js.map +1 -1
  8. package/dist/esm/diacritics.js +44 -31
  9. package/dist/esm/diacritics.js.map +1 -1
  10. package/dist/esm/sifter.js +9 -4
  11. package/dist/esm/sifter.js.map +1 -1
  12. package/dist/esm/utils.js +1 -0
  13. package/dist/esm/utils.js.map +1 -1
  14. package/dist/types/diacritics.d.ts +2 -7
  15. package/dist/types/sifter.d.ts +3 -3
  16. package/dist/types/types.d.ts +10 -8
  17. package/dist/types/utils.d.ts +1 -1
  18. package/dist/umd/sifter.js +51 -34
  19. package/dist/umd/sifter.js.map +1 -1
  20. package/dist/umd/sifter.min.js +32 -30
  21. package/dist/umd/sifter.min.js.map +1 -1
  22. package/lib/diacritics.ts +63 -215
  23. package/lib/sifter.ts +9 -4
  24. package/lib/types.ts +12 -8
  25. package/lib/utils.ts +4 -1
  26. package/package.json +11 -6
  27. package/.config/babel.config.json +0 -17
  28. package/.config/rollup.config.js +0 -95
  29. package/.config/tsconfig.json +0 -16
  30. package/.travis.yml +0 -9
  31. package/Makefile +0 -27
  32. package/build/cjs/diacritics.js +0 -173
  33. package/build/cjs/diacritics.js.map +0 -1
  34. package/build/cjs/sifter.js +0 -372
  35. package/build/cjs/sifter.js.map +0 -1
  36. package/build/cjs/utils.js +0 -113
  37. package/build/cjs/utils.js.map +0 -1
  38. package/build/esm/diacritics.js +0 -164
  39. package/build/esm/diacritics.js.map +0 -1
  40. package/build/esm/sifter.js +0 -370
  41. package/build/esm/sifter.js.map +0 -1
  42. package/build/esm/utils.js +0 -103
  43. package/build/esm/utils.js.map +0 -1
  44. package/build/umd/sifter.js +0 -634
  45. package/build/umd/sifter.js.map +0 -1
  46. package/build/umd/sifter.min.js +0 -81
  47. package/build/umd/sifter.min.js.map +0 -1
  48. package/coverage/clover.xml +0 -258
  49. package/coverage/coverage-final.json +0 -4
  50. package/coverage/lcov-report/base.css +0 -224
  51. package/coverage/lcov-report/block-navigation.js +0 -79
  52. package/coverage/lcov-report/diacritics.ts.html +0 -1133
  53. package/coverage/lcov-report/favicon.png +0 -0
  54. package/coverage/lcov-report/index.html +0 -141
  55. package/coverage/lcov-report/prettify.css +0 -1
  56. package/coverage/lcov-report/prettify.js +0 -2
  57. package/coverage/lcov-report/sifter.ts.html +0 -1157
  58. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  59. package/coverage/lcov-report/sorter.js +0 -170
  60. package/coverage/lcov-report/utils.ts.html +0 -410
  61. package/coverage/lcov.info +0 -505
  62. package/release.sh +0 -119
  63. package/test/diacritics.js +0 -64
  64. package/test/getScoreFunction.js +0 -55
  65. package/test/prepareSearch.js +0 -40
  66. package/test/search.js +0 -335
  67. package/test/sorting.js +0 -193
  68. package/test/tokenize.js +0 -63
  69. package/test/utils.js +0 -28
  70. package/x-test.js +0 -125
@@ -1,55 +0,0 @@
1
- var assert = require('assert');
2
- var Sifter = require('../build/cjs/sifter.js');
3
-
4
- describe('#getScoreFunction()', function() {
5
-
6
- it('should acknowledge AND "conjunction" option', function() {
7
- var score, search, sifter = new Sifter([]);
8
-
9
- score = sifter.getScoreFunction('one two', {fields: ['a','b'], conjunction: 'and'});
10
- assert.equal(score({a: 'one'}) > 0, false);
11
- assert.equal(score({a: 'one', b: 'two'}) > 0, true);
12
- assert.equal(score({a: 'one', b: 'one'}) > 0, false);
13
- assert.equal(score({a: 'one', b: 'three'}) > 0, false);
14
- assert.equal(score({a: 'three', b: 'three'}) > 0, false);
15
- });
16
-
17
- it('should acknowledge OR "conjunction" option', function() {
18
- var score, search, sifter = new Sifter([]);
19
-
20
- score = sifter.getScoreFunction('one two', {fields: ['a','b'], conjunction: 'or'});
21
- assert.equal(score({a: 'one'}) > 0, true);
22
- assert.equal(score({a: 'one', b: 'two'}) > 0, true);
23
- assert.equal(score({a: 'one', b: 'one'}) > 0, true);
24
- assert.equal(score({a: 'one', b: 'three'}) > 0, true);
25
- assert.equal(score({a: 'three', b: 'three'}) > 0, false);
26
- });
27
-
28
- describe('with query and options', function() {
29
-
30
- it('should return a function that returns a number', function() {
31
- var score, search, sifter = new Sifter([]);
32
-
33
- score = sifter.getScoreFunction('test', {fields: ['a','b']});
34
- assert.equal(typeof score({a: 'test'}), 'number');
35
- assert.equal(score({a: 'test'}) > 0, true);
36
- assert.equal(typeof score({}), 'number');
37
- });
38
-
39
- });
40
-
41
- describe('with pre-prepared search', function() {
42
-
43
- it('should return a function that returns a number', function() {
44
- var score, search, sifter = new Sifter([]);
45
-
46
- search = sifter.prepareSearch('test', {fields: ['a','b']});
47
- score = sifter._getScoreFunction(search);
48
- assert.equal(typeof score({a: 'test'}), 'number');
49
- assert.equal(score({a: 'test'}) > 0, true);
50
- assert.equal(typeof score({}), 'number');
51
- });
52
-
53
- });
54
-
55
- });
@@ -1,40 +0,0 @@
1
- var assert = require('assert');
2
- var Sifter = require('../build/cjs/sifter.js');
3
-
4
- describe('#prepareSearch()', function() {
5
-
6
- it('should normalize options', function() {
7
- var sifter = new Sifter([{fielda: 'a'}, {}]);
8
- var search = sifter.prepareSearch('a', {
9
- fields: [{field: 'fielda'}],
10
- sort: {field: 'fielda'},
11
- sort_empty: {field: 'fielda'}
12
- });
13
- //assert.equal(Array.isArray(search.options.fields), true);
14
- assert.equal(Array.isArray(search.options.sort), true);
15
- assert.equal(Array.isArray(search.options.sort_empty), true);
16
- });
17
-
18
- describe('returned object', function() {
19
- var sifter = new Sifter([{field: 'a'}, {}]);
20
- var search = sifter.prepareSearch('hello world');
21
-
22
- it('should contain "total" (int)', function() {
23
- assert.equal(search.total, 0);
24
- });
25
- it('should contain "tokens" (array)', function() {
26
- assert.equal(Array.isArray(search.tokens), true);
27
- assert.equal(search.tokens.length, 2);
28
- });
29
- it('should contain "items" (array)', function() {
30
- assert.equal(Array.isArray(search.items), true);
31
- assert.equal(search.items.length, 0);
32
- });
33
- it('should contain "options" (array)', function() {
34
- assert.equal(search.options !== null, true);
35
- assert.equal(typeof search.options, 'object');
36
- assert.equal(Array.isArray(search.options), false);
37
- });
38
- });
39
-
40
- });
package/test/search.js DELETED
@@ -1,335 +0,0 @@
1
- var assert = require('assert');
2
- var Sifter = require('../build/cjs/sifter.js');
3
-
4
-
5
- describe('#search()', function() {
6
-
7
- it('should not throw if an element does not contain search field', function() {
8
- assert.doesNotThrow(function() {
9
- var sifter = new Sifter([{field: 'a'}, {}]);
10
- var result = sifter.search('hello', {fields: ['field']});
11
- });
12
- });
13
-
14
- it('should allow "fields" option to be a string', function() {
15
- var sifter = new Sifter([{field: 'a'}, {}]);
16
- var result = sifter.search('a', {fields: 'field'});
17
- assert.equal(result.items[0].id, 0);
18
- });
19
-
20
- it('should allow to search nested fields', function() {
21
- var sifter = new Sifter([
22
- {fields: {nested: 'aaa'}},
23
- {fields: {nested: 'add'}},
24
- {fields: {nested: 'abb'}}
25
- ]);
26
- var result = sifter.search('aaa', {
27
- fields: 'fields.nested',
28
- nesting: true
29
- });
30
-
31
- assert.equal(result.items.length, 1);
32
- assert.equal(result.items[0].id, 0);
33
- });
34
-
35
- it('should allow word boundaries to be respected', function() {
36
- var sifter = new Sifter([
37
- {name: 'John Smith'},
38
- {name: 'Jane Doe'},
39
- ]);
40
- var result = sifter.search('mith', {fields: 'name'});
41
- assert.equal(result.items.length, 1);
42
-
43
- var result = sifter.search('mith', {fields: 'name', respect_word_boundaries: true});
44
- assert.equal(result.items.length, 0);
45
-
46
- var result = sifter.search('Smi', {fields: 'name', respect_word_boundaries: true});
47
- assert.equal(result.items.length, 1);
48
-
49
- var result = sifter.search('John Sm', {fields: 'name', respect_word_boundaries: true});
50
- assert.equal(result.items.length, 1);
51
-
52
- var result = sifter.search('ohn Smith', {fields: 'name', respect_word_boundaries: true, conjunction: 'and'});
53
- assert.equal(result.items.length, 0);
54
- });
55
-
56
- it('should work if search data is an object', function() {
57
- var sifter = new Sifter({a:{field: 'a'}, b:{field:'b'}});
58
- var result = sifter.search('a', {fields: ['field']});
59
- assert.equal(result.items.length, 1);
60
- assert.equal(result.items[0].id, 'a');
61
- });
62
-
63
- it('should handle accents', function() {
64
- var sifter = new Sifter([
65
- {fields: 'a'},
66
- {fields: 'Ḁ'},
67
- {fields: 'ḁ'},
68
- {fields: 'Ă'},
69
- {fields: 'ă'},
70
- {fields: 'Â'},
71
- ]);
72
- var result = sifter.search('ą', {
73
- fields: 'fields',
74
- sort:[{field:'fields'}],
75
- });
76
-
77
- assert.equal(result.items.length, 6);
78
- });
79
-
80
- describe('returned results', function() {
81
- var sifter, options, result, result_empty, result_all;
82
-
83
- beforeAll(function() {
84
- sifter = new Sifter([
85
- {title: 'Matterhorn', location: 'Switzerland', continent: 'Europe'},
86
- {title: 'Eiger', location: 'Switzerland', continent: 'Europe'},
87
- {title: 'Everest', location: 'Nepal', continent: 'Asia'},
88
- {title: 'Gannett', location: 'Wyoming', continent: 'North America'},
89
- {title: 'Denali', location: 'Alaska', continent: 'North America'}
90
- ]);
91
-
92
- options = {limit: 1, fields: ['title', 'location', 'continent']};
93
- result = sifter.search('switzerland europe', options);
94
- result_empty = sifter.search('awawfawfawf', options);
95
- result_all = sifter.search('', {
96
- fields: ['title', 'location', 'continent'],
97
- sort: [{field: 'title'}]
98
- });
99
- });
100
-
101
- it('should not vary when using an array vs a hash as a data source', function() {
102
- var sifter_hash = new Sifter({
103
- 'a': {title: 'Matterhorn', location: 'Switzerland', continent: 'Europe'},
104
- 'b': {title: 'Eiger', location: 'Switzerland', continent: 'Europe'},
105
- 'c': {title: 'Everest', location: 'Nepal', continent: 'Asia'},
106
- 'd': {title: 'Gannett', location: 'Wyoming', continent: 'North America'},
107
- 'e': {title: 'Denali', location: 'Alaska', continent: 'North America'}
108
- });
109
- var result_hash = sifter.search('switzerland europe', options);
110
- assert.deepEqual(result_hash, result);
111
- });
112
-
113
- describe('"items" array', function() {
114
- it('should be an array', function() {
115
- assert.equal(Array.isArray(result.items), true);
116
- assert.equal(Array.isArray(result_empty.items), true);
117
- assert.equal(Array.isArray(result_all.items), true);
118
- });
119
- it('should include entire set if no query provided', function() {
120
- assert.equal(result_all.items.length, 5);
121
- });
122
- it('should not have a length that exceeds "limit" option', function() {
123
- assert.equal(result.items.length > options.limit, false);
124
- });
125
- it('should not contain any items with a score not equal to 1 (without query)', function() {
126
- for (var i = 0, n = result_all.items.length; i < n; i++) {
127
- assert.equal(result_all.items[i].score, 1);
128
- }
129
- });
130
- it('should not contain any items with a score of zero (with query)', function() {
131
- for (var i = 0, n = result.items.length; i < n; i++) {
132
- assert.notEqual(result.items[i].score, 0);
133
- }
134
- });
135
- it('should be empty when no results match', function() {
136
- assert.equal(result_empty.items.length , 0);
137
- });
138
-
139
- describe('elements', function() {
140
- it('should be objects', function() {
141
- assert.equal(typeof result.items[0], 'object');
142
- assert.equal(Array.isArray(result.items[0]), false);
143
- });
144
- describe('"score" property', function() {
145
- it('should exist', function() {
146
- assert.notEqual(typeof result.items[0].score, 'undefined');
147
- assert.notEqual(typeof result_all.items[0].score, 'undefined');
148
- });
149
- it('should be a number', function() {
150
- assert.equal(typeof result.items[0].score, 'number');
151
- assert.equal(typeof result_all.items[0].score, 'number');
152
- });
153
- });
154
- describe('"id" property', function() {
155
- it('should exist', function() {
156
- assert.notEqual(typeof result.items[0].id, 'undefined');
157
- assert.notEqual(typeof result_all.items[0].id, 'undefined');
158
- });
159
- });
160
- });
161
- });
162
-
163
- describe('"options"', function() {
164
- it('should not be a reference to original options', function() {
165
- assert.equal(result.options === options, false);
166
- });
167
- //it('should match original search options', function() {
168
- // assert.deepEqual(result.options, options);
169
- //});
170
- });
171
-
172
- describe('"tokens"', function() {
173
- it('should be an array', function() {
174
- assert.equal(Array.isArray(result.tokens), true);
175
- });
176
- describe('elements', function() {
177
- it('should be a object', function() {
178
- assert.equal(typeof result.tokens[0], 'object');
179
- assert.equal(Array.isArray(result.tokens[0]), false);
180
- });
181
- describe('"string" property', function() {
182
- it('should exist', function() {
183
- assert.notEqual(typeof result.tokens[0].string, 'undefined');
184
- });
185
- it('should be a string', function() {
186
- assert.equal(typeof result.tokens[0].string, 'string');
187
- });
188
- it('should be valid', function() {
189
- assert.equal(result.tokens[0].string, 'switzerland');
190
- assert.equal(result.tokens[1].string, 'europe');
191
- });
192
- });
193
- describe('"regex" property', function() {
194
- it('should exist', function() {
195
- assert.notEqual(typeof result.tokens[0].regex, 'undefined');
196
- });
197
- it('should be a RegExp object', function() {
198
- assert.equal(result.tokens[0].regex instanceof RegExp, true);
199
- });
200
- });
201
- });
202
- });
203
-
204
- describe('"query"', function() {
205
- it('should match original query', function() {
206
- assert.equal(result.query, 'switzerland europe');
207
- });
208
- });
209
-
210
- describe('"total"', function() {
211
- it('should be an integer', function() {
212
- assert.equal(typeof result.total, 'number');
213
- assert.equal(Math.floor(result.total), Math.ceil(result.total));
214
- });
215
- it('should be valid', function() {
216
- assert.equal(result.total, 2);
217
- assert.equal(result_empty.total, 0);
218
- });
219
- });
220
-
221
- });
222
-
223
- describe('#field searching', function() {
224
-
225
- var data = [
226
- {fieldx: 'aa', fieldy: 'a'},
227
- {fieldx: 'ab', fieldy: 'a', fieldz: 'b' }
228
- ];
229
- var sifter = new Sifter(data);
230
-
231
- it('should return two matching rows', function() {
232
- var result = sifter.search('fieldx:a', {fields: ['fieldx','fieldy','fieldz']});
233
- assert.equal(result.items.length, 2);
234
- });
235
-
236
- it('should return one matching row', function() {
237
- var result = sifter.search('fieldz:b', {fields: ['fieldx','fieldy','fieldz']});
238
- assert.equal(result.items.length, 1);
239
- });
240
-
241
- it('should return one matching row', function() {
242
- var result = sifter.search('fieldz:', {fields: ['fieldx','fieldy','fieldz']});
243
- assert.equal(result.items.length, 1);
244
- });
245
-
246
- });
247
-
248
- describe('#field weights', function() {
249
-
250
- var data = [
251
- {fieldx: 'aaa', fieldy: 'abb'},
252
- {fieldx: 'abb', fieldy: 'aaa'}
253
- ];
254
- var sifter = new Sifter(data);
255
-
256
- it('row 0 should be first', function() {
257
- var result = sifter.search('b', {
258
- fields: [
259
- {field:'fieldx',weight:1},
260
- {field:'fieldy',weight:2}
261
- ]
262
- });
263
- assert.equal(result.items[0].id,0);
264
- });
265
-
266
- it('row 1 should be first', function() {
267
- var result = sifter.search('b', {
268
- fields: [
269
- {field:'fieldx',weight:2},
270
- {field:'fieldy',weight:1}
271
- ]
272
- });
273
- assert.equal(result.items[0].id,1);
274
- });
275
-
276
- it('only row 0', function() {
277
- var result = sifter.search('b', {
278
- fields: [
279
- {field:'fieldx',weight:0},
280
- {field:'fieldy',weight:1}
281
- ]
282
- });
283
- assert.equal(result.items.length,1);
284
- assert.equal(result.items[0].id,0);
285
- });
286
-
287
- });
288
-
289
-
290
- it('empty field', function() {
291
-
292
- var data = [
293
- {fieldx: 'aaa', fieldy: 'abb'},
294
- {fieldx: 'abb', fieldy: 'aaa'}
295
- ];
296
- var sifter = new Sifter(data);
297
-
298
- var result = sifter.search('b', {
299
- fields: []
300
- });
301
-
302
- assert.equal(result.items.length,2);
303
- assert.equal(result.items[0].id,0);
304
- });
305
-
306
- it('should match one item', function() {
307
-
308
- var data = [
309
- {fieldx: 'ffal'},
310
- {fieldx: 'ffla'}
311
- ];
312
- var sifter = new Sifter(data);
313
-
314
- var result = sifter.search('ffl', {
315
- fields: ['fieldx']
316
- });
317
-
318
- assert.equal(result.items.length,1);
319
- });
320
-
321
- it('should escape regex characters', function() {
322
-
323
- var data = [
324
- {fieldx: '$()*+.?[]^{|}\\'}
325
- ];
326
- var sifter = new Sifter(data);
327
-
328
- var result = sifter.search('$()*+.?[]^{|}\\', {
329
- fields: ['fieldx']
330
- });
331
-
332
- assert.equal(result.items.length,1);
333
- });
334
-
335
- });
package/test/sorting.js DELETED
@@ -1,193 +0,0 @@
1
- var assert = require('assert');
2
- var Sifter = require('../build/cjs/sifter.js');
3
-
4
- describe('sorting', function() {
5
-
6
- it('should respect "sort_empty" option when query absent', function() {
7
- var sifter = new Sifter([
8
- {field: 'aaa'},
9
- {field: 'add'},
10
- {field: 'abb'}
11
- ]);
12
- var result = sifter.search('', {
13
- fields: 'field',
14
- sort: {field: 'field', direction: 'asc'},
15
- sort_empty: {field: 'field', direction: 'desc'}
16
- });
17
- assert.equal(result.items[0].id, 1);
18
- assert.equal(result.items[1].id, 2);
19
- assert.equal(result.items[2].id, 0);
20
- });
21
-
22
- it('should work with one field (as object)', function() {
23
- var sifter = new Sifter([
24
- {field: 'aaa'},
25
- {field: 'add'},
26
- {field: 'abb'}
27
- ]);
28
- var result = sifter.search('', {
29
- fields: 'field',
30
- sort: {field: 'field'}
31
- });
32
- assert.equal(result.items[0].id, 0);
33
- assert.equal(result.items[1].id, 2);
34
- assert.equal(result.items[2].id, 1);
35
- });
36
-
37
- it('should work with one field (as array)', function() {
38
- var sifter = new Sifter([
39
- {field: 'aaa'},
40
- {field: 'add'},
41
- {field: 'abb'}
42
- ]);
43
- var result = sifter.search('', {
44
- fields: 'field',
45
- sort: [{field: 'field'}]
46
- });
47
- assert.equal(result.items[0].id, 0);
48
- assert.equal(result.items[1].id, 2);
49
- assert.equal(result.items[2].id, 1);
50
- });
51
-
52
- it('should work with multiple fields and respect priority', function() {
53
- var sifter = new Sifter([
54
- {a: 'bbb', b: 'bbb'},
55
- {a: 'bbb', b: 'ccc'},
56
- {a: 'bbb', b: 'aaa'},
57
- {a: 'aaa'}
58
- ]);
59
- var result = sifter.search('', {
60
- fields: 'field',
61
- sort: [
62
- {field: 'a'},
63
- {field: 'b'}
64
- ]
65
- });
66
- assert.equal(result.items[0].id, 3);
67
- assert.equal(result.items[1].id, 2);
68
- assert.equal(result.items[2].id, 0);
69
- assert.equal(result.items[3].id, 1);
70
- });
71
-
72
- it('should respect numeric fields', function() {
73
- var sifter = new Sifter([
74
- {field: 1.0},
75
- {field: 12.9},
76
- {field: 9.1},
77
- {field: -9.0}
78
- ]);
79
- var result = sifter.search('', {
80
- fields: 'field',
81
- sort: [{field: 'field'}]
82
- });
83
- assert.equal(result.items[0].id, 3);
84
- assert.equal(result.items[1].id, 0);
85
- assert.equal(result.items[2].id, 2);
86
- assert.equal(result.items[3].id, 1);
87
- });
88
-
89
- it('should respect sort direction', function() {
90
- var sifter = new Sifter([
91
- {a: 'bbb', b: 'rrr'},
92
- {a: 'bbb', b: 'aaa'},
93
- {a: 'aaa', b: 'rrr'},
94
- {a: 'aaa', b: 'aaa'}
95
- ]);
96
- var result = sifter.search('', {
97
- fields: 'field',
98
- sort: [
99
- {field: 'b', direction: 'desc'},
100
- {field: 'a', direction: 'asc'}
101
- ]
102
- });
103
- assert.equal(result.items[0].id, 2);
104
- assert.equal(result.items[1].id, 0);
105
- assert.equal(result.items[2].id, 3);
106
- assert.equal(result.items[3].id, 1);
107
- });
108
-
109
- it('should add implicit "$score" field when query present', function() {
110
- var sifter = new Sifter([
111
- {field: 'yoo'},
112
- {field: 'book'}
113
- ]);
114
- var result = sifter.search('oo', {
115
- fields: 'field',
116
- sort: [{field: 'field'}]
117
- });
118
- assert.equal(result.items[0].id, 0);
119
- assert.equal(result.items[1].id, 1);
120
- });
121
-
122
- it('should not add implicit "$score" field if explicitly given', function() {
123
- var sifter = new Sifter([
124
- {field: 'boooo'},
125
- {field: 'yoo'},
126
- {field: 'aaa'}
127
- ]);
128
- var result = sifter.search('oo', {
129
- filter: false,
130
- fields: 'field',
131
- sort: [{field: 'field'}, {field: '$score'}]
132
- });
133
- assert.equal(result.items[0].id, 2);
134
- assert.equal(result.items[1].id, 0);
135
- assert.equal(result.items[2].id, 1);
136
- });
137
-
138
- it('should be locale-aware', function() {
139
- var sifter = new Sifter([
140
- {field: 'Zoom Test'},
141
- {field: 'Água Test'}
142
- ]);
143
- var result = sifter.search('', {
144
- fields: 'field',
145
- sort: [{field: 'field', direction: 'asc'}]
146
- });
147
- assert.equal(result.items[0].id, 1);
148
- assert.equal(result.items[1].id, 0);
149
- });
150
-
151
- it('should work with nested fields', function() {
152
- var sifter = new Sifter([
153
- {fields: {nested: 'aaa'}},
154
- {fields: {nested: 'add'}},
155
- {fields: {nested: 'abb'}}
156
- ]);
157
- var result = sifter.search('', {
158
- fields: ['fields.nested'],
159
- sort: {field: 'fields.nested'},
160
- nesting: true
161
- });
162
- assert.equal(result.items[0].id, 0);
163
- assert.equal(result.items[1].id, 2);
164
- assert.equal(result.items[2].id, 1);
165
- });
166
-
167
- it('should not order results by $score', function() {
168
-
169
- var data = [
170
- {fielda:'aaab',$order:1},
171
- {fielda:'babaaa',$order:2},
172
- {fielda:'abaaaaa',$order:10},
173
- {fielda:'aabaaaaaaa',$order:12},
174
- {fielda:'abbaaaaa',$order:20},
175
- {fielda:'abaaaaaaa',$order:22},
176
- ];
177
-
178
- var sifter = new Sifter(data);
179
- var search = sifter.search('a', {
180
- fields: [{field: 'fielda'}],
181
- sort: [{field: '$order'},{field:'$score'}],
182
- });
183
-
184
-
185
- assert.equal(search.items[0].id,0);
186
- assert.equal(search.items[1].id,1);
187
- assert.equal(search.items[2].id,2);
188
- assert.equal(search.items[3].id,3);
189
- assert.equal(search.items[4].id,4);
190
- assert.equal(search.items[5].id,5);
191
- });
192
-
193
- });
package/test/tokenize.js DELETED
@@ -1,63 +0,0 @@
1
- var assert = require('assert');
2
- var Sifter = require('../build/cjs/sifter.js');
3
-
4
- describe('#tokenize()', function() {
5
- var sifter, tokens;
6
-
7
- it('should return an empty array when given an empty string', function() {
8
- var sifter = new Sifter([]);
9
- var tokens = sifter.tokenize('');
10
- assert.equal(tokens.length, 0);
11
- });
12
-
13
- it('should return an array', function() {
14
- var sifter = new Sifter([]);
15
- var tokens = sifter.tokenize('hello world');
16
- assert.equal(Array.isArray(tokens), true);
17
- });
18
-
19
- it('should split string by spaces', function() {
20
- var sifter = new Sifter([]);
21
- var tokens = sifter.tokenize('hello world');
22
- assert.equal(tokens.length, 2);
23
- });
24
-
25
- describe('returned tokens', function() {
26
- beforeAll(function() {
27
- sifter = new Sifter([]);
28
- tokens = sifter.tokenize('hello world');
29
- });
30
- describe('"string" property', function() {
31
- it('should exist', function() {
32
- assert.notEqual(typeof tokens[0].string, 'undefined');
33
- });
34
- it('should be a string', function() {
35
- assert.equal(typeof tokens[0].string, 'string');
36
- });
37
- it('should be valid', function() {
38
- assert.equal(tokens[0].string, 'hello');
39
- assert.equal(tokens[1].string, 'world');
40
- });
41
- });
42
- describe('"regex" property', function() {
43
- it('should exist', function() {
44
- assert.notEqual(typeof tokens[0].regex, 'undefined');
45
- });
46
- it('should be a RegExp object', function() {
47
- assert.equal(tokens[0].regex instanceof RegExp, true);
48
- });
49
- it('should ignore case', function() {
50
- assert.equal(tokens[0].regex.test('HelLO'), true);
51
- assert.equal(tokens[1].regex.test('wORLD'), true);
52
- });
53
- it('should not be too greedy', function() {
54
- assert.equal(tokens[0].regex.test('afawfaf'), false);
55
- });
56
- it('should match international characters', function() {
57
- assert.equal(tokens[0].regex.test('Ḧęŀlö'), true);
58
- assert.equal(tokens[1].regex.test('ẘÕⓡlḋ'), true);
59
- });
60
- });
61
- });
62
-
63
- });