11import { compile } from './compile'
2- import { isArray } from './is'
2+ import { isArray , isEqual } from './is'
33import type {
44 Entry ,
55 FunctionBuilder ,
@@ -27,6 +27,20 @@ export function buildFunction(fn: (...args: unknown[]) => unknown): FunctionBuil
2727 }
2828}
2929
30+ const gt = ( a : unknown , b : unknown ) => {
31+ if (
32+ ( typeof a === 'number' && typeof b === 'number' ) ||
33+ ( typeof a === 'string' && typeof b === 'string' )
34+ ) {
35+ return a > b
36+ }
37+
38+ throw new TypeError ( 'Two numbers or two strings expected' )
39+ }
40+ const gte = ( a : unknown , b : unknown ) => isEqual ( a , b ) || gt ( a , b )
41+ const lt = ( a : unknown , b : unknown ) => gt ( b , a )
42+ const lte = ( a : unknown , b : unknown ) => gte ( b , a )
43+
3044export const functions : FunctionBuildersMap = {
3145 pipe : ( ...entries : JSONQuery [ ] ) => {
3246 const _entries = entries . map ( ( entry ) => compile ( entry ) )
@@ -130,7 +144,7 @@ export const functions: FunctionBuildersMap = {
130144 function compare ( itemA : unknown , itemB : unknown ) {
131145 const a = getter ( itemA )
132146 const b = getter ( itemB )
133- return a > b ? sign : a < b ? - sign : 0
147+ return gt ( a , b ) ? sign : lt ( a , b ) ? - sign : 0
134148 }
135149
136150 return ( data : T [ ] ) => data . slice ( ) . sort ( compare )
@@ -216,7 +230,17 @@ export const functions: FunctionBuildersMap = {
216230
217231 uniq :
218232 ( ) =>
219- < T > ( data : T [ ] ) => [ ...new Set ( data ) ] ,
233+ < T > ( data : T [ ] ) => {
234+ const res : T [ ] = [ ]
235+
236+ for ( const item of data ) {
237+ if ( ! res . find ( ( resItem ) => isEqual ( resItem , item ) ) ) {
238+ res . push ( item )
239+ }
240+ }
241+
242+ return res
243+ } ,
220244
221245 uniqBy :
222246 < T > ( path : JSONQueryProperty ) =>
@@ -268,11 +292,16 @@ export const functions: FunctionBuildersMap = {
268292
269293 return ( data : unknown ) => ( truthy ( _condition ( data ) ) ? _valueIfTrue ( data ) : _valueIfFalse ( data ) )
270294 } ,
271- in : ( path : string , values : JSONQuery ) => {
272- const getter = compile ( path )
273- const _values = compile ( values )
295+ in : ( value : JSONQuery , values : JSONQuery ) => {
296+ const getValue = compile ( value )
297+ const getValues = compile ( values )
274298
275- return ( data : unknown ) => ( _values ( data ) as string [ ] ) . includes ( getter ( data ) as string )
299+ return ( data : unknown ) => {
300+ const _value = getValue ( data )
301+ const _values = getValues ( data ) as unknown [ ]
302+
303+ return ! ! _values . find ( ( item ) => isEqual ( item , _value ) )
304+ }
276305 } ,
277306 'not in' : ( path : string , values : JSONQuery ) => {
278307 const _in = functions . in ( path , values )
@@ -286,12 +315,12 @@ export const functions: FunctionBuildersMap = {
286315 return ( data : unknown ) => regex . test ( getter ( data ) as string )
287316 } ,
288317
289- eq : buildFunction ( ( a , b ) => a === b ) ,
290- gt : buildFunction ( ( a , b ) => a > b ) ,
291- gte : buildFunction ( ( a , b ) => a >= b ) ,
292- lt : buildFunction ( ( a , b ) => a < b ) ,
293- lte : buildFunction ( ( a , b ) => a <= b ) ,
294- ne : buildFunction ( ( a , b ) => a !== b ) ,
318+ eq : buildFunction ( isEqual ) ,
319+ gt : buildFunction ( gt ) ,
320+ gte : buildFunction ( gte ) ,
321+ lt : buildFunction ( lt ) ,
322+ lte : buildFunction ( lte ) ,
323+ ne : buildFunction ( ( a , b ) => ! isEqual ( a , b ) ) ,
295324
296325 add : buildFunction ( ( a : number , b : number ) => a + b ) ,
297326 subtract : buildFunction ( ( a : number , b : number ) => a - b ) ,
0 commit comments