Skip to content

Commit 9475ea5

Browse files
author
Brian Vaughn
committed
Throw an error if Search is instantiated without the required uidFieldName constructor parameter.
1 parent 2e20252 commit 9475ea5

File tree

7 files changed

+117
-1
lines changed

7 files changed

+117
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## 1.4.1
4+
Throw an error if `Search` is instantiated without the required `uidFieldName` constructor parameter.
5+
36
## 1.4.0
47
Search uid field can now be an array (for nested/deep keys).
58

benchmarks/index.html

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<html>
2+
<head>
3+
<script src="../dist/umd/js-search.js"></script>
4+
<script>
5+
var books;
6+
7+
fetch('./books.json', {
8+
method: 'get'
9+
}).then(async response => {
10+
var data = await response.json();
11+
books = data.books;
12+
13+
var search = new JsSearch.Search('isbn');
14+
search.addIndex('title');
15+
search.addIndex('author');
16+
17+
for (var i = 0; i < 10000; i++) {
18+
search.addDocuments(books);
19+
20+
search.search('the');
21+
search.search('an');
22+
search.search('of');
23+
}
24+
});
25+
</script>
26+
</head>
27+
<body></body>
28+
</html>

benchmarks/test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const Benchmark = require('benchmark');
2+
const bb = require('beautify-benchmark');
3+
4+
function doThing({
5+
foo,
6+
bar,
7+
baz,
8+
qux
9+
}) {
10+
return foo + bar + baz + qux;
11+
}
12+
function doThingAlt(foo, bar, baz, qux) {
13+
return foo + bar + baz + qux;
14+
}
15+
16+
new Benchmark.Suite()
17+
.on('cycle', (event) => {
18+
bb.add(event.target);
19+
})
20+
.on('complete', () => {
21+
bb.log();
22+
})
23+
.add('recreate', () => {
24+
for (var i = 1000; i--;) {
25+
doThing({
26+
// foo: Math.random(),
27+
// bar: Math.random(),
28+
// baz: Math.random(),
29+
qux: Math.random()
30+
});
31+
}
32+
})
33+
.add('recycle', () => {
34+
var doThingParams = {
35+
foo: null,
36+
bar: null,
37+
baz: null,
38+
qux: null
39+
}
40+
for (var i = 1000; i--;) {
41+
doThingParams.foo = Math.random()
42+
doThingParams.bar = Math.random()
43+
doThingParams.baz = Math.random()
44+
doThingParams.qux = Math.random()
45+
doThing(doThingParams);
46+
}
47+
})
48+
.add('unnamed', () => {
49+
for (var i = 1000; i--;) {
50+
doThingAlt(Math.random(), Math.random(), Math.random(), Math.random());
51+
}
52+
})
53+
.run({ 'async': true });

benchmarks/trace.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var { Search, AllSubstringsIndexStrategy, ExactWordIndexStrategy } = require('../dist/umd/js-search');
2+
3+
function identity(text) {
4+
return text;
5+
}
6+
7+
var search = new Search('id');
8+
search.indexStrategy = new AllSubstringsIndexStrategy();
9+
search.addIndex('text');
10+
11+
12+
for (var i = 0; i < 10000; i++) {
13+
search.addDocuments([
14+
{ id: 1, text: 'foo bar' },
15+
{ id: 2, text: 'bar baz' },
16+
{ id: 3, text: 'baz foo' }
17+
]);
18+
19+
search.search('foo');
20+
search.search('bar');
21+
search.search('baz');
22+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "js-search",
3-
"version": "1.4.0",
3+
"version": "1.4.1",
44
"description": "JS Search is an efficient, client-side search library for JavaScript and JSON objects",
55
"main": "dist/commonjs/index.js",
66
"directories": {

source/Search.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ export class Search {
3838
* to ensure that a search result set does not contain duplicate objects.
3939
*/
4040
constructor(uidFieldName : string | Array<string>) {
41+
if (!uidFieldName) {
42+
throw Error('js-search requires a uid field name constructor parameter');
43+
}
44+
4145
this._uidFieldName = uidFieldName;
4246

4347
// Set default/recommended strategies

source/Search.test.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ describe('Search', function() {
4444
}
4545
});
4646

47+
it('should throw an error if instantiated without the :uidFieldName parameter', function() {
48+
expect(function() {
49+
new Search();
50+
}).toThrow();
51+
});
52+
4753
it('should index a new document on all searchable fields', function() {
4854
search.addIndex('title');
4955
spyOn(search._indexStrategy, 'expandToken').and.returnValue([]);

0 commit comments

Comments
 (0)