-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathregion.js
1422 lines (1371 loc) · 47.8 KB
/
region.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
'use strict';
/* HEADERS */
const get_nav_connection = () => new Promise(resolve => {
const METRIC = "connection"
let notation = ""
function finish(value, display) {
if (isSmart) {notation = display == "undefined" ? default_green: default_red}
log_display(5, METRIC, display + notation)
return resolve([METRIC, value])
}
try {
let nav = navigator.connection
if (runSE) {foo++
} else if (runST) {nav = null
} else if (runSL) {sData[SECT99].push("Navigator.connection")
}
if (nav === undefined || "object" === typeof nav) {
if (nav === undefined) {
nav +=""
let display = nav
if (isProxy && sData[SECT99].includes("Navigator.connection")) {
nav = zLIE
display = colorFn(display)
log_known(SECT5, METRIC)
}
finish(nav, display)
} else {
if (nav +"" === "[object NetworkInformation]") {
// at this point there should be no lies
let oTemp = {}, oNetwork = {}
// https://developer.mozilla.org/en-US/docs/Web/API/NetworkInformation
let keyTypes = {
"addEventListener": "function",
"dispatchEvent": "function",
"ontypechange": "object",
"removeEventListener": "function",
"type": "string",
}
for (let key in navigator.connection) {
let keyValue = navigator.connection[key]
let keyType = typeof keyValue
if ("function" === keyType) {oTemp[key] = keyType} else {oTemp[key] = keyValue}
}
// sort
for (const k of Object.keys(oTemp).sort()) {oNetwork[k] = oTemp[k]}
if (Object.keys(oNetwork).length > 0) {
let hash = mini(oNetwork), isLies = false
if (isSmart) {
notation = default_red
if (isProxy && sData[SECT99].includes("Navigator.connection")) {
addDetail(METRIC, oNetwork)
log_display(5, METRIC, colorFn(hash) + addButton(5, METRIC) + notation)
log_known(SECT5, METRIC)
return resolve([METRIC, zLIE])
}
}
// yay!
addData(5, METRIC, oNetwork, hash)
log_display(5, METRIC, hash + addButton(5, METRIC) + notation)
return resolve()
} else {
// can't imagine this but who knows
finish(zErr, log_error(SECT5, METRIC, zErrEmpty +": "+ cleanFn(oNetwork)))
}
} else {
finish(zErr, log_error(SECT5, METRIC, zErrInvalid + cleanFn(nav)))
}
}
} else {
finish(zErr, log_error(SECT5, METRIC, zErrType + typeof nav))
}
} catch(e) {
finish(zErr, log_error(SECT5, METRIC, e))
}
})
const get_nav_dnt = () => new Promise(resolve => {
// expected nav: i.e you can't remove it
const METRIC = "doNotTrack"
function exit(value, display) {
log_display(5, METRIC, display +"")
return resolve([METRIC, value])
}
try {
let value = navigator[METRIC]
if (runST) {value = 1}
let display = value
if (isGecko) {
if ("1" !== value && "unspecified" !== value) {
if ("string" === typeof value) {
display = log_error(SECT5, METRIC, zErrInvalid + cleanFn(value))
} else {
display = log_error(SECT5, METRIC, zErrType + typeof value)
}
value = zErr
}
}
exit(value, display)
} catch(e) {
exit(zErr, log_error(SECT5, METRIC, e))
}
})
const get_nav_gpc = () => new Promise(resolve => {
// GPC: 1670058
// privacy.globalprivacycontrol.functionality.enabled = navigator
// privacy.globalprivacycontrol.enabled = true/false
const METRIC = "globalPrivacyControl"
function exit(value, display) {
let notation = ""
if (isSmart) {
if (isVer < 120) {
notation = (value == "undefined" ? default_green : default_red)
}
// FF120+ desktop (not sure about android): gpc enabled: default false, but true pbmode
// since we can't really distinguish PBmode (cache aside for now) then don't both notating
}
log_display(5, METRIC, display + notation)
return resolve([METRIC, value])
}
try {
let value = navigator[METRIC]
if ("boolean" !== typeof value) {value = cleanFn(value)}
if (runST) {value = zUQ} else if (runSL) {sData[SECT99].push("Navigator."+ METRIC)}
let display = value
if ("boolean" !== typeof value && value !== "undefined") {
value = zErr
display = log_error(SECT5, METRIC, zErrType + typeof value)
} else if (isProxy && sData[SECT99].includes("Navigator."+ METRIC)) {
value = zLIE
display = colorFn(display)
log_known(SECT5, METRIC)
}
exit(value, display)
} catch(e) {
exit(zErr, log_error(SECT5, METRIC, e))
}
})
/* REGION */
const set_isLanguageSmart = () => new Promise(resolve => {
// set once: ignore android for now
if (isOS === "android" || !gLoad || !isSmart) {return}
// TB/MB always or FF if we the locale matches
// resource://gre/res/multilocale.txt
isLanguageSmart = isTB
const en = "en-US, en"
languagesSupported = {
// language = existing key | languages = key + value[0] | locale = key unless value[1] !== undefined
"ar": [en],
"ca": [en],
"cs": ["sk, "+ en],
"da": [en],
"de": [en],
"el-GR": ["el, "+ en, "el"],
"en-US": ["en"],
"es-ES": ["es, "+ en],
"fa-IR": ["fa, "+ en, "fa"],
"fi-FI": ["fi, "+ en, "fi"],
"fr": ["fr-FR, "+ en],
"ga-IE": ["ga, en-IE, en-GB, "+ en],
"he": ["he-IL, "+ en],
"hu-HU": ["hu, "+ en, "hu"],
"id": [en],
"is": [en],
"it-IT": ["it, "+ en, "it"],
"ja": [en],
"ka-GE": ["ka, "+ en, "ka"],
"ko-KR": ["ko, "+ en, "ko"],
"lt": [en +", ru, pl"],
"mk-MK": ["mk, "+ en, "mk"],
"ms": [en],
"my": ["en-GB, en"],
"nb-NO": ["nb, no-NO, no, nn-NO, nn, "+ en],
"nl": [en],
"pl": [en],
"pt-BR": ["pt, "+ en],
"ro-RO": ["ro, en-US, en-GB, en", "ro"],
"ru-RU": ["ru, "+ en, "ru"],
"sq": ["sq-AL, "+ en],
"sv-SE": ["sv, "+ en],
"th": [en],
"tr-TR": ["tr, "+ en, "tr"],
"uk-UA": ["uk, "+ en, "uk"],
"vi-VN": ["vi, "+ en, "vi"],
"zh-CN": ["zh, zh-TW, zh-HK, "+ en, "zh-Hans-CN"],
"zh-TW": ["zh, "+ en, "zh-Hant-TW"],
}
localesSupported = {
// v hashes are with localized NumberRangeOver/Underflow
"ar": {m: "1f9a06e3", v: "1dfb5b8c", x: "ebfbdc43"},
"ca": {m: "d856d812", v: "6b3bb3d8", x: "81f31519"},
"cs": {m: "c92accb0", v: "de3ab0ad", x: "45f277f7"},
"da": {m: "39169214", v: "479797a1", x: "44535972"},
"de": {m: "298d11c6", v: "f9e2eae6", x: "f4b2a56f"},
// el: xml n30 = english but is would-be-n39 "reserved prefix (xmlns) must not be declared or undeclared"
// changing to spoof english returns n30.. phew!
"el": {m: "7053311d", v: "b1a88a13", x: "da8c80af"},
"en-US": {m: "05c30936", v: "41310558", x: "945f8952"},
"es-ES": {m: "96b78cbd", v: "97c3f5a9", x: "3eeba3bc"},
"fa": {m: "6648d919", v: "8ef57409", x: "113d0a7e"},
"fi": {m: "82d079c7", v: "3e29e6e7", x: "71abeeec"},
"fr": {m: "024d0fce", v: "34e28fa2", x: "74f5df3d"},
"ga-IE": {m: "97fca229", v: "2bf1321d", x: "d9761e70"},
// he: xml n27 n28 n30 = english
"he": {m: "cdde832b", v: "e47dbb82", x: "786876d5"},
"hu": {m: "db7366e6", v: "dad6d689", x: "9f537fe6"},
"id": {m: "1e275882", v: "71224946", x: "79f3851e"},
"is": {m: "204c8f73", v: "d150027b", x: "7f3e38b8"},
"it": {m: "716e7242", v: "3b781f09", x: "469cb2af"},
"ja": {m: "ab56d7cb", v: "48645d06", x: "6823cee8"},
"ka": {m: "6961b7e4", v: "40feb44f", x: "4e712712"},
"ko": {m: "c758b027", v: "d3b54047", x: "fc4c50ed"},
"lt": {m: "c36fbafb", v: "d5f9b95d", x: "f50f2b50"},
// mk: v = english but not number format, and xml n30 = english but is would-be-n39 (same as el)
// and n27 n28 = english
"mk": {m: "78274f1b", v: "333aae58", x: "3b22df8b"},
"ms": {m: "3e26c6be", v: "9dadbc64", x: "f23d0969"},
// my: two items in english: date+over/under
"my": {m: "939f2013", v: "43cc3aa3", x: "11d4d458"},
// nb-NO: xml most is english
"nb-NO": {m: "1d496fea", v: "84ce54eb", x: "50426960"},
"nl": {m: "e1d3b281", v: "326cbfd2", x: "b03574e4"},
"pl": {m: "0bd88e98", v: "95ad4851", x: "c1295e2b"},
"pt-BR": {m: "39835e93", v: "de2c3569", x: "96f79e68"},
"ro": {m: "3e321768", v: "d72a350b", x: "cf85bb64"},
"ru": {m: "8e9b7945", v: "2391fbec", x: "2178a2b6"},
"sq": {m: "91943e67", v: "e0259277", x: "a732eca1"},
"sv-SE": {m: "bc792ce2", v: "d9d7828b", x: "80f52165"},
"th": {m: "a32d70a7", v: "8448474c", x: "e29567ce"},
"tr": {m: "4217ef80", v: "169730ca", x: "1e9d0192"},
"uk": {m: "3e2b3e39", v: "24cce2c1", x: "cc85d2f5"},
"vi": {m: "bba6c980", v: "b8137d59", x: "7cf3c6f9"},
"zh-Hans-CN": {m: "550ea53e", v:"55d25655", x: "328cc79b"},
"zh-Hant-TW": {m: "66b515a4", v: "8e4cfa0e", x: "87abb9fa"},
}
// mac: japanese languages are the same but the locale is 'ja-JP' not 'ja'
if (isOS == "mac") {
languagesSupported['ja'].push('ja-JP')
let macvalue = localesSupported.ja
delete localesSupported['ja']
localesSupported["ja-JP"] = macvalue
}
if (isMullvad) {
// 22 of 38 supported
let notSupported = [
// lang
"ca","cs","el-GR","ga-IE","he","hu-HU","id","is","ka-GE","lt","mk-MK","ms","ro-RO","sq","uk-UA","vi-VN",
// + locales
"el","hu","ka","mk","ro","uk","vi",
]
notSupported.forEach(function(key){
delete languagesSupported[key]
delete localesSupported[key]
})
}
return resolve()
})
const set_oIntlTests = () => new Promise(resolve => {
let unitN = {"narrow": [1]}, unitL = {"long": [1]}, unitB = {"long": [1], "narrow": [1]}
let tzDays = [new Date("August 1, 2019 0:00:00 UTC")],
tzLG = {"longGeneric": tzDays},
tzSG = {"shortGeneric": tzDays}
let curN = {"name": [-1]},
curS = {"symbol": [1000]},
curB = {"name": [-1], "symbol": [1000]},
curA = {"accounting": [-1000], "name": [-1], "symbol": [1000]}
// all dates must be timezone resistent: we do not want the noise: we are checking locales
// and want to maintain max entropy checks: timezone entropy == timezonename
let dates = {
FSD: new Date("2023-06-11T01:12:34.5678"), // no Z
Era: new Date(-1, -11, -30),
Jan: new Date("2023-01-15"),
Sep: new Date("2023-09-15"),
Nov: new Date("2023-11-15"),
Wed: new Date("January 18, 2023 1:00:00"), // doubles as hour 1
Fri: new Date("January 20, 2023 13:00:00"), // doubles as hour 13
}
oIntlTests = {
"collation": [
'A','a','aa','ch','ez','kz','ng','ph','ts','tt','y','\u00E2','\u00E4','\u00E7\a','\u00EB','\u00ED','\u00EE','\u00F0',
'\u00F1','\u00F6','\u0107','\u0109','\u0137\a','\u0144','\u0149','\u01FB','\u025B','\u03B1','\u040E','\u0439','\u0453',
'\u0457','\u04F0','\u0503','\u0561','\u05EA','\u0627','\u0649','\u06C6','\u06C7','\u06CC','\u06FD','\u0934','\u0935',
'\u09A4','\u09CE','\u0A85','\u0B05','\u0B85','\u0C05','\u0C85','\u0D85','\u0E24','\u0E9A','\u10350','\u10D0','\u1208',
'\u1780','\u1820','\u1D95','\u1DD9','\u1ED9','\u1EE3','\u311A','\u3147','\u4E2D','\uA647','\uFB4A',
],
"compact": {
"long": [0/0, 1000, 2e6, 6.6e12, 7e15],
"short": [-1100000000],
},
"currency": {"KES": curB, "MOP": curS, "USD": curA, "XXX": curN,},
"dayperiod": {"long": [8,22], "narrow": [8,15], "short": [12,15,18]},
"listformat": {
"narrow": ["conjunction","disjunction","unit"],
"short": ["conjunction","unit"]
},
"datetimeformat": {
"era": {
// we need to control the date part so toLocaleString matches
"long": [{era: "long", year: "numeric", month: "numeric", day: "numeric"}, [dates.Era]]
},
"fractionalSecondDigits": {
"1": [{minute: "numeric", second: 'numeric', fractionalSecondDigits: 1}, [dates.FSD]]
},
"hour": {
"numeric": [{hour: "numeric"}, [dates.Wed]],
},
"hourCycle": {
"h11-2-digit": [{hour: "2-digit", hourCycle: "h11"}, [dates.Wed]]
},
"month": {
"narrow": [{month: "narrow"}, [dates.Nov] ],
"short": [{month: "short"}, [dates.Jan, dates.Sep]],
},
"weekday": {
"long": [{weekday: "long"}, [dates.Wed, dates.Fri]],
"narrow": [{weekday: "narrow"}, [dates.Wed, dates.Fri]],
"short": [{weekday: "short"}, [dates.Fri]],
},
},
"notation": {
"scientific": {"decimal": []},
"standard": {"decimal": [0/0, -1000, 987654], "percent": [1000]},
},
"numberformat_ftp": {
"decimal": [1.2],"group": [1000, 99999],"infinity": [Infinity],"minusSign": [-5],"nan": ["a"]
},
"pluralrules": {
"cardinal": [0, 1, 2, 3, 7, 21, 100], // 1859752 ICU 74: add ordinal 81 to keep lij unique from it,sc
"ordinal": [1, 2, 3, 5, 8, 10, 81]
},
"relativetimeformat": { // 8 of 12
"always": {"narrow": [[1, "day"], [0, "year"]]},
"auto": {"long": [[1, "second"]],"narrow": [[3,"day"],[0,"quarter"],[0,"second"],[1,"second"],[3,"second"]]},
},
"relativetimeformat_ftp": { // 4 of 12
"auto": {"narrow": [[0,"day"],[1,"day"],[1,"week"],[1,"year"]]}
},
"sign": {"always": [-1, 0/0]},
"timezonename": {
"Africa/Douala": tzLG,
"Asia/Hong_Kong": tzSG,
"Asia/Muscat": tzSG,
"Asia/Seoul": tzLG,
"Europe/London": tzSG,
},
"unit": {
"byte": unitN, // ICU 74
"fahrenheit": unitB,
"foot": unitL,
"hectare": {"long": [1], "short": [987654]},
"kilometer-per-hour": unitN,
"millimeter": unitN,
"month": unitB,
"nanosecond": unitN,
"percent": unitB,
"second": {"long": [1], "narrow": [1], "short": [987654]},
"terabyte": unitL,
}
}
try {oIntlTests["compact"]["long"].push(BigInt("987354000000000000"))} catch(e) {}
let nBig = 987654
try {nBig = BigInt("987354000000000000")} catch(e) {}
oIntlTests["notation"]["scientific"]["decimal"].push(nBig)
})
const get_geo = () => new Promise(resolve => {
const METRIC = "geolocation"
// nav
let value, display
try {
let keys = Object.keys(Object.getOwnPropertyDescriptors(Navigator.prototype))
/* test
keys = keys.filter(x => !["geolocation"].includes(x))
keys.push("geolocation")
//*/
value = keys.includes(METRIC) ? zE : zD
display = value
if (isSmart) {
// this only detects enabled as untrustworthy
if (keys.indexOf(METRIC) > keys.indexOf("constructor")) {
value = zLIE
display = colorFn(zE)
log_known(SECT4, METRIC +"_navigator")
}
}
} catch(e) {
value = zErr, display = value
log_error(SECT4, METRIC +"_navigator", e)
}
// window
let geoWin
try {
geoWin = "Geolocation" in window ? true : false
} catch(e) {
geoWin = zErr
log_error(SECT4, METRIC +"_window", e)
}
value += " | "+ geoWin
display += " | "+ geoWin
function exit() {
let notation = ""
if (isSmart) {
let rhash = mini(display) // use display
if (isTB) {
// TB ESR78+: disabled, true, prompt
notation = rhash == "94bf0c73" ? default_green : default_red
} else {
// FF72+: enabled, true, prompt
notation = rhash == "6046a6a8" ? default_green : default_red
}
}
log_display(4, METRIC, display + notation)
addData(4, METRIC, value)
return resolve()
}
function geoState(state) {
value += " | "+ state
display += " | "+ state
exit()
}
try {
if (runSE) {foo++}
navigator.permissions.query({name:"geolocation"}).then(e => geoState(e.state))
} catch(e) {
log_error(SECT4, METRIC +"_permission", e)
value += " | "+ zErr
display += " | "+ zErr
exit()
}
})
const get_language_locale = () => new Promise(resolve => {
// reset
isLocaleValid = false
isLocaleValue = undefined
// LANGUAGES
function get_langmetric(m) {
try {
let test = navigator[m]
if (m == "language") {
if ("string" !== typeof test) {log_error(SECT4, m, zErrType + typeof test); return zErr} else {return test}
} else if ("object" !== typeof test) {
log_error(SECT4, m, zErrType + typeof check)
return zErr +" "+ check + " | "+ typeof check
} else {
return test.join(", ") // catch non-arrays
}
} catch(e) {
log_error(SECT4, m, e)
return zErr
}
}
let oData = {}, metrics = ['language','languages'], notation = ""
metrics.forEach(function(m) {oData[m] = cleanFn(get_langmetric(m))})
Object.keys(oData).forEach(function(METRIC){
notation = ""
if (isLanguageSmart) {
notation = tb_red
if (languagesSupported[oData.language] !== undefined) {
if (METRIC == "language") {notation = tb_green
} else {if (oData[METRIC] == oData.language +", "+ languagesSupported[oData.language][0]) {notation = tb_green}
}
}
}
log_display(4, METRIC, oData[METRIC] + notation)
addData(4, METRIC, oData[METRIC])
})
// LOCALES
function get_locmetric(m) {
let METRIC = "locales_"+ m
let r
try {
if (m == "collator") {if (runSL) {r = "en-FAKE"} else {r = Intl.Collator().resolvedOptions().locale}
} else if (m == "datetimeformat") {r = Intl.DateTimeFormat().resolvedOptions().locale
} else if (m == "displaynames") {r = new Intl.DisplayNames(undefined, {type: "region"}).resolvedOptions().locale
} else if (m == "listformat") {r = new Intl.ListFormat().resolvedOptions().locale
} else if (m == "numberformat") {r = new Intl.NumberFormat().resolvedOptions().locale
} else if (m == "pluralrules") {r = new Intl.PluralRules().resolvedOptions().locale
} else if (m == "relativetimeformat") {r = new Intl.RelativeTimeFormat().resolvedOptions().locale
} else if (m == "segmenter") {r = new Intl.Segmenter().resolvedOptions().locale
}
//r="fa" // test returning all the same but a different locale to actual
if ("string" !== typeof r) {
log_error(SECT4, METRIC, zErrType + typeof r); r = zErr
} else if (!Intl.DateTimeFormat.supportedLocalesOf([r]).length) {
log_error(SECT4, METRIC, zErrInvalid + "locale not supported"); r = zErr
}
return r
} catch(e) {
log_error(SECT4, METRIC, e)
return zErr
}
}
let res = []
metrics = [
"collator","datetimeformat","displaynames","listformat",
"numberformat","pluralrules","relativetimeformat","segmenter",
]
metrics.forEach(function(m) {
res.push(cleanFn(get_locmetric(m)))
})
// LOCALES
let METRIC = "locale", fpvalue
let value = res.join(" | ")
log_display(4, METRIC +"data", value)
// LOCALE
notation = ""
// remove errors and dupes
res = res.filter(x => ![zErr].includes(x))
res = res.filter(function(item, position) {return res.indexOf(item) === position})
if (res.length == 1) {
value = res[0]
fpvalue = value
isLocaleValue = value
isLocaleValid = true
} else if (res.length == 0) {
value = zErr; fpvalue = zErr
} else {
value = "mixed"
fpvalue = "mixed"
if (isSmart) {
fpvalue = zLIE
value = colorFn(value)
log_known(SECT4, METRIC)
}
}
addData(4, METRIC, fpvalue)
if (isLanguageSmart) {
notation = tb_red
let key = oData.language
// only green if TB supported
if (languagesSupported[key] !== undefined) {
let expected = languagesSupported[key][1] == undefined ? key : languagesSupported[key][1]
if (value === expected) {notation = tb_green}
}
}
log_display(4, METRIC, value + notation)
return resolve()
})
const get_locale_intl = () => new Promise(resolve => {
function get_metric(m, code, isIntl) {
try {
let obj = {}, tests = oIntlTests[m], value
if (m == "collation") {
let data = tests.sort() // always re-sort
return data.sort(Intl.Collator(code).compare).join(", ")
} else if (m == "compact") {
Object.keys(tests).forEach(function(key) {
let option = {notation: m, compactDisplay: key, useGrouping: true}, data = [], formatter
if (isIntl) {formatter = new Intl.NumberFormat(code, option)}
tests[key].forEach(function(n) {
value = (isIntl ? formatter.format(n) : (n).toLocaleString(code, option)); data.push(value)
})
obj[key] = data
})
} else if (m == "currency") {
Object.keys(tests).forEach(function(key) {
obj[key] = {}
Object.keys(tests[key]).forEach(function(s) {
let option = s == "accounting" ? {style: m, currency: key, currencySign: s} : {style: m, currency: key, currencyDisplay: s}, data = []
tests[key][s].forEach(function(n) {
value = (isIntl ? Intl.NumberFormat(code, option).format(n) : (n).toLocaleString(code, option)); data.push(value)
})
obj[key][s] = data
})
})
} else if (m == "datetimeformat") {
Object.keys(tests).forEach(function(key) {
obj[key] = {}
Object.keys(tests[key]).forEach(function(s) {
let option = tests[key][s][0]
let formatter = new Intl.DateTimeFormat(code, option), data = []
tests[key][s][1].forEach(function(n){
value = (isIntl ? formatter.format(n) : (n).toLocaleString(code, option)); data.push(value)
})
obj[key][s] = data
})
})
} else if (m == "dayperiod") {
Object.keys(tests).forEach(function(key) {
let formatter = new Intl.DateTimeFormat(code, {hourCycle: "h12", dayPeriod: key}), data = []
tests[key].forEach(function(item) {data.push(formatter.format(dayperiods[item]))})
obj[key] = data
})
} else if (m == "listformat") {
Object.keys(tests).forEach(function(key) {
let data = []
tests[key].forEach(function(item) {data.push(new Intl.ListFormat(code, {style: key, type: item}).format(["a","b","c"]))})
obj[key] = data
})
} else if (m == "notation") {
Object.keys(tests).forEach(function(key) {
obj[key] = {}
Object.keys(tests[key]).forEach(function(s) {
let formatter = (isIntl ? Intl.NumberFormat(code, {notation: key, style: s}) : undefined), data = []
tests[key][s].forEach(function(n){
value = (isIntl ? formatter.format(n) : (n).toLocaleString(code, {notation: key, style: s})); data.push(value)
})
obj[key][s] = data
})
})
} else if (m == "numberformat_ftp") {
function get_value(type, aParts) {
for (let i = 0 ; i < aParts.length; i++) {
if (aParts[i].type === type) {str = aParts[i].value; return (str.length == 1 ? str.charCodeAt(0) : str)}
}
return "none"
}
let formatter = Intl.NumberFormat(code), str
Object.keys(tests).forEach(function(key){
let data = []
tests[key].forEach(function(num){data.push(get_value(key, formatter.formatToParts(num)))})
obj[key] = data
})
} else if (m == "pluralrules") {
for (const key of Object.keys(tests)) {
let formatter = new Intl.PluralRules(code, {type: key}), nos = tests[key], prev="", current="", data = []
nos.forEach(function(n) {
current = formatter.select(n)
if (prev !== current) {data.push(n +": "+ current)}
prev = current
})
obj[key] = data
}
} else if (m == "relativetimeformat") {
Object.keys(tests).forEach(function(key) {
obj[key] = {}
Object.keys(tests[key]).forEach(function(item) {
let formatter = new Intl.RelativeTimeFormat(code, {style: item, numeric: key}), data = []
tests[key][item].forEach(function(pair){data.push(formatter.format(pair[0], pair[1]))})
obj[key][item] = data
})
})
} else if (m == "relativetimeformat_ftp") {
function parts(length, value) {
let output = "", tmp = formatter.formatToParts(length, value)
for (let x=0; x < tmp.length; x++) {output += tmp[x].value}
return output
}
let formatter, data = []
Object.keys(tests).forEach(function(key) {
obj[key] = {}
Object.keys(tests[key]).forEach(function(s) {
formatter = new Intl.RelativeTimeFormat(code, {style: s, numeric: key}), data = []
tests[key][s].forEach(function(pair){data.push(parts(pair[0], pair[1]))})
obj[key][s] = data
})
})
} else if (m == "sign") {
Object.keys(tests).forEach(function(key) {
let formatter = (isIntl ? new Intl.NumberFormat(code, {signDisplay: key}) : undefined), data = []
tests[key].forEach(function(n){
value = (isIntl ? formatter.format(n) : (n).toLocaleString(code, {signDisplay: key})); data.push(value)
})
obj[key] = data
})
} else if (m == "timezonename") {
Object.keys(tests).forEach(function(tz){
let data = []
Object.keys(tests[tz]).forEach(function(tzn){
try {
// use y+m+d numeric so toLocaleString matches
// use hour12 in case - https://bugzilla.mozilla.org/show_bug.cgi?id=1645115#c9
let option = {year: "numeric", month: "numeric", day: "numeric", hour12: true, timeZone: tz, timeZoneName: tzn}
let formatter = (isIntl ? Intl.DateTimeFormat(code, option) : undefined)
tests[tz][tzn].forEach(function(dte){
value = (isIntl ? formatter.format(dte) : (dte).toLocaleString(code, option)); data.push(value)
})
} catch (e) {} // ignore invalid
if (data.length) {obj[tz] = data}
})
})
if (!Object.keys(obj).length) {let trap = Intl.DateTimeFormat(code, {timeZoneName: "longGeneric"})} // trap error
} else if (m == "unit") {
Object.keys(tests).sort().forEach(function(u){
let data = []
Object.keys(tests[u]).forEach(function(ud){
try {
let formatter = (isIntl ? Intl.NumberFormat(code, {style: "unit", unit: u, unitDisplay: ud}) : undefined)
tests[u][ud].forEach(function(n){
value = (isIntl ? formatter.format(n) : (n).toLocaleString(code, {style: "unit", unit: u, unitDisplay: ud})); data.push(value)
})
} catch (e) {} // ignore invalid
})
if (data.length) {obj[u] = data}
})
if (!Object.keys(obj).length) {let trap = Intl.NumberFormat(code, {style: "unit", unit: "day"})} // trap error
}
return {"hash": mini(obj), "metrics": obj}
} catch(e) {
log_error(SECT4, METRIC +"_"+ m, e)
return zErr
}
}
const dayperiods = { // set per run
8: new Date("2019-01-30T08:00:00"),
12: new Date("2019-01-30T12:00:00"),
15: new Date("2019-01-30T15:00:00"),
18: new Date("2019-01-30T18:00:00"),
22: new Date("2019-01-30T22:00:00"),
}
const oMetrics = {
"intl" : [
"collation","compact", "currency", "datetimeformat","dayperiod", "listformat","notation","numberformat_ftp",
"pluralrules","relativetimeformat","relativetimeformat_ftp","sign","timezonename","unit"
],
"tolocalestring": ["compact","currency","datetimeformat","notation","sign","timezonename","unit"],
}
let t0 = nowFn(), notation = ""
let METRIC, oString = {}
Object.keys(oMetrics).forEach(function(list){
METRIC = "locale_"+ list
let t0 = nowFn(), isIntl = list == "intl", notation = ""
let oData = {}, oCheck = {}
oMetrics[list].forEach(function(m) {
let value = get_metric(m, undefined, isIntl)
oData[m] = value
if (isIntl && oMetrics["tolocalestring"].includes(m)) {oString[m] = value} // create an intl version of tolocalestring
})
if (isSmart && isLocaleValid) {oMetrics[list].forEach(function(m) {oCheck[m] = get_metric(m, isLocaleValue, isIntl)})}
let hash = mini(oData)
let isLies = false, btnDiff = ""
if (isSmart) {
if (!isIntl) {log_display(4, METRIC +"_matches_intl", (hash == mini(oString) ? intl_green : intl_red))}
notation = locale_red
if (isLocaleValid) {
if (hash == mini(oCheck)) {
notation = locale_green
} else {
addDetail(METRIC +"_check", oCheck)
btnDiff = addButton(4, METRIC +"_check", isLocaleValue +" check")
}
}
}
addData(4, METRIC, oData, hash)
log_display(4, METRIC, hash + addButton(4, METRIC) + btnDiff + notation)
log_perf(SECT4, METRIC, t0)
if (!isIntl) {return resolve()}
})
})
const get_locale_resolvedoptions = () => new Promise(resolve => {
const METRIC = "locale_resolvedoptions"
function get_metric(m, code) {
let r
let type = "string"
try {
// collator
if (m == "caseFirst") {r = Intl.Collator(code).resolvedOptions().caseFirst
} else if (m == "ignorePunctuation") {type = "boolean"; r = Intl.Collator(code).resolvedOptions().ignorePunctuation
// DTF
} else if (m == "calendar") {r = Intl.DateTimeFormat(code).resolvedOptions().calendar
} else if (m == "day") {r = Intl.DateTimeFormat(code).resolvedOptions().day
} else if (m == "hourCycle") {r = Intl.DateTimeFormat(code, {hour: "numeric"}).resolvedOptions().hourCycle
} else if (m == "month") {r = Intl.DateTimeFormat(code).resolvedOptions().month
} else if (m == "numberingSystem_dtf") {r = Intl.DateTimeFormat(code).resolvedOptions().numberingSystem
// NF
} else if (m == "numberingSystem_nf") {r = new Intl.NumberFormat(code).resolvedOptions().numberingSystem
// PR
} else if (m == "pluralCategories") {r = new Intl.PluralRules(code).resolvedOptions().pluralCategories.join(", ")
// RTF
} else if (m == "numberingSystem_rtf") {r = new Intl.RelativeTimeFormat(code).resolvedOptions().numberingSystem
}
if (type !== typeof r) {
log_error(SECT4, METRIC +"_"+ m, zErrType + typeof r); r = zErr
} else if (r === "") {
log_error(SECT4, METRIC +"_"+ m, zErrInvalid + "empty string"); r = zErr
}
return r
} catch(e) {
log_error(SECT4, METRIC +"_"+ m, e)
return zErr
}
}
let oData = {}, oCheck = {}, notation = ""
let metrics = [
"calendar","caseFirst","day","hourCycle","ignorePunctuation","month",
"numberingSystem_dtf","numberingSystem_nf","numberingSystem_rtf","pluralCategories",
]
metrics.forEach(function(m) {
oData[m] = get_metric(m, undefined)
})
if (isSmart && isLocaleValid) {
metrics.forEach(function(m) {
oCheck[m] = get_metric(m, isLocaleValue)
})
}
let hash = mini(oData)
let isLies = false, btnDiff = ""
if (isSmart) {
notation = locale_red
if (isLocaleValid) {
if (hash == mini(oCheck)) {
notation = locale_green
} else {
addDetail(METRIC +"_check", oCheck)
btnDiff = addButton(4, METRIC +"_check", isLocaleValue +" check")
}
}
}
addData(4, METRIC, oData, hash)
log_display(4, METRIC, hash + addButton(4, METRIC) + btnDiff + notation)
return resolve()
})
const get_timezone = () => new Promise(resolve => {
let t0 = nowFn(), notation = ""
const METRICtz = "timezone"
const METRIC = "timezone_offsets"
// reset
isTimeZoneValid = false
isTimeZoneValue = undefined
let days = ["January 1","July 1"], years = [1879, 1921, 1952, 1976, 2025]
let aMethods = [
'date','date.parse','date.valueOf','getTime','getTimezoneOffset','Symbol.toPrimitive',
]
let isTimeZoneErr = true
function get_tz() {
try {
let tz = Intl.DateTimeFormat().resolvedOptions().timeZone
if (runSE) {foo++} else if (runST) {tz = "groot"}
let tzType = typeFn(tz)
if ("string" !== tzType) {
return ([log_error(SECT4, METRICtz, zErrType + tzType)])
} else {
try {
let tztest = (new Date("January 1, 2018 13:00:00 UTC")).toLocaleString('en', {timeZone: tz})
isTimeZoneErr = false
return (tz)
} catch(e) {
return ([log_error(SECT4, METRICtz, e)]) // not supported
}
}
} catch(e) {
return ([log_error(SECT4, METRICtz, e)])
}
}
function get_offsets() {
let oData = {}, oErrors = {}
aMethods.forEach(function(method) {
oData[method] = {}
years.forEach(function(year) {oData[method][year] = []})
})
years.forEach(function(year) {
days.forEach(function(day) {
try {
if (runSE) {foo++}
let datetime = day +", "+ year +" 13:00:00"
let control = new Date(datetime +" UTC")
let test = new Date(datetime)
aMethods.forEach(function(method) {
let offset, k = 60000
try {
if (method == "getTimezoneOffset") {
offset = test.getTimezoneOffset()
k = 1
} else {
if (method == "date.parse") {
offset = Date.parse(test) - Date.parse(control)
} else if (method == "date.valueOf") {
offset = test.valueOf() - control.valueOf()
} else if (method == "Symbol.toPrimitive") {
offset = test[Symbol.toPrimitive]('number') - control[Symbol.toPrimitive]('number')
} else if (method == "getTime") {
offset = test.getTime() - control.getTime()
} else if (method == "date") {
offset = test - control
}
}
if ("number" === typeof offset && !Number.isNaN(offset)) {
oData[method][year].push(offset/k)
} else {
let offsetType = Number.isNaN(offset) ? "NaN": typeof offset
oErrors[method] = log_error(SECT4, METRIC +"_"+ method, zErrType + offsetType)
}
} catch(e) {
oErrors[method] = log_error(SECT4, METRIC +"_"+ method, e)
}
})
} catch(e) {
oData = log_error(SECT4, METRIC, e)
}
})
})
if (oData !== zErr) {
for (const k of Object.keys(oErrors)) {oData[k] = oErrors[k]}
}
return oData
}
Promise.all([
get_tz(),
get_offsets(),
]).then(function(res){
// TZ: we returned a string (valid but could be a lie) or an array [error]
let tz = res[0], tzdisplay = tz
if ("string" !== typeof tz) {tz = zErr} else {isTimeZoneValue = tz}
// OFFSETS
let oOffsets = res[1], offset = "", display = "", notation = ""
let go = true, aHash = {}, countErr = 0, allHash
// stop: overall error
if ("string" == typeof oOffsets) {
if (isSmart) {notation = tz_red}
log_display(4, METRIC, oOffsets + notation)
addData(4, METRIC, zErr)
go = false
}
// display errors + collect hashes
if (go) {
for (const k of Object.keys(oOffsets)) {
if ("string" == typeof oOffsets[k]) {
log_display(4, k, oOffsets[k])
countErr++
} else {
let hash = mini(oOffsets[k])
if (aHash[hash] == undefined) {aHash[hash] = []}
aHash[hash].push(k)
}
}
// stop: all errors
if (countErr == aMethods.length) {
if (isSmart) {notation = tz_red}
log_display(4, METRIC, zErr + notation)
addData(4, METRIC, zErr)
go = false
}
}
// display hashes + btns
if (go) {
let isHashMixed = (Object.keys(aHash).length > 1 || countErr > 0) ? true : false // includes errors
for (const k of Object.keys(aHash)) {
allHash = k
let items = aHash[k]
for (let i=0; i < items.length; i++) {
let metric = items[i], btn = ""
if (isHashMixed && i == 0) {
// btns for 1st of each hash
sDetail[isScope][METRIC +"_"+ metric] = oOffsets[metric]
btn = addButton(4, METRIC +"_"+ metric)
}
log_display(4, metric, k + btn)
}
}
// stop: not all same + valid
if (isHashMixed) {
display = "mixed"
offset = display
if (isSmart) {
notation = tz_red
display = colorFn(display)
offset = zLIE
log_known(SECT4, METRIC)
}
log_display(4, METRIC, display + notation)
addData(4, METRIC, offset)
go = false
}
}
// all valid + same
if (go) {
let isLies = false
if (isSmart) {
notation = tz_red
if (isTimeZoneValue !== undefined) {
try {
let oTest = {}
// just use date.parse
years.forEach(function(year) {
oTest[year] = []
days.forEach(function(day) {
let datetime = day +", "+ year +" 13:00:00"
let control = new Date(datetime)
let test = control.toLocaleString("en", {timeZone: "UTC"})