1
1
import { v4 as uuidv4 } from 'uuid' ;
2
2
import DocumentContext from "./DocumentContext" ;
3
+ import { BigDecimal } from "./functions/common/FunctionHelpers" ;
4
+ import { areSimilar } from "@nlighten/json-schema-utils" ;
3
5
4
6
const JSONPATH_ROOT = "$" ,
5
7
JSONPATH_ROOT_ESC = "\\$" ,
@@ -29,15 +31,17 @@ const numberCompare = (a: number, b: number) => {
29
31
return a < b ? - 1 : ( a === b ? 0 : 1 ) ;
30
32
}
31
33
34
+ const numberType = ( a : any ) => typeof a === 'number' || typeof a === 'bigint' || a instanceof BigDecimal ;
35
+
32
36
const compareTo = ( a : any , b : any ) => {
33
37
if ( Array . isArray ( a ) && Array . isArray ( b ) ) {
34
38
return numberCompare ( a . length , b . length ) ;
35
39
} else if ( a && b && typeof a === 'object' && typeof b === 'object' ) {
36
40
return numberCompare ( Object . keys ( a ) . length , Object . keys ( b ) . length ) ;
37
41
} else if ( typeof a === 'string' && typeof b === 'string' ) {
38
42
return a . localeCompare ( b ) ;
39
- } else if ( typeof a === 'number' && typeof b === 'number' ) {
40
- return numberCompare ( a , b ) ;
43
+ } else if ( numberType ( a ) && numberType ( b ) ) {
44
+ return BigDecimal ( a ) . comparedTo ( BigDecimal ( b ) ) ;
41
45
} else if ( typeof a === 'boolean' && typeof b === 'boolean' ) {
42
46
return a === b ? 0 : ( a ? 1 : - 1 ) ;
43
47
} else if ( isNullOrUndefined ( a ) && ! isNullOrUndefined ( b ) ) {
@@ -143,29 +147,42 @@ const lenientJsonParse = (input: string) => {
143
147
144
148
const BIGINT_ZERO = BigInt ( 0 ) ;
145
149
const isTruthy = ( value : any , javascriptStyle ?: boolean ) => {
146
- if ( Array . isArray ( value ) ) {
147
- return value . length > 0 ;
148
- } else if ( value && typeof value === 'object' ) {
149
- return Object . keys ( value ) . length > 0 ;
150
- }
151
150
if ( typeof value === 'boolean' ) {
152
151
return value ;
153
152
} else if ( typeof value === 'number' ) {
154
153
return value != 0 ;
155
154
} else if ( typeof value === 'bigint' ) {
156
155
return value !== BIGINT_ZERO ;
156
+ } else if ( value instanceof BigDecimal ) {
157
+ return ! value . isZero ( ) ;
157
158
} else if ( typeof value === 'string' ) {
158
- return javascriptStyle ? value !== '' : value === 'true' ;
159
+ return javascriptStyle ? Boolean ( value ) : value . toLowerCase ( ) === 'true' ;
160
+ } else if ( Array . isArray ( value ) ) {
161
+ return value . length > 0 ;
162
+ } else if ( value && typeof value === 'object' ) {
163
+ return Object . keys ( value ) . length > 0 ;
159
164
}
160
165
return ! isNullOrUndefined ( value ) ;
161
166
}
162
167
168
+ const isEqual = ( value : any , other : any ) : boolean => {
169
+ if ( value === other ) {
170
+ return true ;
171
+ }
172
+ if ( numberType ( value ) && numberType ( other ) ) {
173
+ return BigDecimal ( value ) . eq ( BigDecimal ( other ) ) ;
174
+ }
175
+ return areSimilar ( value , other ) ;
176
+ }
177
+
178
+
163
179
export {
164
180
isNullOrUndefined ,
165
181
createPayloadResolver ,
166
182
getAsString ,
167
183
compareTo ,
168
184
getDocumentContext ,
169
185
lenientJsonParse ,
170
- isTruthy
186
+ isTruthy ,
187
+ isEqual
171
188
} ;
0 commit comments