Skip to content

Commit 226d868

Browse files
committed
fix: handle booleans in the data
With this update, booleans are stringified and search correclty. closes #469
1 parent 99ce7f7 commit 226d868

4 files changed

Lines changed: 70 additions & 10 deletions

File tree

dist/fuse.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,8 @@
213213
}
214214

215215
function isArray(value) {
216-
return !Array.isArray ? Object.prototype.toString.call(value) === '[object Array]' : Array.isArray(value);
217-
} // Adapted from:
218-
// https://github.com/lodash/lodash/blob/f4ca396a796435422bd4fd41fadbd225edddf175/.internal/baseToString.js
216+
return !Array.isArray ? getTag(value) === '[object Array]' : Array.isArray(value);
217+
} // Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
219218

220219
var INFINITY = 1 / 0;
221220
function baseToString(value) {
@@ -235,15 +234,28 @@
235234
}
236235
function isNumber(value) {
237236
return typeof value === 'number';
237+
} // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
238+
239+
function isBoolean(value) {
240+
return value === true || value === false || isObjectLike(value) && getTag(value) == '[object Boolean]';
238241
}
239242
function isObject(value) {
240243
return _typeof(value) === 'object';
244+
} // Checks if `value` is object-like.
245+
246+
function isObjectLike(value) {
247+
return isObject(value) && value !== null;
241248
}
242249
function isDefined(value) {
243250
return value !== undefined && value !== null;
244251
}
245252
function isBlank(value) {
246253
return !value.trim().length;
254+
} // Gets the `toStringTag` of `value`.
255+
// Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
256+
257+
function getTag(value) {
258+
return value == null ? value === undefined ? '[object Undefined]' : '[object Null]' : Object.prototype.toString.call(value);
247259
}
248260

249261
var EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
@@ -364,9 +376,11 @@
364376

365377
if (!isDefined(value)) {
366378
return;
367-
}
379+
} // If we're at the last value in the path, and if it's a string/number/bool,
380+
// add it to the list
381+
368382

369-
if (index === path.length - 1 && (isString(value) || isNumber(value))) {
383+
if (index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
370384
list.push(toString(value));
371385
} else if (isArray(value)) {
372386
arr = true; // Search each item in the array.

src/helpers/get.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { isDefined, isString, isNumber, isArray, toString } from './types'
1+
import {
2+
isDefined,
3+
isString,
4+
isNumber,
5+
isBoolean,
6+
isArray,
7+
toString
8+
} from './types'
29

310
export default function get(obj, path) {
411
let list = []
@@ -17,7 +24,12 @@ export default function get(obj, path) {
1724
return
1825
}
1926

20-
if (index === path.length - 1 && (isString(value) || isNumber(value))) {
27+
// If we're at the last value in the path, and if it's a string/number/bool,
28+
// add it to the list
29+
if (
30+
index === path.length - 1 &&
31+
(isString(value) || isNumber(value) || isBoolean(value))
32+
) {
2133
list.push(toString(value))
2234
} else if (isArray(value)) {
2335
arr = true

src/helpers/types.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
export function isArray(value) {
22
return !Array.isArray
3-
? Object.prototype.toString.call(value) === '[object Array]'
3+
? getTag(value) === '[object Array]'
44
: Array.isArray(value)
55
}
66

7-
// Adapted from:
8-
// https://github.com/lodash/lodash/blob/f4ca396a796435422bd4fd41fadbd225edddf175/.internal/baseToString.js
7+
// Adapted from: https://github.com/lodash/lodash/blob/master/.internal/baseToString.js
98
const INFINITY = 1 / 0
109
export function baseToString(value) {
1110
// Exit early for strings to avoid a performance hit in some environments.
@@ -28,14 +27,38 @@ export function isNumber(value) {
2827
return typeof value === 'number'
2928
}
3029

30+
// Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
31+
export function isBoolean(value) {
32+
return (
33+
value === true ||
34+
value === false ||
35+
(isObjectLike(value) && getTag(value) == '[object Boolean]')
36+
)
37+
}
38+
3139
export function isObject(value) {
3240
return typeof value === 'object'
3341
}
3442

43+
// Checks if `value` is object-like.
44+
export function isObjectLike(value) {
45+
return isObject(value) && value !== null
46+
}
47+
3548
export function isDefined(value) {
3649
return value !== undefined && value !== null
3750
}
3851

3952
export function isBlank(value) {
4053
return !value.trim().length
4154
}
55+
56+
// Gets the `toStringTag` of `value`.
57+
// Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
58+
function getTag(value) {
59+
return value == null
60+
? value === undefined
61+
? '[object Undefined]'
62+
: '[object Null]'
63+
: Object.prototype.toString.call(value)
64+
}

test/fuzzy-search.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,3 +1127,14 @@ describe('Standard dotted keys', () => {
11271127
expect(result).toHaveLength(2)
11281128
})
11291129
})
1130+
1131+
describe('Breaking values', () => {
1132+
test('Non-strings are still processed', () => {
1133+
const data = [{ first: false }]
1134+
const options = { keys: [{ name: 'first' }] }
1135+
const fuse = new Fuse(data, options)
1136+
1137+
const result = fuse.search('fa')
1138+
expect(result).toHaveLength(1)
1139+
})
1140+
})

0 commit comments

Comments
 (0)