feat(complex-matcher) : raw numbers can match strings (#3552)

Fixes #2906
This commit is contained in:
Enishowk 2018-10-17 16:24:50 +00:00 committed by Julien Fontanet
parent 64d295ee3f
commit 1a528adfbb
4 changed files with 40 additions and 3 deletions

View File

@ -13,6 +13,7 @@
- [Usage Report] Add top 3 VMs which use the most IOPS read/write/total [#3308](https://github.com/vatesfr/xen-orchestra/issues/3308) (PR [#3463](https://github.com/vatesfr/xen-orchestra/pull/3463))
- [Settings/logs] Homogenize action buttons in table and enable bulk deletion [#3179](https://github.com/vatesfr/xen-orchestra/issues/3179) (PR [#3528](https://github.com/vatesfr/xen-orchestra/pull/3528))
- [Settings/acls] Add bulk deletion [#3179](https://github.com/vatesfr/xen-orchestra/issues/3179) (PR [#3536](https://github.com/vatesfr/xen-orchestra/pull/3536))
- [Home] Improve search usage: raw numbers also match in names [#2906](https://github.com/vatesfr/xen-orchestra/issues/2906) (PR [#3552](https://github.com/vatesfr/xen-orchestra/pull/3552))
### Bug fixes
@ -26,6 +27,7 @@
- xo-common v0.1.2
- @xen-orchestra/fs v0.4.0
- complex-matcher v0.5.0
- vhd-lib v0.4.0
- xen-api v0.20.0
- xo-server-usage-report v0.7.0

View File

@ -11,7 +11,7 @@ export const ast = new CM.And([
new CM.Or([new CM.String('wonderwoman'), new CM.String('batman')])
),
new CM.TruthyProperty('hasCape'),
new CM.Property('age', new CM.Number(32)),
new CM.Property('age', new CM.NumberOrStringNode('32')),
new CM.GlobPattern('chi*go'),
new CM.RegExp('^foo/bar\\.', 'i'),
])

View File

@ -153,6 +153,34 @@ export class NumberNode extends Node {
}
export { NumberNode as Number }
export class NumberOrStringNode extends Node {
constructor (value) {
super()
this.value = value
// should not be enumerable for the tests
Object.defineProperty(this, 'match', {
value: this.match.bind(this, value.toLowerCase(), +value),
})
}
match (lcValue, numValue, value) {
return (
value === numValue ||
(typeof value === 'string'
? value.toLowerCase().indexOf(lcValue) !== -1
: (Array.isArray(value) || isPlainObject(value)) &&
some(value, this.match))
)
}
toString () {
return this.value
}
}
export { NumberOrStringNode as NumberOrString }
export class Property extends Node {
constructor (name, child) {
super()
@ -564,7 +592,7 @@ const parser = P.grammar({
const asNum = +str
return Number.isNaN(asNum)
? new GlobPattern(str)
: new NumberNode(asNum)
: new NumberOrStringNode(str)
})
),
ws: P.regex(/\s*/),

View File

@ -6,6 +6,7 @@ import {
GlobPattern,
Null,
NumberNode,
NumberOrStringNode,
parse,
setPropertyClause,
} from './'
@ -32,7 +33,7 @@ describe('parse', () => {
node = parse('32')
expect(node.match(32)).toBe(true)
expect(node.match('32')).toBe(false)
expect(node.match('32')).toBe(true)
expect(node.toString()).toBe('32')
node = parse('"32"')
@ -54,6 +55,12 @@ describe('Number', () => {
})
})
describe('NumberOrStringNode', () => {
it('match a string', () => {
expect(new NumberOrStringNode('123').match([{ foo: '123' }])).toBe(true)
})
})
describe('setPropertyClause', () => {
it('creates a node if none passed', () => {
expect(setPropertyClause(undefined, 'foo', 'bar').toString()).toBe(