@altangent/graphql-filter-array
Advanced tools
Comparing version 1.0.1-1 to 1.1.0
{ | ||
"name": "@altangent/graphql-filter-array", | ||
"version": "1.0.1-1", | ||
"version": "1.1.0", | ||
"description": "Graphql filtering exection to an array of objects", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
@@ -23,3 +23,9 @@ let flattenDeep = require('lodash.flattendeep'); | ||
return processOp(results, key, filter, columnMap); | ||
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); | ||
} | ||
@@ -39,23 +45,34 @@ | ||
function processOp(results, key, filter, columnMap) { | ||
let [target, op] = key.split('_'); | ||
let test = filter[key]; | ||
function processOp(results, column, test) { | ||
if (Object.keys(test).length > 1) throw new Error('Unsupported filter operation'); | ||
let column = columnMap.get(target); | ||
if (!column) { | ||
throw new Error(`${key} is not a supported filter`); | ||
if (test._eq) { | ||
return processEq(results, column, test._eq); | ||
} | ||
if (op === 'eq') { | ||
return processEq(results, column, test); | ||
if (test._in) { | ||
return processIn(results, column, test._in); | ||
} | ||
if (op === 'in') { | ||
return processIn(results, column, test); | ||
if (test._like) { | ||
return processLike(results, column, test._like); | ||
} | ||
if (op === 'like') { | ||
return processLike(results, column, test); | ||
if (test._gt !== undefined) { | ||
console.log(test); | ||
return processGt(results, column, test._gt); | ||
} | ||
if (test._gte !== undefined) { | ||
return processGte(results, column, test._gte); | ||
} | ||
if (test._lt !== undefined) { | ||
return processLt(results, column, test._lt); | ||
} | ||
if (test._lte !== undefined) { | ||
return processLte(results, column, test._lte); | ||
} | ||
throw new Error('Unsupported filter operation'); | ||
@@ -65,3 +82,3 @@ } | ||
function processEq(results, column, testValue) { | ||
return results.filter(p => safeLower(p[column]) === safeLower(testValue)); | ||
return results.filter(p => safeInvariant(p[column]) === safeInvariant(testValue)); | ||
} | ||
@@ -78,4 +95,20 @@ | ||
function safeLower(str) { | ||
function processGt(results, column, testValue) { | ||
return results.filter(p => p[column] > testValue); | ||
} | ||
function processGte(results, column, testValue) { | ||
return results.filter(p => p[column] >= testValue); | ||
} | ||
function processLt(results, column, testValue) { | ||
return results.filter(p => p[column] < testValue); | ||
} | ||
function processLte(results, column, testValue) { | ||
return results.filter(p => p[column] <= testValue); | ||
} | ||
function safeInvariant(str) { | ||
if (str && str.toLowerCase) return str.toLowerCase(); | ||
} |
@@ -8,11 +8,12 @@ const { expect } = require('chai'); | ||
['currencySymbol', 'currency_symbol'], | ||
['marketcap', 'marketcap'], | ||
]); | ||
let array = [ | ||
{ currency_name: 'Bitcoin', currency_symbol: 'BTC' }, | ||
{ currency_name: 'Litecoin', currency_symbol: 'LTC' }, | ||
{ currency_name: 'Bitcoin Cash', currency_symbol: 'BCH' }, | ||
{ currency_name: 'Ethereum', currency_symbol: 'ETH' }, | ||
{ currency_name: 'Ethereum Classic', currency_symbol: 'ETC' }, | ||
{ currency_name: 'Ardor', currency_symbol: 'ARDR' }, | ||
{ currency_name: 'Bitcoin', currency_symbol: 'BTC', marketcap: 10 }, | ||
{ currency_name: 'Litecoin', currency_symbol: 'LTC', marketcap: 9 }, | ||
{ currency_name: 'Bitcoin Cash', currency_symbol: 'BCH', marketcap: 8 }, | ||
{ currency_name: 'Ethereum', currency_symbol: 'ETH', marketcap: 7 }, | ||
{ currency_name: 'Ethereum Classic', currency_symbol: 'ETC', marketcap: 2 }, | ||
{ currency_name: 'Ardor', currency_symbol: 'ARDR', marketcap: 1 }, | ||
]; | ||
@@ -23,3 +24,3 @@ | ||
it: 'should perform equal filter', | ||
filter: { currencyName_eq: 'bitcoin' }, | ||
filter: { currencyName: { _eq: 'bitcoin' } }, | ||
expected: [array[0]], | ||
@@ -29,3 +30,3 @@ }, | ||
it: 'should perform like filter', | ||
filter: { currencyName_like: '%bitcoin%' }, | ||
filter: { currencyName: { _like: '%bitcoin%' } }, | ||
expected: [array[0], array[2]], | ||
@@ -35,3 +36,3 @@ }, | ||
it: 'should perform in filter', | ||
filter: { currencySymbol_in: ['BTC', 'LTC'] }, | ||
filter: { currencySymbol: { _in: ['BTC', 'LTC'] } }, | ||
expected: [array[0], array[1]], | ||
@@ -41,3 +42,3 @@ }, | ||
it: 'should implicit and multiple filters', | ||
filter: { currencyName_like: 'bitcoin%', currencySymbol_eq: 'BTC' }, | ||
filter: { currencyName: { _like: 'bitcoin%' }, currencySymbol: { _eq: 'BTC' } }, | ||
expected: [array[0]], | ||
@@ -48,3 +49,3 @@ }, | ||
filter: { | ||
_and: [{ currencyName_like: 'bitcoin%' }, { currencySymbol_eq: 'BTC' }], | ||
_and: [{ currencyName: { _like: 'bitcoin%' } }, { currencySymbol: { _eq: 'BTC' } }], | ||
}, | ||
@@ -56,3 +57,3 @@ expected: [array[0]], | ||
filter: { | ||
_or: [{ currencyName_eq: 'bitcoin' }, { currencyName_eq: 'litecoin' }], | ||
_or: [{ currencyName: { _eq: 'bitcoin' } }, { currencyName: { _eq: 'litecoin' } }], | ||
}, | ||
@@ -64,4 +65,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' } }], | ||
}, | ||
@@ -74,4 +75,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' } }] }, | ||
], | ||
@@ -81,2 +82,22 @@ }, | ||
}, | ||
{ | ||
it: 'should perform greater than filter', | ||
filter: { marketcap: { _gt: 9 } }, | ||
expected: [array[0]], | ||
}, | ||
{ | ||
it: 'should perform greater than or equal to filter', | ||
filter: { marketcap: { _gte: 9 } }, | ||
expected: [array[0], array[1]], | ||
}, | ||
{ | ||
it: 'should perform less than filter', | ||
filter: { marketcap: { _lt: 2 } }, | ||
expected: [array[5]], | ||
}, | ||
{ | ||
it: 'should perform greater than or equal to filter', | ||
filter: { marketcap: { _lte: 2 } }, | ||
expected: [array[4], array[5]], | ||
}, | ||
]; | ||
@@ -97,5 +118,10 @@ | ||
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'); | ||
}); | ||
}); |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
11131
299
1