@altangent/graphql-filter-array
Advanced tools
Comparing version 1.0.0 to 1.0.1-1
{ | ||
"name": "@altangent/graphql-filter-array", | ||
"version": "1.0.0", | ||
"version": "1.0.1-1", | ||
"description": "Graphql filtering exection to an array of objects", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -23,9 +23,3 @@ let flattenDeep = require('lodash.flattendeep'); | ||
let column = columnMap.get(key); | ||
if (!column) { | ||
throw new Error(`${key} is not a supported filter`); | ||
} | ||
let test = filter[key]; | ||
return processOp(results, column, test); | ||
return processOp(results, key, filter, columnMap); | ||
} | ||
@@ -45,17 +39,23 @@ | ||
function processOp(results, column, test) { | ||
if (Object.keys(test).length > 1) throw new Error('Unsupported filter operation'); | ||
function processOp(results, key, filter, columnMap) { | ||
let [target, op] = key.split('_'); | ||
let test = filter[key]; | ||
if (test._eq) { | ||
return processEq(results, column, test._eq); | ||
let column = columnMap.get(target); | ||
if (!column) { | ||
throw new Error(`${key} is not a supported filter`); | ||
} | ||
if (test._in) { | ||
return processIn(results, column, test._in); | ||
if (op === 'eq') { | ||
return processEq(results, column, test); | ||
} | ||
if (test._like) { | ||
return processLike(results, column, test._like); | ||
if (op === 'in') { | ||
return processIn(results, column, test); | ||
} | ||
if (op === 'like') { | ||
return processLike(results, column, test); | ||
} | ||
throw new Error('Unsupported filter operation'); | ||
@@ -62,0 +62,0 @@ } |
@@ -22,3 +22,3 @@ const { expect } = require('chai'); | ||
it: 'should perform equal filter', | ||
filter: { currencyName: { _eq: 'bitcoin' } }, | ||
filter: { currencyName_eq: 'bitcoin' }, | ||
expected: [array[0]], | ||
@@ -28,3 +28,3 @@ }, | ||
it: 'should perform like filter', | ||
filter: { currencyName: { _like: '%bitcoin%' } }, | ||
filter: { currencyName_like: '%bitcoin%' }, | ||
expected: [array[0], array[2]], | ||
@@ -34,3 +34,3 @@ }, | ||
it: 'should perform in filter', | ||
filter: { currencySymbol: { _in: ['BTC', 'LTC'] } }, | ||
filter: { currencySymbol_in: ['BTC', 'LTC'] }, | ||
expected: [array[0], array[1]], | ||
@@ -40,3 +40,3 @@ }, | ||
it: 'should implicit and multiple filters', | ||
filter: { currencyName: { _like: 'bitcoin%' }, currencySymbol: { _eq: 'BTC' } }, | ||
filter: { currencyName_like: 'bitcoin%', currencySymbol_eq: 'BTC' }, | ||
expected: [array[0]], | ||
@@ -47,3 +47,3 @@ }, | ||
filter: { | ||
_and: [{ currencyName: { _like: 'bitcoin%' } }, { currencySymbol: { _eq: 'BTC' } }], | ||
_and: [{ currencyName_like: 'bitcoin%' }, { currencySymbol_eq: 'BTC' }], | ||
}, | ||
@@ -55,3 +55,3 @@ expected: [array[0]], | ||
filter: { | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencyName: { _eq: 'litecoin' } }], | ||
_or: [{ currencyName_eq: 'bitcoin' }, { currencyName_eq: 'litecoin' }], | ||
}, | ||
@@ -63,4 +63,4 @@ expected: [array[0], array[1]], | ||
filter: { | ||
currencyName: { _like: '%coin%' }, | ||
_or: [{ currencySymbol: { _eq: 'BTC' } }, { currencySymbol: { _eq: 'LTC' } }], | ||
currencyName_like: '%coin%', | ||
_or: [{ currencySymbol_eq: 'BTC' }, { currencySymbol_eq: 'LTC' }], | ||
}, | ||
@@ -73,4 +73,4 @@ expected: [array[0], array[1]], | ||
_and: [ | ||
{ _or: [{ currencyName: { _like: 'bit%' } }, { currencyName: { _like: 'lite%' } }] }, | ||
{ _or: [{ currencySymbol: { _eq: 'BTC' } }, { currencySymbol: { _eq: 'LTC' } }] }, | ||
{ _or: [{ currencyName_like: 'bit%' }, { currencyName_like: 'lite%' }] }, | ||
{ _or: [{ currencySymbol_eq: 'BTC' }, { currencySymbol_eq: 'LTC' }] }, | ||
], | ||
@@ -95,10 +95,5 @@ }, | ||
it('should throw exception when operation is unknown', () => { | ||
let filter = { currencySymbol: { _derp: 'ok' } }; | ||
let filter = { currencySymbol_derp: 'ok' }; | ||
expect(() => filterArray(array, filter, columnMap)).to.throw('Unsupported filter operation'); | ||
}); | ||
it('should throw exception when operation has too many keys', () => { | ||
let filter = { currencySymbol: { _eq: 'BTC', _like: 'BTC' } }; | ||
expect(() => filterArray(array, filter, columnMap)).to.throw('Unsupported filter operation'); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
9268
249
2