@flagdock/core 0.2.0 → 0.3.1
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/conditions.d.ts +6 -0
- package/dist/conditions.js +53 -4
- package/package.json +2 -2
package/dist/conditions.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
1
|
import type { Audience, Condition, EvaluationContext } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Milliseconds since 1970 for an ISO 8601 date or date-time, or undefined for anything else. A date-time
|
|
4
|
+
* without an offset is read as UTC, so the answer never depends on the time zone of the machine. Every SDK
|
|
5
|
+
* parses dates this same way (Date.parse would also accept "7" or "Dec 1", differently per engine).
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseIsoTime(text: string): number | undefined;
|
|
2
8
|
/**
|
|
3
9
|
* Reads an attribute the app passed in. Only the context's own properties count, so an attribute named like
|
|
4
10
|
* something every object has (`constructor`, `toString`, `__proto__`) is missing unless the app set it.
|
package/dist/conditions.js
CHANGED
|
@@ -14,15 +14,64 @@ function asNumber(v) {
|
|
|
14
14
|
}
|
|
15
15
|
return undefined;
|
|
16
16
|
}
|
|
17
|
+
// ISO 8601: 2026, 2026-12, 2026-12-01, then optionally T10:00, T10:00:00 or T10:00:00.123, then Z or an offset.
|
|
18
|
+
const ISO = /^([+-]\d{6}|\d{4})(?:-(\d{2})(?:-(\d{2}))?)?(?:[Tt](\d{2}):(\d{2})(?::(\d{2})(?:\.(\d+))?)?)?([Zz]|[+-]\d{2}:?\d{2})?$/;
|
|
19
|
+
/** Days since 1970-01-01 in the proleptic Gregorian calendar. Days past a month's end roll over, as Date.parse does. */
|
|
20
|
+
function daysFromCivil(y, m, d) {
|
|
21
|
+
y -= m <= 2 ? 1 : 0;
|
|
22
|
+
const era = Math.floor(y / 400);
|
|
23
|
+
const yoe = y - era * 400;
|
|
24
|
+
const doy = Math.floor((153 * ((m + 9) % 12) + 2) / 5);
|
|
25
|
+
const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
|
|
26
|
+
return era * 146097 + doe - 719468 + (d - 1);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Milliseconds since 1970 for an ISO 8601 date or date-time, or undefined for anything else. A date-time
|
|
30
|
+
* without an offset is read as UTC, so the answer never depends on the time zone of the machine. Every SDK
|
|
31
|
+
* parses dates this same way (Date.parse would also accept "7" or "Dec 1", differently per engine).
|
|
32
|
+
*/
|
|
33
|
+
export function parseIsoTime(text) {
|
|
34
|
+
const m = ISO.exec(text.trim());
|
|
35
|
+
if (!m)
|
|
36
|
+
return undefined;
|
|
37
|
+
const [, ys, ms, ds, hh, mi, ss, frac, offset] = m;
|
|
38
|
+
if (ys === '-000000')
|
|
39
|
+
return undefined;
|
|
40
|
+
if (hh !== undefined && ds === undefined)
|
|
41
|
+
return undefined;
|
|
42
|
+
if (offset !== undefined && hh === undefined)
|
|
43
|
+
return undefined;
|
|
44
|
+
const year = Number(ys);
|
|
45
|
+
const month = ms ? Number(ms) : 1;
|
|
46
|
+
const day = ds ? Number(ds) : 1;
|
|
47
|
+
if (month < 1 || month > 12 || day < 1 || day > 31)
|
|
48
|
+
return undefined;
|
|
49
|
+
const hour = hh ? Number(hh) : 0;
|
|
50
|
+
const minute = mi ? Number(mi) : 0;
|
|
51
|
+
const second = ss ? Number(ss) : 0;
|
|
52
|
+
const millis = Number((frac ?? '0').slice(0, 3).padEnd(3, '0'));
|
|
53
|
+
if (minute > 59 || second > 59)
|
|
54
|
+
return undefined;
|
|
55
|
+
if (hour > 24 || (hour === 24 && (minute || second || millis)))
|
|
56
|
+
return undefined;
|
|
57
|
+
let t = (daysFromCivil(year, month, day) * 86400 + hour * 3600 + minute * 60 + second) * 1000 + millis;
|
|
58
|
+
if (offset !== undefined && offset !== 'Z' && offset !== 'z') {
|
|
59
|
+
const digits = offset.slice(1).replace(':', '');
|
|
60
|
+
const oh = Number(digits.slice(0, 2));
|
|
61
|
+
const om = Number(digits.slice(2));
|
|
62
|
+
if (oh > 23 || om > 59)
|
|
63
|
+
return undefined;
|
|
64
|
+
t -= (offset[0] === '-' ? -1 : 1) * (oh * 60 + om) * 60_000;
|
|
65
|
+
}
|
|
66
|
+
return Math.abs(t) > 8.64e15 ? undefined : t;
|
|
67
|
+
}
|
|
17
68
|
function asTime(v) {
|
|
18
69
|
if (v instanceof Date)
|
|
19
70
|
return Number.isNaN(v.getTime()) ? undefined : v.getTime();
|
|
20
71
|
if (typeof v === 'number')
|
|
21
72
|
return Number.isFinite(v) ? v : undefined;
|
|
22
|
-
if (typeof v === 'string')
|
|
23
|
-
|
|
24
|
-
return Number.isNaN(t) ? undefined : t;
|
|
25
|
-
}
|
|
73
|
+
if (typeof v === 'string')
|
|
74
|
+
return parseIsoTime(v);
|
|
26
75
|
return undefined;
|
|
27
76
|
}
|
|
28
77
|
/** Parses "1.2.3", "v1.2" or "1.2.3-beta.1". Pre-release sorts before release. */
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@flagdock/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.1",
|
|
4
4
|
"description": "Flag evaluation engine shared by the Flagdock SDKs",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"feature-flags",
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"homepage": "https://flagdock.com",
|
|
13
13
|
"repository": {
|
|
14
14
|
"type": "git",
|
|
15
|
-
"url": "git+https://github.com/paleka/
|
|
15
|
+
"url": "git+https://github.com/paleka/flagdock-js.git",
|
|
16
16
|
"directory": "packages/core"
|
|
17
17
|
},
|
|
18
18
|
"bugs": {
|