forked from jsonata-js/jsonata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonata.js
7650 lines (7086 loc) · 287 KB
/
jsonata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.jsonata = f()}})(function(){var define,module,exports;return (function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
'use strict';
var isMergeableObject = function isMergeableObject(value) {
return isNonNullObject(value)
&& !isSpecial(value)
};
function isNonNullObject(value) {
return !!value && typeof value === 'object'
}
function isSpecial(value) {
var stringValue = Object.prototype.toString.call(value);
return stringValue === '[object RegExp]'
|| stringValue === '[object Date]'
|| isReactElement(value)
}
// see https://github.com/facebook/react/blob/b5ac963fb791d1298e7f396236383bc955f916c1/src/isomorphic/classic/element/ReactElement.js#L21-L25
var canUseSymbol = typeof Symbol === 'function' && Symbol.for;
var REACT_ELEMENT_TYPE = canUseSymbol ? Symbol.for('react.element') : 0xeac7;
function isReactElement(value) {
return value.$$typeof === REACT_ELEMENT_TYPE
}
function emptyTarget(val) {
return Array.isArray(val) ? [] : {}
}
function cloneUnlessOtherwiseSpecified(value, options) {
return (options.clone !== false && options.isMergeableObject(value))
? deepmerge(emptyTarget(value), value, options)
: value
}
function defaultArrayMerge(target, source, options) {
return target.concat(source).map(function(element) {
return cloneUnlessOtherwiseSpecified(element, options)
})
}
function getMergeFunction(key, options) {
if (!options.customMerge) {
return deepmerge
}
var customMerge = options.customMerge(key);
return typeof customMerge === 'function' ? customMerge : deepmerge
}
function getEnumerableOwnPropertySymbols(target) {
return Object.getOwnPropertySymbols
? Object.getOwnPropertySymbols(target).filter(function(symbol) {
return target.propertyIsEnumerable(symbol)
})
: []
}
function getKeys(target) {
return Object.keys(target).concat(getEnumerableOwnPropertySymbols(target))
}
function propertyIsOnObject(object, property) {
try {
return property in object
} catch(_) {
return false
}
}
// Protects from prototype poisoning and unexpected merging up the prototype chain.
function propertyIsUnsafe(target, key) {
return propertyIsOnObject(target, key) // Properties are safe to merge if they don't exist in the target yet,
&& !(Object.hasOwnProperty.call(target, key) // unsafe if they exist up the prototype chain,
&& Object.propertyIsEnumerable.call(target, key)) // and also unsafe if they're nonenumerable.
}
function mergeObject(target, source, options) {
var destination = {};
if (options.isMergeableObject(target)) {
getKeys(target).forEach(function(key) {
destination[key] = cloneUnlessOtherwiseSpecified(target[key], options);
});
}
getKeys(source).forEach(function(key) {
if (propertyIsUnsafe(target, key)) {
return
}
if (propertyIsOnObject(target, key) && options.isMergeableObject(source[key])) {
destination[key] = getMergeFunction(key, options)(target[key], source[key], options);
} else {
destination[key] = cloneUnlessOtherwiseSpecified(source[key], options);
}
});
return destination
}
function deepmerge(target, source, options) {
options = options || {};
options.arrayMerge = options.arrayMerge || defaultArrayMerge;
options.isMergeableObject = options.isMergeableObject || isMergeableObject;
// cloneUnlessOtherwiseSpecified is added to `options` so that custom arrayMerge()
// implementations can use it. The caller may not replace it.
options.cloneUnlessOtherwiseSpecified = cloneUnlessOtherwiseSpecified;
var sourceIsArray = Array.isArray(source);
var targetIsArray = Array.isArray(target);
var sourceAndTargetTypesMatch = sourceIsArray === targetIsArray;
if (!sourceAndTargetTypesMatch) {
return cloneUnlessOtherwiseSpecified(source, options)
} else if (sourceIsArray) {
return options.arrayMerge(target, source, options)
} else {
return mergeObject(target, source, options)
}
}
deepmerge.all = function deepmergeAll(array, options) {
if (!Array.isArray(array)) {
throw new Error('first argument should be an array')
}
return array.reduce(function(prev, next) {
return deepmerge(prev, next, options)
}, {})
};
var deepmerge_1 = deepmerge;
module.exports = deepmerge_1;
},{}],2:[function(require,module,exports){
/**
* © Copyright IBM Corp. 2018 All Rights Reserved
* Project name: JSONata
* This project is licensed under the MIT License, see LICENSE
*/
const utils = require('./utils');
/**
* DateTime formatting and parsing functions
* Implements the xpath-functions format-date-time specification
* @type {{formatInteger, formatDateTime, parseInteger, parseDateTime}}
*/
const dateTime = (function () {
'use strict';
const stringToArray = utils.stringToArray;
const few = ['Zero', 'One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten',
'Eleven', 'Twelve', 'Thirteen', 'Fourteen', 'Fifteen', 'Sixteen', 'Seventeen', 'Eighteen', 'Nineteen'];
const ordinals = ['Zeroth', 'First', 'Second', 'Third', 'Fourth', 'Fifth', 'Sixth', 'Seventh', 'Eighth', 'Ninth', 'Tenth',
'Eleventh', 'Twelfth', 'Thirteenth', 'Fourteenth', 'Fifteenth', 'Sixteenth', 'Seventeenth', 'Eighteenth', 'Nineteenth'];
const decades = ['Twenty', 'Thirty', 'Forty', 'Fifty', 'Sixty', 'Seventy', 'Eighty', 'Ninety', 'Hundred'];
const magnitudes = ['Thousand', 'Million', 'Billion', 'Trillion'];
/**
* converts a number into english words
* @param {string} value - the value to format
* @param {boolean} ordinal - ordinal or cardinal form
* @returns {string} - representation in words
*/
function numberToWords(value, ordinal) {
var lookup = function (num, prev, ord) {
var words = '';
if (num <= 19) {
words = (prev ? ' and ' : '') + (ord ? ordinals[num] : few[num]);
} else if (num < 100) {
const tens = Math.floor(num / 10);
const remainder = num % 10;
words = (prev ? ' and ' : '') + decades[tens - 2];
if (remainder > 0) {
words += '-' + lookup(remainder, false, ord);
} else if (ord) {
words = words.substring(0, words.length - 1) + 'ieth';
}
} else if (num < 1000) {
const hundreds = Math.floor(num / 100);
const remainder = num % 100;
words = (prev ? ', ' : '') + few[hundreds] + ' Hundred';
if (remainder > 0) {
words += lookup(remainder, true, ord);
} else if (ord) {
words += 'th';
}
} else {
var mag = Math.floor(Math.log10(num) / 3);
if (mag > magnitudes.length) {
mag = magnitudes.length; // the largest word
}
const factor = Math.pow(10, mag * 3);
const mant = Math.floor(num / factor);
const remainder = num - mant * factor;
words = (prev ? ', ' : '') + lookup(mant, false, false) + ' ' + magnitudes[mag - 1];
if (remainder > 0) {
words += lookup(remainder, true, ord);
} else if (ord) {
words += 'th';
}
}
return words;
};
var words = lookup(value, false, ordinal);
return words;
}
const wordValues = {};
few.forEach(function (word, index) {
wordValues[word.toLowerCase()] = index;
});
ordinals.forEach(function (word, index) {
wordValues[word.toLowerCase()] = index;
});
decades.forEach(function (word, index) {
const lword = word.toLowerCase();
wordValues[lword] = (index + 2) * 10;
wordValues[lword.substring(0, word.length - 1) + 'ieth'] = wordValues[lword];
});
wordValues.hundredth = 100;
magnitudes.forEach(function (word, index) {
const lword = word.toLowerCase();
const val = Math.pow(10, (index + 1) * 3);
wordValues[lword] = val;
wordValues[lword + 'th'] = val;
});
/**
* Converts a number in english words to numeric value
* @param {string} text - the number in words
* @returns {number} - the numeric value
*/
function wordsToNumber(text) {
const parts = text.split(/,\s|\sand\s|[\s\\-]/);
const values = parts.map(part => wordValues[part]);
let segs = [0];
values.forEach(value => {
if (value < 100) {
let top = segs.pop();
if (top >= 1000) {
segs.push(top);
top = 0;
}
segs.push(top + value);
} else {
segs.push(segs.pop() * value);
}
});
const result = segs.reduce((a, b) => a + b, 0);
return result;
}
const romanNumerals = [
[1000, 'm'],
[900, 'cm'],
[500, 'd'],
[400, 'cd'],
[100, 'c'],
[90, 'xc'],
[50, 'l'],
[40, 'xl'],
[10, 'x'],
[9, 'ix'],
[5, 'v'],
[4, 'iv'],
[1, 'i']
];
const romanValues = {'M': 1000, 'D': 500, 'C': 100, 'L': 50, 'X': 10, 'V': 5, 'I': 1};
/**
* converts a number to roman numerals
* @param {number} value - the number
* @returns {string} - the number in roman numerals
*/
function decimalToRoman(value) {
for (var index = 0; index < romanNumerals.length; index++) {
const numeral = romanNumerals[index];
if (value >= numeral[0]) {
return numeral[1] + decimalToRoman(value - numeral[0]);
}
}
return '';
}
/**
* converts roman numerals to a number
* @param {string} roman - roman number
* @returns {number} - the numeric value
*/
function romanToDecimal(roman) {
var decimal = 0;
var max = 1;
for (var i = roman.length - 1; i >= 0; i--) {
const digit = roman[i];
const value = romanValues[digit];
if (value < max) {
decimal -= value;
} else {
max = value;
decimal += value;
}
}
return decimal;
}
/**
* converts a number to spreadsheet style letters
* @param {number} value - the number
* @param {string} aChar - the character representing the start of the sequence, e.g. 'A'
* @returns {string} - the letters
*/
function decimalToLetters(value, aChar) {
var letters = [];
var aCode = aChar.charCodeAt(0);
while (value > 0) {
letters.unshift(String.fromCharCode((value - 1) % 26 + aCode));
value = Math.floor((value - 1) / 26);
}
return letters.join('');
}
/**
* converts spreadsheet style letters to a number
* @param {string} letters - the letters
* @param {string} aChar - the character representing the start of the sequence, e.g. 'A'
* @returns {number} - the numeric value
*/
function lettersToDecimal(letters, aChar) {
var aCode = aChar.charCodeAt(0);
var decimal = 0;
for (var i = 0; i < letters.length; i++) {
decimal += (letters.charCodeAt(letters.length - i - 1) - aCode + 1) * Math.pow(26, i);
}
return decimal;
}
/**
* Formats an integer as specified by the XPath fn:format-integer function
* See https://www.w3.org/TR/xpath-functions-31/#func-format-integer
* @param {number} value - the number to be formatted
* @param {string} picture - the picture string that specifies the format
* @returns {string} - the formatted number
*/
function formatInteger(value, picture) {
if (typeof value === 'undefined') {
return undefined;
}
value = Math.floor(value);
const format = analyseIntegerPicture(picture);
return _formatInteger(value, format);
}
const formats = {
DECIMAL: 'decimal',
LETTERS: 'letters',
ROMAN: 'roman',
WORDS: 'words',
SEQUENCE: 'sequence'
};
const tcase = {
UPPER: 'upper',
LOWER: 'lower',
TITLE: 'title'
};
/**
* formats an integer using a preprocessed representation of the picture string
* @param {number} value - the number to be formatted
* @param {object} format - the preprocessed representation of the pucture string
* @returns {string} - the formatted number
* @private
*/
function _formatInteger(value, format) {
let formattedInteger;
const negative = value < 0;
value = Math.abs(value);
switch (format.primary) {
case formats.LETTERS:
formattedInteger = decimalToLetters(value, format.case === tcase.UPPER ? 'A' : 'a');
break;
case formats.ROMAN:
formattedInteger = decimalToRoman(value);
if (format.case === tcase.UPPER) {
formattedInteger = formattedInteger.toUpperCase();
}
break;
case formats.WORDS:
formattedInteger = numberToWords(value, format.ordinal);
if (format.case === tcase.UPPER) {
formattedInteger = formattedInteger.toUpperCase();
} else if (format.case === tcase.LOWER) {
formattedInteger = formattedInteger.toLowerCase();
}
break;
case formats.DECIMAL:
formattedInteger = '' + value;
// TODO use functionPad
var padLength = format.mandatoryDigits - formattedInteger.length;
if (padLength > 0) {
var padding = (new Array(padLength + 1)).join('0');
formattedInteger = padding + formattedInteger;
}
if (format.zeroCode !== 0x30) {
formattedInteger = stringToArray(formattedInteger).map(code => {
return String.fromCodePoint(code.codePointAt(0) + format.zeroCode - 0x30);
}).join('');
}
// insert the grouping-separator-signs, if any
if (format.regular) {
const n = Math.floor((formattedInteger.length - 1) / format.groupingSeparators.position);
for (let ii = n; ii > 0; ii--) {
const pos = formattedInteger.length - ii * format.groupingSeparators.position;
formattedInteger = formattedInteger.substr(0, pos) + format.groupingSeparators.character + formattedInteger.substr(pos);
}
} else {
format.groupingSeparators.reverse().forEach(separator => {
const pos = formattedInteger.length - separator.position;
formattedInteger = formattedInteger.substr(0, pos) + separator.character + formattedInteger.substr(pos);
});
}
if (format.ordinal) {
var suffix123 = {'1': 'st', '2': 'nd', '3': 'rd'};
var lastDigit = formattedInteger[formattedInteger.length - 1];
var suffix = suffix123[lastDigit];
if (!suffix || (formattedInteger.length > 1 && formattedInteger[formattedInteger.length - 2] === '1')) {
suffix = 'th';
}
formattedInteger = formattedInteger + suffix;
}
break;
case formats.SEQUENCE:
throw {
code: 'D3130',
value: format.token
};
}
if (negative) {
formattedInteger = '-' + formattedInteger;
}
return formattedInteger;
}
//TODO what about decimal groups in the unicode supplementary planes (surrogate pairs) ???
const decimalGroups = [0x30, 0x0660, 0x06F0, 0x07C0, 0x0966, 0x09E6, 0x0A66, 0x0AE6, 0x0B66, 0x0BE6, 0x0C66, 0x0CE6, 0x0D66, 0x0DE6, 0x0E50, 0x0ED0, 0x0F20, 0x1040, 0x1090, 0x17E0, 0x1810, 0x1946, 0x19D0, 0x1A80, 0x1A90, 0x1B50, 0x1BB0, 0x1C40, 0x1C50, 0xA620, 0xA8D0, 0xA900, 0xA9D0, 0xA9F0, 0xAA50, 0xABF0, 0xFF10];
/**
* preprocesses the picture string
* @param {string} picture - picture string
* @returns {{type: string, primary: string, case: string, ordinal: boolean}} - analysed picture
*/
function analyseIntegerPicture(picture) {
const format = {
type: 'integer',
primary: formats.DECIMAL,
case: tcase.LOWER,
ordinal: false
};
let primaryFormat, formatModifier;
const semicolon = picture.lastIndexOf(';');
if (semicolon === -1) {
primaryFormat = picture;
} else {
primaryFormat = picture.substring(0, semicolon);
formatModifier = picture.substring(semicolon + 1);
if (formatModifier[0] === 'o') {
format.ordinal = true;
}
}
/* eslnt-disable-next no-fallthrough */
switch (primaryFormat) {
case 'A':
format.case = tcase.UPPER;
/* eslnt-disable-next-line no-fallthrough */
case 'a':
format.primary = formats.LETTERS;
break;
case 'I':
format.case = tcase.UPPER;
/* eslnt-disable-next-line no-fallthrough */
case 'i':
format.primary = formats.ROMAN;
break;
case 'W':
format.case = tcase.UPPER;
format.primary = formats.WORDS;
break;
case 'Ww':
format.case = tcase.TITLE;
format.primary = formats.WORDS;
break;
case 'w':
format.primary = formats.WORDS;
break;
default: {
// this is a decimal-digit-pattern if it contains a decimal digit (from any unicode decimal digit group)
let zeroCode = null;
let mandatoryDigits = 0;
let optionalDigits = 0;
let groupingSeparators = [];
let separatorPosition = 0;
const formatCodepoints = stringToArray(primaryFormat).map(c => c.codePointAt(0)).reverse(); // reverse the array to determine positions of grouping-separator-signs
formatCodepoints.forEach((codePoint) => {
// step though each char in the picture to determine the digit group
let digit = false;
for (let ii = 0; ii < decimalGroups.length; ii++) {
const group = decimalGroups[ii];
if (codePoint >= group && codePoint <= group + 9) {
// codepoint is part of this decimal group
digit = true;
mandatoryDigits++;
separatorPosition++;
if (zeroCode === null) {
zeroCode = group;
} else if (group !== zeroCode) {
// error! different decimal groups in the same pattern
throw {
code: 'D3131'
};
}
break;
}
}
if (!digit) {
if (codePoint === 0x23) { // # - optional-digit-sign
separatorPosition++;
optionalDigits++;
} else {
// neither a decimal-digit-sign ot optional-digit-sign, assume it is a grouping-separator-sign
groupingSeparators.push({
position: separatorPosition,
character: String.fromCodePoint(codePoint)
});
}
}
});
if (mandatoryDigits > 0) {
format.primary = formats.DECIMAL;
// TODO validate decimal-digit-pattern
// the decimal digit family (codepoint offset)
format.zeroCode = zeroCode;
// the number of mandatory digits
format.mandatoryDigits = mandatoryDigits;
// the number of optional digits
format.optionalDigits = optionalDigits;
// grouping separator template
// are the grouping-separator-signs 'regular'?
const regularRepeat = function (separators) {
// are the grouping positions regular? i.e. same interval between each of them
// is there at least one separator?
if (separators.length === 0) {
return 0;
}
// are all the characters the same?
const sepChar = separators[0].character;
for (let ii = 1; ii < separators.length; ii++) {
if (separators[ii].character !== sepChar) {
return 0;
}
}
// are they equally spaced?
const indexes = separators.map(separator => separator.position);
const gcd = function (a, b) {
return b === 0 ? a : gcd(b, a % b);
};
// find the greatest common divisor of all the positions
const factor = indexes.reduce(gcd);
// is every position separated by this divisor? If so, it's regular
for (let index = 1; index <= indexes.length; index++) {
if (indexes.indexOf(index * factor) === -1) {
return 0;
}
}
return factor;
};
const regular = regularRepeat(groupingSeparators);
if (regular > 0) {
format.regular = true;
format.groupingSeparators = {
position: regular,
character: groupingSeparators[0].character
};
} else {
format.regular = false;
format.groupingSeparators = groupingSeparators;
}
} else {
// this is a 'numbering sequence' which the spec says is implementation-defined
// this implementation doesn't support any numbering sequences at the moment.
format.primary = formats.SEQUENCE;
format.token = primaryFormat;
}
}
}
return format;
}
const defaultPresentationModifiers = {
Y: '1', M: '1', D: '1', d: '1', F: 'n', W: '1', w: '1', X: '1', x: '1', H: '1', h: '1',
P: 'n', m: '01', s: '01', f: '1', Z: '01:01', z: '01:01', C: 'n', E: 'n'
};
// §9.8.4.1 the format specifier is an array of string literals and variable markers
/**
* analyse the date-time picture string
* @param {string} picture - picture string
* @returns {{type: string, parts: Array}} - the analysed string
*/
function analyseDateTimePicture(picture) {
var spec = [];
const format = {
type: 'datetime',
parts: spec
};
const addLiteral = function (start, end) {
if (end > start) {
let literal = picture.substring(start, end);
// replace any doubled ]] with single ]
// what if there are instances of single ']' ? - the spec doesn't say
literal = literal.split(']]').join(']');
spec.push({type: 'literal', value: literal});
}
};
var start = 0, pos = 0;
while (pos < picture.length) {
if (picture.charAt(pos) === '[') {
// check it's not a doubled [[
if (picture.charAt(pos + 1) === '[') {
// literal [
addLiteral(start, pos);
spec.push({type: 'literal', value: '['});
pos += 2;
start = pos;
continue;
}
// start of variable marker
// push the string literal (if there is one) onto the array
addLiteral(start, pos);
start = pos;
// search forward to closing ]
pos = picture.indexOf(']', start);
// TODO handle error case if pos === -1
if(pos === -1) {
// error - no closing bracket
throw {
code: 'D3135'
};
}
let marker = picture.substring(start + 1, pos);
// whitespace within a variable marker is ignored (i.e. remove it)
marker = marker.split(/\s+/).join('');
var def = {
type: 'marker',
component: marker.charAt(0) // 1. The component specifier is always present and is always a single letter.
};
var comma = marker.lastIndexOf(','); // 2. The width modifier may be recognized by the presence of a comma
var presMod; // the presentation modifiers
if (comma !== -1) {
// §9.8.4.2 The Width Modifier
const widthMod = marker.substring(comma + 1);
const dash = widthMod.indexOf('-');
let min, max;
const parseWidth = function (wm) {
if (typeof wm === 'undefined' || wm === '*') {
return undefined;
} else {
// TODO validate wm is an unsigned int
return parseInt(wm);
}
};
if (dash === -1) {
min = widthMod;
} else {
min = widthMod.substring(0, dash);
max = widthMod.substring(dash + 1);
}
const widthDef = {
min: parseWidth(min),
max: parseWidth(max)
};
def.width = widthDef;
presMod = marker.substring(1, comma);
} else {
presMod = marker.substring(1);
}
if (presMod.length === 1) {
def.presentation1 = presMod; // first presentation modifier
//TODO validate the first presentation modifier - it's either N, n, Nn or it passes analyseIntegerPicture
} else if (presMod.length > 1) {
var lastChar = presMod.charAt(presMod.length - 1);
if ('atco'.indexOf(lastChar) !== -1) {
def.presentation2 = lastChar;
if (lastChar === 'o') {
def.ordinal = true;
}
// 'c' means 'cardinal' and is the default (i.e. not 'ordinal')
// 'a' & 't' are ignored (not sure of their relevance to English numbering)
def.presentation1 = presMod.substring(0, presMod.length - 1);
} else {
def.presentation1 = presMod;
//TODO validate the first presentation modifier - it's either N, n, Nn or it passes analyseIntegerPicture,
// doesn't use ] as grouping separator, and if grouping separator is , then must have width modifier
}
} else {
// no presentation modifier specified - apply the default;
def.presentation1 = defaultPresentationModifiers[def.component];
}
if (typeof def.presentation1 === 'undefined') {
// unknown component specifier
throw {
code: 'D3132',
value: def.component
};
}
if (def.presentation1[0] === 'n') {
def.names = tcase.LOWER;
} else if (def.presentation1[0] === 'N') {
if (def.presentation1[1] === 'n') {
def.names = tcase.TITLE;
} else {
def.names = tcase.UPPER;
}
} else if ('YMDdFWwXxHhmsf'.indexOf(def.component) !== -1) {
var integerPattern = def.presentation1;
if (def.presentation2) {
integerPattern += ';' + def.presentation2;
}
def.integerFormat = analyseIntegerPicture(integerPattern);
if (def.width && def.width.min !== undefined) {
if (def.integerFormat.mandatoryDigits < def.width.min) {
def.integerFormat.mandatoryDigits = def.width.min;
}
}
if (def.component === 'Y') {
// §9.8.4.4
def.n = -1;
if (def.width && def.width.max !== undefined) {
def.n = def.width.max;
def.integerFormat.mandatoryDigits = def.n;
} else {
var w = def.integerFormat.mandatoryDigits + def.integerFormat.optionalDigits;
if (w >= 2) {
def.n = w;
}
}
}
}
if (def.component === 'Z' || def.component === 'z') {
def.integerFormat = analyseIntegerPicture(def.presentation1);
}
spec.push(def);
start = pos + 1;
}
pos++;
}
addLiteral(start, pos);
return format;
}
const days = ['', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'];
const months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
const millisInADay = 1000 * 60 * 60 * 24;
const startOfFirstWeek = function (ym) {
// ISO 8601 defines the first week of the year to be the week that contains the first Thursday
// XPath F&O extends this same definition for the first week of a month
// the week starts on a Monday - calculate the millis for the start of the first week
// millis for given 1st Jan of that year (at 00:00 UTC)
const jan1 = Date.UTC(ym.year, ym.month);
var dayOfJan1 = (new Date(jan1)).getUTCDay();
if (dayOfJan1 === 0) {
dayOfJan1 = 7;
}
// if Jan 1 is Fri, Sat or Sun, then add the number of days (in millis) to jan1 to get the start of week 1
return dayOfJan1 > 4 ? jan1 + (8 - dayOfJan1) * millisInADay : jan1 - (dayOfJan1 - 1) * millisInADay;
};
const yearMonth = function (year, month) {
return {
year: year,
month: month,
nextMonth: function () {
return (month === 11) ? yearMonth(year + 1, 0) : yearMonth(year, month + 1);
},
previousMonth: function () {
return (month === 0) ? yearMonth(year - 1, 11) : yearMonth(year, month - 1);
},
nextYear: function () {
return yearMonth(year + 1, month);
},
previousYear: function () {
return yearMonth(year - 1, month);
}
};
};
const deltaWeeks = function (start, end) {
return (end - start) / (millisInADay * 7) + 1;
};
const getDateTimeFragment = (date, component) => {
let componentValue;
switch (component) {
case 'Y': // year
componentValue = date.getUTCFullYear();
break;
case 'M': // month in year
componentValue = date.getUTCMonth() + 1;
break;
case 'D': // day in month
componentValue = date.getUTCDate();
break;
case 'd': { // day in year
// millis for given date (at 00:00 UTC)
const today = Date.UTC(date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate());
// millis for given 1st Jan of that year (at 00:00 UTC)
const firstJan = Date.UTC(date.getUTCFullYear(), 0);
componentValue = (today - firstJan) / millisInADay + 1;
break;
}
case 'F': // day of week
componentValue = date.getUTCDay();
if (componentValue === 0) {
// ISO 8601 defines days 1-7: Mon-Sun
componentValue = 7;
}
break;
case 'W': { // week in year
const thisYear = yearMonth(date.getUTCFullYear(), 0);
const startOfWeek1 = startOfFirstWeek(thisYear);
const today = Date.UTC(thisYear.year, date.getUTCMonth(), date.getUTCDate());
let week = deltaWeeks(startOfWeek1, today);
if (week > 52) {
// might be first week of the following year
const startOfFollowingYear = startOfFirstWeek(thisYear.nextYear());
if (today >= startOfFollowingYear) {
week = 1;
}
} else if (week < 1) {
// must be end of the previous year
const startOfPreviousYear = startOfFirstWeek(thisYear.previousYear());
week = deltaWeeks(startOfPreviousYear, today);
}
componentValue = Math.floor(week);
break;
}
case 'w': { // week in month
const thisMonth = yearMonth(date.getUTCFullYear(), date.getUTCMonth());
const startOfWeek1 = startOfFirstWeek(thisMonth);
const today = Date.UTC(thisMonth.year, thisMonth.month, date.getUTCDate());
let week = deltaWeeks(startOfWeek1, today);
if (week > 4) {
// might be first week of the following month
const startOfFollowingMonth = startOfFirstWeek(thisMonth.nextMonth());
if (today >= startOfFollowingMonth) {
week = 1;
}
} else if (week < 1) {
// must be end of the previous month
const startOfPreviousMonth = startOfFirstWeek(thisMonth.previousMonth());
week = deltaWeeks(startOfPreviousMonth, today);
}
componentValue = Math.floor(week);
break;
}
case 'X': { // ISO week-numbering year
// Extension: The F&O spec says nothing about how to access the year associated with the week-of-the-year
// e.g. Sat 1 Jan 2005 is in the 53rd week of 2004.
// The 'W' component specifier gives 53, but 'Y' will give 2005.
// I propose to add 'X' as the component specifier to give the ISO week-numbering year (2004 in this example)
const thisYear = yearMonth(date.getUTCFullYear(), 0);
const startOfISOYear = startOfFirstWeek(thisYear);
const endOfISOYear = startOfFirstWeek(thisYear.nextYear());
const now = date.getTime();
if (now < startOfISOYear) {
componentValue = thisYear.year - 1;
} else if (now >= endOfISOYear) {
componentValue = thisYear.year + 1;
} else {
componentValue = thisYear.year;
}
break;
}
case 'x': { // ISO week-numbering month
// Extension: The F&O spec says nothing about how to access the month associated with the week-of-the-month
// e.g. Sat 1 Jan 2005 is in the 5th week of December 2004.
// The 'w' component specifier gives 5, but 'W' will give January and 'Y' will give 2005.
// I propose to add 'x' as the component specifier to give the 'week-numbering' month (December in this example)
const thisMonth = yearMonth(date.getUTCFullYear(), date.getUTCMonth());
const startOfISOMonth = startOfFirstWeek(thisMonth);
const nextMonth = thisMonth.nextMonth();
const endOfISOMonth = startOfFirstWeek(nextMonth);
const now = date.getTime();
if (now < startOfISOMonth) {
componentValue = thisMonth.previousMonth().month + 1;
} else if (now >= endOfISOMonth) {
componentValue = nextMonth.month + 1;
} else {
componentValue = thisMonth.month + 1;
}
break;
}
case 'H': // hour in day (24 hours)
componentValue = date.getUTCHours();
break;
case 'h': // hour in half-day (12 hours)
componentValue = date.getUTCHours();
componentValue = componentValue % 12;
if (componentValue === 0) {
componentValue = 12;
}
break;
case 'P': // am/pm marker
componentValue = date.getUTCHours() >= 12 ? 'pm' : 'am';
break;
case 'm': // minute in hour
componentValue = date.getUTCMinutes();
break;
case 's': // second in minute
componentValue = date.getUTCSeconds();
break;
case 'f': // fractional seconds
componentValue = date.getUTCMilliseconds();
break;
case 'Z': // timezone
case 'z':
// since the date object is constructed from epoch millis, the TZ component is always be UTC.
break;
case 'C': // calendar name
componentValue = 'ISO';
break;
case 'E': // era
componentValue = 'ISO';
break;
}
return componentValue;
};
let iso8601Spec = null;
/**
* formats the date/time as specified by the XPath fn:format-dateTime function
* @param {number} millis - the timestamp to be formatted, in millis since the epoch
* @param {string} picture - the picture string that specifies the format
* @param {string} timezone - the timezone to use
* @returns {string} - the formatted timestamp
*/
function formatDateTime(millis, picture, timezone) {
var offsetHours = 0;
var offsetMinutes = 0;
if (typeof timezone !== 'undefined') {
// parse the hour and minute offsets
// assume for now the format supplied is +hhmm
const offset = parseInt(timezone);
offsetHours = Math.floor(offset / 100);
offsetMinutes = offset % 100;
}
var formatComponent = function (date, markerSpec) {
var componentValue = getDateTimeFragment(date, markerSpec.component);
// §9.8.4.3 Formatting Integer-Valued Date/Time Components
if ('YMDdFWwXxHhms'.indexOf(markerSpec.component) !== -1) {
if (markerSpec.component === 'Y') {
// §9.8.4.4 Formatting the Year Component
if (markerSpec.n !== -1) {
componentValue = componentValue % Math.pow(10, markerSpec.n);
}
}
if (markerSpec.names) {
if (markerSpec.component === 'M' || markerSpec.component === 'x') {
componentValue = months[componentValue - 1];
} else if (markerSpec.component === 'F') {
componentValue = days[componentValue];
} else {
throw {
code: 'D3133',
value: markerSpec.component
};
}
if (markerSpec.names === tcase.UPPER) {
componentValue = componentValue.toUpperCase();