项目作者: rse

项目描述 :
Query-Based Item-List Reduction
高级语言: JavaScript
项目地址: git://github.com/rse/sieving.git
创建时间: 2018-03-18T17:45:01Z
项目社区:https://github.com/rse/sieving

开源协议:

下载


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…

  1. foo bar^ +baz -quux, baz

…on a result set basis and mathematically means…

  1. (((MATCH("foo") BOOST(MATCH("bar"), 1)) MATCH("baz")) MATCH("quux")) MATCH("baz")

…or expressed in a functional way:

  1. UNION(
  2. SUBTRACT(
  3. UNION(
  4. INTERSECT(
  5. MATCH("foo"),
  6. BOOST(MATCH("bar"), 1)),
  7. MATCH("baz")),
  8. MATCH("quux")),
  9. MATCH("baz"))

Installation

  1. $ npm install sieving

Usage

  1. const Sieving = require("sieving")
  2. let items = [ "foo", "bar", "baz", "quux", "foo bar", "foo baz", "foo quux", "foo bar quux" ]
  3. /* step-by-step usage */
  4. let sieving = new Sieving()
  5. sieving.parse("foo +bar -quux, baz^")
  6. console.log(sieving.format())
  7. console.log(sieving.dump())
  8. let result = sieving.sieve(items)
  9. console.log(result)
  10. /* all-in-one usage */
  11. result = Sieving.sieve(items, "foo +bar -quux, baz^")
  12. console.log(result)

Output:

  1. queries [1,1]
  2. ├── query [1,1]
  3. ├── term (value: "foo", type: "bare") [1,1]
  4. ├── term (op: "union", value: "bar", type: "bare") [1,5]
  5. └── term (op: "subtraction", value: "quux", type: "bare") [1,10]
  6. └── query [1,17]
  7. └── term (value: "baz", type: "bare", boost: 1) [1,17]
  8. [ 'baz', 'foo', 'foo bar', 'foo baz', 'bar' ]
  9. [ 'baz', 'foo', 'foo bar', 'foo baz', 'bar' ]

Examples

  1. const { sieve } = require("sieving")
  2. const { expect } = require("chai")
  3. let items = [ "foo", "bar", "baz", "quux", "foo bar baz quux" ]
  4. expect(sieve(items, "foo")) .deep.equal([ "foo", "foo bar baz quux" ])
  5. expect(sieve(items, "fo?")) .deep.equal([ "foo", "foo bar baz quux" ])
  6. expect(sieve(items, "/fo./")).deep.equal([ "foo", "foo bar baz quux" ])
  7. expect(sieve(items, "'foo'")).deep.equal([ "foo" ])
  8. expect(sieve(items, "bax", { fuzzy: true })).deep.equal([ "bar", "baz" ])
  9. expect(sieve(items, "fox", { fuzzy: true })).deep.equal([ "foo" ])
  10. expect(sieve(items, "qxux", { fuzzy: true })).deep.equal([ "quux" ])
  11. expect(sieve(items, "foo")) .deep.equal([ "foo", "foo bar baz quux" ])
  12. expect(sieve(items, "foo bar")) .deep.equal([ "foo bar baz quux" ])
  13. expect(sieve(items, "foo +bar")) .deep.equal([ "foo", "foo bar baz quux", "bar" ])
  14. expect(sieve(items, "foo +bar -baz")).deep.equal([ "foo", "bar" ])
  15. expect(sieve(items, "foo, bar")) .deep.equal([ "foo", "foo bar baz quux", "bar" ])
  16. 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.

  1. queries ::= query ("," query)* // union of queries
  2. query ::= term (" " term)* // intersection of terms
  3. term ::= operation? namespace? text boost? // single query term
  4. operation ::= "+" | "-" // force union or subtraction of term
  5. namespace ::= symbol | id ":" // match against a particular namespace
  6. text ::= quoted | regexp | glob | bareword // four variants of the term
  7. boost ::= "^" number? // optionally boost the results
  8. quoted ::= /"(\\"|[^"])*"/ | /'(\\'|[^'])*'/ // double- or single-quoted term
  9. regexp ::= /\/(\\\/|[^\/])*\// // regular expression term
  10. glob ::= /.*[*?[\]{}].*/ // glob-style term
  11. bareword ::= /.+/ // bareword term
  12. number ::= /\d*\.\d+/ | /\d+/ // floating or integer number
  13. symbol ::= /^[$#%@&]$/ // namespace symbol
  14. 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.

  1. declare module "Sieving" {
  2. class Sieving {
  3. /* create Sieving instance */
  4. public constructor(
  5. options?: {
  6. fieldsVal: string[], /* names of fields in items of type object (default: [ "value" ]) */
  7. fieldId: string /* name of optional field of unique identifier in items of type object (default: "id") */
  8. }
  9. )
  10. /* parse query into an internal AST */
  11. parse(
  12. query: string, /* query string */
  13. options?: {
  14. lts: boolean, /* parse query string into LST (default: true) */
  15. ast: boolean /* parse query string into AST (default: true) */
  16. }
  17. ): void
  18. /* dump internal AST as text (for debugging purposes only) */
  19. dump(
  20. colorize?: boolean /* whether to colorize output (default: true) */
  21. ): string
  22. /* format internal AST into query string */
  23. format(
  24. format: string /* format of output (default "text", or "html", "xml" or "json") */
  25. ): string
  26. /* evaluate internal AST (for custom matching) */
  27. evaluate(
  28. queryResults: (
  29. ns: string, /* term namespace (default: "") */
  30. type: string, /* term type ("regexp", "glob", "squoted", "dquoted", or "bareword") */
  31. value: string /* term value */
  32. ) => any[]
  33. ): any[]
  34. /* sieve items by evaluating query with standard matching */
  35. sieve(
  36. items: any[], /* list of items to sieve/filter */
  37. options?: {
  38. fuzzy: boolean, /* whether to fuzzy match quoted and bare terms (default: false) */
  39. nocase: boolean, /* whether to match case-insensitive (default: false) */
  40. maxLS: number, /* maximum Levenshtein distance for fuzzy matching (default: 2) */
  41. minDC: number /* minimum Dice-Coefficient for fuzzy matching (default: 0.50) */
  42. }
  43. ): any[]
  44. /* sieve items by evaluating query with standard matching (stand-alone) */
  45. static sieve(
  46. items: any[], /* list of items to sieve/filter */
  47. query: string, /* query string */
  48. options?: {
  49. fieldsVal: string[], /* names of fields in items of type object (default: [ "value" ]) */
  50. fieldId: string /* name of optional field of unique identifier in items of type object (default: "id") */
  51. fuzzy: boolean, /* whether to fuzzy match quoted and bare terms (default: false) */
  52. nocase: boolean, /* whether to match case-insensitive (default: false) */
  53. maxLS: number, /* maximum Levenshtein distance for fuzzy matching (default: 2) */
  54. minDC: number /* minimum Dice-Coefficient for fuzzy matching (default: 0.50) */
  55. debug: boolean /* whether to dump the internal AST to stdout */
  56. }
  57. ): any[]
  58. }
  59. export = Sieving
  60. }

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.