@formatjs/ecma402-abstract 1.11.6 → 1.11.9
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.
- package/262.ts +372 -0
- package/BUILD +97 -0
- package/CHANGELOG.md +709 -0
- package/CanonicalizeLocaleList.ts +8 -0
- package/CanonicalizeTimeZoneName.ts +29 -0
- package/CoerceOptionsToObject.ts +13 -0
- package/DefaultNumberOption.ts +28 -0
- package/GetNumberOption.ts +29 -0
- package/GetOption.ts +38 -0
- package/GetOptionsObject.ts +14 -0
- package/IsSanctionedSimpleUnitIdentifier.ts +66 -0
- package/IsValidTimeZoneName.ts +27 -0
- package/IsWellFormedCurrencyCode.ts +23 -0
- package/IsWellFormedUnitIdentifier.ts +32 -0
- package/LICENSE.md +0 -0
- package/NumberFormat/ComputeExponent.ts +49 -0
- package/NumberFormat/ComputeExponentForMagnitude.ts +71 -0
- package/NumberFormat/CurrencyDigits.ts +13 -0
- package/NumberFormat/FormatNumericToParts.ts +22 -0
- package/NumberFormat/FormatNumericToString.ts +71 -0
- package/NumberFormat/InitializeNumberFormat.ts +147 -0
- package/NumberFormat/PartitionNumberPattern.ts +86 -0
- package/NumberFormat/SetNumberFormatDigitOptions.ts +45 -0
- package/NumberFormat/SetNumberFormatUnitOptions.ts +77 -0
- package/NumberFormat/ToRawFixed.ts +56 -0
- package/NumberFormat/ToRawPrecision.ts +86 -0
- package/NumberFormat/digit-mapping.generated.ts +1 -0
- package/NumberFormat/format_to_parts.ts +571 -0
- package/PartitionPattern.ts +38 -0
- package/README.md +0 -0
- package/SupportedLocales.ts +31 -0
- package/data.ts +9 -0
- package/index.ts +45 -0
- package/package.json +3 -3
- package/regex.generated.ts +2 -0
- package/scripts/digit-mapping.ts +109 -0
- package/scripts/regex-gen.ts +19 -0
- package/tests/PartitionPattern.test.ts +38 -0
- package/tests/ToRawFixed.test.tsx +41 -0
- package/tests/ToRawPrecision.test.tsx +65 -0
- package/tsconfig.json +5 -0
- package/types/core.ts +11 -0
- package/types/date-time.ts +199 -0
- package/types/displaynames.ts +48 -0
- package/types/list.ts +22 -0
- package/types/number.ts +240 -0
- package/types/plural-rules.ts +18 -0
- package/types/relative-time.ts +74 -0
- package/utils.ts +128 -0
- package/262.d.ts +0 -92
- package/262.d.ts.map +0 -1
- package/262.js +0 -362
- package/CanonicalizeLocaleList.d.ts +0 -6
- package/CanonicalizeLocaleList.d.ts.map +0 -1
- package/CanonicalizeLocaleList.js +0 -12
- package/CanonicalizeTimeZoneName.d.ts +0 -9
- package/CanonicalizeTimeZoneName.d.ts.map +0 -1
- package/CanonicalizeTimeZoneName.js +0 -21
- package/CoerceOptionsToObject.d.ts +0 -7
- package/CoerceOptionsToObject.d.ts.map +0 -1
- package/CoerceOptionsToObject.js +0 -16
- package/DefaultNumberOption.d.ts +0 -9
- package/DefaultNumberOption.d.ts.map +0 -1
- package/DefaultNumberOption.js +0 -14
- package/GetNumberOption.d.ts +0 -10
- package/GetNumberOption.d.ts.map +0 -1
- package/GetNumberOption.js +0 -18
- package/GetOption.d.ts +0 -10
- package/GetOption.d.ts.map +0 -1
- package/GetOption.js +0 -35
- package/GetOptionsObject.d.ts +0 -7
- package/GetOptionsObject.d.ts.map +0 -1
- package/GetOptionsObject.js +0 -18
- package/IsSanctionedSimpleUnitIdentifier.d.ts +0 -14
- package/IsSanctionedSimpleUnitIdentifier.d.ts.map +0 -1
- package/IsSanctionedSimpleUnitIdentifier.js +0 -68
- package/IsValidTimeZoneName.d.ts +0 -10
- package/IsValidTimeZoneName.d.ts.map +0 -1
- package/IsValidTimeZoneName.js +0 -23
- package/IsWellFormedCurrencyCode.d.ts +0 -5
- package/IsWellFormedCurrencyCode.d.ts.map +0 -1
- package/IsWellFormedCurrencyCode.js +0 -25
- package/IsWellFormedUnitIdentifier.d.ts +0 -6
- package/IsWellFormedUnitIdentifier.d.ts.map +0 -1
- package/IsWellFormedUnitIdentifier.js +0 -32
- package/NumberFormat/ComputeExponent.d.ts +0 -12
- package/NumberFormat/ComputeExponent.d.ts.map +0 -1
- package/NumberFormat/ComputeExponent.js +0 -43
- package/NumberFormat/ComputeExponentForMagnitude.d.ts +0 -10
- package/NumberFormat/ComputeExponentForMagnitude.d.ts.map +0 -1
- package/NumberFormat/ComputeExponentForMagnitude.js +0 -64
- package/NumberFormat/CurrencyDigits.d.ts +0 -7
- package/NumberFormat/CurrencyDigits.d.ts.map +0 -1
- package/NumberFormat/CurrencyDigits.js +0 -14
- package/NumberFormat/FormatNumericToParts.d.ts +0 -5
- package/NumberFormat/FormatNumericToParts.d.ts.map +0 -1
- package/NumberFormat/FormatNumericToParts.js +0 -18
- package/NumberFormat/FormatNumericToString.d.ts +0 -9
- package/NumberFormat/FormatNumericToString.d.ts.map +0 -1
- package/NumberFormat/FormatNumericToString.js +0 -45
- package/NumberFormat/InitializeNumberFormat.d.ts +0 -13
- package/NumberFormat/InitializeNumberFormat.d.ts.map +0 -1
- package/NumberFormat/InitializeNumberFormat.js +0 -68
- package/NumberFormat/PartitionNumberPattern.d.ts +0 -8
- package/NumberFormat/PartitionNumberPattern.d.ts.map +0 -1
- package/NumberFormat/PartitionNumberPattern.js +0 -80
- package/NumberFormat/SetNumberFormatDigitOptions.d.ts +0 -6
- package/NumberFormat/SetNumberFormatDigitOptions.d.ts.map +0 -1
- package/NumberFormat/SetNumberFormatDigitOptions.js +0 -40
- package/NumberFormat/SetNumberFormatUnitOptions.d.ts +0 -8
- package/NumberFormat/SetNumberFormatUnitOptions.d.ts.map +0 -1
- package/NumberFormat/SetNumberFormatUnitOptions.js +0 -43
- package/NumberFormat/ToRawFixed.d.ts +0 -10
- package/NumberFormat/ToRawFixed.d.ts.map +0 -1
- package/NumberFormat/ToRawFixed.js +0 -55
- package/NumberFormat/ToRawPrecision.d.ts +0 -3
- package/NumberFormat/ToRawPrecision.d.ts.map +0 -1
- package/NumberFormat/ToRawPrecision.js +0 -78
- package/NumberFormat/digit-mapping.generated.d.ts +0 -2
- package/NumberFormat/digit-mapping.generated.d.ts.map +0 -1
- package/NumberFormat/digit-mapping.generated.js +0 -4
- package/NumberFormat/format_to_parts.d.ts +0 -22
- package/NumberFormat/format_to_parts.d.ts.map +0 -1
- package/NumberFormat/format_to_parts.js +0 -424
- package/PartitionPattern.d.ts +0 -9
- package/PartitionPattern.d.ts.map +0 -1
- package/PartitionPattern.js +0 -39
- package/SupportedLocales.d.ts +0 -10
- package/SupportedLocales.d.ts.map +0 -1
- package/SupportedLocales.js +0 -24
- package/data.d.ts +0 -6
- package/data.d.ts.map +0 -1
- package/data.js +0 -17
- package/index.d.ts +0 -37
- package/index.d.ts.map +0 -1
- package/index.js +0 -48
- package/lib/262.d.ts +0 -92
- package/lib/262.d.ts.map +0 -1
- package/lib/262.js +0 -336
- package/lib/CanonicalizeLocaleList.d.ts +0 -6
- package/lib/CanonicalizeLocaleList.d.ts.map +0 -1
- package/lib/CanonicalizeLocaleList.js +0 -8
- package/lib/CanonicalizeTimeZoneName.d.ts +0 -9
- package/lib/CanonicalizeTimeZoneName.d.ts.map +0 -1
- package/lib/CanonicalizeTimeZoneName.js +0 -17
- package/lib/CoerceOptionsToObject.d.ts +0 -7
- package/lib/CoerceOptionsToObject.d.ts.map +0 -1
- package/lib/CoerceOptionsToObject.js +0 -12
- package/lib/DefaultNumberOption.d.ts +0 -9
- package/lib/DefaultNumberOption.d.ts.map +0 -1
- package/lib/DefaultNumberOption.js +0 -10
- package/lib/GetNumberOption.d.ts +0 -10
- package/lib/GetNumberOption.d.ts.map +0 -1
- package/lib/GetNumberOption.js +0 -14
- package/lib/GetOption.d.ts +0 -10
- package/lib/GetOption.d.ts.map +0 -1
- package/lib/GetOption.js +0 -31
- package/lib/GetOptionsObject.d.ts +0 -7
- package/lib/GetOptionsObject.d.ts.map +0 -1
- package/lib/GetOptionsObject.js +0 -14
- package/lib/IsSanctionedSimpleUnitIdentifier.d.ts +0 -14
- package/lib/IsSanctionedSimpleUnitIdentifier.d.ts.map +0 -1
- package/lib/IsSanctionedSimpleUnitIdentifier.js +0 -63
- package/lib/IsValidTimeZoneName.d.ts +0 -10
- package/lib/IsValidTimeZoneName.d.ts.map +0 -1
- package/lib/IsValidTimeZoneName.js +0 -19
- package/lib/IsWellFormedCurrencyCode.d.ts +0 -5
- package/lib/IsWellFormedCurrencyCode.d.ts.map +0 -1
- package/lib/IsWellFormedCurrencyCode.js +0 -21
- package/lib/IsWellFormedUnitIdentifier.d.ts +0 -6
- package/lib/IsWellFormedUnitIdentifier.d.ts.map +0 -1
- package/lib/IsWellFormedUnitIdentifier.js +0 -28
- package/lib/NumberFormat/ComputeExponent.d.ts +0 -12
- package/lib/NumberFormat/ComputeExponent.d.ts.map +0 -1
- package/lib/NumberFormat/ComputeExponent.js +0 -39
- package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts +0 -10
- package/lib/NumberFormat/ComputeExponentForMagnitude.d.ts.map +0 -1
- package/lib/NumberFormat/ComputeExponentForMagnitude.js +0 -60
- package/lib/NumberFormat/CurrencyDigits.d.ts +0 -7
- package/lib/NumberFormat/CurrencyDigits.d.ts.map +0 -1
- package/lib/NumberFormat/CurrencyDigits.js +0 -10
- package/lib/NumberFormat/FormatNumericToParts.d.ts +0 -5
- package/lib/NumberFormat/FormatNumericToParts.d.ts.map +0 -1
- package/lib/NumberFormat/FormatNumericToParts.js +0 -14
- package/lib/NumberFormat/FormatNumericToString.d.ts +0 -9
- package/lib/NumberFormat/FormatNumericToString.d.ts.map +0 -1
- package/lib/NumberFormat/FormatNumericToString.js +0 -41
- package/lib/NumberFormat/InitializeNumberFormat.d.ts +0 -13
- package/lib/NumberFormat/InitializeNumberFormat.d.ts.map +0 -1
- package/lib/NumberFormat/InitializeNumberFormat.js +0 -64
- package/lib/NumberFormat/PartitionNumberPattern.d.ts +0 -8
- package/lib/NumberFormat/PartitionNumberPattern.d.ts.map +0 -1
- package/lib/NumberFormat/PartitionNumberPattern.js +0 -75
- package/lib/NumberFormat/SetNumberFormatDigitOptions.d.ts +0 -6
- package/lib/NumberFormat/SetNumberFormatDigitOptions.d.ts.map +0 -1
- package/lib/NumberFormat/SetNumberFormatDigitOptions.js +0 -36
- package/lib/NumberFormat/SetNumberFormatUnitOptions.d.ts +0 -8
- package/lib/NumberFormat/SetNumberFormatUnitOptions.d.ts.map +0 -1
- package/lib/NumberFormat/SetNumberFormatUnitOptions.js +0 -39
- package/lib/NumberFormat/ToRawFixed.d.ts +0 -10
- package/lib/NumberFormat/ToRawFixed.d.ts.map +0 -1
- package/lib/NumberFormat/ToRawFixed.js +0 -51
- package/lib/NumberFormat/ToRawPrecision.d.ts +0 -3
- package/lib/NumberFormat/ToRawPrecision.d.ts.map +0 -1
- package/lib/NumberFormat/ToRawPrecision.js +0 -74
- package/lib/NumberFormat/digit-mapping.generated.d.ts +0 -2
- package/lib/NumberFormat/digit-mapping.generated.d.ts.map +0 -1
- package/lib/NumberFormat/digit-mapping.generated.js +0 -1
- package/lib/NumberFormat/format_to_parts.d.ts +0 -22
- package/lib/NumberFormat/format_to_parts.d.ts.map +0 -1
- package/lib/NumberFormat/format_to_parts.js +0 -421
- package/lib/PartitionPattern.d.ts +0 -9
- package/lib/PartitionPattern.d.ts.map +0 -1
- package/lib/PartitionPattern.js +0 -35
- package/lib/SupportedLocales.d.ts +0 -10
- package/lib/SupportedLocales.d.ts.map +0 -1
- package/lib/SupportedLocales.js +0 -20
- package/lib/data.d.ts +0 -6
- package/lib/data.d.ts.map +0 -1
- package/lib/data.js +0 -13
- package/lib/index.d.ts +0 -37
- package/lib/index.d.ts.map +0 -1
- package/lib/index.js +0 -34
- package/lib/regex.generated.d.ts +0 -2
- package/lib/regex.generated.d.ts.map +0 -1
- package/lib/regex.generated.js +0 -2
- package/lib/types/core.d.ts +0 -11
- package/lib/types/core.d.ts.map +0 -1
- package/lib/types/core.js +0 -1
- package/lib/types/date-time.d.ts +0 -135
- package/lib/types/date-time.d.ts.map +0 -1
- package/lib/types/date-time.js +0 -6
- package/lib/types/displaynames.d.ts +0 -47
- package/lib/types/displaynames.d.ts.map +0 -1
- package/lib/types/displaynames.js +0 -1
- package/lib/types/list.d.ts +0 -19
- package/lib/types/list.d.ts.map +0 -1
- package/lib/types/list.js +0 -1
- package/lib/types/number.d.ts +0 -147
- package/lib/types/number.d.ts.map +0 -1
- package/lib/types/number.js +0 -1
- package/lib/types/plural-rules.d.ts +0 -17
- package/lib/types/plural-rules.d.ts.map +0 -1
- package/lib/types/plural-rules.js +0 -1
- package/lib/types/relative-time.d.ts +0 -41
- package/lib/types/relative-time.d.ts.map +0 -1
- package/lib/types/relative-time.js +0 -1
- package/lib/utils.d.ts +0 -24
- package/lib/utils.d.ts.map +0 -1
- package/lib/utils.js +0 -78
- package/regex.generated.d.ts +0 -2
- package/regex.generated.d.ts.map +0 -1
- package/regex.generated.js +0 -5
- package/types/core.d.ts +0 -11
- package/types/core.d.ts.map +0 -1
- package/types/core.js +0 -2
- package/types/date-time.d.ts +0 -135
- package/types/date-time.d.ts.map +0 -1
- package/types/date-time.js +0 -9
- package/types/displaynames.d.ts +0 -47
- package/types/displaynames.d.ts.map +0 -1
- package/types/displaynames.js +0 -2
- package/types/list.d.ts +0 -19
- package/types/list.d.ts.map +0 -1
- package/types/list.js +0 -2
- package/types/number.d.ts +0 -147
- package/types/number.d.ts.map +0 -1
- package/types/number.js +0 -2
- package/types/plural-rules.d.ts +0 -17
- package/types/plural-rules.d.ts.map +0 -1
- package/types/plural-rules.js +0 -2
- package/types/relative-time.d.ts +0 -41
- package/types/relative-time.d.ts.map +0 -1
- package/types/relative-time.js +0 -2
- package/utils.d.ts +0 -24
- package/utils.d.ts.map +0 -1
- package/utils.js +0 -90
package/262.ts
ADDED
|
@@ -0,0 +1,372 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* https://tc39.es/ecma262/#sec-tostring
|
|
3
|
+
*/
|
|
4
|
+
export function ToString(o: unknown): string {
|
|
5
|
+
// Only symbol is irregular...
|
|
6
|
+
if (typeof o === 'symbol') {
|
|
7
|
+
throw TypeError('Cannot convert a Symbol value to a string')
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
return String(o)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* https://tc39.es/ecma262/#sec-tonumber
|
|
15
|
+
* @param val
|
|
16
|
+
*/
|
|
17
|
+
export function ToNumber(val: any): number {
|
|
18
|
+
if (val === undefined) {
|
|
19
|
+
return NaN
|
|
20
|
+
}
|
|
21
|
+
if (val === null) {
|
|
22
|
+
return +0
|
|
23
|
+
}
|
|
24
|
+
if (typeof val === 'boolean') {
|
|
25
|
+
return val ? 1 : +0
|
|
26
|
+
}
|
|
27
|
+
if (typeof val === 'number') {
|
|
28
|
+
return val
|
|
29
|
+
}
|
|
30
|
+
if (typeof val === 'symbol' || typeof val === 'bigint') {
|
|
31
|
+
throw new TypeError('Cannot convert symbol/bigint to number')
|
|
32
|
+
}
|
|
33
|
+
return Number(val)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* https://tc39.es/ecma262/#sec-tointeger
|
|
38
|
+
* @param n
|
|
39
|
+
*/
|
|
40
|
+
function ToInteger(n: any) {
|
|
41
|
+
const number = ToNumber(n)
|
|
42
|
+
if (isNaN(number) || SameValue(number, -0)) {
|
|
43
|
+
return 0
|
|
44
|
+
}
|
|
45
|
+
if (isFinite(number)) {
|
|
46
|
+
return number
|
|
47
|
+
}
|
|
48
|
+
let integer = Math.floor(Math.abs(number))
|
|
49
|
+
if (number < 0) {
|
|
50
|
+
integer = -integer
|
|
51
|
+
}
|
|
52
|
+
if (SameValue(integer, -0)) {
|
|
53
|
+
return 0
|
|
54
|
+
}
|
|
55
|
+
return integer
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* https://tc39.es/ecma262/#sec-timeclip
|
|
60
|
+
* @param time
|
|
61
|
+
*/
|
|
62
|
+
export function TimeClip(time: number) {
|
|
63
|
+
if (!isFinite(time)) {
|
|
64
|
+
return NaN
|
|
65
|
+
}
|
|
66
|
+
if (Math.abs(time) > 8.64 * 1e15) {
|
|
67
|
+
return NaN
|
|
68
|
+
}
|
|
69
|
+
return ToInteger(time)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* https://tc39.es/ecma262/#sec-toobject
|
|
74
|
+
* @param arg
|
|
75
|
+
*/
|
|
76
|
+
export function ToObject<T>(
|
|
77
|
+
arg: T
|
|
78
|
+
): T extends null ? never : T extends undefined ? never : T {
|
|
79
|
+
if (arg == null) {
|
|
80
|
+
throw new TypeError('undefined/null cannot be converted to object')
|
|
81
|
+
}
|
|
82
|
+
return Object(arg)
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* https://www.ecma-international.org/ecma-262/11.0/index.html#sec-samevalue
|
|
87
|
+
* @param x
|
|
88
|
+
* @param y
|
|
89
|
+
*/
|
|
90
|
+
export function SameValue(x: any, y: any) {
|
|
91
|
+
if (Object.is) {
|
|
92
|
+
return Object.is(x, y)
|
|
93
|
+
}
|
|
94
|
+
// SameValue algorithm
|
|
95
|
+
if (x === y) {
|
|
96
|
+
// Steps 1-5, 7-10
|
|
97
|
+
// Steps 6.b-6.e: +0 != -0
|
|
98
|
+
return x !== 0 || 1 / x === 1 / y
|
|
99
|
+
}
|
|
100
|
+
// Step 6.a: NaN == NaN
|
|
101
|
+
return x !== x && y !== y
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* https://www.ecma-international.org/ecma-262/11.0/index.html#sec-arraycreate
|
|
106
|
+
* @param len
|
|
107
|
+
*/
|
|
108
|
+
export function ArrayCreate(len: number) {
|
|
109
|
+
return new Array(len)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* https://www.ecma-international.org/ecma-262/11.0/index.html#sec-hasownproperty
|
|
114
|
+
* @param o
|
|
115
|
+
* @param prop
|
|
116
|
+
*/
|
|
117
|
+
export function HasOwnProperty(o: object, prop: string) {
|
|
118
|
+
return Object.prototype.hasOwnProperty.call(o, prop)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* https://www.ecma-international.org/ecma-262/11.0/index.html#sec-type
|
|
123
|
+
* @param x
|
|
124
|
+
*/
|
|
125
|
+
export function Type(x: any) {
|
|
126
|
+
if (x === null) {
|
|
127
|
+
return 'Null'
|
|
128
|
+
}
|
|
129
|
+
if (typeof x === 'undefined') {
|
|
130
|
+
return 'Undefined'
|
|
131
|
+
}
|
|
132
|
+
if (typeof x === 'function' || typeof x === 'object') {
|
|
133
|
+
return 'Object'
|
|
134
|
+
}
|
|
135
|
+
if (typeof x === 'number') {
|
|
136
|
+
return 'Number'
|
|
137
|
+
}
|
|
138
|
+
if (typeof x === 'boolean') {
|
|
139
|
+
return 'Boolean'
|
|
140
|
+
}
|
|
141
|
+
if (typeof x === 'string') {
|
|
142
|
+
return 'String'
|
|
143
|
+
}
|
|
144
|
+
if (typeof x === 'symbol') {
|
|
145
|
+
return 'Symbol'
|
|
146
|
+
}
|
|
147
|
+
if (typeof x === 'bigint') {
|
|
148
|
+
return 'BigInt'
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const MS_PER_DAY = 86400000
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* https://www.ecma-international.org/ecma-262/11.0/index.html#eqn-modulo
|
|
156
|
+
* @param x
|
|
157
|
+
* @param y
|
|
158
|
+
* @return k of the same sign as y
|
|
159
|
+
*/
|
|
160
|
+
function mod(x: number, y: number): number {
|
|
161
|
+
return x - Math.floor(x / y) * y
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* https://tc39.es/ecma262/#eqn-Day
|
|
166
|
+
* @param t
|
|
167
|
+
*/
|
|
168
|
+
export function Day(t: number) {
|
|
169
|
+
return Math.floor(t / MS_PER_DAY)
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/**
|
|
173
|
+
* https://tc39.es/ecma262/#sec-week-day
|
|
174
|
+
* @param t
|
|
175
|
+
*/
|
|
176
|
+
export function WeekDay(t: number) {
|
|
177
|
+
return mod(Day(t) + 4, 7)
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* https://tc39.es/ecma262/#sec-year-number
|
|
182
|
+
* @param y
|
|
183
|
+
*/
|
|
184
|
+
export function DayFromYear(y: number) {
|
|
185
|
+
return Date.UTC(y, 0) / MS_PER_DAY
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* https://tc39.es/ecma262/#sec-year-number
|
|
190
|
+
* @param y
|
|
191
|
+
*/
|
|
192
|
+
export function TimeFromYear(y: number) {
|
|
193
|
+
return Date.UTC(y, 0)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* https://tc39.es/ecma262/#sec-year-number
|
|
198
|
+
* @param t
|
|
199
|
+
*/
|
|
200
|
+
export function YearFromTime(t: number) {
|
|
201
|
+
return new Date(t).getUTCFullYear()
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export function DaysInYear(y: number) {
|
|
205
|
+
if (y % 4 !== 0) {
|
|
206
|
+
return 365
|
|
207
|
+
}
|
|
208
|
+
if (y % 100 !== 0) {
|
|
209
|
+
return 366
|
|
210
|
+
}
|
|
211
|
+
if (y % 400 !== 0) {
|
|
212
|
+
return 365
|
|
213
|
+
}
|
|
214
|
+
return 366
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export function DayWithinYear(t: number) {
|
|
218
|
+
return Day(t) - DayFromYear(YearFromTime(t))
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
export function InLeapYear(t: number): 0 | 1 {
|
|
222
|
+
return DaysInYear(YearFromTime(t)) === 365 ? 0 : 1
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* https://tc39.es/ecma262/#sec-month-number
|
|
227
|
+
* @param t
|
|
228
|
+
*/
|
|
229
|
+
export function MonthFromTime(t: number) {
|
|
230
|
+
const dwy = DayWithinYear(t)
|
|
231
|
+
const leap = InLeapYear(t)
|
|
232
|
+
if (dwy >= 0 && dwy < 31) {
|
|
233
|
+
return 0
|
|
234
|
+
}
|
|
235
|
+
if (dwy < 59 + leap) {
|
|
236
|
+
return 1
|
|
237
|
+
}
|
|
238
|
+
if (dwy < 90 + leap) {
|
|
239
|
+
return 2
|
|
240
|
+
}
|
|
241
|
+
if (dwy < 120 + leap) {
|
|
242
|
+
return 3
|
|
243
|
+
}
|
|
244
|
+
if (dwy < 151 + leap) {
|
|
245
|
+
return 4
|
|
246
|
+
}
|
|
247
|
+
if (dwy < 181 + leap) {
|
|
248
|
+
return 5
|
|
249
|
+
}
|
|
250
|
+
if (dwy < 212 + leap) {
|
|
251
|
+
return 6
|
|
252
|
+
}
|
|
253
|
+
if (dwy < 243 + leap) {
|
|
254
|
+
return 7
|
|
255
|
+
}
|
|
256
|
+
if (dwy < 273 + leap) {
|
|
257
|
+
return 8
|
|
258
|
+
}
|
|
259
|
+
if (dwy < 304 + leap) {
|
|
260
|
+
return 9
|
|
261
|
+
}
|
|
262
|
+
if (dwy < 334 + leap) {
|
|
263
|
+
return 10
|
|
264
|
+
}
|
|
265
|
+
if (dwy < 365 + leap) {
|
|
266
|
+
return 11
|
|
267
|
+
}
|
|
268
|
+
throw new Error('Invalid time')
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
export function DateFromTime(t: number) {
|
|
272
|
+
const dwy = DayWithinYear(t)
|
|
273
|
+
const mft = MonthFromTime(t)
|
|
274
|
+
const leap = InLeapYear(t)
|
|
275
|
+
if (mft === 0) {
|
|
276
|
+
return dwy + 1
|
|
277
|
+
}
|
|
278
|
+
if (mft === 1) {
|
|
279
|
+
return dwy - 30
|
|
280
|
+
}
|
|
281
|
+
if (mft === 2) {
|
|
282
|
+
return dwy - 58 - leap
|
|
283
|
+
}
|
|
284
|
+
if (mft === 3) {
|
|
285
|
+
return dwy - 89 - leap
|
|
286
|
+
}
|
|
287
|
+
if (mft === 4) {
|
|
288
|
+
return dwy - 119 - leap
|
|
289
|
+
}
|
|
290
|
+
if (mft === 5) {
|
|
291
|
+
return dwy - 150 - leap
|
|
292
|
+
}
|
|
293
|
+
if (mft === 6) {
|
|
294
|
+
return dwy - 180 - leap
|
|
295
|
+
}
|
|
296
|
+
if (mft === 7) {
|
|
297
|
+
return dwy - 211 - leap
|
|
298
|
+
}
|
|
299
|
+
if (mft === 8) {
|
|
300
|
+
return dwy - 242 - leap
|
|
301
|
+
}
|
|
302
|
+
if (mft === 9) {
|
|
303
|
+
return dwy - 272 - leap
|
|
304
|
+
}
|
|
305
|
+
if (mft === 10) {
|
|
306
|
+
return dwy - 303 - leap
|
|
307
|
+
}
|
|
308
|
+
if (mft === 11) {
|
|
309
|
+
return dwy - 333 - leap
|
|
310
|
+
}
|
|
311
|
+
throw new Error('Invalid time')
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const HOURS_PER_DAY = 24
|
|
315
|
+
const MINUTES_PER_HOUR = 60
|
|
316
|
+
const SECONDS_PER_MINUTE = 60
|
|
317
|
+
const MS_PER_SECOND = 1e3
|
|
318
|
+
const MS_PER_MINUTE = MS_PER_SECOND * SECONDS_PER_MINUTE
|
|
319
|
+
const MS_PER_HOUR = MS_PER_MINUTE * MINUTES_PER_HOUR
|
|
320
|
+
|
|
321
|
+
export function HourFromTime(t: number) {
|
|
322
|
+
return mod(Math.floor(t / MS_PER_HOUR), HOURS_PER_DAY)
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
export function MinFromTime(t: number) {
|
|
326
|
+
return mod(Math.floor(t / MS_PER_MINUTE), MINUTES_PER_HOUR)
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export function SecFromTime(t: number) {
|
|
330
|
+
return mod(Math.floor(t / MS_PER_SECOND), SECONDS_PER_MINUTE)
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function IsCallable(fn: any): fn is Function {
|
|
334
|
+
return typeof fn === 'function'
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* The abstract operation OrdinaryHasInstance implements
|
|
339
|
+
* the default algorithm for determining if an object O
|
|
340
|
+
* inherits from the instance object inheritance path
|
|
341
|
+
* provided by constructor C.
|
|
342
|
+
* @param C class
|
|
343
|
+
* @param O object
|
|
344
|
+
* @param internalSlots internalSlots
|
|
345
|
+
*/
|
|
346
|
+
export function OrdinaryHasInstance(
|
|
347
|
+
C: Object,
|
|
348
|
+
O: any,
|
|
349
|
+
internalSlots?: {boundTargetFunction: any}
|
|
350
|
+
) {
|
|
351
|
+
if (!IsCallable(C)) {
|
|
352
|
+
return false
|
|
353
|
+
}
|
|
354
|
+
if (internalSlots?.boundTargetFunction) {
|
|
355
|
+
let BC = internalSlots?.boundTargetFunction
|
|
356
|
+
return O instanceof BC
|
|
357
|
+
}
|
|
358
|
+
if (typeof O !== 'object') {
|
|
359
|
+
return false
|
|
360
|
+
}
|
|
361
|
+
let P = C.prototype
|
|
362
|
+
if (typeof P !== 'object') {
|
|
363
|
+
throw new TypeError(
|
|
364
|
+
'OrdinaryHasInstance called on an object with an invalid prototype property.'
|
|
365
|
+
)
|
|
366
|
+
}
|
|
367
|
+
return Object.prototype.isPrototypeOf.call(P, O)
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
export function msFromTime(t: number): number {
|
|
371
|
+
return mod(t, MS_PER_SECOND)
|
|
372
|
+
}
|
package/BUILD
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
load("@aspect_bazel_lib//lib:write_source_files.bzl", "write_source_files")
|
|
2
|
+
load("@aspect_rules_js//npm/private:npm_package.bzl", "npm_package")
|
|
3
|
+
load("@npm//:defs.bzl", "npm_link_all_packages")
|
|
4
|
+
load("//tools:index.bzl", "check_format", "generate_src_file", "package_json_test", "ts_compile")
|
|
5
|
+
load("//tools:jest.bzl", "jest_test")
|
|
6
|
+
|
|
7
|
+
npm_link_all_packages(name = "node_modules")
|
|
8
|
+
|
|
9
|
+
PACKAGE_NAME = "ecma402-abstract"
|
|
10
|
+
|
|
11
|
+
npm_package(
|
|
12
|
+
name = PACKAGE_NAME,
|
|
13
|
+
srcs = [
|
|
14
|
+
"LICENSE.md",
|
|
15
|
+
"README.md",
|
|
16
|
+
"package.json",
|
|
17
|
+
":dist",
|
|
18
|
+
],
|
|
19
|
+
package = "@formatjs/%s" % PACKAGE_NAME,
|
|
20
|
+
visibility = ["//visibility:public"],
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
SRCS = glob(
|
|
24
|
+
[
|
|
25
|
+
"**/*.ts",
|
|
26
|
+
"NumberFormat/*.json",
|
|
27
|
+
],
|
|
28
|
+
exclude = [
|
|
29
|
+
"scripts/*.ts",
|
|
30
|
+
"tests/*.ts",
|
|
31
|
+
],
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
SRC_DEPS = [
|
|
35
|
+
":node_modules/@formatjs/intl-localematcher",
|
|
36
|
+
]
|
|
37
|
+
|
|
38
|
+
ts_compile(
|
|
39
|
+
name = "dist",
|
|
40
|
+
srcs = SRCS,
|
|
41
|
+
package = "@formatjs/%s" % PACKAGE_NAME,
|
|
42
|
+
skip_esm = False,
|
|
43
|
+
deps = SRC_DEPS,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
jest_test(
|
|
47
|
+
name = "unit",
|
|
48
|
+
srcs = SRCS + glob([
|
|
49
|
+
"tests/**/*.ts",
|
|
50
|
+
"tests/**/*.tsx",
|
|
51
|
+
"tests/**/*.snap",
|
|
52
|
+
]),
|
|
53
|
+
deps = SRC_DEPS + [
|
|
54
|
+
],
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
write_source_files(
|
|
58
|
+
name = "tsconfig_json",
|
|
59
|
+
files = {"tsconfig.json": "//tools:tsconfig.golden.json"},
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
# digit-mapping
|
|
63
|
+
generate_src_file(
|
|
64
|
+
name = "digit-mapping",
|
|
65
|
+
src = "NumberFormat/digit-mapping.generated.ts",
|
|
66
|
+
entry_point = "scripts/digit-mapping.ts",
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
check_format(
|
|
70
|
+
name = "prettier",
|
|
71
|
+
srcs = glob(
|
|
72
|
+
[
|
|
73
|
+
"**/*",
|
|
74
|
+
],
|
|
75
|
+
exclude = [
|
|
76
|
+
"**/*.generated.*",
|
|
77
|
+
"CHANGELOG.md",
|
|
78
|
+
"NumberFormat/digit-mapping.json",
|
|
79
|
+
],
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
generate_src_file(
|
|
84
|
+
name = "regex",
|
|
85
|
+
src = "regex.generated.ts",
|
|
86
|
+
data = [
|
|
87
|
+
"//:node_modules/@types/regenerate",
|
|
88
|
+
"//:node_modules/@unicode/unicode-13.0.0",
|
|
89
|
+
"//:node_modules/regenerate",
|
|
90
|
+
],
|
|
91
|
+
entry_point = "scripts/regex-gen.ts",
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
package_json_test(
|
|
95
|
+
name = "package_json_test",
|
|
96
|
+
deps = SRC_DEPS,
|
|
97
|
+
)
|