Fixed search matching array indexes

This commit is contained in:
jos 2017-12-27 17:54:21 +01:00
parent 8bb9cb10cc
commit e5c6459f73
2 changed files with 19 additions and 3 deletions

View File

@ -84,8 +84,8 @@ export function esonToJson (eson) {
* Transform an eson object, traverse over the whole object (excluding the _meta) * Transform an eson object, traverse over the whole object (excluding the _meta)
* objects, and allow replacing Objects/Arrays/values * objects, and allow replacing Objects/Arrays/values
* @param {ESON} eson * @param {ESON} eson
* @param {function (eson, path)} callback * @param {function (ESON, Path) : ESON} callback
* @param {Path} path * @param {Path} [path]
* @return {ESON} * @return {ESON}
*/ */
export function transform (eson, callback, path = []) { export function transform (eson, callback, path = []) {
@ -243,7 +243,8 @@ export function search (eson, text) {
// check property name // check property name
const prop = last(path) const prop = last(path)
if (typeof prop === 'string' && text !== '' && containsCaseInsensitive(prop, text)) { if (text !== '' && containsCaseInsensitive(prop, text) &&
getIn(eson, initial(path))[META].type === 'Object') { // parent must be an Object
const searchState = isEmpty(matches) ? 'active' : 'normal' const searchState = isEmpty(matches) ? 'active' : 'normal'
matches.push({path, area: 'property'}) matches.push({path, area: 'property'})
updatedValue = setIn(updatedValue, [META, 'searchProperty'], searchState) updatedValue = setIn(updatedValue, [META, 'searchProperty'], searchState)

View File

@ -268,6 +268,21 @@ test('search', () => {
assertDeepEqualEson(esonWithSearch, expected) assertDeepEqualEson(esonWithSearch, expected)
}) })
test('search number', () => {
const eson = jsonToEson({
"2": "two",
"arr": ["a", "b", "c", "2"]
})
const result = search(eson, '2')
const matches = result.searchResult.matches
// should not match an array index, only props and values
expect(matches).toEqual([
{path: ['2'], area: 'property'},
{path: ['arr', '3'], area: 'value'}
])
})
test('nextSearchResult', () => { test('nextSearchResult', () => {
const eson = jsonToEson({ const eson = jsonToEson({
"obj": { "obj": {