Order search results

This commit is contained in:
jos 2018-09-19 14:50:53 +02:00
parent e298d63311
commit 3e795b3275
5 changed files with 71 additions and 1 deletions

View File

@ -8,6 +8,8 @@ import isEqual from 'lodash/isEqual'
import naturalSort from 'javascript-natural-sort' import naturalSort from 'javascript-natural-sort'
import times from 'lodash/times' import times from 'lodash/times'
import { immutableJSONPatch } from './immutableJSONPatch' import { immutableJSONPatch } from './immutableJSONPatch'
import { compareArrays } from './utils/arrayUtils'
import { compareStrings } from './utils/stringUtils'
export const ID = typeof Symbol === 'function' ? Symbol('id') : '@jsoneditor-id' export const ID = typeof Symbol === 'function' ? Symbol('id') : '@jsoneditor-id'
export const TYPE = typeof Symbol === 'function' ? Symbol('type') : '@jsoneditor-type' // 'object', 'array', 'value', or 'undefined' export const TYPE = typeof Symbol === 'function' ? Symbol('type') : '@jsoneditor-type' // 'object', 'array', 'value', or 'undefined'
@ -247,6 +249,18 @@ export function search (eson, text) {
return updatedValue return updatedValue
}) })
matches.sort((a, b) => {
const arrayOrder = compareArrays(a.path, b.path)
if (arrayOrder !== 0) {
return arrayOrder
}
// order property first, value second.
// we can use regular string ordering here because the word
// "property" comes before "value"
return compareStrings(a.area, b.area)
})
return { return {
eson: updatedEson, eson: updatedEson,
searchResult: { searchResult: {

View File

@ -44,3 +44,19 @@ export function strictShallowEqual (a, b) {
return true return true
} }
export function compareArrays(a, b) {
const minLength = Math.min(a.length, b.length)
for (let i = 0; i < minLength; i++) {
if (a[i] < b[i]) {
return -1
}
if (a[i] > b[i]) {
return 1
}
}
return a.length - b.length
}

View File

@ -0,0 +1,27 @@
import { compareArrays } from './arrayUtils'
test('compareArrays', () => {
expect(compareArrays([], [])).toEqual(0)
expect(compareArrays(['a'], ['a'])).toEqual(0)
expect(compareArrays(['a'], ['b'])).toEqual(-1)
expect(compareArrays(['b'], ['a'])).toEqual(1)
expect(compareArrays(['a'], ['a', 'b'])).toEqual(-1)
expect(compareArrays(['a', 'b'], ['a'])).toEqual(1)
expect(compareArrays(['a', 'b'], ['a', 'b'])).toEqual(0)
const arrays = [
['b', 'a'],
['a'],
[],
['b', 'c'],
['b'],
]
expect(arrays.sort(compareArrays)).toEqual([
[],
['a'],
['b'],
['b', 'a'],
['b', 'c']
])
})

View File

@ -123,3 +123,7 @@ export function toCapital(text) {
? text[0].toUpperCase() + text.substr(1).toLowerCase() ? text[0].toUpperCase() + text.substr(1).toLowerCase()
: text : text
} }
export function compareStrings (a, b) {
return (a < b) ? -1 : (a > b) ? 1 : 0
}

View File

@ -1,4 +1,4 @@
import { escapeHTML, unescapeHTML, findUniqueName, toCapital } from './stringUtils' import { escapeHTML, unescapeHTML, findUniqueName, toCapital, compareStrings } from './stringUtils'
test('escapeHTML', () => { test('escapeHTML', () => {
expect(escapeHTML(' hello ')).toEqual('\u00A0\u00A0 hello \u00A0') expect(escapeHTML(' hello ')).toEqual('\u00A0\u00A0 hello \u00A0')
@ -31,3 +31,12 @@ test('toCapital', () => {
expect(toCapital('')).toEqual('') expect(toCapital('')).toEqual('')
expect(toCapital(undefined)).toEqual(undefined) expect(toCapital(undefined)).toEqual(undefined)
}) })
test('compareStrings', () => {
expect(compareStrings('a', 'b')).toEqual(-1)
expect(compareStrings('b', 'a')).toEqual(1)
expect(compareStrings('a', 'a')).toEqual(0)
const array = ['b', 'c', 'a']
expect(array.sort(compareStrings)).toEqual(['a', 'b', 'c'])
})