queryfilters
Advanced tools
Comparing version 0.3.31 to 0.3.32
@@ -6,378 +6,388 @@ "use strict"; | ||
function Combinators() { | ||
var COMBINATORS_AND = "and"; | ||
var COMBINATORS_DEFAULT = "filtering"; | ||
var COMBINATORS_FILTERING_OR = "filtering_or"; | ||
var COMBINATORS_OR = "or"; | ||
const COMBINATORS_AND = "and"; | ||
const COMBINATORS_DEFAULT = "filtering"; | ||
const COMBINATORS_FILTERING_OR = "filtering_or"; | ||
const COMBINATORS_OR = "or"; | ||
this.getCombinators = function () { | ||
return [ | ||
COMBINATORS_AND, | ||
COMBINATORS_DEFAULT, | ||
COMBINATORS_FILTERING_OR, | ||
COMBINATORS_OR | ||
]; | ||
}; | ||
this.getCombinators = function () { | ||
return [ | ||
COMBINATORS_AND, | ||
COMBINATORS_DEFAULT, | ||
COMBINATORS_FILTERING_OR, | ||
COMBINATORS_OR | ||
]; | ||
}; | ||
this.getDefaultCombinator = function () { | ||
return COMBINATORS_DEFAULT; | ||
}; | ||
this.getDefaultCombinator = function () { | ||
return COMBINATORS_DEFAULT; | ||
}; | ||
} | ||
function QueryStringBuilder() { | ||
this.filters = []; | ||
function QueryStringBuilder(options) { | ||
this.filters = []; | ||
this.sorters = []; | ||
this.sorters = []; | ||
var combinators = new Combinators(); | ||
const combinators = new Combinators(); | ||
this.combinators = combinators.getCombinators(); | ||
this.combinators = combinators.getCombinators(); | ||
this.combine = combinators.getDefaultCombinator(); | ||
this.combine = combinators.getDefaultCombinator(); | ||
this.ensureHaveValidCombinator = function () { | ||
if (!this.combinators.includes(this.getCombinator())) { | ||
throw new Error("combinator " + this.getCombinator() + " is not available"); | ||
} | ||
return this; | ||
}; | ||
this.ensureHaveValidCombinator = function () { | ||
if (!this.combinators.includes(this.getCombinator())) { | ||
throw new Error("combinator " + this.getCombinator() + " is not available"); | ||
} | ||
return this; | ||
}; | ||
this.setCombinator = function (combinator) { | ||
this.combine = combinator; | ||
return this; | ||
}; | ||
this.setCombinator = function (combinator) { | ||
this.combine = combinator; | ||
return this; | ||
}; | ||
this.getCombinator = function () { | ||
return this.combine; | ||
}; | ||
this.getCombinator = function () { | ||
return this.combine; | ||
}; | ||
this.setSorters = function (sorters) { | ||
this.sorters = sorters; | ||
return this; | ||
}; | ||
this.setSorters = function (sorters) { | ||
this.sorters = sorters; | ||
return this; | ||
}; | ||
this.setFilters = function (filters) { | ||
this.filters = filters; | ||
return this; | ||
}; | ||
this.setFilters = function (filters) { | ||
this.filters = filters; | ||
return this; | ||
}; | ||
this.setRestrictions = function(restrictions) { | ||
this.restrictions = restrictions; | ||
return this; | ||
}; | ||
this.setRestrictions = function(restrictions) { | ||
this.restrictions = restrictions; | ||
return this; | ||
}; | ||
this.build = function (opt) { | ||
var qs = ""; | ||
var rel = ""; | ||
var filterKeys = Object.keys(this.filters); | ||
var i = 0, l = 0, filter = [], splitted = []; | ||
var relationAdded = []; | ||
for (l = filterKeys.length; i < l; i += 1) { | ||
filter = this.filters[filterKeys[i]]; | ||
this.build = function (opt) { | ||
let qs = ""; | ||
let rel = ""; | ||
const filterKeys = Object.keys(this.filters); | ||
let i = 0, l = 0, filter = [], splitted = []; | ||
const relationAdded = []; | ||
for (l = filterKeys.length; i < l; i += 1) { | ||
filter = this.filters[filterKeys[i]]; | ||
if (this.containsRelations(filter)) { | ||
splitted = filter.field.split("."); | ||
splitted.pop(); | ||
if (this.containsEmbedded(filter)) { | ||
splitted.shift(); | ||
} | ||
if (this.containsRelations(filter)) { | ||
splitted = filter.field.split("."); | ||
splitted.pop(); | ||
if (this.containsEmbedded(filter)) { | ||
splitted.shift(); | ||
} | ||
var relationName = splitted.join(); | ||
const relationName = splitted.join(); | ||
if (false == relationAdded.includes(relationName)) { | ||
rel += (rel != "") | ||
? "," + relationName | ||
: "rel=" + relationName; | ||
} | ||
if (false == relationAdded.includes(relationName)) { | ||
rel += (rel != "") | ||
? "," + relationName | ||
: "rel=" + relationName; | ||
} | ||
relationAdded.push(relationName); | ||
} | ||
relationAdded.push(relationName); | ||
} | ||
if (qs != "") { | ||
qs += "&"; | ||
} | ||
if (qs != "") { | ||
qs += "&"; | ||
} | ||
qs += this.combine + "[" | ||
+ this.getFilterIndex(filter) | ||
+ "]=" | ||
+ filter.value | ||
; | ||
if ("undefined" !== typeof options) { | ||
if (this.combine === "or" && options.oldStyle === true) { | ||
this.combine = "filtering_or"; | ||
} | ||
} | ||
for (var s in this.sorters) { | ||
var sorter = this.sorters[s]; | ||
qs += "&sorting[" | ||
+ this.getFilterIndex(sorter) | ||
+ "]=" | ||
+ sorter.value; | ||
} | ||
qs += this.combine + "[" | ||
+ this.getFilterIndex(filter) | ||
+ "]=" | ||
+ filter.value | ||
; | ||
} | ||
var j = ""; | ||
if (rel != "" && qs != "") { | ||
j = "&"; | ||
} | ||
for (const s in this.sorters) { | ||
const sorter = this.sorters[s]; | ||
qs += "&sorting[" | ||
+ this.getFilterIndex(sorter) | ||
+ "]=" | ||
+ sorter.value; | ||
} | ||
var limit = ''; | ||
if (0 > this.restrictions.limit) { | ||
if (rel != "" || qs != "") { | ||
limit = '&'; | ||
} | ||
let j = ""; | ||
if (rel != "" && qs != "") { | ||
j = "&"; | ||
} | ||
limit += 'limit=99999'; | ||
} | ||
let limit = ''; | ||
if (0 > this.restrictions.limit) { | ||
if (rel != "" || qs != "") { | ||
limit = '&'; | ||
} | ||
if (opt != null && opt.skiprel == true) { | ||
rel = ''; | ||
j = j.substring(1, j.length); | ||
} | ||
limit += 'limit=99999'; | ||
} | ||
return rel + j + qs + limit; | ||
}; | ||
if (opt != null && opt.skiprel == true) { | ||
rel = ''; | ||
j = j.substring(1, j.length); | ||
} | ||
this.getFilterIndex = function (filter) { | ||
if (!this.containsEmbedded(filter) && this.containsRelations(filter)) { | ||
return "_embedded."+ filter.field; | ||
} | ||
return rel + j + qs + limit; | ||
}; | ||
return filter.field; | ||
}; | ||
this.getFilterIndex = function (filter) { | ||
if (!this.containsEmbedded(filter) && this.containsRelations(filter)) { | ||
return "_embedded."+ filter.field; | ||
} | ||
this.containsRelations = function (filter) { | ||
return filter.field.indexOf(".") !== -1; | ||
}; | ||
return filter.field; | ||
}; | ||
this.containsEmbedded = function (filter) { | ||
return filter.field.indexOf("_embedded") === 0; | ||
}; | ||
this.containsRelations = function (filter) { | ||
return filter.field.indexOf(".") !== -1; | ||
}; | ||
this.containsEmbedded = function (filter) { | ||
return filter.field.indexOf("_embedded") === 0; | ||
}; | ||
} | ||
function SortingManager() { | ||
this.sorters = []; | ||
this.sorters = []; | ||
this.push = function (sorter) { | ||
this.sorters.push(sorter); | ||
}; | ||
this.push = function (sorter) { | ||
this.sorters.push(sorter); | ||
}; | ||
this.getSorters = function () { | ||
return this.sorters; | ||
}; | ||
this.getSorters = function () { | ||
return this.sorters; | ||
}; | ||
} | ||
function FilterManager() { | ||
this.filters = []; | ||
this.reset = function () { | ||
this.filters = []; | ||
}; | ||
this.reset = function () { | ||
this.filters = []; | ||
}; | ||
this.push = function (filter) { | ||
this.filters.push(filter); | ||
}; | ||
this.push = function (filter) { | ||
this.filters.push(filter); | ||
}; | ||
this.getFilters = function () { | ||
return this.filters; | ||
}; | ||
this.getFilters = function () { | ||
return this.filters; | ||
}; | ||
this.getFilter = function (index) { | ||
return this.filters[index]; | ||
}; | ||
this.getFilter = function (index) { | ||
return this.filters[index]; | ||
}; | ||
this.getFields = function () { | ||
var filters = [], i = 0, l = 0; | ||
var filterKeys = Object.keys(this.getFilters()); | ||
for (l = filterKeys.length; i < l; i += 1) { | ||
filters.push(this.getFilter(filterKeys[i]).field); | ||
} | ||
return filters; | ||
}; | ||
this.getFields = function () { | ||
let filters = [], i = 0, l = 0; | ||
const filterKeys = Object.keys(this.getFilters()); | ||
for (l = filterKeys.length; i < l; i += 1) { | ||
filters.push(this.getFilter(filterKeys[i]).field); | ||
} | ||
return filters; | ||
}; | ||
} | ||
function QueryQl(options) { | ||
this.restrictionConfiguration = {}; | ||
this.restrictionConfiguration = {}; | ||
var relation = 1; | ||
const relation = 1; | ||
this.setCombinator = function (combinator) { | ||
this.builder.setCombinator(combinator); | ||
return this; | ||
}; | ||
this.setCombinator = function (combinator) { | ||
this.builder.setCombinator(combinator); | ||
return this; | ||
}; | ||
this.builder = new QueryStringBuilder(); | ||
this.builder = new QueryStringBuilder(options); | ||
this.filterManager = new FilterManager(); | ||
this.filterManager = new FilterManager(); | ||
this.sortingManager = new SortingManager(); | ||
this.sortingManager = new SortingManager(); | ||
this.rel = []; | ||
this.rel = []; | ||
this.applySorting = function (sorter) { | ||
this.sortingManager.push(sorter); | ||
}; | ||
this.applySorting = function (sorter) { | ||
this.sortingManager.push(sorter); | ||
}; | ||
this.applyFilter = function (filter) { | ||
this.filterManager.push(filter); | ||
}; | ||
this.applyFilter = function (filter) { | ||
this.filterManager.push(filter); | ||
}; | ||
this.applyFilters = function (filters) { | ||
const filterManager = this.filterManager; | ||
filters.forEach( function(filter){ | ||
filterManager.push(filter); | ||
}) | ||
}; | ||
this.applyFilters = function (filters) { | ||
const filterManager = this.filterManager; | ||
filters.forEach( function(filter){ | ||
filterManager.push(filter); | ||
}) | ||
}; | ||
this.getRels = function () { | ||
this.rel = []; | ||
for (var f in this.filterManager.getFilters()) { | ||
var filter = this.filterManager.getFilter(f); | ||
if (this.builder.containsRelations(filter)) { | ||
var filterRelation = filter.field.split(".")[relation]; | ||
this.rel.push(filterRelation); | ||
} | ||
} | ||
return this.rel; | ||
}; | ||
this.getRels = function () { | ||
this.rel = []; | ||
for (const f in this.filterManager.getFilters()) { | ||
const filter = this.filterManager.getFilter(f); | ||
if (this.builder.containsRelations(filter)) { | ||
const filterRelation = filter.field.split(".")[relation]; | ||
this.rel.push(filterRelation); | ||
} | ||
} | ||
return this.rel; | ||
}; | ||
this.getQueryString = function (opt) { | ||
return this.builder | ||
.ensureHaveValidCombinator() | ||
.setFilters(this.filterManager.getFilters()) | ||
.setSorters(this.sortingManager.getSorters()) | ||
.setRestrictions(this.restrictionConfiguration) | ||
.build(opt); | ||
}; | ||
this.getQueryString = function (opt) { | ||
return this.builder | ||
.ensureHaveValidCombinator() | ||
.setFilters(this.filterManager.getFilters()) | ||
.setSorters(this.sortingManager.getSorters()) | ||
.setRestrictions(this.restrictionConfiguration) | ||
.build(opt); | ||
}; | ||
this.getFilters = function () { | ||
return this.filterManager | ||
.getFields(); | ||
}; | ||
this.getFilters = function () { | ||
return this.filterManager | ||
.getFields(); | ||
}; | ||
this.containsCombinator = function (jsonQuery) { | ||
for (var i in jsonQuery) { | ||
if ("object" == typeof jsonQuery[i]) { | ||
return true; | ||
} | ||
} | ||
this.containsCombinator = function (jsonQuery) { | ||
for (const i in jsonQuery) { | ||
if ("object" == typeof jsonQuery[i]) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
return false; | ||
}; | ||
this.detectCombinator = function (jsonQuery) { | ||
for (var i in jsonQuery) { | ||
if ("object" == typeof jsonQuery[i]) { | ||
return i; | ||
} | ||
} | ||
this.detectCombinator = function (jsonQuery) { | ||
for (const i in jsonQuery) { | ||
if ("object" == typeof jsonQuery[i]) { | ||
return i; | ||
} | ||
} | ||
return false; | ||
}; | ||
return false; | ||
}; | ||
this.keepDefaultCombinator = function () { | ||
return this; | ||
}; | ||
this.keepDefaultCombinator = function () { | ||
return this; | ||
}; | ||
this.keepOrFilteringCombinator = function () { | ||
this.setCombinator("filtering_or"); | ||
return this; | ||
}; | ||
this.keepOrFilteringCombinator = function () { | ||
this.setCombinator("filtering_or"); | ||
return this; | ||
}; | ||
this.json = function (jsonQuery) { | ||
var fields = this.buildFieldsWithRightCombinator(jsonQuery); | ||
for (var i in fields) { | ||
if ("object" == typeof fields[i]) { | ||
var position = 1; | ||
for (var f in fields[i]) { | ||
var positionalField = i + "|" + position; | ||
var positionalValue = fields[i][f]; | ||
position += 1; | ||
this.applyFilter({ | ||
field: positionalField, | ||
value: positionalValue | ||
}); | ||
} | ||
} else { | ||
this.applyFilter({ field: i, value: fields[i] }); | ||
} | ||
this.json = function (jsonQuery) { | ||
const fields = this.buildFieldsWithRightCombinator(jsonQuery); | ||
for (const i in fields) { | ||
if (i === 'conditions') { | ||
for (let j = 0; j < fields.conditions.length; j++) { | ||
for (let k in fields.conditions[j] ) { | ||
const positionalField = k + '|' + j; | ||
const positionalValue = fields.conditions[j][k]; | ||
this.applyFilter({ | ||
field: positionalField, | ||
value: positionalValue | ||
}); | ||
} | ||
} | ||
}; | ||
this.sort = function(jsonSort) { | ||
for (var i in jsonSort) { | ||
this.applySorting({field:i,value:jsonSort[i]}); | ||
} else if ("object" == typeof fields[i]) { | ||
let position = 1; | ||
for (const f in fields[i]) { | ||
const positionalField = i + "|" + position; | ||
const positionalValue = fields[i][f]; | ||
position += 1; | ||
this.applyFilter({ | ||
field: positionalField, | ||
value: positionalValue | ||
}); | ||
} | ||
}; | ||
} else { | ||
this.applyFilter({ field: i, value: fields[i] }); | ||
} | ||
} | ||
}; | ||
this.buildFieldsWithRightCombinator = function (jsonQuery) { | ||
var fields = jsonQuery; | ||
if (this.containsCombinator(jsonQuery)) { | ||
var combinator = this.detectCombinator(jsonQuery); | ||
this.setCombinator(combinator); | ||
fields = jsonQuery[combinator]; | ||
} | ||
return fields; | ||
}; | ||
this.sort = function(jsonSort) { | ||
for (const i in jsonSort) { | ||
this.applySorting({field:i,value:jsonSort[i]}); | ||
} | ||
}; | ||
this.parseRawField = function (rawField) { | ||
var explodedRawField = rawField.split("|"); | ||
var operatorFound = explodedRawField[1]; | ||
var availableOperators = ["list", "eq"]; | ||
if (-1 != availableOperators.indexOf(operatorFound)) { | ||
return { | ||
field: explodedRawField[0], | ||
operator: operatorFound | ||
}; | ||
} | ||
this.buildFieldsWithRightCombinator = function (jsonQuery) { | ||
let fields = jsonQuery; | ||
if (this.containsCombinator(jsonQuery)) { | ||
const combinator = this.detectCombinator(jsonQuery); | ||
this.setCombinator(combinator); | ||
fields = jsonQuery[combinator]; | ||
} | ||
return fields; | ||
}; | ||
if ("undefined" == typeof operatorFound) { | ||
return { | ||
field: explodedRawField[0], | ||
operator: "eq" | ||
}; | ||
} | ||
this.parseRawField = function (rawField) { | ||
const explodedRawField = rawField.split("|"); | ||
const operatorFound = explodedRawField[1]; | ||
const availableOperators = ["list", "eq"]; | ||
if (-1 != availableOperators.indexOf(operatorFound)) { | ||
return { | ||
field: explodedRawField[0], | ||
operator: operatorFound | ||
}; | ||
} | ||
throw new Error("operator " + operatorFound + " is not supported"); | ||
}; | ||
if ("undefined" == typeof operatorFound) { | ||
return { | ||
field: explodedRawField[0], | ||
operator: "eq" | ||
}; | ||
} | ||
this.getFieldOperator = function (fieldName) { | ||
for (var i in this.filterManager.getFilters()) { | ||
if (this.filterManager.getFilters()[i].field.indexOf(fieldName) != -1) { | ||
var parsed = this.parseRawField( | ||
this.filterManager.getFilters()[i].field | ||
); | ||
throw new Error("operator " + operatorFound + " is not supported"); | ||
}; | ||
if ("string" == typeof parsed.operator) { | ||
return parsed.operator; | ||
} | ||
this.getFieldOperator = function (fieldName) { | ||
for (const i in this.filterManager.getFilters()) { | ||
if (this.filterManager.getFilters()[i].field.indexOf(fieldName) != -1) { | ||
const parsed = this.parseRawField( | ||
this.filterManager.getFilters()[i].field | ||
); | ||
return "eq"; | ||
} | ||
if ("string" == typeof parsed.operator) { | ||
return parsed.operator; | ||
} | ||
}; | ||
this.or = function (jsonQuery, options) { | ||
if ("object" == typeof options) { | ||
if (options.oldStyle == true) { | ||
this.json({ | ||
"filtering_or": jsonQuery | ||
}); | ||
return; | ||
} | ||
} | ||
return "eq"; | ||
} | ||
} | ||
}; | ||
this.or = function (jsonQuery, opt) { | ||
if ("object" == typeof opt) { | ||
if (opt.oldStyle == true) { | ||
this.json({ | ||
"or": jsonQuery | ||
"filtering_or": jsonQuery | ||
}); | ||
return; | ||
}; | ||
} | ||
} | ||
this.and = function (jsonQuery, opt) { | ||
if ("object" == typeof opt) { | ||
if (opt.oldStyle == true) { | ||
this.json({ | ||
"filtering": jsonQuery | ||
}); | ||
this.json({ | ||
"or": jsonQuery | ||
}); | ||
return; | ||
} | ||
} | ||
if (typeof options != 'undefined' && options.oldStyle == true) { | ||
return; | ||
}; | ||
this.and = function (jsonQuery, opt) { | ||
if ("object" == typeof opt) { | ||
if (opt.oldStyle == true) { | ||
this.json({ | ||
@@ -389,78 +399,87 @@ "filtering": jsonQuery | ||
} | ||
} | ||
this.json({ | ||
"and": jsonQuery | ||
}); | ||
if (typeof options != 'undefined' && options.oldStyle == true) { | ||
this.json({ | ||
"filtering": jsonQuery | ||
}); | ||
return; | ||
}; | ||
return; | ||
} | ||
this.andFiltering = function (jsonQuery) { | ||
var newJsonQuery = { | ||
"filtering": jsonQuery | ||
}; | ||
this.json(newJsonQuery); | ||
this.json({ | ||
"and": jsonQuery | ||
}); | ||
return; | ||
}; | ||
this.andFiltering = function (jsonQuery) { | ||
const newJsonQuery = { | ||
"filtering": jsonQuery | ||
}; | ||
this.json(newJsonQuery); | ||
}; | ||
this.orFiltering = function (jsonQuery) { | ||
var newJsonQuery = { | ||
"filtering_or": jsonQuery | ||
}; | ||
this.json(newJsonQuery); | ||
this.orFiltering = function (jsonQuery) { | ||
const newJsonQuery = { | ||
"filtering_or": jsonQuery | ||
}; | ||
this.json(newJsonQuery); | ||
}; | ||
this.storedQueries = []; | ||
this.storedQueries = []; | ||
this.store = function(query, alias, parameter) { | ||
this.storedQueries[alias] = { | ||
query: query, | ||
parameter: parameter | ||
}; | ||
this.store = function(query, alias, parameter) { | ||
this.storedQueries[alias] = { | ||
query: query, | ||
parameter: parameter | ||
}; | ||
}; | ||
this.getStoredQuery = function(alias) { | ||
this.filterManager.reset(); | ||
this.json(this.storedQueries[alias].query); | ||
return this.getQueryString(); | ||
}; | ||
this.getStoredQuery = function(alias) { | ||
this.filterManager.reset(); | ||
this.json(this.storedQueries[alias].query); | ||
return this.getQueryString(); | ||
}; | ||
this.getQueryParameter = function(alias) { | ||
return this.storedQueries[alias].parameter; | ||
}; | ||
this.getQueryParameter = function(alias) { | ||
return this.storedQueries[alias].parameter; | ||
}; | ||
this.getRawQuery = function(alias) { | ||
return this.getStoredQuery(alias); | ||
}; | ||
this.getRawQuery = function(alias) { | ||
return this.getStoredQuery(alias); | ||
}; | ||
this.getFinalQuery = function(alias, param) { | ||
var rawQuery = this.getRawQuery(alias); | ||
return rawQuery.replace( | ||
"##" + this.getQueryParameter(alias) + "##", | ||
param | ||
); | ||
}; | ||
this.getFinalQuery = function(alias, param) { | ||
const rawQuery = this.getRawQuery(alias); | ||
return rawQuery.replace( | ||
"##" + this.getQueryParameter(alias) + "##", | ||
param | ||
); | ||
}; | ||
this.build = function(conf) { | ||
this.json(conf.query); | ||
this.sort(conf.sort); | ||
return this.getQueryString(); | ||
}; | ||
this.build = function(conf) { | ||
this.json(conf.query); | ||
this.sort(conf.sort); | ||
return this.getQueryString(); | ||
}; | ||
this.between = function(data, operator) { | ||
var jsonQuery = {}; | ||
jsonQuery[data.what + "|gte"] = data.from; | ||
jsonQuery[data.what + "|lte"] = data.to; | ||
if ("undefined" == typeof operator) { | ||
this.andFiltering(jsonQuery); | ||
} else { | ||
if (operator == "or") { | ||
this.orFiltering(jsonQuery); | ||
} | ||
} | ||
return this.getQueryString(); | ||
}; | ||
this.between = function(data, operator) { | ||
const jsonQuery = {}; | ||
jsonQuery[data.what + "|gte"] = data.from; | ||
jsonQuery[data.what + "|lte"] = data.to; | ||
if ("undefined" == typeof operator) { | ||
this.andFiltering(jsonQuery); | ||
} else { | ||
if (operator == "or") { | ||
this.orFiltering(jsonQuery); | ||
} | ||
} | ||
return this.getQueryString(); | ||
}; | ||
this.restriction = function(restriction) { | ||
this.restrictionConfiguration = restriction; | ||
}; | ||
this.restriction = function(restriction) { | ||
this.restrictionConfiguration = restriction; | ||
}; | ||
@@ -467,0 +486,0 @@ this.clear = () => { |
{ | ||
"name": "queryfilters", | ||
"version": "v0.3.31", | ||
"version": "v0.3.32", | ||
"scripts": { | ||
@@ -26,3 +26,3 @@ "test": "jest", | ||
"eslint-plugin-standard": "^3.1.0", | ||
"gulp": "^3.9.1", | ||
"gulp": "^4.0.0", | ||
"gulp-minify": "^2.1.0", | ||
@@ -29,0 +29,0 @@ "gulp-shell": "^0.6.5", |
@@ -49,1 +49,2 @@ queryfilters | ||
* [wrap combinators](doc/examples/combinators.md) | ||
* [grouping](doc/examples/grouping.md) |
const QueryQl = require('./../lib/queryql'); | ||
test('accept json as query', () => { | ||
var jsonQuery = { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.json(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.json(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('accept combinator as root element of jsonQuery', () => { | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
expect(queryQl.containsCombinator(jsonQuery)).toEqual(true); | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
expect(queryQl.containsCombinator(jsonQuery)).toEqual(true); | ||
}); | ||
test('detect combinator', () => { | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
expect(queryQl.detectCombinator(jsonQuery)).toEqual('or'); | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
expect(queryQl.detectCombinator(jsonQuery)).toEqual('or'); | ||
}); | ||
test('accept json as query', () => { | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.json(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&or[_embedded.relation.nick]=@sensorario' | ||
+ '&or[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'or': { | ||
'_embedded.relation.nick': '@sensorario', | ||
'_embedded.foo.bar.name': 'Simone' | ||
} | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.json(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&or[_embedded.relation.nick]=@sensorario' | ||
+ '&or[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('query al fields with or using or() alias', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.or(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&or[_embedded.relation.nick]=@sensorario' | ||
+ '&or[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.or(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&or[_embedded.relation.nick]=@sensorario' | ||
+ '&or[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('query all fields with and using and() alias', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.and(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&and[_embedded.relation.nick]=@sensorario' | ||
+ '&and[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.and(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&and[_embedded.relation.nick]=@sensorario' | ||
+ '&and[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('query all fields with filtering using andFiltering() alias', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.andFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.andFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('query all fields with filtering_or using orFiltering() alias', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.orFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering_or[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering_or[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.orFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering_or[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering_or[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('force old style querystring even if and alias is used', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.and(jsonQuery, {oldStyle:true}); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.and(jsonQuery, {oldStyle:true}); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('force old style querystring even if or alias is used', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.or(jsonQuery, {oldStyle:true}); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering_or[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering_or[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.or(jsonQuery, {oldStyle:true}); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering_or[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering_or[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('allow contains operator with arrays', () => { | ||
var jsonQuery = { | ||
'relation.nick|contains': ['senso', 'rario'], | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.andFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation' | ||
+ '&filtering[_embedded.relation.nick|contains|1]=senso' | ||
+ '&filtering[_embedded.relation.nick|contains|2]=rario' | ||
); | ||
var jsonQuery = { | ||
'relation.nick|contains': ['senso', 'rario'], | ||
}; | ||
var queryQl = new QueryQl() | ||
queryQl.andFiltering(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation' | ||
+ '&filtering[_embedded.relation.nick|contains|1]=senso' | ||
+ '&filtering[_embedded.relation.nick|contains|2]=rario' | ||
); | ||
}); | ||
test('force old style directly at init', () => { | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl({oldStyle:true}) | ||
queryQl.and(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
var jsonQuery = { | ||
'relation.nick': '@sensorario', | ||
'foo.bar.name': 'Simone' | ||
}; | ||
var queryQl = new QueryQl({oldStyle:true}) | ||
queryQl.and(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'rel=relation,foo,bar' | ||
+ '&filtering[_embedded.relation.nick]=@sensorario' | ||
+ '&filtering[_embedded.foo.bar.name]=Simone' | ||
); | ||
}); | ||
test('filter with or grouping', () => { | ||
var jsonQuery = { | ||
'or': { | ||
'conditions': [{ | ||
'role|contains': 'baz', | ||
'group|contains': 'baz', | ||
},{ | ||
'name|eq': 'Simone', | ||
'surname|eq': 'Gentili', | ||
}] | ||
} | ||
} | ||
var queryQl = new QueryQl({oldStyle:true}) | ||
queryQl.json(jsonQuery); | ||
expect(queryQl.getQueryString()).toEqual( | ||
'filtering_or[role|contains|0]=baz&' + | ||
'filtering_or[group|contains|0]=baz&' + | ||
'filtering_or[name|eq|1]=Simone&' + | ||
'filtering_or[surname|eq|1]=Gentili' | ||
); | ||
}); |
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
39
1392
50
64868