@wholebuzz/query
v0.0.3
Published
Parse text query language to object filter
Downloads
3
Readme
@wholebuzz/query
Regex extended filtrex object query language.
Example
import { parseFilterQuery } from '@wholebuzz/query/lib/query'
interface Record {
title: string
count: number
}
const records: Record[] = [
{ title: 'Blues guitar volume 1', count: 35 },
{ title: 'Rock guitar volume 3', count: 46 },
{ title: 'Blues harmonica volume 1', count: 10 },
]
const parseRecordQuery = (q: string) =>
parseFilterQuery<Record>(q, new Set(['title', 'count']), ['title'])
// [
// { title: 'Blues guitar volume 1', count: 35 },
// { title: 'Blues harmonica volume 1', count: 10 }
// ]
console.log(records.filter(parseRecordQuery('blues')))
//[ { title: 'Blues guitar volume 1', count: 35 } ]
console.log(records.filter(parseRecordQuery('blues count>10')))
// [
// { title: 'Blues guitar volume 1', count: 35 },
// { title: 'Rock guitar volume 3', count: 46 }
// ]
console.log(records.filter(parseRecordQuery('count>=35')))
// [ { title: 'Rock guitar volume 3', count: 46 } ]
console.log(records.filter(parseRecordQuery('rock')))
Table of contents
Interfaces
Variables
Functions
Variables
split
• Const
split: any
Defined in: query.ts:3
Functions
parseFilterQuery
▸ parseFilterQuery<X>(docs/query
: string, objectFields
: Set<string>, defaultFields?
: string[], extraFunctions?
: { [T: string]: Function; }, customProps?
: { [T: string]: (x
: X) => any; }, options?
: ParseFilterQueryOptions): function
Creates regex-expanded Filtrex object-filter described by query.
optional
defaultFields The default fields to text search for non-property-query-terms.
optional
extraFunctions Map of extra functions to make available to query, e.g. startsWith(docs/).
optional
customProps Custom properties to make available to query, e.g. .authors.
optional
options Extra query options.
Type parameters
| Name |
| :------ |
| X
|
Parameters
| Name | Type | Default value | Description |
| :------ | :------ | :------ | :------ |
| query
| string | - | The query describing the filter. |
| objectFields
| Set<string> | - | The object fields to make accessible to the query. |
| defaultFields
| string[] | [] | - |
| extraFunctions
| object | {} | - |
| customProps
| object | {} | - |
| options?
| ParseFilterQueryOptions | - | - |
Returns: (docs/x
: X) => boolean
Defined in: query.ts:21