Skip to content

Commit c331433

Browse files
committed
introduce format
1 parent 335ebd7 commit c331433

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/storage/filters/jsonQuery.ts

+54-8
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
2+
import { canonicalDouble } from '@iden3/js-jsonld-merklization/dist/types/lib/types/types';
23
import { W3CCredential, ProofQuery } from '../../verifiable';
34

45
/**
@@ -10,7 +11,18 @@ export enum SearchError {
1011
NotDefinedQueryKey = 'not defined query key',
1112
NotDefinedComparator = 'not defined comparator'
1213
}
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+
}
1426
/** allowed operators to search */
1527
export type FilterOperatorMethod =
1628
| '$noop'
@@ -85,10 +97,19 @@ const greaterThan = (
8597
b: ComparableType | ComparableType[]
8698
) => {
8799
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;
90112
}
91-
return a > b;
92113
};
93114

94115
return operatorIndependentCheck(a, b, predicate);
@@ -99,10 +120,19 @@ const greaterThanOrEqual = (
99120
b: ComparableType | ComparableType[]
100121
) => {
101122
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;
104135
}
105-
return a >= b;
106136
};
107137

108138
return operatorIndependentCheck(a, b, predicate);
@@ -319,4 +349,20 @@ const operatorIndependentCheck = (
319349
return predicate(a as ComparableType, b as ComparableType);
320350
};
321351

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[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))$/;
356+
const regExBoolean = /^(true)|(false)$/;
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;

tests/credentials/credential-wallet.test.ts

+15
Original file line numberDiff line numberDiff line change
@@ -610,5 +610,20 @@ describe('credential-wallet', () => {
610610
});
611611
expect(1).to.be.equal(cred.length);
612612
expect(cred[0].credentialSubject['date-time1']).to.be.equal('2025-04-09T15:48:08.800+02:00');
613+
614+
// datetime YYYY-MM-DD
615+
cred = await credentialStorage.findCredentialsByQuery({
616+
allowedIssuers: ['*'],
617+
context: 'ipfs://Qmb48rJ5SiQMLXjVkaLQB6fWbT7C8LK75MHsCoHv8GAc15',
618+
credentialSubject: {
619+
'date-time1': {
620+
$gt: '2022-04-01'
621+
}
622+
},
623+
groupId: 1745320529,
624+
type: 'operators'
625+
});
626+
expect(1).to.be.equal(cred.length);
627+
expect(cred[0].credentialSubject['date-time1']).to.be.equal('2025-04-09T15:48:08.800+02:00');
613628
});
614629
});

0 commit comments

Comments
 (0)