1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
+ import { canonicalDouble } from '@iden3/js-jsonld-merklization/dist/types/lib/types/types' ;
2
3
import { W3CCredential , ProofQuery } from '../../verifiable' ;
3
4
4
5
/**
@@ -10,7 +11,18 @@ export enum SearchError {
10
11
NotDefinedQueryKey = 'not defined query key' ,
11
12
NotDefinedComparator = 'not defined comparator'
12
13
}
13
-
14
+ /**
15
+ * supported data formats
16
+ *
17
+ * @enum {number}
18
+ */
19
+ export enum SupportedDataFormat {
20
+ BigInt ,
21
+ Boolean ,
22
+ Double ,
23
+ DateTime ,
24
+ String
25
+ }
14
26
/** allowed operators to search */
15
27
export type FilterOperatorMethod =
16
28
| '$noop'
@@ -85,10 +97,19 @@ const greaterThan = (
85
97
b : ComparableType | ComparableType [ ]
86
98
) => {
87
99
const predicate = ( a : ComparableType , b : ComparableType ) => {
88
- if ( isNumeric ( a . toString ( ) ) && isNumeric ( b . toString ( ) ) ) {
89
- return BigInt ( a ) > BigInt ( b ) ;
100
+ const dataFormat = detectDataFormat ( a . toString ( ) ) ;
101
+
102
+ switch ( dataFormat ) {
103
+ case SupportedDataFormat . BigInt :
104
+ case SupportedDataFormat . Boolean :
105
+ return BigInt ( a ) > BigInt ( b ) ;
106
+ case SupportedDataFormat . DateTime :
107
+ return Date . parse ( a . toString ( ) ) > Date . parse ( b . toString ( ) ) ; /// nanoseconds won't be compared.
108
+ case SupportedDataFormat . Double :
109
+ case SupportedDataFormat . String :
110
+ default :
111
+ return a > b ;
90
112
}
91
- return a > b ;
92
113
} ;
93
114
94
115
return operatorIndependentCheck ( a , b , predicate ) ;
@@ -99,10 +120,19 @@ const greaterThanOrEqual = (
99
120
b : ComparableType | ComparableType [ ]
100
121
) => {
101
122
const predicate = ( a : ComparableType , b : ComparableType ) => {
102
- if ( isNumeric ( a . toString ( ) ) && isNumeric ( b . toString ( ) ) ) {
103
- return BigInt ( a ) >= BigInt ( b ) ;
123
+ const dataFormat = detectDataFormat ( a . toString ( ) ) ;
124
+
125
+ switch ( dataFormat ) {
126
+ case SupportedDataFormat . BigInt :
127
+ case SupportedDataFormat . Boolean :
128
+ return BigInt ( a ) >= BigInt ( b ) ;
129
+ case SupportedDataFormat . DateTime :
130
+ return Date . parse ( a . toString ( ) ) >= Date . parse ( b . toString ( ) ) ; /// nanoseconds won't be compared.
131
+ case SupportedDataFormat . Double :
132
+ case SupportedDataFormat . String :
133
+ default :
134
+ return a >= b ;
104
135
}
105
- return a >= b ;
106
136
} ;
107
137
108
138
return operatorIndependentCheck ( a , b , predicate ) ;
@@ -319,4 +349,20 @@ const operatorIndependentCheck = (
319
349
return predicate ( a as ComparableType , b as ComparableType ) ;
320
350
} ;
321
351
322
- const isNumeric = ( s : string ) => / ^ [ + - ] ? \d + ( \. \d + ) ? $ / . test ( s ) ;
352
+ const regExBigInt = / ^ [ + - ] ? \d + $ / ;
353
+ const regExDouble = / ^ [ + - ] ? \d + $ / ;
354
+ const regExDateTimeRFC3339Nano =
355
+ / ^ ( [ 0 - 9 ] + ) - ( 0 [ 1 - 9 ] | 1 [ 0 1 2 ] ) - ( 0 [ 1 - 9 ] | [ 1 2 ] [ 0 - 9 ] | 3 [ 0 1 ] ) [ T t ] ( [ 0 1 ] [ 0 - 9 ] | 2 [ 0 - 3 ] ) : ( [ 0 - 5 ] [ 0 - 9 ] ) : ( [ 0 - 5 ] [ 0 - 9 ] | 6 0 ) ( \. [ 0 - 9 ] + ) ? ( ( [ Z z ] ) | ( [ \+ | \- ] ( [ 0 1 ] [ 0 - 9 ] | 2 [ 0 - 3 ] ) : [ 0 - 5 ] [ 0 - 9 ] ) ) $ / ;
356
+ const regExBoolean = / ^ ( t r u e ) | ( f a l s e ) $ / ;
357
+ const regExDateTimeYYYYMMDD = / ^ \d { 4 } - \d { 2 } - \d { 2 } $ / ;
358
+
359
+ const detectDataFormat = ( s : string ) : SupportedDataFormat =>
360
+ regExBigInt . test ( s )
361
+ ? SupportedDataFormat . BigInt
362
+ : regExDouble . test ( s )
363
+ ? SupportedDataFormat . Double
364
+ : regExDateTimeRFC3339Nano . test ( s ) || regExDateTimeYYYYMMDD . test ( s )
365
+ ? SupportedDataFormat . DateTime
366
+ : regExBoolean . test ( s )
367
+ ? SupportedDataFormat . Boolean
368
+ : SupportedDataFormat . String ;
0 commit comments