nhb-toolbox 3.6.50 → 3.7.7
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/dist/date/Chronos.d.ts +41 -6
- package/dist/date/Chronos.d.ts.map +1 -1
- package/dist/date/Chronos.js +162 -14
- package/dist/date/types.d.ts +12 -0
- package/dist/date/types.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/date/Chronos.d.ts
CHANGED
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import type { LocaleCode } from '../number/types';
|
|
2
|
-
import type { FormatOptions, TimeUnit, TimeZone, UTCOffSet } from './types';
|
|
2
|
+
import type { ChronosObject, FormatOptions, TimeUnit, TimeZone, UTCOffSet } from './types';
|
|
3
|
+
declare const ORIGIN: unique symbol;
|
|
3
4
|
export declare class Chronos {
|
|
4
5
|
#private;
|
|
6
|
+
[ORIGIN]?: string;
|
|
5
7
|
/**
|
|
6
8
|
* * Creates a new immutable `Chronos` instance.
|
|
7
9
|
*
|
|
@@ -12,6 +14,7 @@ export declare class Chronos {
|
|
|
12
14
|
* - If a Chronos object is provided, it will be converted to a Date object.
|
|
13
15
|
*/
|
|
14
16
|
constructor(value?: number | string | Date | Chronos);
|
|
17
|
+
[Symbol.iterator](): IterableIterator<[string, number]>;
|
|
15
18
|
get [Symbol.toStringTag](): string;
|
|
16
19
|
/**
|
|
17
20
|
* * Enables primitive coercion like `console.log`, `${chronos}`, etc.
|
|
@@ -19,6 +22,8 @@ export declare class Chronos {
|
|
|
19
22
|
* @returns The primitive value based on the hint.
|
|
20
23
|
*/
|
|
21
24
|
[Symbol.toPrimitive](hint: string): string | number;
|
|
25
|
+
/** Returns a debug-friendly string for console.log or util.inspect */
|
|
26
|
+
inspect(): string;
|
|
22
27
|
/** * Clones and returns a new Chronos instance with the same date. */
|
|
23
28
|
clone(): Chronos;
|
|
24
29
|
/** * Enables JSON.stringify and console logging to show readable output. */
|
|
@@ -29,6 +34,10 @@ export declare class Chronos {
|
|
|
29
34
|
toDate(): Date;
|
|
30
35
|
/** * Returns a string representation of a date. The format of the string depends on the locale. */
|
|
31
36
|
toString(): string;
|
|
37
|
+
/** * Returns ISO string with local time zone offset */
|
|
38
|
+
toLocalISOString(): string;
|
|
39
|
+
/** * Returns a date as a string value in ISO format. */
|
|
40
|
+
toISOString(): string;
|
|
32
41
|
/**
|
|
33
42
|
* * Wrapper over native `toLocaleString`
|
|
34
43
|
* @description Converts a date and time to a string by using the current or specified locale.
|
|
@@ -37,8 +46,6 @@ export declare class Chronos {
|
|
|
37
46
|
* @param options An object that contains one or more properties that specify comparison options.
|
|
38
47
|
*/
|
|
39
48
|
toLocaleString(locale?: LocaleCode | Intl.Locale | (LocaleCode | Intl.Locale)[], options?: Intl.DateTimeFormatOptions): string;
|
|
40
|
-
/** * Returns a date as a string value in ISO format. */
|
|
41
|
-
toISOString(): string;
|
|
42
49
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
43
50
|
getTimeStamp(): number;
|
|
44
51
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
@@ -292,9 +299,37 @@ export declare class Chronos {
|
|
|
292
299
|
* @returns `true` if the current date is within the specified range based on the `inclusive` mode.
|
|
293
300
|
*/
|
|
294
301
|
isBetween(start: number | string | Date | Chronos, end: number | string | Date | Chronos, inclusive?: '[]' | '[)' | '(]' | '()'): boolean;
|
|
295
|
-
/**
|
|
296
|
-
* Returns number of days in current month
|
|
297
|
-
*/
|
|
302
|
+
/** * Returns number of days in current month */
|
|
298
303
|
daysInMonth(): number;
|
|
304
|
+
/** * Converts to object with all date unit parts */
|
|
305
|
+
toObject(): ChronosObject;
|
|
306
|
+
/** * Converts to array with all date unit parts */
|
|
307
|
+
toArray(): any[];
|
|
308
|
+
/** * Returns offset like +06:00 */
|
|
309
|
+
getUTCOffset(): string;
|
|
310
|
+
/** * Checks if currently in DST */
|
|
311
|
+
isDST(): boolean;
|
|
312
|
+
/** * Returns new Chronos instance in UTC */
|
|
313
|
+
toUTC(): Chronos;
|
|
314
|
+
/** * Returns new Chronos instance in local time */
|
|
315
|
+
toLocal(): Chronos;
|
|
316
|
+
/** @static Parses a date string with a given format (partial support) */
|
|
317
|
+
static parse(dateStr: string, format: string): Chronos;
|
|
318
|
+
/**
|
|
319
|
+
* @static Creates UTC Chronos
|
|
320
|
+
* @param dateLike Date input to create utc time.
|
|
321
|
+
*/
|
|
322
|
+
static utc(dateLike: number | string | Date | Chronos): Chronos;
|
|
323
|
+
/**
|
|
324
|
+
* @static Returns earliest Chronos
|
|
325
|
+
* @param dates Date inputs.
|
|
326
|
+
*/
|
|
327
|
+
static min(...dates: (number | string | Date | Chronos)[]): Chronos;
|
|
328
|
+
/**
|
|
329
|
+
* @static Returns latest Chronos
|
|
330
|
+
* @param dates Date inputs.
|
|
331
|
+
*/
|
|
332
|
+
static max(...dates: (number | string | Date | Chronos)[]): Chronos;
|
|
299
333
|
}
|
|
334
|
+
export {};
|
|
300
335
|
//# sourceMappingURL=Chronos.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,EAEX,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,qBAAa,OAAO;;
|
|
1
|
+
{"version":3,"file":"Chronos.d.ts","sourceRoot":"","sources":["../../src/date/Chronos.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAGlD,OAAO,KAAK,EAEX,aAAa,EACb,aAAa,EACb,QAAQ,EACR,QAAQ,EACR,SAAS,EACT,MAAM,SAAS,CAAC;AAGjB,QAAA,MAAM,MAAM,eAAmB,CAAC;AAEhC,qBAAa,OAAO;;IAEnB,CAAC,M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O;IAIP,mCAAmC;IACnC,YAAY,IAAI,MAAM;IAUtB,mCAAmC;IACnC,KAAK,IAAI,OAAO;IAUhB,4CAA4C;IAC5C,KAAK,IAAI,OAAO;IAMhB,mDAAmD;IACnD,OAAO,IAAI,OAAO;IAMlB,yEAAyE;IACzE,MAAM,CAAC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,OAAO;IA6CtD;;;OAGG;IACH,MAAM,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,GAAG,OAAO;IAM/D;;;OAGG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,EAAE,GAAG,OAAO;IAMnE;;;OAGG;IACH,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,OAAO,CAAC,EAAE,GAAG,OAAO;CAKnE"}
|
package/dist/date/Chronos.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { DAYS, MONTHS, sortedFormats, TIME_ZONES } from './constants';
|
|
2
2
|
import { isValidUTCOffSet } from './guards';
|
|
3
3
|
import { extractMinutesFromUTC } from './utils';
|
|
4
|
+
const ORIGIN = Symbol('origin');
|
|
4
5
|
export class Chronos {
|
|
5
6
|
#date;
|
|
6
|
-
|
|
7
|
+
[ORIGIN];
|
|
7
8
|
/**
|
|
8
9
|
* * Creates a new immutable `Chronos` instance.
|
|
9
10
|
*
|
|
@@ -16,10 +17,33 @@ export class Chronos {
|
|
|
16
17
|
constructor(value) {
|
|
17
18
|
const date = this.#toNewDate(value);
|
|
18
19
|
this.#date = date;
|
|
19
|
-
|
|
20
|
+
this[ORIGIN] = 'root';
|
|
21
|
+
}
|
|
22
|
+
*[Symbol.iterator]() {
|
|
23
|
+
yield ['year', this.year];
|
|
24
|
+
yield ['month', this.month];
|
|
25
|
+
yield ['isoMonth', this.month + 1];
|
|
26
|
+
yield ['date', this.date];
|
|
27
|
+
yield ['day', this.day];
|
|
28
|
+
yield ['isoDay', this.day + 1];
|
|
29
|
+
yield ['hour', this.hour];
|
|
30
|
+
yield ['minute', this.minute];
|
|
31
|
+
yield ['second', this.second];
|
|
32
|
+
yield ['millisecond', this.millisecond];
|
|
33
|
+
}
|
|
34
|
+
#withOrigin(origin) {
|
|
35
|
+
const instance = new Chronos(this.#date);
|
|
36
|
+
instance[ORIGIN] = origin;
|
|
37
|
+
return instance;
|
|
20
38
|
}
|
|
21
39
|
get [Symbol.toStringTag]() {
|
|
22
|
-
|
|
40
|
+
switch (this[ORIGIN]) {
|
|
41
|
+
case 'toUTC':
|
|
42
|
+
case 'utc':
|
|
43
|
+
return this.#toLocalISOString().replace(this.getUTCOffset(), 'Z');
|
|
44
|
+
default:
|
|
45
|
+
return this.#toLocalISOString();
|
|
46
|
+
}
|
|
23
47
|
}
|
|
24
48
|
/**
|
|
25
49
|
* * Enables primitive coercion like `console.log`, `${chronos}`, etc.
|
|
@@ -29,7 +53,11 @@ export class Chronos {
|
|
|
29
53
|
[Symbol.toPrimitive](hint) {
|
|
30
54
|
if (hint === 'number')
|
|
31
55
|
return this.valueOf();
|
|
32
|
-
return this.
|
|
56
|
+
return this.toLocalISOString();
|
|
57
|
+
}
|
|
58
|
+
/** Returns a debug-friendly string for console.log or util.inspect */
|
|
59
|
+
inspect() {
|
|
60
|
+
return `[Chronos ${this.toLocalISOString()}]`;
|
|
33
61
|
}
|
|
34
62
|
/** * Clones and returns a new Chronos instance with the same date. */
|
|
35
63
|
clone() {
|
|
@@ -37,7 +65,7 @@ export class Chronos {
|
|
|
37
65
|
}
|
|
38
66
|
/** * Enables JSON.stringify and console logging to show readable output. */
|
|
39
67
|
toJSON() {
|
|
40
|
-
return this.
|
|
68
|
+
return this.toLocalISOString();
|
|
41
69
|
}
|
|
42
70
|
/** * Enables arithmetic and comparison operations (e.g., +new Chronos()). */
|
|
43
71
|
valueOf() {
|
|
@@ -49,7 +77,44 @@ export class Chronos {
|
|
|
49
77
|
}
|
|
50
78
|
/** * Returns a string representation of a date. The format of the string depends on the locale. */
|
|
51
79
|
toString() {
|
|
52
|
-
|
|
80
|
+
switch (this[ORIGIN]) {
|
|
81
|
+
case 'toUTC':
|
|
82
|
+
case 'utc': {
|
|
83
|
+
const mins = extractMinutesFromUTC(`UTC${this.getUTCOffset()}`);
|
|
84
|
+
const date = this.addMinutes(mins);
|
|
85
|
+
return date.toString();
|
|
86
|
+
}
|
|
87
|
+
default:
|
|
88
|
+
return this.#date.toString();
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
/** * Returns ISO string with local time zone offset */
|
|
92
|
+
#toLocalISOString() {
|
|
93
|
+
const pad = (n, p = 2) => String(n).padStart(p, '0');
|
|
94
|
+
return `${this.year}-${pad(this.month + 1)}-${pad(this.date)}T${pad(this.hour)}:${pad(this.minute)}:${pad(this.second)}.${pad(this.millisecond, 3)}${this.getUTCOffset()}`;
|
|
95
|
+
}
|
|
96
|
+
/** * Returns ISO string with local time zone offset */
|
|
97
|
+
toLocalISOString() {
|
|
98
|
+
switch (this[ORIGIN]) {
|
|
99
|
+
case 'toUTC':
|
|
100
|
+
case 'utc': {
|
|
101
|
+
const mins = extractMinutesFromUTC(`UTC${this.getUTCOffset()}`);
|
|
102
|
+
const date = this.addMinutes(mins);
|
|
103
|
+
return date.#toLocalISOString();
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
return this.#toLocalISOString();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/** * Returns a date as a string value in ISO format. */
|
|
110
|
+
toISOString() {
|
|
111
|
+
switch (this[ORIGIN]) {
|
|
112
|
+
case 'toUTC':
|
|
113
|
+
case 'utc':
|
|
114
|
+
return this.#toLocalISOString().replace(this.getUTCOffset(), 'Z');
|
|
115
|
+
default:
|
|
116
|
+
return this.#date.toISOString();
|
|
117
|
+
}
|
|
53
118
|
}
|
|
54
119
|
/**
|
|
55
120
|
* * Wrapper over native `toLocaleString`
|
|
@@ -61,10 +126,6 @@ export class Chronos {
|
|
|
61
126
|
toLocaleString(locale, options) {
|
|
62
127
|
return this.#date.toLocaleString(locale, options);
|
|
63
128
|
}
|
|
64
|
-
/** * Returns a date as a string value in ISO format. */
|
|
65
|
-
toISOString() {
|
|
66
|
-
return this.#date.toISOString();
|
|
67
|
-
}
|
|
68
129
|
/** * Returns the time value in milliseconds since midnight, January 1, 1970 UTC. */
|
|
69
130
|
getTimeStamp() {
|
|
70
131
|
return this.#date.getTime();
|
|
@@ -232,7 +293,13 @@ export class Chronos {
|
|
|
232
293
|
* @returns Formatted date string in desired format (UTC time).
|
|
233
294
|
*/
|
|
234
295
|
formatUTC(format = 'dd, MMM DD, YYYY HH:mm:ss:mss') {
|
|
235
|
-
|
|
296
|
+
switch (this[ORIGIN]) {
|
|
297
|
+
case 'toUTC':
|
|
298
|
+
case 'utc':
|
|
299
|
+
return this.#format(format, false);
|
|
300
|
+
default:
|
|
301
|
+
return this.#format(format, true);
|
|
302
|
+
}
|
|
236
303
|
}
|
|
237
304
|
/**
|
|
238
305
|
* * Adds seconds and returns a new immutable instance.
|
|
@@ -831,10 +898,91 @@ export class Chronos {
|
|
|
831
898
|
return t > s && t < e;
|
|
832
899
|
}
|
|
833
900
|
}
|
|
834
|
-
/**
|
|
835
|
-
* Returns number of days in current month
|
|
836
|
-
*/
|
|
901
|
+
/** * Returns number of days in current month */
|
|
837
902
|
daysInMonth() {
|
|
838
903
|
return new Date(this.year, this.month + 1, 0).getDate();
|
|
839
904
|
}
|
|
905
|
+
/** * Converts to object with all date unit parts */
|
|
906
|
+
toObject() {
|
|
907
|
+
return Object.fromEntries([...this]);
|
|
908
|
+
}
|
|
909
|
+
/** * Converts to array with all date unit parts */
|
|
910
|
+
toArray() {
|
|
911
|
+
return Object.values(this.toObject());
|
|
912
|
+
}
|
|
913
|
+
/** * Returns offset like +06:00 */
|
|
914
|
+
getUTCOffset() {
|
|
915
|
+
const offset = -this.#date.getTimezoneOffset();
|
|
916
|
+
const sign = offset >= 0 ? '+' : '-';
|
|
917
|
+
const pad = (n) => String(Math.floor(Math.abs(n))).padStart(2, '0');
|
|
918
|
+
return `${sign}${pad(offset / 60)}:${pad(offset % 60)}`;
|
|
919
|
+
}
|
|
920
|
+
/** * Checks if currently in DST */
|
|
921
|
+
isDST() {
|
|
922
|
+
const jan = new Date(this.year, 0, 1);
|
|
923
|
+
const jul = new Date(this.year, 6, 1);
|
|
924
|
+
return (Math.min(jan.getTimezoneOffset(), jul.getTimezoneOffset()) !==
|
|
925
|
+
this.#date.getTimezoneOffset());
|
|
926
|
+
}
|
|
927
|
+
/** * Returns new Chronos instance in UTC */
|
|
928
|
+
toUTC() {
|
|
929
|
+
const date = this.#date;
|
|
930
|
+
const utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
|
|
931
|
+
return new Chronos(utc).#withOrigin('toUTC');
|
|
932
|
+
}
|
|
933
|
+
/** * Returns new Chronos instance in local time */
|
|
934
|
+
toLocal() {
|
|
935
|
+
const date = this.#date;
|
|
936
|
+
const utc = new Date(date.getTime() - date.getTimezoneOffset() * 60000);
|
|
937
|
+
return new Chronos(utc).#withOrigin('toLocal');
|
|
938
|
+
}
|
|
939
|
+
/** @static Parses a date string with a given format (partial support) */
|
|
940
|
+
static parse(dateStr, format) {
|
|
941
|
+
const formatMap = {
|
|
942
|
+
YYYY: 'year',
|
|
943
|
+
MM: 'month',
|
|
944
|
+
DD: 'date',
|
|
945
|
+
HH: 'hour',
|
|
946
|
+
mm: 'minute',
|
|
947
|
+
ss: 'second',
|
|
948
|
+
};
|
|
949
|
+
const regex = format.replace(/YYYY|MM|DD|HH|mm|ss/g, (match) => {
|
|
950
|
+
return `(?<${match}>\\d{${match.length}})`;
|
|
951
|
+
});
|
|
952
|
+
const match = new RegExp(`^${regex}$`).exec(dateStr);
|
|
953
|
+
if (!match?.groups) {
|
|
954
|
+
throw new Error('Invalid date format');
|
|
955
|
+
}
|
|
956
|
+
const values = Object.entries(match.groups).reduce((acc, [key, val]) => {
|
|
957
|
+
const map = formatMap[key];
|
|
958
|
+
if (map) {
|
|
959
|
+
acc[map] = Number(val);
|
|
960
|
+
}
|
|
961
|
+
return acc;
|
|
962
|
+
}, {});
|
|
963
|
+
return new Chronos(new Date(values.year ?? 1970, (values.month ?? 1) - 1, values.date ?? 1, values.hour ?? 0, values.minute ?? 0, values.second ?? 0));
|
|
964
|
+
}
|
|
965
|
+
/**
|
|
966
|
+
* @static Creates UTC Chronos
|
|
967
|
+
* @param dateLike Date input to create utc time.
|
|
968
|
+
*/
|
|
969
|
+
static utc(dateLike) {
|
|
970
|
+
const date = new Chronos(dateLike).#date;
|
|
971
|
+
const utc = new Date(date.getTime() + date.getTimezoneOffset() * 60000);
|
|
972
|
+
return new Chronos(utc).#withOrigin('utc');
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* @static Returns earliest Chronos
|
|
976
|
+
* @param dates Date inputs.
|
|
977
|
+
*/
|
|
978
|
+
static min(...dates) {
|
|
979
|
+
return new Chronos(Math.min(...dates.map((d) => new Chronos(d).valueOf())));
|
|
980
|
+
}
|
|
981
|
+
/**
|
|
982
|
+
* @static Returns latest Chronos
|
|
983
|
+
* @param dates Date inputs.
|
|
984
|
+
*/
|
|
985
|
+
static max(...dates) {
|
|
986
|
+
return new Chronos(Math.max(...dates.map((d) => new Chronos(d).valueOf())));
|
|
987
|
+
}
|
|
840
988
|
}
|
package/dist/date/types.d.ts
CHANGED
|
@@ -49,6 +49,18 @@ export type Millisecond = (typeof MILLISECOND_FORMATS)[number];
|
|
|
49
49
|
export type TimeFormats = (typeof TIME_FORMATS)[number];
|
|
50
50
|
export type ChronosFormat = Year | Month | Day | Date | Hour | Minute | Second | Millisecond | TimeFormats;
|
|
51
51
|
export type ChronosDate = number | string | Date | Chronos;
|
|
52
|
+
export interface ChronosObject {
|
|
53
|
+
year: number;
|
|
54
|
+
month: number;
|
|
55
|
+
isoMonth: number;
|
|
56
|
+
date: number;
|
|
57
|
+
day: number;
|
|
58
|
+
isoDay: number;
|
|
59
|
+
hour: number;
|
|
60
|
+
minute: number;
|
|
61
|
+
second: number;
|
|
62
|
+
millisecond: number;
|
|
63
|
+
}
|
|
52
64
|
export type TimeZone = keyof typeof TIME_ZONES;
|
|
53
65
|
export type PositiveUTCHour = '+00' | '+01' | '+02' | '+03' | '+04' | '+05' | '+06' | '+07' | '+08' | '+09' | '+10' | '+11' | '+12' | '+13' | '+14';
|
|
54
66
|
export type NegativeUTCHour = '-00' | '-01' | '-02' | '-03' | '-04' | '-05' | '-06' | '-07' | '-08' | '-09' | '-10' | '-11' | '-12' | '-13' | '-14';
|
package/dist/date/types.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/date/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACzC,OAAO,KAAK,EACX,YAAY,EACZ,WAAW,EACX,YAAY,EACZ,mBAAmB,EACnB,cAAc,EACd,aAAa,EACb,cAAc,EACd,YAAY,EACZ,UAAU,EACV,YAAY,EACZ,MAAM,aAAa,CAAC;AAErB,mDAAmD;AACnD,MAAM,MAAM,KAAK,GACd,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,CAAC;AAER,mDAAmD;AACnD,MAAM,MAAM,OAAO,GAChB,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAAI,GACJ,IAA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