Skip to content

Commit

Permalink
fix: handle booleans in the data
Browse files Browse the repository at this point in the history
With this update, booleans are stringified and search correclty.

closes #469
  • Loading branch information
krisk committed Jul 26, 2020
1 parent 99ce7f7 commit 226d868
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 10 deletions.
24 changes: 19 additions & 5 deletions dist/fuse.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,8 @@
}

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

var INFINITY = 1 / 0;
function baseToString(value) {
Expand All @@ -235,15 +234,28 @@
}
function isNumber(value) {
return typeof value === 'number';
} // Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js

function isBoolean(value) {
return value === true || value === false || isObjectLike(value) && getTag(value) == '[object Boolean]';
}
function isObject(value) {
return _typeof(value) === 'object';
} // Checks if `value` is object-like.

function isObjectLike(value) {
return isObject(value) && value !== null;
}
function isDefined(value) {
return value !== undefined && value !== null;
}
function isBlank(value) {
return !value.trim().length;
} // Gets the `toStringTag` of `value`.
// Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js

function getTag(value) {
return value == null ? value === undefined ? '[object Undefined]' : '[object Null]' : Object.prototype.toString.call(value);
}

var EXTENDED_SEARCH_UNAVAILABLE = 'Extended search is not available';
Expand Down Expand Up @@ -364,9 +376,11 @@

if (!isDefined(value)) {
return;
}
} // If we're at the last value in the path, and if it's a string/number/bool,
// add it to the list


if (index === path.length - 1 && (isString(value) || isNumber(value))) {
if (index === path.length - 1 && (isString(value) || isNumber(value) || isBoolean(value))) {
list.push(toString(value));
} else if (isArray(value)) {
arr = true; // Search each item in the array.
Expand Down
16 changes: 14 additions & 2 deletions src/helpers/get.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { isDefined, isString, isNumber, isArray, toString } from './types'
import {
isDefined,
isString,
isNumber,
isBoolean,
isArray,
toString
} from './types'

export default function get(obj, path) {
let list = []
Expand All @@ -17,7 +24,12 @@ export default function get(obj, path) {
return
}

if (index === path.length - 1 && (isString(value) || isNumber(value))) {
// If we're at the last value in the path, and if it's a string/number/bool,
// add it to the list
if (
index === path.length - 1 &&
(isString(value) || isNumber(value) || isBoolean(value))
) {
list.push(toString(value))
} else if (isArray(value)) {
arr = true
Expand Down
29 changes: 26 additions & 3 deletions src/helpers/types.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
export function isArray(value) {
return !Array.isArray
? Object.prototype.toString.call(value) === '[object Array]'
? getTag(value) === '[object Array]'
: Array.isArray(value)
}

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

// Adapted from: https://github.com/lodash/lodash/blob/master/isBoolean.js
export function isBoolean(value) {
return (
value === true ||
value === false ||
(isObjectLike(value) && getTag(value) == '[object Boolean]')
)
}

export function isObject(value) {
return typeof value === 'object'
}

// Checks if `value` is object-like.
export function isObjectLike(value) {
return isObject(value) && value !== null
}

export function isDefined(value) {
return value !== undefined && value !== null
}

export function isBlank(value) {
return !value.trim().length
}

// Gets the `toStringTag` of `value`.
// Adapted from: https://github.com/lodash/lodash/blob/master/.internal/getTag.js
function getTag(value) {
return value == null
? value === undefined
? '[object Undefined]'
: '[object Null]'
: Object.prototype.toString.call(value)
}
11 changes: 11 additions & 0 deletions test/fuzzy-search.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1127,3 +1127,14 @@ describe('Standard dotted keys', () => {
expect(result).toHaveLength(2)
})
})

describe('Breaking values', () => {
test('Non-strings are still processed', () => {
const data = [{ first: false }]
const options = { keys: [{ name: 'first' }] }
const fuse = new Fuse(data, options)

const result = fuse.search('fa')
expect(result).toHaveLength(1)
})
})

0 comments on commit 226d868

Please sign in to comment.