Sieving
Query-Based Item-List Reduction for JavaScript
About
Sieving is a JavaScript library for reducing/filtering a JavaScript
list of items, based on a simple simple query language. It is intended
to be used for item list filtering or item list searching purposes
within Browser or Node.js environments. Internally, Sieving is based
on a full-blown query parser, supports optional fuzzy matching and
namespacing of items, and supports union, intersection and subtraction
of the result sets.
A query like...
foo bar^ +baz -quux, baz
...on a result set basis and mathematically means...
(((MATCH("foo") ∩ BOOST(MATCH("bar"), 1)) ∪ MATCH("baz")) ∖ MATCH("quux")) ∪ MATCH("baz")
...or expressed in a functional way:
UNION(
SUBTRACT(
UNION(
INTERSECT(
MATCH("foo"),
BOOST(MATCH("bar"), 1)),
MATCH("baz")),
MATCH("quux")),
MATCH("baz"))
Installation
$ npm install sieving
Usage
const Sieving = require("sieving")
let items = [ "foo", "bar", "baz", "quux", "foo bar", "foo baz", "foo quux", "foo bar quux" ]
let sieving = new Sieving()
sieving.parse("foo +bar -quux, baz^")
console.log(sieving.format())
console.log(sieving.dump())
let result = sieving.sieve(items)
console.log(result)
result = Sieving.sieve(items, "foo +bar -quux, baz^")
console.log(result)
Output:
queries [1,1]
├── query [1,1]
│ ├── term (value: "foo", type: "bare") [1,1]
│ ├── term (op: "union", value: "bar", type: "bare") [1,5]
│ └── term (op: "subtraction", value: "quux", type: "bare") [1,10]
└── query [1,17]
└── term (value: "baz", type: "bare", boost: 1) [1,17]
[ 'baz', 'foo', 'foo bar', 'foo baz', 'bar' ]
[ 'baz', 'foo', 'foo bar', 'foo baz', 'bar' ]
Examples
const { sieve } = require("sieving")
const { expect } = require("chai")
let items = [ "foo", "bar", "baz", "quux", "foo bar baz quux" ]
expect(sieve(items, "foo")) .deep.equal([ "foo", "foo bar baz quux" ])
expect(sieve(items, "fo?")) .deep.equal([ "foo", "foo bar baz quux" ])
expect(sieve(items, "/fo./")).deep.equal([ "foo", "foo bar baz quux" ])
expect(sieve(items, "'foo'")).deep.equal([ "foo" ])
expect(sieve(items, "bax", { fuzzy: true })).deep.equal([ "bar", "baz" ])
expect(sieve(items, "fox", { fuzzy: true })).deep.equal([ "foo" ])
expect(sieve(items, "qxux", { fuzzy: true })).deep.equal([ "quux" ])
expect(sieve(items, "foo")) .deep.equal([ "foo", "foo bar baz quux" ])
expect(sieve(items, "foo bar")) .deep.equal([ "foo bar baz quux" ])
expect(sieve(items, "foo +bar")) .deep.equal([ "foo", "foo bar baz quux", "bar" ])
expect(sieve(items, "foo +bar -baz")).deep.equal([ "foo", "bar" ])
expect(sieve(items, "foo, bar")) .deep.equal([ "foo", "foo bar baz quux", "bar" ])
expect(sieve(items, "bar, foo")) .deep.equal([ "bar", "foo bar baz quux", "foo" ])
Query Syntax
The following is a symbolic grammar describing the supported
query syntax. For more subtle details, see the actual PEG grammar
of the underlying parser.
queries ::= query ("," query)* // union of queries
query ::= term (" " term)* // intersection of terms
term ::= operation? namespace? text boost? // single query term
operation ::= "+" | "-" // force union or subtraction of term
namespace ::= symbol | id ":" // match against a particular namespace
text ::= quoted | regexp | glob | bareword // four variants of the term
boost ::= "^" number? // optionally boost the results
quoted ::= /"(\\"|[^"])*"/ | /'(\\'|[^'])*'/ // double- or single-quoted term
regexp ::= /\/(\\\/|[^\/])*\// // regular expression term
glob ::= /.*[*?[\]{}].*/ // glob-style term
bareword ::= /.+/ // bareword term
number ::= /\d*\.\d+/ | /\d+/ // floating or integer number
symbol ::= /^[$#%@&]$/ // namespace symbol
id ::= /^[a-zA-Z][a-zA-Z0-9-_]*$/ // namespace identifier
Application Programming Interface (API)
The following is the API as a TypeScript declaration.
See also the actual TypeScript definition file.
declare module "Sieving" {
class Sieving {
public constructor(
options?: {
fieldsVal: string[], /* names of fields in items of type object (default: [ "value" ]) */
fieldId: string /* name of optional field of unique identifier in items of type object (default: "id") */
}
)
parse(
query: string,
options?: {
lts: boolean,
ast: boolean
}
): void
dump(
colorize?: boolean
): string
format(
format: string
): string
evaluate(
queryResults: (
ns: string,
type: string,
value: string
) => any[]
): any[]
sieve(
items: any[],
options?: {
fuzzy: boolean,
nocase: boolean,
maxLS: number,
minDC: number
}
): any[]
static sieve(
items: any[],
query: string,
options?: {
fieldsVal: string[],
fieldId: string
fuzzy: boolean,
nocase: boolean,
maxLS: number,
minDC: number
debug: boolean
}
): any[]
}
export = Sieving
}
License
Copyright © 2018-2024 Dr. Ralf S. Engelschall (http://engelschall.com/)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.