@anteros/core 0.0.7 → 0.0.8
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/package.json +1 -1
- package/utils/func.ts +24 -24
package/package.json
CHANGED
package/utils/func.ts
CHANGED
|
@@ -209,22 +209,14 @@ function cleanDeep<T extends DeepRecord | DeepRecord[]>(data: T): Partial<T> {
|
|
|
209
209
|
}
|
|
210
210
|
|
|
211
211
|
/**
|
|
212
|
-
* Vérifie si une string est une date valide.
|
|
213
|
-
*
|
|
214
|
-
*
|
|
212
|
+
* Vérifie si une string est une date ISO 8601 valide.
|
|
213
|
+
* Les timestamps numériques (ex: "1704067200") ne sont PAS convertis ici —
|
|
214
|
+
* ils passent par `isEpochNumber` quand la valeur est un `number`.
|
|
215
215
|
*/
|
|
216
216
|
function isDate(value: string): boolean {
|
|
217
217
|
if (!value || typeof value !== 'string') return false;
|
|
218
218
|
const trimmed = value.trim();
|
|
219
219
|
|
|
220
|
-
// Timestamps numériques purs (10 ou 13 chiffres)
|
|
221
|
-
if (/^\d+$/.test(trimmed)) {
|
|
222
|
-
const len = trimmed.length;
|
|
223
|
-
if (len !== 10 && len !== 13) return false;
|
|
224
|
-
const ms = new Date(Number(trimmed)).getTime();
|
|
225
|
-
return !isNaN(ms);
|
|
226
|
-
}
|
|
227
|
-
|
|
228
220
|
// ISO 8601 : YYYY-MM-DD avec heure optionnelle
|
|
229
221
|
if (!/^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d{1,3})?)?(Z|[+-]\d{2}:\d{2})?)?$/.test(trimmed)) {
|
|
230
222
|
return false;
|
|
@@ -234,26 +226,31 @@ function isDate(value: string): boolean {
|
|
|
234
226
|
return !isNaN(date.getTime());
|
|
235
227
|
}
|
|
236
228
|
|
|
237
|
-
/**
|
|
229
|
+
/** Convertit une string ISO 8601 en Date. */
|
|
238
230
|
function stringToDate(value: string): Date {
|
|
239
|
-
|
|
240
|
-
if (/^\d+$/.test(trimmed)) {
|
|
241
|
-
const len = trimmed.length;
|
|
242
|
-
if (len === 10 || len === 13) {
|
|
243
|
-
return new Date(Number(trimmed));
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
return new Date(trimmed);
|
|
231
|
+
return new Date(value.trim());
|
|
247
232
|
}
|
|
248
233
|
|
|
249
|
-
/**
|
|
234
|
+
/**
|
|
235
|
+
* Nombre entier type timestamp UNIX (s ou ms).
|
|
236
|
+
* Restreint à ≥ an 2000 pour éviter les faux positifs (ex: numéros de téléphone).
|
|
237
|
+
*/
|
|
250
238
|
function isEpochNumber(value: number): boolean {
|
|
251
239
|
if (typeof value !== 'number' || !Number.isFinite(value)) return false;
|
|
252
240
|
const int = Math.trunc(value);
|
|
253
241
|
if (int !== value) return false;
|
|
254
242
|
const s = String(Math.abs(int));
|
|
255
|
-
if (s.length
|
|
256
|
-
|
|
243
|
+
if (s.length === 10) {
|
|
244
|
+
// Secondes UNIX : ≥ 2000-01-01
|
|
245
|
+
if (int < 946684800) return false;
|
|
246
|
+
return !isNaN(new Date(int * 1000).getTime());
|
|
247
|
+
}
|
|
248
|
+
if (s.length === 13) {
|
|
249
|
+
// Millisecondes UNIX : ≥ 2000-01-01
|
|
250
|
+
if (int < 946684800000) return false;
|
|
251
|
+
return !isNaN(new Date(int).getTime());
|
|
252
|
+
}
|
|
253
|
+
return false;
|
|
257
254
|
}
|
|
258
255
|
|
|
259
256
|
function deepCopy<T>(value: T): T {
|
|
@@ -339,7 +336,10 @@ function formatToDate(data: any, options?: FormatToDateOptions, path = ''): any
|
|
|
339
336
|
}
|
|
340
337
|
} else if (typeof value === 'number' && isEpochNumber(value)) {
|
|
341
338
|
if (shouldFormatDateAtPath(p, options)) {
|
|
342
|
-
|
|
339
|
+
// 10 chiffres = secondes (×1000), 13 chiffres = millisecondes (déjà en ms)
|
|
340
|
+
const absVal = Math.abs(Math.trunc(value));
|
|
341
|
+
const asMs = String(absVal).length === 10 ? value * 1000 : value;
|
|
342
|
+
result[key] = new Date(asMs);
|
|
343
343
|
} else {
|
|
344
344
|
result[key] = value;
|
|
345
345
|
}
|