jsonata 2.2.2.1 → 2.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.rubocop_local.yml +5 -0
- data/AGENTS.md +14 -3
- data/README.md +15 -5
- data/lib/jsonata/datetime.rb +573 -125
- data/lib/jsonata/evaluator.rb +200 -90
- data/lib/jsonata/functions.rb +237 -205
- data/lib/jsonata/parser.rb +179 -33
- data/lib/jsonata/signature.rb +1 -1
- data/lib/jsonata/utils.rb +83 -11
- data/lib/jsonata/version.rb +1 -1
- metadata +6 -6
data/lib/jsonata/functions.rb
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
# The functions are defined as instance methods, and are invoked with a Jsonata::Focus object as the receiver,
|
|
10
10
|
# which is the equivalent of the `this` object that jsonata-js invokes them with.
|
|
11
11
|
|
|
12
|
+
require 'set'
|
|
13
|
+
|
|
12
14
|
module Jsonata
|
|
13
15
|
module Functions
|
|
14
16
|
# Sum function
|
|
15
|
-
# @param args
|
|
16
|
-
# @
|
|
17
|
+
# @param args [Object] Arguments
|
|
18
|
+
# @return [Numeric] Total value of arguments
|
|
17
19
|
def sum(args)
|
|
18
20
|
# undefined inputs always return undefined
|
|
19
21
|
return nil if args.nil?
|
|
@@ -26,8 +28,8 @@ module Jsonata
|
|
|
26
28
|
end
|
|
27
29
|
|
|
28
30
|
# Count function
|
|
29
|
-
# @param args
|
|
30
|
-
# @
|
|
31
|
+
# @param args [Object] Arguments
|
|
32
|
+
# @return [Numeric] Number of elements in the array
|
|
31
33
|
def count(args)
|
|
32
34
|
# undefined inputs always return undefined
|
|
33
35
|
return 0 if args.nil?
|
|
@@ -36,8 +38,8 @@ module Jsonata
|
|
|
36
38
|
end
|
|
37
39
|
|
|
38
40
|
# Max function
|
|
39
|
-
# @param args
|
|
40
|
-
# @
|
|
41
|
+
# @param args [Object] Arguments
|
|
42
|
+
# @return [Numeric] Max element in the array
|
|
41
43
|
def max(args)
|
|
42
44
|
# undefined inputs always return undefined
|
|
43
45
|
return nil if args.nil? || args.empty?
|
|
@@ -46,8 +48,8 @@ module Jsonata
|
|
|
46
48
|
end
|
|
47
49
|
|
|
48
50
|
# Min function
|
|
49
|
-
# @param args
|
|
50
|
-
# @
|
|
51
|
+
# @param args [Object] Arguments
|
|
52
|
+
# @return [Numeric] Min element in the array
|
|
51
53
|
def min(args)
|
|
52
54
|
# undefined inputs always return undefined
|
|
53
55
|
return nil if args.nil? || args.empty?
|
|
@@ -56,8 +58,8 @@ module Jsonata
|
|
|
56
58
|
end
|
|
57
59
|
|
|
58
60
|
# Average function
|
|
59
|
-
# @param args
|
|
60
|
-
# @
|
|
61
|
+
# @param args [Object] Arguments
|
|
62
|
+
# @return [Numeric] Average element in the array
|
|
61
63
|
def average(args)
|
|
62
64
|
# undefined inputs always return undefined
|
|
63
65
|
return nil if args.nil? || args.empty?
|
|
@@ -70,9 +72,9 @@ module Jsonata
|
|
|
70
72
|
end
|
|
71
73
|
|
|
72
74
|
# Stringify arguments
|
|
73
|
-
# @param arg
|
|
74
|
-
# @param prettify
|
|
75
|
-
# @
|
|
75
|
+
# @param arg [Object] Arguments
|
|
76
|
+
# @param prettify [Boolean] Pretty print the result
|
|
77
|
+
# @return [String] String from arguments
|
|
76
78
|
def string(arg, prettify = false)
|
|
77
79
|
# undefined inputs always return undefined
|
|
78
80
|
return nil if arg.nil?
|
|
@@ -99,10 +101,10 @@ module Jsonata
|
|
|
99
101
|
end
|
|
100
102
|
|
|
101
103
|
# Create substring based on character number and length
|
|
102
|
-
# @param str
|
|
103
|
-
# @param start
|
|
104
|
-
# @param length
|
|
105
|
-
# @
|
|
104
|
+
# @param str [String] String to evaluate
|
|
105
|
+
# @param start [Integer] Character number to start substring
|
|
106
|
+
# @param length [Integer] Number of characters in substring
|
|
107
|
+
# @return [String, Object] Substring
|
|
106
108
|
def substring(str, start, length)
|
|
107
109
|
# undefined inputs always return undefined
|
|
108
110
|
return nil if str.nil?
|
|
@@ -123,9 +125,9 @@ module Jsonata
|
|
|
123
125
|
end
|
|
124
126
|
|
|
125
127
|
# Create substring up until a character
|
|
126
|
-
# @param str
|
|
127
|
-
# @param chars
|
|
128
|
-
# @
|
|
128
|
+
# @param str [String] String to evaluate
|
|
129
|
+
# @param chars [String] Character to define substring boundary
|
|
130
|
+
# @return [Object] Substring
|
|
129
131
|
def substring_before(str, chars)
|
|
130
132
|
# undefined inputs always return undefined
|
|
131
133
|
return nil if str.nil?
|
|
@@ -139,9 +141,9 @@ module Jsonata
|
|
|
139
141
|
end
|
|
140
142
|
|
|
141
143
|
# Create substring after a character
|
|
142
|
-
# @param str
|
|
143
|
-
# @param chars
|
|
144
|
-
# @
|
|
144
|
+
# @param str [String] String to evaluate
|
|
145
|
+
# @param chars [String] Character to define substring boundary
|
|
146
|
+
# @return [Object] Substring
|
|
145
147
|
def substring_after(str, chars)
|
|
146
148
|
# undefined inputs always return undefined
|
|
147
149
|
return nil if str.nil?
|
|
@@ -155,8 +157,8 @@ module Jsonata
|
|
|
155
157
|
end
|
|
156
158
|
|
|
157
159
|
# Lowercase a string
|
|
158
|
-
# @param str
|
|
159
|
-
# @
|
|
160
|
+
# @param str [String] String to evaluate
|
|
161
|
+
# @return [String] Lowercase string
|
|
160
162
|
def lowercase(str)
|
|
161
163
|
# undefined inputs always return undefined
|
|
162
164
|
return nil if str.nil?
|
|
@@ -165,8 +167,8 @@ module Jsonata
|
|
|
165
167
|
end
|
|
166
168
|
|
|
167
169
|
# Uppercase a string
|
|
168
|
-
# @param str
|
|
169
|
-
# @
|
|
170
|
+
# @param str [String] String to evaluate
|
|
171
|
+
# @return [String] Uppercase string
|
|
170
172
|
def uppercase(str)
|
|
171
173
|
# undefined inputs always return undefined
|
|
172
174
|
return nil if str.nil?
|
|
@@ -175,8 +177,8 @@ module Jsonata
|
|
|
175
177
|
end
|
|
176
178
|
|
|
177
179
|
# length of a string
|
|
178
|
-
# @param str
|
|
179
|
-
# @
|
|
180
|
+
# @param str [String] string
|
|
181
|
+
# @return [Numeric] The number of characters in the string
|
|
180
182
|
def length(str)
|
|
181
183
|
# undefined inputs always return undefined
|
|
182
184
|
return nil if str.nil?
|
|
@@ -185,8 +187,8 @@ module Jsonata
|
|
|
185
187
|
end
|
|
186
188
|
|
|
187
189
|
# Normalize and trim whitespace within a string
|
|
188
|
-
# @param str
|
|
189
|
-
# @
|
|
190
|
+
# @param str [String] string to be trimmed
|
|
191
|
+
# @return [String] trimmed string
|
|
190
192
|
def trim(str)
|
|
191
193
|
# undefined inputs always return undefined
|
|
192
194
|
return nil if str.nil?
|
|
@@ -205,10 +207,10 @@ module Jsonata
|
|
|
205
207
|
end
|
|
206
208
|
|
|
207
209
|
# Pad a string to a minimum width by adding characters to the start or end
|
|
208
|
-
# @param str
|
|
209
|
-
# @param width
|
|
210
|
-
# @param char
|
|
211
|
-
# @
|
|
210
|
+
# @param str [String] string to be padded
|
|
211
|
+
# @param width [Numeric] the minimum width; +ve pads to the right, -ve pads to the left
|
|
212
|
+
# @param char [String] the pad character(s); defaults to ' '
|
|
213
|
+
# @return [String] padded string
|
|
212
214
|
def pad(str, width, char)
|
|
213
215
|
# undefined inputs always return undefined
|
|
214
216
|
return nil if str.nil?
|
|
@@ -218,6 +220,11 @@ module Jsonata
|
|
|
218
220
|
width = width.truncate
|
|
219
221
|
pad_length = width.abs - length(str)
|
|
220
222
|
if pad_length.positive?
|
|
223
|
+
# limit the length of the padding to ten million characters (1e7) to
|
|
224
|
+
# protect against excessive memory allocation, consistent with the hard
|
|
225
|
+
# limit imposed by the range operator (see error D2014)
|
|
226
|
+
raise Jsonata::Error.new('D2016', :value => pad_length) if pad_length > 1e7
|
|
227
|
+
|
|
221
228
|
padding = char * pad_length
|
|
222
229
|
padding = substring(padding, 0, pad_length) if char.length > 1
|
|
223
230
|
if width.positive?
|
|
@@ -232,9 +239,9 @@ module Jsonata
|
|
|
232
239
|
|
|
233
240
|
# Evaluate the matcher function against the str arg
|
|
234
241
|
#
|
|
235
|
-
# @param matcher
|
|
236
|
-
# @param str
|
|
237
|
-
# @
|
|
242
|
+
# @param matcher [Object] matching function (native or lambda)
|
|
243
|
+
# @param str [String] the string to match against
|
|
244
|
+
# @return [Object] structure that represents the match(es)
|
|
238
245
|
def evaluate_matcher(matcher, str = nil)
|
|
239
246
|
result = call_function(matcher, [str])
|
|
240
247
|
if Utils.truthy?(result) && !(result.kind_of?(Hash) && (result['start'].kind_of?(Numeric) || result['end'] == 'number' ||
|
|
@@ -247,9 +254,9 @@ module Jsonata
|
|
|
247
254
|
end
|
|
248
255
|
|
|
249
256
|
# Tests if the str contains the token
|
|
250
|
-
# @param str
|
|
251
|
-
# @param token
|
|
252
|
-
# @
|
|
257
|
+
# @param str [String] string to test
|
|
258
|
+
# @param token [String] substring or regex to find
|
|
259
|
+
# @return [Boolean] true if str contains token
|
|
253
260
|
def contains(str, token)
|
|
254
261
|
# undefined inputs always return undefined
|
|
255
262
|
return nil if str.nil? || token.nil?
|
|
@@ -263,10 +270,10 @@ module Jsonata
|
|
|
263
270
|
end
|
|
264
271
|
|
|
265
272
|
# Match a string with a regex returning an array of object containing details of each match
|
|
266
|
-
# @param str
|
|
267
|
-
# @param regex
|
|
268
|
-
# @param limit
|
|
269
|
-
# @
|
|
273
|
+
# @param str [String] string
|
|
274
|
+
# @param regex [String] the regex applied to the string
|
|
275
|
+
# @param limit [Integer] max number of matches to return
|
|
276
|
+
# @return [Array] The array of match objects
|
|
270
277
|
def match(str, regex, limit)
|
|
271
278
|
# undefined inputs always return undefined
|
|
272
279
|
return nil if str.nil?
|
|
@@ -296,14 +303,14 @@ module Jsonata
|
|
|
296
303
|
end
|
|
297
304
|
|
|
298
305
|
# Match a string with a regex returning an array of object containing details of each match
|
|
299
|
-
# @param str
|
|
300
|
-
# @param pattern
|
|
301
|
-
# @param replacement
|
|
302
|
-
# @param limit
|
|
303
|
-
# @
|
|
306
|
+
# @param str [String] string
|
|
307
|
+
# @param pattern [String] the substring/regex applied to the string
|
|
308
|
+
# @param replacement [String] text to replace the matched substrings
|
|
309
|
+
# @param limit [Integer] max number of matches to return
|
|
310
|
+
# @return [Array] The array of match objects
|
|
304
311
|
def replace(str, pattern, replacement, limit)
|
|
305
312
|
# undefined inputs always return undefined
|
|
306
|
-
return nil if str.nil?
|
|
313
|
+
return nil if str.nil? || pattern.nil?
|
|
307
314
|
|
|
308
315
|
# pattern cannot be an empty string
|
|
309
316
|
raise Jsonata::Error.new('D3010', :value => pattern, :index => 2) if pattern == ''
|
|
@@ -407,8 +414,8 @@ module Jsonata
|
|
|
407
414
|
end
|
|
408
415
|
|
|
409
416
|
# Base64 encode a string
|
|
410
|
-
# @param str
|
|
411
|
-
# @
|
|
417
|
+
# @param str [String] string
|
|
418
|
+
# @return [String] Base 64 encoding of the binary data
|
|
412
419
|
def base64encode(str)
|
|
413
420
|
# undefined inputs always return undefined
|
|
414
421
|
return nil if str.nil?
|
|
@@ -419,8 +426,8 @@ module Jsonata
|
|
|
419
426
|
end
|
|
420
427
|
|
|
421
428
|
# Base64 decode a string
|
|
422
|
-
# @param str
|
|
423
|
-
# @
|
|
429
|
+
# @param str [String] string
|
|
430
|
+
# @return [String] Base 64 encoding of the binary data
|
|
424
431
|
def base64decode(str)
|
|
425
432
|
# undefined inputs always return undefined
|
|
426
433
|
return nil if str.nil?
|
|
@@ -430,8 +437,8 @@ module Jsonata
|
|
|
430
437
|
end
|
|
431
438
|
|
|
432
439
|
# Encode a string into a component for a url
|
|
433
|
-
# @param str
|
|
434
|
-
# @
|
|
440
|
+
# @param str [String] String to encode
|
|
441
|
+
# @return [String] Encoded string
|
|
435
442
|
def encode_url_component(str)
|
|
436
443
|
# undefined inputs always return undefined
|
|
437
444
|
return nil if str.nil?
|
|
@@ -443,8 +450,8 @@ module Jsonata
|
|
|
443
450
|
end
|
|
444
451
|
|
|
445
452
|
# Encode a string into a url
|
|
446
|
-
# @param str
|
|
447
|
-
# @
|
|
453
|
+
# @param str [String] String to encode
|
|
454
|
+
# @return [String] Encoded string
|
|
448
455
|
def encode_url(str)
|
|
449
456
|
# undefined inputs always return undefined
|
|
450
457
|
return nil if str.nil?
|
|
@@ -456,8 +463,8 @@ module Jsonata
|
|
|
456
463
|
end
|
|
457
464
|
|
|
458
465
|
# Decode a string from a component for a url
|
|
459
|
-
# @param str
|
|
460
|
-
# @
|
|
466
|
+
# @param str [String] String to decode
|
|
467
|
+
# @return [String] Decoded string
|
|
461
468
|
def decode_url_component(str)
|
|
462
469
|
# undefined inputs always return undefined
|
|
463
470
|
return nil if str.nil?
|
|
@@ -469,8 +476,8 @@ module Jsonata
|
|
|
469
476
|
end
|
|
470
477
|
|
|
471
478
|
# Decode a string from a url
|
|
472
|
-
# @param str
|
|
473
|
-
# @
|
|
479
|
+
# @param str [String] String to decode
|
|
480
|
+
# @return [String] Decoded string
|
|
474
481
|
def decode_url(str)
|
|
475
482
|
# undefined inputs always return undefined
|
|
476
483
|
return nil if str.nil?
|
|
@@ -482,13 +489,13 @@ module Jsonata
|
|
|
482
489
|
end
|
|
483
490
|
|
|
484
491
|
# Split a string into an array of substrings
|
|
485
|
-
# @param str
|
|
486
|
-
# @param separator
|
|
487
|
-
# @param limit
|
|
488
|
-
# @
|
|
492
|
+
# @param str [String] string
|
|
493
|
+
# @param separator [String] the token or regex that splits the string
|
|
494
|
+
# @param limit [Integer] max number of substrings
|
|
495
|
+
# @return [Array] The array of string
|
|
489
496
|
def split(str, separator, limit)
|
|
490
497
|
# undefined inputs always return undefined
|
|
491
|
-
return nil if str.nil?
|
|
498
|
+
return nil if str.nil? || separator.nil?
|
|
492
499
|
|
|
493
500
|
# limit, if specified, must be a non-negative number
|
|
494
501
|
raise Jsonata::Error.new('D3020', :value => limit, :index => 3) if !limit.nil? && limit.negative?
|
|
@@ -520,9 +527,9 @@ module Jsonata
|
|
|
520
527
|
end
|
|
521
528
|
|
|
522
529
|
# Join an array of strings
|
|
523
|
-
# @param strs
|
|
524
|
-
# @param separator
|
|
525
|
-
# @
|
|
530
|
+
# @param strs [Array] array of string
|
|
531
|
+
# @param separator [String] the token that splits the string
|
|
532
|
+
# @return [String] The concatenated string
|
|
526
533
|
def join(strs, separator)
|
|
527
534
|
# undefined inputs always return undefined
|
|
528
535
|
return nil if strs.nil?
|
|
@@ -534,10 +541,10 @@ module Jsonata
|
|
|
534
541
|
end
|
|
535
542
|
|
|
536
543
|
# Formats a number into a decimal string representation using XPath 3.1 F&O fn:format-number spec
|
|
537
|
-
# @param value
|
|
538
|
-
# @param picture
|
|
539
|
-
# @param options
|
|
540
|
-
# @
|
|
544
|
+
# @param value [Numeric] number to format
|
|
545
|
+
# @param picture [String] picture string definition
|
|
546
|
+
# @param options [Object] override locale defaults
|
|
547
|
+
# @return [String] The formatted string
|
|
541
548
|
def format_number(value, picture, options)
|
|
542
549
|
# undefined inputs always return undefined
|
|
543
550
|
return nil if value.nil?
|
|
@@ -759,7 +766,11 @@ module Jsonata
|
|
|
759
766
|
# TODO cache the result of the analysis
|
|
760
767
|
|
|
761
768
|
# format the number
|
|
762
|
-
# bullet 1:
|
|
769
|
+
# bullet 1: JSON can't hold NaN or infinity, but a JavaScript caller can pass them in
|
|
770
|
+
if value.kind_of?(Float) && value.nan?
|
|
771
|
+
return properties['NaN']
|
|
772
|
+
end
|
|
773
|
+
|
|
763
774
|
# bullet 2:
|
|
764
775
|
pic = if value >= 0
|
|
765
776
|
variables[0]
|
|
@@ -774,40 +785,33 @@ module Jsonata
|
|
|
774
785
|
else
|
|
775
786
|
value
|
|
776
787
|
end
|
|
777
|
-
# bullet 4:
|
|
778
|
-
|
|
788
|
+
# bullet 4: this includes a finite number that overflows when multiplied by 100 or 1000
|
|
789
|
+
if adjusted_number.kind_of?(Float) && adjusted_number.infinite?
|
|
790
|
+
return pic[:prefix] + properties['infinity'] + pic[:suffix]
|
|
791
|
+
end
|
|
792
|
+
|
|
793
|
+
# bullets 5 & 6 work on the decimal digits of the number rather than on the double itself,
|
|
794
|
+
# so that scaling the mantissa by a power of ten and rounding it are both exact
|
|
779
795
|
# bullet 5:
|
|
796
|
+
mantissa = Utils.decimal_digits(adjusted_number)
|
|
780
797
|
exponent = nil
|
|
781
|
-
if pic[:minimum_exponent_size]
|
|
782
|
-
mantissa =
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
min_mantissa = 10.0**(pic[:scaling_factor] - 1)
|
|
787
|
-
mantissa = adjusted_number.to_f
|
|
788
|
-
exponent = 0
|
|
789
|
-
# Compare magnitudes; previously `mantissa < minMantissa` was
|
|
790
|
-
# true for every negative mantissa and `0 * 10 === 0` never
|
|
791
|
-
# crossed the threshold, producing an infinite loop on zero
|
|
792
|
-
# and negative inputs (#785). For zero the desired exponent is
|
|
793
|
-
# simply zero - the XPath F&O Bullet 5 says "if N is zero, set
|
|
794
|
-
# M to zero and E to zero".
|
|
795
|
-
unless mantissa.zero?
|
|
796
|
-
while mantissa.abs < min_mantissa
|
|
797
|
-
mantissa *= 10
|
|
798
|
-
exponent -= 1
|
|
799
|
-
end
|
|
800
|
-
while mantissa.abs > max_mantissa
|
|
801
|
-
mantissa /= 10
|
|
802
|
-
exponent += 1
|
|
803
|
-
end
|
|
804
|
-
end
|
|
798
|
+
if pic[:minimum_exponent_size] != 0
|
|
799
|
+
# mantissa * 10^exponent = adjustedNumber, with scalingFactor digits before the decimal point of the
|
|
800
|
+
# mantissa. If the number is zero, the mantissa and the exponent are both zero
|
|
801
|
+
exponent = mantissa[:digits] == '0' ? 0 : mantissa[:digits].length + mantissa[:exponent] - pic[:scaling_factor]
|
|
802
|
+
mantissa[:exponent] -= exponent
|
|
805
803
|
end
|
|
806
804
|
# bullet 6:
|
|
807
|
-
rounded_number =
|
|
805
|
+
rounded_number = Utils.round_decimal_digits(mantissa, pic[:maximum_factional_part_size])
|
|
806
|
+
if !exponent.nil? && rounded_number[:digits] != '0' &&
|
|
807
|
+
rounded_number[:digits].length + rounded_number[:exponent] > pic[:scaling_factor]
|
|
808
|
+
# rounding has carried the mantissa up to 10^scalingFactor, so divide it by ten
|
|
809
|
+
rounded_number = Utils.round_decimal_digits({:digits => rounded_number[:digits], :exponent => rounded_number[:exponent] - 1},
|
|
810
|
+
pic[:maximum_factional_part_size])
|
|
811
|
+
exponent += 1
|
|
812
|
+
end
|
|
808
813
|
# bullet 7:
|
|
809
|
-
make_string = lambda do |
|
|
810
|
-
str = Utils.to_fixed(val.abs, dp)
|
|
814
|
+
make_string = lambda do |str|
|
|
811
815
|
if zero_digit != '0'
|
|
812
816
|
str = str.each_char.map do |digit|
|
|
813
817
|
if digit.between?('0', '9')
|
|
@@ -819,7 +823,7 @@ module Jsonata
|
|
|
819
823
|
end
|
|
820
824
|
str
|
|
821
825
|
end
|
|
822
|
-
string_value = make_string.call(rounded_number, pic[:maximum_factional_part_size])
|
|
826
|
+
string_value = make_string.call(Utils.decimal_digits_to_string(rounded_number, pic[:maximum_factional_part_size]))
|
|
823
827
|
decimal_pos = string_value.index('.')
|
|
824
828
|
string_value = if decimal_pos.nil?
|
|
825
829
|
string_value + decimal_separator
|
|
@@ -844,9 +848,11 @@ module Jsonata
|
|
|
844
848
|
end
|
|
845
849
|
else
|
|
846
850
|
pic[:integer_part_grouping_positions].each do |pos|
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
851
|
+
if decimal_pos > pos
|
|
852
|
+
string_value = [Utils.js_slice(string_value, 0, decimal_pos - pos), grouping_separator,
|
|
853
|
+
Utils.js_slice(string_value, decimal_pos - pos)].join
|
|
854
|
+
decimal_pos += 1
|
|
855
|
+
end
|
|
850
856
|
end
|
|
851
857
|
end
|
|
852
858
|
# bullet 11:
|
|
@@ -862,7 +868,7 @@ module Jsonata
|
|
|
862
868
|
end
|
|
863
869
|
# bullet 13:
|
|
864
870
|
unless exponent.nil?
|
|
865
|
-
string_exponent = make_string.call(exponent
|
|
871
|
+
string_exponent = make_string.call(exponent.abs.to_s)
|
|
866
872
|
pad_left = pic[:minimum_exponent_size] - string_exponent.length
|
|
867
873
|
string_exponent = (zero_digit * pad_left) + string_exponent if pad_left.positive?
|
|
868
874
|
string_value = string_value + properties['exponent-separator'] + (exponent.negative? ? minus_sign : '') + string_exponent
|
|
@@ -872,9 +878,9 @@ module Jsonata
|
|
|
872
878
|
end
|
|
873
879
|
|
|
874
880
|
# Converts a number to a string using a specified number base
|
|
875
|
-
# @param value
|
|
876
|
-
# @param radix
|
|
877
|
-
# @
|
|
881
|
+
# @param value [Numeric] the number to convert
|
|
882
|
+
# @param radix [Numeric] the number base; must be between 2 and 36. Defaults to 10
|
|
883
|
+
# @return [String] the converted string
|
|
878
884
|
def format_base(value, radix)
|
|
879
885
|
# undefined inputs always return undefined
|
|
880
886
|
return nil if value.nil?
|
|
@@ -893,8 +899,8 @@ module Jsonata
|
|
|
893
899
|
end
|
|
894
900
|
|
|
895
901
|
# Cast argument to number
|
|
896
|
-
# @param arg
|
|
897
|
-
# @
|
|
902
|
+
# @param arg [Object] Argument
|
|
903
|
+
# @return [Numeric] numeric value of argument
|
|
898
904
|
def number(arg)
|
|
899
905
|
# undefined inputs always return undefined
|
|
900
906
|
return nil if arg.nil?
|
|
@@ -918,8 +924,8 @@ module Jsonata
|
|
|
918
924
|
end
|
|
919
925
|
|
|
920
926
|
# Absolute value of a number
|
|
921
|
-
# @param arg
|
|
922
|
-
# @
|
|
927
|
+
# @param arg [Numeric] Argument
|
|
928
|
+
# @return [Numeric] absolute value of argument
|
|
923
929
|
def abs(arg)
|
|
924
930
|
# undefined inputs always return undefined
|
|
925
931
|
return nil if arg.nil?
|
|
@@ -928,8 +934,8 @@ module Jsonata
|
|
|
928
934
|
end
|
|
929
935
|
|
|
930
936
|
# Rounds a number down to integer
|
|
931
|
-
# @param arg
|
|
932
|
-
# @
|
|
937
|
+
# @param arg [Numeric] Argument
|
|
938
|
+
# @return [Numeric] rounded integer
|
|
933
939
|
def floor(arg)
|
|
934
940
|
# undefined inputs always return undefined
|
|
935
941
|
return nil if arg.nil?
|
|
@@ -938,8 +944,8 @@ module Jsonata
|
|
|
938
944
|
end
|
|
939
945
|
|
|
940
946
|
# Rounds a number up to integer
|
|
941
|
-
# @param arg
|
|
942
|
-
# @
|
|
947
|
+
# @param arg [Numeric] Argument
|
|
948
|
+
# @return [Numeric] rounded integer
|
|
943
949
|
def ceil(arg)
|
|
944
950
|
# undefined inputs always return undefined
|
|
945
951
|
return nil if arg.nil?
|
|
@@ -948,9 +954,9 @@ module Jsonata
|
|
|
948
954
|
end
|
|
949
955
|
|
|
950
956
|
# Round to half even
|
|
951
|
-
# @param arg
|
|
952
|
-
# @param precision
|
|
953
|
-
# @
|
|
957
|
+
# @param arg [Numeric] Argument
|
|
958
|
+
# @param precision [Numeric] number of decimal places
|
|
959
|
+
# @return [Numeric] rounded integer
|
|
954
960
|
def round(arg, precision = nil)
|
|
955
961
|
# undefined inputs always return undefined
|
|
956
962
|
return nil if arg.nil?
|
|
@@ -982,8 +988,8 @@ module Jsonata
|
|
|
982
988
|
end
|
|
983
989
|
|
|
984
990
|
# Square root of number
|
|
985
|
-
# @param arg
|
|
986
|
-
# @
|
|
991
|
+
# @param arg [Numeric] Argument
|
|
992
|
+
# @return [Numeric] square root
|
|
987
993
|
def sqrt(arg)
|
|
988
994
|
# undefined inputs always return undefined
|
|
989
995
|
return nil if arg.nil?
|
|
@@ -994,9 +1000,9 @@ module Jsonata
|
|
|
994
1000
|
end
|
|
995
1001
|
|
|
996
1002
|
# Raises number to the power of the second number
|
|
997
|
-
# @param arg
|
|
998
|
-
# @param exp
|
|
999
|
-
# @
|
|
1003
|
+
# @param arg [Numeric] the base
|
|
1004
|
+
# @param exp [Numeric] the exponent
|
|
1005
|
+
# @return [Numeric] rounded integer
|
|
1000
1006
|
def power(arg, exp)
|
|
1001
1007
|
# undefined inputs always return undefined
|
|
1002
1008
|
return nil if arg.nil?
|
|
@@ -1013,14 +1019,14 @@ module Jsonata
|
|
|
1013
1019
|
end
|
|
1014
1020
|
|
|
1015
1021
|
# Returns a random number 0 <= n < 1
|
|
1016
|
-
# @
|
|
1022
|
+
# @return [Numeric] random number
|
|
1017
1023
|
def random
|
|
1018
1024
|
Kernel.rand
|
|
1019
1025
|
end
|
|
1020
1026
|
|
|
1021
1027
|
# Evaluate an input and return a boolean
|
|
1022
|
-
# @param arg
|
|
1023
|
-
# @
|
|
1028
|
+
# @param arg [Object] Arguments
|
|
1029
|
+
# @return [Boolean] Boolean
|
|
1024
1030
|
def boolean(arg)
|
|
1025
1031
|
# cast arg to its effective boolean value
|
|
1026
1032
|
# boolean: unchanged
|
|
@@ -1055,8 +1061,8 @@ module Jsonata
|
|
|
1055
1061
|
end
|
|
1056
1062
|
|
|
1057
1063
|
# returns the Boolean NOT of the arg
|
|
1058
|
-
# @param arg
|
|
1059
|
-
# @
|
|
1064
|
+
# @param arg [Object] argument
|
|
1065
|
+
# @return [Boolean] NOT arg
|
|
1060
1066
|
def not(arg)
|
|
1061
1067
|
# undefined inputs always return undefined
|
|
1062
1068
|
return nil if arg.nil?
|
|
@@ -1066,11 +1072,11 @@ module Jsonata
|
|
|
1066
1072
|
|
|
1067
1073
|
# Helper function to build the arguments to be supplied to the function arg of the
|
|
1068
1074
|
# HOFs map, filter, each, sift and single
|
|
1069
|
-
# @param func
|
|
1070
|
-
# @param arg1
|
|
1071
|
-
# @param arg2
|
|
1072
|
-
# @param arg3
|
|
1073
|
-
# @
|
|
1075
|
+
# @param func [Lambda, NativeFunction, Proc] the function to be invoked
|
|
1076
|
+
# @param arg1 [Object] the first (required) arg - the value
|
|
1077
|
+
# @param arg2 [Object] the second (optional) arg - the position (index or key)
|
|
1078
|
+
# @param arg3 [Object] the third (optional) arg - the whole structure (array or object)
|
|
1079
|
+
# @return [Array] the argument list
|
|
1074
1080
|
def hof_func_args(func, arg1, arg2, arg3)
|
|
1075
1081
|
func_args = [arg1] # the first arg (the value) is required
|
|
1076
1082
|
# the other two are optional - only supply it if the function can take it
|
|
@@ -1081,9 +1087,9 @@ module Jsonata
|
|
|
1081
1087
|
end
|
|
1082
1088
|
|
|
1083
1089
|
# Create a map from an array of arguments
|
|
1084
|
-
# @param arr
|
|
1085
|
-
# @param func
|
|
1086
|
-
# @
|
|
1090
|
+
# @param arr [Array] array to map over
|
|
1091
|
+
# @param func [Lambda, NativeFunction, Proc] function to apply
|
|
1092
|
+
# @return [Array] Map array
|
|
1087
1093
|
def map(arr, func)
|
|
1088
1094
|
# undefined inputs always return undefined
|
|
1089
1095
|
return nil if arr.nil?
|
|
@@ -1101,9 +1107,9 @@ module Jsonata
|
|
|
1101
1107
|
end
|
|
1102
1108
|
|
|
1103
1109
|
# Create a map from an array of arguments
|
|
1104
|
-
# @param arr
|
|
1105
|
-
# @param func
|
|
1106
|
-
# @
|
|
1110
|
+
# @param arr [Array] array to filter
|
|
1111
|
+
# @param func [Lambda, NativeFunction, Proc] predicate function
|
|
1112
|
+
# @return [Array] Map array
|
|
1107
1113
|
def filter(arr, func)
|
|
1108
1114
|
# undefined inputs always return undefined
|
|
1109
1115
|
return nil if arr.nil?
|
|
@@ -1122,9 +1128,9 @@ module Jsonata
|
|
|
1122
1128
|
|
|
1123
1129
|
# Given an array, find the single element matching a specified condition
|
|
1124
1130
|
# Throws an exception if the number of matching elements is not exactly one
|
|
1125
|
-
# @param arr
|
|
1126
|
-
# @param func
|
|
1127
|
-
# @
|
|
1131
|
+
# @param arr [Array] array to filter
|
|
1132
|
+
# @param func [Lambda, NativeFunction, Proc] predicate function
|
|
1133
|
+
# @return [Object] Matching element
|
|
1128
1134
|
def single(arr, func)
|
|
1129
1135
|
# undefined inputs always return undefined
|
|
1130
1136
|
return nil if arr.nil?
|
|
@@ -1154,8 +1160,8 @@ module Jsonata
|
|
|
1154
1160
|
end
|
|
1155
1161
|
|
|
1156
1162
|
# Convolves (zips) each value from a set of arrays
|
|
1157
|
-
# @param args
|
|
1158
|
-
# @
|
|
1163
|
+
# @param args [Array] arrays to zip
|
|
1164
|
+
# @return [Array] Zipped array
|
|
1159
1165
|
def zip(*args)
|
|
1160
1166
|
# this can take a variable number of arguments
|
|
1161
1167
|
result = []
|
|
@@ -1169,10 +1175,10 @@ module Jsonata
|
|
|
1169
1175
|
end
|
|
1170
1176
|
|
|
1171
1177
|
# Fold left function
|
|
1172
|
-
# @param sequence
|
|
1173
|
-
# @param func
|
|
1174
|
-
# @param init
|
|
1175
|
-
# @
|
|
1178
|
+
# @param sequence [Array] Sequence
|
|
1179
|
+
# @param func [Lambda, NativeFunction, Proc] Function
|
|
1180
|
+
# @param init [Object] Initial value
|
|
1181
|
+
# @return [Object] Result
|
|
1176
1182
|
def fold_left(sequence, func, init)
|
|
1177
1183
|
# undefined inputs always return undefined
|
|
1178
1184
|
return nil if sequence.nil?
|
|
@@ -1200,8 +1206,8 @@ module Jsonata
|
|
|
1200
1206
|
end
|
|
1201
1207
|
|
|
1202
1208
|
# Return keys for an object
|
|
1203
|
-
# @param arg
|
|
1204
|
-
# @
|
|
1209
|
+
# @param arg [Object] Object
|
|
1210
|
+
# @return [Array] Array of keys
|
|
1205
1211
|
def keys(arg)
|
|
1206
1212
|
result = create_sequence
|
|
1207
1213
|
|
|
@@ -1220,10 +1226,16 @@ module Jsonata
|
|
|
1220
1226
|
result
|
|
1221
1227
|
end
|
|
1222
1228
|
|
|
1229
|
+
# the names of properties that can't be looked up, even when the inherited option allows looking up
|
|
1230
|
+
# properties that an object inherits from its prototype. They give access to the prototype and constructor of the
|
|
1231
|
+
# object (and so to the Function constructor), or to functions that can change them
|
|
1232
|
+
BLOCKED_PROPERTIES = Set.new(%w[__proto__ constructor prototype
|
|
1233
|
+
__defineGetter__ __defineSetter__ __lookupGetter__ __lookupSetter__]).freeze
|
|
1234
|
+
|
|
1223
1235
|
# Return value from an object for a given key
|
|
1224
|
-
# @param input
|
|
1225
|
-
# @param key
|
|
1226
|
-
# @
|
|
1236
|
+
# @param input [Object] Object/Array
|
|
1237
|
+
# @param key [String] Key in object
|
|
1238
|
+
# @return [Object] Value of key in object
|
|
1227
1239
|
def lookup(input, key)
|
|
1228
1240
|
# lookup the 'name' item in the input
|
|
1229
1241
|
result = nil
|
|
@@ -1239,16 +1251,21 @@ module Jsonata
|
|
|
1239
1251
|
result.push(res)
|
|
1240
1252
|
end
|
|
1241
1253
|
end
|
|
1242
|
-
elsif input.kind_of?(Hash) &&
|
|
1254
|
+
elsif input.kind_of?(Hash) &&
|
|
1255
|
+
(input.key?(key) ||
|
|
1256
|
+
(key.kind_of?(String) && options && options[:inherited] == true && !BLOCKED_PROPERTIES.include?(key)))
|
|
1257
|
+
# an object's own properties can always be looked up, and the properties it inherits only if allowed
|
|
1258
|
+
# (Ruby specific: a Hash has no prototype; the closest equivalent of an inherited property is the Hash's
|
|
1259
|
+
# default value, but the input is converted by Utils.from_ruby, which drops it)
|
|
1243
1260
|
result = input[key]
|
|
1244
1261
|
end
|
|
1245
1262
|
result
|
|
1246
1263
|
end
|
|
1247
1264
|
|
|
1248
1265
|
# Append second argument to first
|
|
1249
|
-
# @param arg1
|
|
1250
|
-
# @param arg2
|
|
1251
|
-
# @
|
|
1266
|
+
# @param arg1 [Array, Object] First argument
|
|
1267
|
+
# @param arg2 [Array, Object] Second argument
|
|
1268
|
+
# @return [Object] Appended arguments
|
|
1252
1269
|
def append(arg1, arg2)
|
|
1253
1270
|
# disregard undefined args
|
|
1254
1271
|
return arg2 if arg1.nil?
|
|
@@ -1264,15 +1281,15 @@ module Jsonata
|
|
|
1264
1281
|
end
|
|
1265
1282
|
|
|
1266
1283
|
# Determines if the argument is undefined
|
|
1267
|
-
# @param arg
|
|
1268
|
-
# @
|
|
1284
|
+
# @param arg [Object] argument
|
|
1285
|
+
# @return [Boolean] False if argument undefined, otherwise true
|
|
1269
1286
|
def exists(arg)
|
|
1270
1287
|
!arg.nil?
|
|
1271
1288
|
end
|
|
1272
1289
|
|
|
1273
1290
|
# Splits an object into an array of object with one property each
|
|
1274
|
-
# @param arg
|
|
1275
|
-
# @
|
|
1291
|
+
# @param arg [Object] the object to split
|
|
1292
|
+
# @return [Object] the array
|
|
1276
1293
|
def spread(arg)
|
|
1277
1294
|
result = create_sequence
|
|
1278
1295
|
|
|
@@ -1293,8 +1310,8 @@ module Jsonata
|
|
|
1293
1310
|
|
|
1294
1311
|
# Merges an array of objects into a single object. Duplicate properties are
|
|
1295
1312
|
# overridden by entries later in the array
|
|
1296
|
-
# @param arg
|
|
1297
|
-
# @
|
|
1313
|
+
# @param arg [Object] the objects to merge
|
|
1314
|
+
# @return [Object] the object
|
|
1298
1315
|
def merge(arg)
|
|
1299
1316
|
# undefined inputs always return undefined
|
|
1300
1317
|
return nil if arg.nil?
|
|
@@ -1310,8 +1327,8 @@ module Jsonata
|
|
|
1310
1327
|
end
|
|
1311
1328
|
|
|
1312
1329
|
# Reverses the order of items in an array
|
|
1313
|
-
# @param arr
|
|
1314
|
-
# @
|
|
1330
|
+
# @param arr [Array] the array to reverse
|
|
1331
|
+
# @return [Array] the reversed array
|
|
1315
1332
|
def reverse(arr)
|
|
1316
1333
|
# undefined inputs always return undefined
|
|
1317
1334
|
return nil if arr.nil?
|
|
@@ -1321,13 +1338,17 @@ module Jsonata
|
|
|
1321
1338
|
Array.new(arr).reverse
|
|
1322
1339
|
end
|
|
1323
1340
|
|
|
1324
|
-
# @param obj
|
|
1325
|
-
# @param func
|
|
1326
|
-
# @
|
|
1341
|
+
# @param obj [Object] the input object to iterate over
|
|
1342
|
+
# @param func [Object] the function to apply to each key/value pair
|
|
1343
|
+
# @return [Array] the resultant array
|
|
1327
1344
|
def each(obj, func)
|
|
1328
1345
|
result = create_sequence
|
|
1329
1346
|
|
|
1330
1347
|
Utils.keys(obj).each do |key|
|
|
1348
|
+
# only read an own property: a callback for an earlier key may have deleted this key, so that
|
|
1349
|
+
# obj[key] would otherwise resolve to an inherited property
|
|
1350
|
+
next unless obj.key?(key)
|
|
1351
|
+
|
|
1331
1352
|
func_args = hof_func_args(func, obj[key], key, obj)
|
|
1332
1353
|
# invoke func
|
|
1333
1354
|
val = call_function(func, func_args)
|
|
@@ -1337,16 +1358,16 @@ module Jsonata
|
|
|
1337
1358
|
result
|
|
1338
1359
|
end
|
|
1339
1360
|
|
|
1340
|
-
# @param message
|
|
1341
|
-
# @
|
|
1361
|
+
# @param message [String] the message to attach to the error
|
|
1362
|
+
# @raise [Jsonata::Error] custom error with code 'D3137'
|
|
1342
1363
|
def error(message)
|
|
1343
1364
|
raise Jsonata::Error.new('D3137', :message => Utils.truthy?(message) ? message : '$error() function evaluated')
|
|
1344
1365
|
end
|
|
1345
1366
|
|
|
1346
|
-
# @param condition
|
|
1347
|
-
# @param message
|
|
1348
|
-
# @
|
|
1349
|
-
# @
|
|
1367
|
+
# @param condition [Boolean] the condition to evaluate
|
|
1368
|
+
# @param message [String] the message to attach to the error
|
|
1369
|
+
# @raise [Jsonata::Error] custom error with code 'D3137'
|
|
1370
|
+
# @return [nil] undefined
|
|
1350
1371
|
def assert(condition, message)
|
|
1351
1372
|
unless condition
|
|
1352
1373
|
raise Jsonata::Error.new('D3141', :message => Utils.truthy?(message) ? message : '$assert() statement failed')
|
|
@@ -1355,8 +1376,8 @@ module Jsonata
|
|
|
1355
1376
|
nil
|
|
1356
1377
|
end
|
|
1357
1378
|
|
|
1358
|
-
# @param value
|
|
1359
|
-
# @
|
|
1379
|
+
# @param value [Object] the input to which the type will be checked
|
|
1380
|
+
# @return [String] the type of the input
|
|
1360
1381
|
def type(value)
|
|
1361
1382
|
return nil if value.nil?
|
|
1362
1383
|
return 'null' if value == NULL
|
|
@@ -1371,9 +1392,9 @@ module Jsonata
|
|
|
1371
1392
|
|
|
1372
1393
|
# Implements the merge sort (stable) with optional comparator function
|
|
1373
1394
|
#
|
|
1374
|
-
# @param arr
|
|
1375
|
-
# @param comparator
|
|
1376
|
-
# @
|
|
1395
|
+
# @param arr [Array] the array to sort
|
|
1396
|
+
# @param comparator [Object] comparator function
|
|
1397
|
+
# @return [Array] sorted array
|
|
1377
1398
|
def sort(arr, comparator)
|
|
1378
1399
|
# undefined inputs always return undefined
|
|
1379
1400
|
return nil if arr.nil?
|
|
@@ -1392,25 +1413,32 @@ module Jsonata
|
|
|
1392
1413
|
comp = comparator
|
|
1393
1414
|
end
|
|
1394
1415
|
|
|
1416
|
+
# iterative merge: a recursive merge would hold one pending call and one copy of the remaining
|
|
1417
|
+
# input per element, so memory would grow quadratically with the array size
|
|
1395
1418
|
merge = lambda do |l, r|
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
if call_function(comp, [l[left], r[right]]) # invoke the comparator function
|
|
1419
|
+
merged = []
|
|
1420
|
+
left_index = 0
|
|
1421
|
+
right_index = 0
|
|
1422
|
+
while left_index < l.length && right_index < r.length
|
|
1423
|
+
if call_function(comp, [l[left_index], r[right_index]]) # invoke the comparator function
|
|
1402
1424
|
# if it returns true - swap left and right
|
|
1403
|
-
|
|
1404
|
-
|
|
1425
|
+
merged.push(r[right_index])
|
|
1426
|
+
right_index += 1
|
|
1405
1427
|
else
|
|
1406
1428
|
# otherwise keep the same order
|
|
1407
|
-
|
|
1408
|
-
|
|
1429
|
+
merged.push(l[left_index])
|
|
1430
|
+
left_index += 1
|
|
1409
1431
|
end
|
|
1410
1432
|
end
|
|
1411
|
-
|
|
1412
|
-
|
|
1413
|
-
|
|
1433
|
+
while left_index < l.length
|
|
1434
|
+
merged.push(l[left_index])
|
|
1435
|
+
left_index += 1
|
|
1436
|
+
end
|
|
1437
|
+
while right_index < r.length
|
|
1438
|
+
merged.push(r[right_index])
|
|
1439
|
+
right_index += 1
|
|
1440
|
+
end
|
|
1441
|
+
merged
|
|
1414
1442
|
end
|
|
1415
1443
|
|
|
1416
1444
|
msort = lambda do |array|
|
|
@@ -1430,8 +1458,8 @@ module Jsonata
|
|
|
1430
1458
|
end
|
|
1431
1459
|
|
|
1432
1460
|
# Randomly shuffles the contents of an array
|
|
1433
|
-
# @param arr
|
|
1434
|
-
# @
|
|
1461
|
+
# @param arr [Array] the input array
|
|
1462
|
+
# @return [Array] the shuffled array
|
|
1435
1463
|
def shuffle(arr)
|
|
1436
1464
|
# undefined inputs always return undefined
|
|
1437
1465
|
return nil if arr.nil?
|
|
@@ -1450,8 +1478,8 @@ module Jsonata
|
|
|
1450
1478
|
end
|
|
1451
1479
|
|
|
1452
1480
|
# Returns the values that appear in a sequence, with duplicates eliminated.
|
|
1453
|
-
# @param arr
|
|
1454
|
-
# @
|
|
1481
|
+
# @param arr [Array] An array or sequence of values
|
|
1482
|
+
# @return [Array] sequence of distinct values
|
|
1455
1483
|
def distinct(arr)
|
|
1456
1484
|
# undefined inputs always return undefined
|
|
1457
1485
|
return nil if arr.nil?
|
|
@@ -1471,13 +1499,17 @@ module Jsonata
|
|
|
1471
1499
|
# Applies a predicate function to each key/value pair in an object, and returns an object containing
|
|
1472
1500
|
# only the key/value pairs that passed the predicate
|
|
1473
1501
|
#
|
|
1474
|
-
# @param arg
|
|
1475
|
-
# @param func
|
|
1476
|
-
# @
|
|
1502
|
+
# @param arg [Object] the object to be sifted
|
|
1503
|
+
# @param func [Object] the predicate function (lambda or native)
|
|
1504
|
+
# @return [Object] sifted object
|
|
1477
1505
|
def sift(arg, func)
|
|
1478
1506
|
result = {}
|
|
1479
1507
|
|
|
1480
1508
|
Utils.keys(arg).each do |item|
|
|
1509
|
+
# only read an own property: a callback for an earlier key may have deleted this key, so that
|
|
1510
|
+
# arg[item] would otherwise resolve to an inherited property
|
|
1511
|
+
next unless arg.key?(item)
|
|
1512
|
+
|
|
1481
1513
|
entry = arg[item]
|
|
1482
1514
|
func_args = hof_func_args(func, entry, item, arg)
|
|
1483
1515
|
# invoke func
|