@g9tm/lynxcontrols-sqlite 1.4.0
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/README.md +12 -0
- package/contract/cases.json +463 -0
- package/dist/.tsbuildinfo +1 -0
- package/dist/codecs/codecs.d.ts +68 -0
- package/dist/codecs/codecs.d.ts.map +1 -0
- package/dist/codecs/codecs.js +201 -0
- package/dist/codecs/codecs.js.map +1 -0
- package/dist/contract/run-contract.d.ts +94 -0
- package/dist/contract/run-contract.d.ts.map +1 -0
- package/dist/contract/run-contract.js +159 -0
- package/dist/contract/run-contract.js.map +1 -0
- package/dist/driver/types.d.ts +87 -0
- package/dist/driver/types.d.ts.map +1 -0
- package/dist/driver/types.js +14 -0
- package/dist/driver/types.js.map +1 -0
- package/dist/errors.d.ts +39 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +57 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/query/builders.d.ts +216 -0
- package/dist/query/builders.d.ts.map +1 -0
- package/dist/query/builders.js +490 -0
- package/dist/query/builders.js.map +1 -0
- package/dist/query/compile.d.ts +42 -0
- package/dist/query/compile.d.ts.map +1 -0
- package/dist/query/compile.js +307 -0
- package/dist/query/compile.js.map +1 -0
- package/dist/query/expr.d.ts +201 -0
- package/dist/query/expr.d.ts.map +1 -0
- package/dist/query/expr.js +247 -0
- package/dist/query/expr.js.map +1 -0
- package/dist/repo/audit.d.ts +19 -0
- package/dist/repo/audit.d.ts.map +1 -0
- package/dist/repo/audit.js +36 -0
- package/dist/repo/audit.js.map +1 -0
- package/dist/repo/repository.d.ts +233 -0
- package/dist/repo/repository.d.ts.map +1 -0
- package/dist/repo/repository.js +715 -0
- package/dist/repo/repository.js.map +1 -0
- package/dist/schema/columns.d.ts +115 -0
- package/dist/schema/columns.d.ts.map +1 -0
- package/dist/schema/columns.js +112 -0
- package/dist/schema/columns.js.map +1 -0
- package/dist/schema/entity.d.ts +135 -0
- package/dist/schema/entity.d.ts.map +1 -0
- package/dist/schema/entity.js +227 -0
- package/dist/schema/entity.js.map +1 -0
- package/dist/schema/policies.d.ts +89 -0
- package/dist/schema/policies.d.ts.map +1 -0
- package/dist/schema/policies.js +54 -0
- package/dist/schema/policies.js.map +1 -0
- package/dist/session/ambient.d.ts +32 -0
- package/dist/session/ambient.d.ts.map +1 -0
- package/dist/session/ambient.js +28 -0
- package/dist/session/ambient.js.map +1 -0
- package/dist/session/cache.d.ts +62 -0
- package/dist/session/cache.d.ts.map +1 -0
- package/dist/session/cache.js +239 -0
- package/dist/session/cache.js.map +1 -0
- package/dist/session/database.d.ts +221 -0
- package/dist/session/database.d.ts.map +1 -0
- package/dist/session/database.js +430 -0
- package/dist/session/database.js.map +1 -0
- package/dist/session/locator.d.ts +33 -0
- package/dist/session/locator.d.ts.map +1 -0
- package/dist/session/locator.js +39 -0
- package/dist/session/locator.js.map +1 -0
- package/dist/session/migrations.d.ts +75 -0
- package/dist/session/migrations.d.ts.map +1 -0
- package/dist/session/migrations.js +88 -0
- package/dist/session/migrations.js.map +1 -0
- package/dist/session/mutex.d.ts +7 -0
- package/dist/session/mutex.d.ts.map +1 -0
- package/dist/session/mutex.js +27 -0
- package/dist/session/mutex.js.map +1 -0
- package/dist/session/rows.d.ts +17 -0
- package/dist/session/rows.d.ts.map +1 -0
- package/dist/session/rows.js +52 -0
- package/dist/session/rows.js.map +1 -0
- package/dist/session/schema-setup.d.ts +21 -0
- package/dist/session/schema-setup.d.ts.map +1 -0
- package/dist/session/schema-setup.js +227 -0
- package/dist/session/schema-setup.js.map +1 -0
- package/dist/session/scope.d.ts +117 -0
- package/dist/session/scope.d.ts.map +1 -0
- package/dist/session/scope.js +233 -0
- package/dist/session/scope.js.map +1 -0
- package/dist/session/write-hooks.d.ts +84 -0
- package/dist/session/write-hooks.d.ts.map +1 -0
- package/dist/session/write-hooks.js +55 -0
- package/dist/session/write-hooks.js.map +1 -0
- package/dist/values/ids.d.ts +46 -0
- package/dist/values/ids.d.ts.map +1 -0
- package/dist/values/ids.js +84 -0
- package/dist/values/ids.js.map +1 -0
- package/dist/values/int64.d.ts +33 -0
- package/dist/values/int64.d.ts.map +1 -0
- package/dist/values/int64.js +105 -0
- package/dist/values/int64.js.map +1 -0
- package/dist/values/local-date-time.d.ts +61 -0
- package/dist/values/local-date-time.d.ts.map +1 -0
- package/dist/values/local-date-time.js +122 -0
- package/dist/values/local-date-time.js.map +1 -0
- package/dist/values/sql-value.d.ts +36 -0
- package/dist/values/sql-value.d.ts.map +1 -0
- package/dist/values/sql-value.js +70 -0
- package/dist/values/sql-value.js.map +1 -0
- package/package.json +27 -0
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical casing for GUID strings the library writes (MAUI `G9IdCase`, ADR-0018). Lower is the default and matches
|
|
3
|
+
* `Guid.ToString()`. Choose once, before the first release: rows already written keep their casing (harmless in SQL,
|
|
4
|
+
* every id column is `COLLATE NOCASE`), but anything that derived a file path or preference key from a normalised id
|
|
5
|
+
* would be orphaned.
|
|
6
|
+
*/
|
|
7
|
+
export declare const G9IdCase: {
|
|
8
|
+
readonly Lower: "lower";
|
|
9
|
+
readonly Upper: "upper";
|
|
10
|
+
};
|
|
11
|
+
export type G9IdCase = (typeof G9IdCase)[keyof typeof G9IdCase];
|
|
12
|
+
/**
|
|
13
|
+
* The 32 hex digits of a GUID in any form .NET `Guid.TryParse` accepts (`D`, `N`, `B`, `P`, `X`), or `null`.
|
|
14
|
+
* Surrounding whitespace is ignored, as .NET does.
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseGuidHex(text: string): string | null;
|
|
17
|
+
/** `true` when `text` is a GUID in a form {@link parseGuidHex} accepts. */
|
|
18
|
+
export declare function isGuidText(text: string): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* MAUI `NormalizeSqliteGuidId(string?)`: `null` for null/empty/whitespace; a GUID in any accepted form becomes its
|
|
21
|
+
* canonical `D` form in `idCase`; any other text is returned trimmed (ids that are not GUIDs pass through).
|
|
22
|
+
*/
|
|
23
|
+
export declare function normalizeId(value: string | null | undefined, idCase?: G9IdCase): string | null;
|
|
24
|
+
/** MAUI `NormalizeRequiredSqliteGuidId`: like {@link normalizeId} but `''` instead of `null`. */
|
|
25
|
+
export declare function normalizeRequiredId(value: string | null | undefined, idCase?: G9IdCase): string;
|
|
26
|
+
/** MAUI `NormalizeIdLikeValue`: `null` stays null, text is normalised (`''` for blank), anything else passes. */
|
|
27
|
+
export declare function normalizeIdLike(value: unknown, idCase?: G9IdCase): unknown;
|
|
28
|
+
/**
|
|
29
|
+
* MAUI `G9IdComparer.Equals`: case-insensitive ordinal comparison of two ids (two nulls are equal). Faster than
|
|
30
|
+
* normalising both and the only correct comparison for GUID strings, which can arrive in any casing.
|
|
31
|
+
*/
|
|
32
|
+
export declare function idEquals(left: string | null | undefined, right: string | null | undefined): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* MAUI `G9IdComparer.Ordinal` for JavaScript collections: a key that is equal for ids that {@link idEquals}. Use it
|
|
35
|
+
* for every id-keyed `Map`/`Set` (`map.set(idKey(row.id), row)`).
|
|
36
|
+
*/
|
|
37
|
+
export declare function idKey(value: string | null | undefined): string;
|
|
38
|
+
/** Case-insensitive ordinal order of two ids (sorting by id). */
|
|
39
|
+
export declare function compareIds(left: string | null | undefined, right: string | null | undefined): number;
|
|
40
|
+
/** Fills 16 random bytes: the platform CSPRNG when present, otherwise `Math.random` (documented weaker fallback). */
|
|
41
|
+
export declare function randomBytes16(): Uint8Array;
|
|
42
|
+
/** MAUI `CreateNewId()`: a random (version 4) GUID in canonical form. */
|
|
43
|
+
export declare function newId(idCase?: G9IdCase, random?: () => Uint8Array): string;
|
|
44
|
+
/** `true` for the all-zero GUID, the placeholder MAUI refused as a merge key. */
|
|
45
|
+
export declare function isEmptyGuid(value: string | null | undefined): boolean;
|
|
46
|
+
//# sourceMapp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aAAa,IAAI,UAAU,CAS1C;AAED,yEAAyE;AACzE,wBAAgB,KAAK,CAAC,MAAM,GAAE,QAAkB,EAAE,MAAM,GAAE,MAAM,UAA0B,GAAG,MAAM,CAQlG;AAED,iFAAiF;AACjF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,IAAI,GAAG,SAAS,GAAG,OAAO,CAGrE"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const G9IdCase = {
|
|
2
|
+
Lower: 'lower',
|
|
3
|
+
Upper: 'upper'
|
|
4
|
+
};
|
|
5
|
+
const HEX32 = /^[0-9a-fA-F]{32}$/;
|
|
6
|
+
const D_FORM = /^([0-9a-fA-F]{8})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{12})$/;
|
|
7
|
+
const X_FORM = /^\{0[xX]([0-9a-fA-F]{1,8}),0[xX]([0-9a-fA-F]{1,4}),0[xX]([0-9a-fA-F]{1,4}),\{((?:0[xX][0-9a-fA-F]{1,2},){7}0[xX][0-9a-fA-F]{1,2})\}\}$/;
|
|
8
|
+
function padHex(text, width) {
|
|
9
|
+
let s = text;
|
|
10
|
+
while(s.length < width)s = `0${s}`;
|
|
11
|
+
return s;
|
|
12
|
+
}
|
|
13
|
+
function parseGuidHex(text) {
|
|
14
|
+
const s = text.trim();
|
|
15
|
+
if (HEX32.test(s)) return s;
|
|
16
|
+
let inner = s;
|
|
17
|
+
if (s.startsWith('{') && s.endsWith('}') || s.startsWith('(') && s.endsWith(')')) inner = s.slice(1, -1);
|
|
18
|
+
const d = D_FORM.exec(inner);
|
|
19
|
+
if (d) return d.slice(1).join('');
|
|
20
|
+
const x = X_FORM.exec(s.replace(/\s+/g, ''));
|
|
21
|
+
if (x) {
|
|
22
|
+
const tail = x[4].split(',').map((b)=>padHex(b.slice(2), 2)).join('');
|
|
23
|
+
return padHex(x[1], 8) + padHex(x[2], 4) + padHex(x[3], 4) + tail;
|
|
24
|
+
}
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
function toCanonical(hex, idCase) {
|
|
28
|
+
const d = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
29
|
+
return 'upper' === idCase ? d.toUpperCase() : d.toLowerCase();
|
|
30
|
+
}
|
|
31
|
+
function isGuidText(text) {
|
|
32
|
+
return null !== parseGuidHex(text);
|
|
33
|
+
}
|
|
34
|
+
function normalizeId(value, idCase = 'lower') {
|
|
35
|
+
if (null == value) return null;
|
|
36
|
+
const trimmed = value.trim();
|
|
37
|
+
if ('' === trimmed) return null;
|
|
38
|
+
const hex = parseGuidHex(trimmed);
|
|
39
|
+
return hex ? toCanonical(hex, idCase) : trimmed;
|
|
40
|
+
}
|
|
41
|
+
function normalizeRequiredId(value, idCase = 'lower') {
|
|
42
|
+
var _normalizeId;
|
|
43
|
+
return null != (_normalizeId = normalizeId(value, idCase)) ? _normalizeId : '';
|
|
44
|
+
}
|
|
45
|
+
function normalizeIdLike(value, idCase = 'lower') {
|
|
46
|
+
if (null == value) return null;
|
|
47
|
+
return 'string' == typeof value ? normalizeRequiredId(value, idCase) : value;
|
|
48
|
+
}
|
|
49
|
+
function idEquals(left, right) {
|
|
50
|
+
if (left === right) return true;
|
|
51
|
+
if (null == left || null == right) return false;
|
|
52
|
+
return left.length === right.length && left.toUpperCase() === right.toUpperCase();
|
|
53
|
+
}
|
|
54
|
+
function idKey(value) {
|
|
55
|
+
return null == value ? '' : value.toUpperCase();
|
|
56
|
+
}
|
|
57
|
+
function compareIds(left, right) {
|
|
58
|
+
const a = idKey(left);
|
|
59
|
+
const b = idKey(right);
|
|
60
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
61
|
+
}
|
|
62
|
+
function randomBytes16() {
|
|
63
|
+
const bytes = new Uint8Array(16);
|
|
64
|
+
const crypto = globalThis.crypto;
|
|
65
|
+
if (crypto && 'function' == typeof crypto.getRandomValues) crypto.getRandomValues(bytes);
|
|
66
|
+
else for(let i = 0; i < 16; i++)bytes[i] = Math.floor(256 * Math.random());
|
|
67
|
+
return bytes;
|
|
68
|
+
}
|
|
69
|
+
function newId(idCase = 'lower', random = randomBytes16) {
|
|
70
|
+
const bytes = random();
|
|
71
|
+
if (16 !== bytes.length) throw new RangeError('A GUID needs exactly 16 random bytes.');
|
|
72
|
+
bytes[6] = 0x0f & bytes[6] | 0x40;
|
|
73
|
+
bytes[8] = 0x3f & bytes[8] | 0x80;
|
|
74
|
+
let hex = '';
|
|
75
|
+
for(let i = 0; i < 16; i++)hex += padHex(bytes[i].toString(16), 2);
|
|
76
|
+
return toCanonical(hex, idCase);
|
|
77
|
+
}
|
|
78
|
+
function isEmptyGuid(value) {
|
|
79
|
+
const hex = value ? parseGuidHex(value) : null;
|
|
80
|
+
return null !== hex && /^0{32}$/.test(hex);
|
|
81
|
+
}
|
|
82
|
+
export { G9IdCase, compareIds, idEquals, idKey, isEmptyGuid, isGuidText, newId, normalizeId, normalizeIdLike, normalizeRequiredId, parseGuidHex, randomBytes16 };
|
|
83
|
+
|
|
84
|
+
//# sourceMappingURL=ids.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"values/ids.js","sources":["../../src/values/ids.ts"],"sourcesContent":["/**\n * Canonical casing for GUID strings the library writes (MAUI `G9IdCase`, ADR-0018). Lower is the default and matches\n * `Guid.ToString()`. Choose once, before the first release: rows already written keep their casing (harmless in SQL,\n * every id column is `COLLATE NOCASE`), but anything that derived a file path or preference key from a normalised id\n * would be orphaned.\n */\nexport const G9IdCase = { Lower: 'lower', Upper: 'upper' } as const;\nexport type G9IdCase = (typeof G9IdCase)[keyof typeof G9IdCase];\n\nconst HEX32 = /^[0-9a-fA-F]{32}$/;\nconst D_FORM = /^([0-9a-fA-F]{8})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{4})-([0-9a-fA-F]{12})$/;\nconst X_FORM =\n /^\\{0[xX]([0-9a-fA-F]{1,8}),0[xX]([0-9a-fA-F]{1,4}),0[xX]([0-9a-fA-F]{1,4}),\\{((?:0[xX][0-9a-fA-F]{1,2},){7}0[xX][0-9a-fA-F]{1,2})\\}\\}$/;\n\nfunction padHex(text: string, width: number): string {\n let s = text;\n while (s.length < width) s = `0${s}`;\n return s;\n}\n\n/**\n * The 32 hex digits of a GUID in any form .NET `Guid.TryParse` accepts (`D`, `N`, `B`, `P`, `X`), or `null`.\n * Surrounding whitespace is ignored, as .NET does.\n */\nexport function parseGuidHex(text: string): string | null {\n const s = text.trim();\n if (HEX32.test(s)) return s;\n let inner = s;\n if ((s.startsWith('{') && s.endsWith('}')) || (s.startsWith('(') && s.endsWith(')'))) inner = s.slice(1, -1);\n const d = D_FORM.exec(inner);\n if (d) return d.slice(1).join('');\n const x = X_FORM.exec(s.replace(/\\s+/g, ''));\n if (x) {\n const tail = (x[4] as string)\n .split(',')\n .map((b) => padHex(b.slice(2), 2))\n .join('');\n return padHex(x[1] as string, 8) + padHex(x[2] as string, 4) + padHex(x[3] as string, 4) + tail;\n }\n return null;\n}\n\nfunction toCanonical(hex: string, idCase: G9IdCase): string {\n const d = `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;\n return idCase === 'upper' ? d.toUpperCase() : d.toLowerCase();\n}\n\n/** `true` when `text` is a GUID in a form {@link parseGuidHex} accepts. */\nexport function isGuidText(text: string): boolean {\n return parseGuidHex(text) !== null;\n}\n\n/**\n * MAUI `NormalizeSqliteGuidId(string?)`: `null` for null/empty/whitespace; a GUID in any accepted form becomes its\n * canonical `D` form in `idCase`; any other text is returned trimmed (ids that are not GUIDs pass through).\n */\nexport function normalizeId(value: string | null | undefined, idCase: G9IdCase = 'lower'): string | null {\n if (value === null || value === undefined) return null;\n const trimmed = value.trim();\n if (trimmed === '') return null;\n const hex = parseGuidHex(trimmed);\n return hex ? toCanonical(hex, idCase) : trimmed;\n}\n\n/** MAUI `NormalizeRequiredSqliteGuidId`: like {@link normalizeId} but `''` instead of `null`. */\nexport function normalizeRequiredId(value: string | null | undefined, idCase: G9IdCase = 'lower'): string {\n return normalizeId(value, idCase) ?? '';\n}\n\n/** MAUI `NormalizeIdLikeValue`: `null` stays null, text is normalised (`''` for blank), anything else passes. */\nexport function normalizeIdLike(value: unknown, idCase: G9IdCase = 'lower'): unknown {\n if (value === null || value === undefined) return null;\n return typeof value === 'string' ? normalizeRequiredId(value, idCase) : value;\n}\n\n/**\n * MAUI `G9IdComparer.Equals`: case-insensitive ordinal comparison of two ids (two nulls are equal). Faster than\n * normalising both and the only correct comparison for GUID strings, which can arrive in any casing.\n */\nexport function idEquals(left: string | null | undefined, right: string | null | undefined): boolean {\n if (left === right) return true;\n if (left === null || left === undefined || right === null || right === undefined) return false;\n return left.length === right.length && left.toUpperCase() === right.toUpperCase();\n}\n\n/**\n * MAUI `G9IdComparer.Ordinal` for JavaScript collections: a key that is equal for ids that {@link idEquals}. Use it\n * for every id-keyed `Map`/`Set` (`map.set(idKey(row.id), row)`).\n */\nexport function idKey(value: string | null | undefined): string {\n return value === null || value === undefined ? '' : value.toUpperCase();\n}\n\n/** Case-insensitive ordinal order of two ids (sorting by id). */\nexport function compareIds(left: string | null | undefined, right: string | null | undefined): number {\n const a = idKey(left);\n const b = idKey(right);\n return a < b ? -1 : a > b ? 1 : 0;\n}\n\ninterface MaybeCrypto {\n readonly crypto?: { getRandomValues?: (array: Uint8Array) => Uint8Array };\n}\n\n/** Fills 16 random bytes: the platform CSPRNG when present, otherwise `Math.random` (documented weaker fallback). */\nexport function randomBytes16(): Uint8Array {\n const bytes = new Uint8Array(16);\n const crypto = (globalThis as MaybeCrypto).crypto;\n if (crypto && typeof crypto.getRandomValues === 'function') {\n crypto.getRandomValues(bytes);\n } else {\n for (let i = 0; i < 16; i++) bytes[i] = Math.floor(Math.random() * 256);\n }\n return bytes;\n}\n\n/** MAUI `CreateNewId()`: a random (version 4) GUID in canonical form. */\nexport function newId(idCase: G9IdCase = 'lower', random: () => Uint8Array = randomBytes16): string {\n const bytes = random();\n if (bytes.length !== 16) throw new RangeError('A GUID needs exactly 16 random bytes.');\n bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40;\n bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80;\n let hex = '';\n for (let i = 0; i < 16; i++) hex += padHex((bytes[i] as number).toString(16), 2);\n return toCanonical(hex, idCase);\n}\n\n/** `true` for the all-zero GUID, the placeholder MAUI refused as a merge key. */\nexport function isEmptyGuid(value: string | null | undefined): boolean {\n const hex = value ? parseGuidHex(value) : null;\n return hex !== null && /^0{32}$/.test(hex);\n}\n"],"names":["G9IdCase","HEX32","D_FORM","X_FORM","padHex","text","width","s","parseGuidHex","inner","d","x","tail","b","toCanonical","hex","idCase","isGuidText","normalizeId","value","trimmed","normalizeRequiredId","_normalizeId","normalizeIdLike","idEquals","left","right","idKey","compareIds","a","randomBytes16","bytes","Uint8Array","crypto","globalThis","i","Math","newId","random","RangeError","isEmptyGuid"],"mappings":"AAMO,MAAMA,WAAW;IAAE,OAAO;IAAS,OAAO;AAAQ;AAGzD,MAAMC,QAAQ;AACd,MAAMC,SAAS;AACf,MAAMC,SACJ;AAEF,SAASC,OAAOC,IAAY,EAAEC,KAAa;IACzC,IAAIC,IAAIF;IACR,MAAOE,EAAE,MAAM,GAAGD,MAAOC,IAAI,CAAC,CAAC,EAAEA,GAAG;IACpC,OAAOA;AACT;AAMO,SAASC,aAAaH,IAAY;IACvC,MAAME,IAAIF,KAAK,IAAI;IACnB,IAAIJ,MAAM,IAAI,CAACM,IAAI,OAAOA;IAC1B,IAAIE,QAAQF;IACZ,IAAKA,EAAE,UAAU,CAAC,QAAQA,EAAE,QAAQ,CAAC,QAAUA,EAAE,UAAU,CAAC,QAAQA,EAAE,QAAQ,CAAC,MAAOE,QAAQF,EAAE,KAAK,CAAC,GAAG;IACzG,MAAMG,IAAIR,OAAO,IAAI,CAACO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AIA,IAAKpB,OAAOX,OAAQ2B,KAAK,CAACI,EAAE,CAAY,QAAQ,CAAC,KAAK;IAC9E,OAAOrB,YAAYC,KAAKC;AAC1B;AAGO,SAASwB,YAAYrB,KAAgC;IAC1D,MAAMJ,MAAMI,QAAQX,aAAaW,SAAS;IAC1C,OAAOJ,AAAQ,SAARA,OAAgB,UAAU,IAAI,CAACA;AACxC"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical signed 64-bit integer as a decimal string: `"-9223372036854775808"` … `"9223372036854775807"`, no plus
|
|
3
|
+
* sign, no leading zeros, `"0"` for zero (plan/specs/05-Persistence.md §3). Lynx has no guaranteed `BigInt`, so a
|
|
4
|
+
* value outside ±(2^53 − 1) never passes through a `number`: it travels as this string end to end.
|
|
5
|
+
*/
|
|
6
|
+
export type G9Int64 = string & {
|
|
7
|
+
readonly __g9Int64: true;
|
|
8
|
+
};
|
|
9
|
+
/** The smallest int64, `-2^63`. */
|
|
10
|
+
export declare const G9_INT64_MIN: G9Int64;
|
|
11
|
+
/** The largest int64, `2^63 − 1`. */
|
|
12
|
+
export declare const G9_INT64_MAX: G9Int64;
|
|
13
|
+
/** `true` when `value` is a canonical {@link G9Int64} string (range included). */
|
|
14
|
+
export declare function isInt64(value: unknown): value is G9Int64;
|
|
15
|
+
/**
|
|
16
|
+
* Parses a decimal integer (optional sign, leading zeros allowed) or a safe-integer `number` into a canonical
|
|
17
|
+
* {@link G9Int64}. Throws a `misuse` {@link G9SqliteError} for anything else, including unsafe numbers (their exact
|
|
18
|
+
* value is already lost) and values outside the int64 range.
|
|
19
|
+
*/
|
|
20
|
+
export declare function parseInt64(value: string | number): G9Int64;
|
|
21
|
+
/** `true` when the int64 lies within ±(2^53 − 1) and so converts to a `number` exactly. */
|
|
22
|
+
export declare function isSafeInt64(value: G9Int64): boolean;
|
|
23
|
+
/** The exact `number` for a safe int64; throws `misuse` when the value would lose precision. */
|
|
24
|
+
export declare function int64ToNumber(value: G9Int64): number;
|
|
25
|
+
/** Total order of two int64 values: negative, zero or positive. */
|
|
26
|
+
export declare function compareInt64(a: G9Int64, b: G9Int64): number;
|
|
27
|
+
/** a + b; throws `misuse` on int64 overflow (never wraps silently). */
|
|
28
|
+
export declare function addInt64(a: G9Int64, b: G9Int64): G9Int64;
|
|
29
|
+
/** a − b; throws `misuse` on overflow. */
|
|
30
|
+
export declare function subtractInt64(a: G9Int64, b: G9Int64): G9Int64;
|
|
31
|
+
/** −a; throws `misuse` for {@link G9_INT64_MIN}, whose negation does not fit. */
|
|
32
|
+
export declare function negateInt64(a: G9Int64): G9Int64;
|
|
33
|
+
//# sourceMappingURL=int64.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"int64.d.ts","sourceRoot":"","sources":["../../src/values/int64.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,MAAM,OAAO,GAAG,MAAM,GAAG;IAAE,QAAQ,CAAC,SAAS,EAAE,IAAI,CAAA;CAAE,CAAC;AAE5D,mCAAmC;AACnC,eAAO,MAAM,YAAY,EAA6B,OAAO,CAAC;AAC9D,qCAAqC;AACrC,eAAO,MAAM,YAAY,EAA4B,OAAO,CAAC;AA+B7D,kFAAkF;AAClF,wBAAgB,OAAO,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,OAAO,CASxD;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,OAAO,CAa1D;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAEnD;AAED,gGAAgG;AAChG,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAKpD;AAED,mEAAmE;AACnE,wBAAgB,YAAY,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,MAAM,CAM3D;AAyCD,uEAAuE;AACvE,wBAAgB,QAAQ,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAIxD;AAED,0CAA0C;AAC1C,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,OAAO,GAAG,OAAO,CAI7D;AAED,iFAAiF;AACjF,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,OAAO,CAG/C"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { G9SqliteError } from "../errors.js";
|
|
2
|
+
const G9_INT64_MIN = '-9223372036854775808';
|
|
3
|
+
const G9_INT64_MAX = '9223372036854775807';
|
|
4
|
+
const MAX_MAGNITUDE = '9223372036854775807';
|
|
5
|
+
const MIN_MAGNITUDE = '9223372036854775808';
|
|
6
|
+
const DIGITS = /^(-?)(\d+)$/;
|
|
7
|
+
function compareMagnitude(a, b) {
|
|
8
|
+
if (a.length !== b.length) return a.length < b.length ? -1 : 1;
|
|
9
|
+
if (a === b) return 0;
|
|
10
|
+
return a < b ? -1 : 1;
|
|
11
|
+
}
|
|
12
|
+
function stripZeros(digits) {
|
|
13
|
+
let i = 0;
|
|
14
|
+
while(i < digits.length - 1 && 48 === digits.charCodeAt(i))i++;
|
|
15
|
+
return digits.slice(i);
|
|
16
|
+
}
|
|
17
|
+
function fits(negative, magnitude) {
|
|
18
|
+
return compareMagnitude(magnitude, negative ? MIN_MAGNITUDE : MAX_MAGNITUDE) <= 0;
|
|
19
|
+
}
|
|
20
|
+
function make(negative, magnitude) {
|
|
21
|
+
if ('0' === magnitude) return '0';
|
|
22
|
+
if (!fits(negative, magnitude)) throw new G9SqliteError('misuse', `Integer ${negative ? '-' : ''}${magnitude} is outside the signed 64-bit range.`);
|
|
23
|
+
return negative ? `-${magnitude}` : magnitude;
|
|
24
|
+
}
|
|
25
|
+
function isInt64(value) {
|
|
26
|
+
if ('string' != typeof value) return false;
|
|
27
|
+
const m = DIGITS.exec(value);
|
|
28
|
+
if (!m) return false;
|
|
29
|
+
const negative = '-' === m[1];
|
|
30
|
+
const magnitude = m[2];
|
|
31
|
+
if (magnitude !== stripZeros(magnitude)) return false;
|
|
32
|
+
if (negative && '0' === magnitude) return false;
|
|
33
|
+
return fits(negative, magnitude);
|
|
34
|
+
}
|
|
35
|
+
function parseInt64(value) {
|
|
36
|
+
if ('number' == typeof value) {
|
|
37
|
+
if (!Number.isSafeInteger(value)) throw new G9SqliteError('misuse', `Number ${value} is not a safe integer; pass int64 values outside ±(2^53 − 1) as decimal strings.`);
|
|
38
|
+
return String(0 === value ? 0 : value);
|
|
39
|
+
}
|
|
40
|
+
const m = /^([+-]?)(\d+)$/.exec('string' == typeof value ? value.trim() : '');
|
|
41
|
+
if (!m) throw new G9SqliteError('misuse', `'${String(value)}' is not a decimal integer.`);
|
|
42
|
+
return make('-' === m[1], stripZeros(m[2]));
|
|
43
|
+
}
|
|
44
|
+
function isSafeInt64(value) {
|
|
45
|
+
return Number.isSafeInteger(Number(value)) && compareMagnitude(value.replace('-', ''), '9007199254740991') <= 0;
|
|
46
|
+
}
|
|
47
|
+
function int64ToNumber(value) {
|
|
48
|
+
if (!isSafeInt64(value)) throw new G9SqliteError('misuse', `Int64 ${value} does not fit a number exactly; keep it as a string.`);
|
|
49
|
+
return Number(value);
|
|
50
|
+
}
|
|
51
|
+
function compareInt64(a, b) {
|
|
52
|
+
const an = 45 === a.charCodeAt(0);
|
|
53
|
+
const bn = 45 === b.charCodeAt(0);
|
|
54
|
+
if (an !== bn) return an ? -1 : 1;
|
|
55
|
+
const c = compareMagnitude(an ? a.slice(1) : a, bn ? b.slice(1) : b);
|
|
56
|
+
return an ? -c : c;
|
|
57
|
+
}
|
|
58
|
+
function digitsToString(codes, from) {
|
|
59
|
+
return stripZeros(String.fromCharCode.apply(null, codes.slice(from)));
|
|
60
|
+
}
|
|
61
|
+
function addMagnitudes(a, b) {
|
|
62
|
+
const codes = new Array(Math.max(a.length, b.length) + 1);
|
|
63
|
+
let k = codes.length;
|
|
64
|
+
let carry = 0;
|
|
65
|
+
for(let i = a.length - 1, j = b.length - 1; i >= 0 || j >= 0 || carry; i--, j--){
|
|
66
|
+
const s = (i >= 0 ? a.charCodeAt(i) - 48 : 0) + (j >= 0 ? b.charCodeAt(j) - 48 : 0) + carry;
|
|
67
|
+
codes[--k] = 48 + s % 10;
|
|
68
|
+
carry = s >= 10 ? 1 : 0;
|
|
69
|
+
}
|
|
70
|
+
return digitsToString(codes, k);
|
|
71
|
+
}
|
|
72
|
+
function subtractMagnitudes(a, b) {
|
|
73
|
+
const codes = new Array(a.length);
|
|
74
|
+
let borrow = 0;
|
|
75
|
+
for(let i = a.length - 1, j = b.length - 1; i >= 0; i--, j--){
|
|
76
|
+
let d = a.charCodeAt(i) - 48 - borrow - (j >= 0 ? b.charCodeAt(j) - 48 : 0);
|
|
77
|
+
borrow = d < 0 ? 1 : 0;
|
|
78
|
+
if (d < 0) d += 10;
|
|
79
|
+
codes[i] = 48 + d;
|
|
80
|
+
}
|
|
81
|
+
return digitsToString(codes, 0);
|
|
82
|
+
}
|
|
83
|
+
function signedAdd(an, am, bn, bm) {
|
|
84
|
+
if (an === bn) return make(an, addMagnitudes(am, bm));
|
|
85
|
+
const c = compareMagnitude(am, bm);
|
|
86
|
+
if (0 === c) return '0';
|
|
87
|
+
return c > 0 ? make(an, subtractMagnitudes(am, bm)) : make(bn, subtractMagnitudes(bm, am));
|
|
88
|
+
}
|
|
89
|
+
function addInt64(a, b) {
|
|
90
|
+
const an = 45 === a.charCodeAt(0);
|
|
91
|
+
const bn = 45 === b.charCodeAt(0);
|
|
92
|
+
return signedAdd(an, an ? a.slice(1) : a, bn, bn ? b.slice(1) : b);
|
|
93
|
+
}
|
|
94
|
+
function subtractInt64(a, b) {
|
|
95
|
+
const an = 45 === a.charCodeAt(0);
|
|
96
|
+
const bn = 45 === b.charCodeAt(0);
|
|
97
|
+
return signedAdd(an, an ? a.slice(1) : a, !bn && '0' !== b, bn ? b.slice(1) : b);
|
|
98
|
+
}
|
|
99
|
+
function negateInt64(a) {
|
|
100
|
+
if ('0' === a) return a;
|
|
101
|
+
return 45 === a.charCodeAt(0) ? make(false, a.slice(1)) : make(true, a);
|
|
102
|
+
}
|
|
103
|
+
export { G9_INT64_MAX, G9_INT64_MIN, addInt64, compareInt64, int64ToNumber, isInt64, isSafeInt64, negateInt64, parseInt64, subtractInt64 };
|
|
104
|
+
|
|
105
|
+
//# sourceMappingURL=int64.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"values/int64.js","sources":["../../src/values/int64.ts"],"sourcesContent":["import { G9SqliteError } from '../errors';\n\n/**\n * Canonical signed 64-bit integer as a decimal string: `\"-9223372036854775808\"` … `\"9223372036854775807\"`, no plus\n * sign, no leading zeros, `\"0\"` for zero (plan/specs/05-Persistence.md §3). Lynx has no guaranteed `BigInt`, so a\n * value outside ±(2^53 − 1) never passes through a `number`: it travels as this string end to end.\n */\nexport type G9Int64 = string & { readonly __g9Int64: true };\n\n/** The smallest int64, `-2^63`. */\nexport const G9_INT64_MIN = '-9223372036854775808' as G9Int64;\n/** The largest int64, `2^63 − 1`. */\nexport const G9_INT64_MAX = '9223372036854775807' as G9Int64;\n\nconst MAX_MAGNITUDE = '9223372036854775807';\nconst MIN_MAGNITUDE = '9223372036854775808';\nconst DIGITS = /^(-?)(\\d+)$/;\n\n/** Compares two non-negative decimal digit strings without leading zeros. */\nfunction compareMagnitude(a: string, b: string): number {\n if (a.length !== b.length) return a.length < b.length ? -1 : 1;\n if (a === b) return 0;\n return a < b ? -1 : 1;\n}\n\nfunction stripZeros(digits: string): string {\n let i = 0;\n while (i < digits.length - 1 && digits.charCodeAt(i) === 48) i++;\n return digits.slice(i);\n}\n\nfunction fits(negative: boolean, magnitude: string): boolean {\n return compareMagnitude(magnitude, negative ? MIN_MAGNITUDE : MAX_MAGNITUDE) <= 0;\n}\n\nfunction make(negative: boolean, magnitude: string): G9Int64 {\n if (magnitude === '0') return '0' as G9Int64;\n if (!fits(negative, magnitude)) {\n throw new G9SqliteError('misuse', `Integer ${negative ? '-' : ''}${magnitude} is outside the signed 64-bit range.`);\n }\n return (negative ? `-${magnitude}` : magnitude) as G9Int64;\n}\n\n/** `true` when `value` is a canonical {@link G9Int64} string (range included). */\nexport function isInt64(value: unknown): value is G9Int64 {\n if (typeof value !== 'string') return false;\n const m = DIGITS.exec(value);\n if (!m) return false;\n const negative = m[1] === '-';\n const magnitude = m[2] as string;\n if (magnitude !== stripZeros(magnitude)) return false;\n if (negative && magnitude === '0') return false;\n return fits(negative, magnitude);\n}\n\n/**\n * Parses a decimal integer (optional sign, leading zeros allowed) or a safe-integer `number` into a canonical\n * {@link G9Int64}. Throws a `misuse` {@link G9SqliteError} for anything else, including unsafe numbers (their exact\n * value is already lost) and values outside the int64 range.\n */\nexport function parseInt64(value: string | number): G9Int64 {\n if (typeof value === 'number') {\n if (!Number.isSafeInteger(value)) {\n throw new G9SqliteError(\n 'misuse',\n `Number ${value} is not a safe integer; pass int64 values outside ±(2^53 − 1) as decimal strings.`,\n );\n }\n return String(value === 0 ? 0 : value) as G9Int64;\n }\n const m = /^([+-]?)(\\d+)$/.exec(typeof value === 'string' ? value.trim() : '');\n if (!m) throw new G9SqliteError('misuse', `'${String(value)}' is not a decimal integer.`);\n return make(m[1] === '-', stripZeros(m[2] as string));\n}\n\n/** `true` when the int64 lies within ±(2^53 − 1) and so converts to a `number` exactly. */\nexport function isSafeInt64(value: G9Int64): boolean {\n return Number.isSafeInteger(Number(value)) && compareMagnitude(value.replace('-', ''), '9007199254740991') <= 0;\n}\n\n/** The exact `number` for a safe int64; throws `misuse` when the value would lose precision. */\nexport function int64ToNumber(value: G9Int64): number {\n if (!isSafeInt64(value)) {\n throw new G9SqliteError('misuse', `Int64 ${value} does not fit a number exactly; keep it as a string.`);\n }\n return Number(value);\n}\n\n/** Total order of two int64 values: negative, zero or positive. */\nexport function compareInt64(a: G9Int64, b: G9Int64): number {\n const an = a.charCodeAt(0) === 45;\n const bn = b.charCodeAt(0) === 45;\n if (an !== bn) return an ? -1 : 1;\n const c = compareMagnitude(an ? a.slice(1) : a, bn ? b.slice(1) : b);\n return an ? -c : c;\n}\n\n// Digits are written into a char-code array from the end and turned into a string once: prepending one digit at a\n// time (`String(d) + out`) allocates a string per digit, which measured 2× slower on Node 24 than on Node 22 and is\n// copy-heavy on engines without ropes.\nfunction digitsToString(codes: number[], from: number): string {\n return stripZeros(String.fromCharCode.apply(null, codes.slice(from)));\n}\n\nfunction addMagnitudes(a: string, b: string): string {\n const codes = new Array<number>(Math.max(a.length, b.length) + 1);\n let k = codes.length;\n let carry = 0;\n for (let i = a.length - 1, j = b.length - 1; i >= 0 || j >= 0 || carry; i--, j--) {\n const s = (i >= 0 ? a.charCodeAt(i) - 48 : 0) + (j >= 0 ? b.charCodeAt(j) - 48 : 0) + carry;\n codes[--k] = 48 + (s % 10);\n carry = s >= 10 ? 1 : 0;\n }\n return digitsToString(codes, k);\n}\n\n/** a − b for magnitudes with a ≥ b. */\nfunction subtractMagnitudes(a: string, b: string): string {\n const codes = new Array<number>(a.length);\n let borrow = 0;\n for (let i = a.length - 1, j = b.length - 1; i >= 0; i--, j--) {\n let d = a.charCodeAt(i) - 48 - borrow - (j >= 0 ? b.charCodeAt(j) - 48 : 0);\n borrow = d < 0 ? 1 : 0;\n if (d < 0) d += 10;\n codes[i] = 48 + d;\n }\n return digitsToString(codes, 0);\n}\n\nfunction signedAdd(an: boolean, am: string, bn: boolean, bm: string): G9Int64 {\n if (an === bn) return make(an, addMagnitudes(am, bm));\n const c = compareMagnitude(am, bm);\n if (c === 0) return '0' as G9Int64;\n return c > 0 ? make(an, subtractMagnitudes(am, bm)) : make(bn, subtractMagnitudes(bm, am));\n}\n\n/** a + b; throws `misuse` on int64 overflow (never wraps silently). */\nexport function addInt64(a: G9Int64, b: G9Int64): G9Int64 {\n const an = a.charCodeAt(0) === 45;\n const bn = b.charCodeAt(0) === 45;\n return signedAdd(an, an ? a.slice(1) : a, bn, bn ? b.slice(1) : b);\n}\n\n/** a − b; throws `misuse` on overflow. */\nexport function subtractInt64(a: G9Int64, b: G9Int64): G9Int64 {\n const an = a.charCodeAt(0) === 45;\n const bn = b.charCodeAt(0) === 45;\n return signedAdd(an, an ? a.slice(1) : a, !bn && b !== '0', bn ? b.slice(1) : b);\n}\n\n/** −a; throws `misuse` for {@link G9_INT64_MIN}, whose negation does not fit. */\nexport function negateInt64(a: G9Int64): G9Int64 {\n if (a === '0') return a;\n return a.charCodeAt(0) === 45 ? make(false, a.slice(1)) : make(true, a);\n}\n"],"names":["G9_INT64_MIN","G9_INT64_MAX","MAX_MAGNITUDE","MIN_MAGNITUDE","DIGITS","compareMagnitude","a","b","stripZeros","digits","i","fits","negative","magnitude","make","G9SqliteError","isInt64","value","m","parseInt64","Number","String","isSafeInt64","int64ToNumber","compareInt64","an","bn","c","digitsToString","codes","from","addMagnitudes","Array","Math","k","carry","j","s","subtractMagnitudes","borrow","d","signedAdd","am","bm","addInt64","subtractInt64","negateInt64"],"mappings":";AAUO,MAAMA,eAAe;AAErB,MAAMC,eAAe;AAE5B,MAAMC,gBAAgB;AACtB,MAAMC,gBAAgB;AACtB,MAAMC,SAAS;AAGf,SAASC,iBAAiBC,CAAS,EAAEC,CAAS;IAC5C,IAAID,EAAE,MAAM,KAAKC,EAAE,MAAM,EAAE,OAAOD,EAAE,MAAM,GAAGC,EAAE,MAAM,GAAG,KAAK;IAC7D,IAAID,MAAMC,GAAG,OAAO;IACpB,OAAOD,IAAIC,IAAI,KAAK;AACtB;AAEA,SAASC,WAAWC,MAAc;IAChC,IAAIC,IAAI;IACR,MAAOA,IAAID,OAAO,MAAM,GAAG,KAAKA,AAAyB,OAAzBA,OAAO,UAAU,CAACC,GAAWA;IAC7D,OAAOD,OAAO,KAAK,CAACC;AACtB;AAEA,SAASC,KAAKC,QAAiB,EAAEC,SAAiB;IAChD,OAAOR,iBAAiBQ,WAAWD,WAAWT,gBAAgBD,kBAAkB;AAClF;AAEA,SAASY,KAAKF,QAAi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,EAAE,KAAK,CAAC,KAAKA,GAAG,CAACoB,MAAMnB,AAAM,QAANA,GAAWmB,KAAKnB,EAAE,KAAK,CAAC,KAAKA;AAChF;AAGO,SAASuC,YAAYxC,CAAU;IACpC,IAAIA,AAAM,QAANA,GAAW,OAAOA;IACtB,OAAOA,AAAoB,OAApBA,EAAE,UAAU,CAAC,KAAYQ,KAAK,OAAOR,EAAE,KAAK,CAAC,MAAMQ,KAAK,MAAMR;AACvE"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A calendar date and wall-clock time with .NET `DateTime` precision (100 ns ticks), no time zone and no offset: the
|
|
3
|
+
* shape MAUI stored in TEXT columns (plan/specs/05-Persistence.md §2). Never converted through `Date` unless asked,
|
|
4
|
+
* so no precision and no zone is invented.
|
|
5
|
+
*/
|
|
6
|
+
export interface G9LocalDateTime {
|
|
7
|
+
readonly year: number;
|
|
8
|
+
readonly month: number;
|
|
9
|
+
readonly day: number;
|
|
10
|
+
readonly hour: number;
|
|
11
|
+
readonly minute: number;
|
|
12
|
+
readonly second: number;
|
|
13
|
+
/** 100-nanosecond ticks within the second, 0 … 9 999 999. */
|
|
14
|
+
readonly ticks: number;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* How a {@link G9LocalDateTime} is written as text.
|
|
18
|
+
* - `provider`: the MAUI connection provider's `yyyy-MM-dd HH:mm:ss.FFFFFFF` (space; trailing fraction zeros and an
|
|
19
|
+
* all-zero fraction omitted). The default.
|
|
20
|
+
* - `sync`: G9SyncData's `yyyy-MM-dd'T'HH:mm:ss.fffffff` (T; always seven digits).
|
|
21
|
+
*/
|
|
22
|
+
export type G9DateTimeWriteProfile = 'provider' | 'sync';
|
|
23
|
+
/** Days in a month of the proleptic Gregorian calendar. */
|
|
24
|
+
export declare function daysInMonth(year: number, month: number): number;
|
|
25
|
+
/** Validates and freezes a {@link G9LocalDateTime}. Throws `misuse` for an impossible date or time. */
|
|
26
|
+
export declare function createLocalDateTime(fields: {
|
|
27
|
+
year: number;
|
|
28
|
+
month: number;
|
|
29
|
+
day: number;
|
|
30
|
+
hour?: number;
|
|
31
|
+
minute?: number;
|
|
32
|
+
second?: number;
|
|
33
|
+
ticks?: number;
|
|
34
|
+
}): G9LocalDateTime;
|
|
35
|
+
/** .NET `default(DateTime)`: 0001-01-01 00:00:00. MAUI used it to mean "not set". */
|
|
36
|
+
export declare const G9_DATETIME_MIN: G9LocalDateTime;
|
|
37
|
+
/**
|
|
38
|
+
* Reads both stored families losslessly: `yyyy-MM-dd HH:mm:ss[.F…]` (provider) and `yyyy-MM-ddTHH:mm:ss[.f…]`
|
|
39
|
+
* (sync), with 0–7 fraction digits. Anything else (an offset, a `Z`, a date alone) is rejected with `misuse`:
|
|
40
|
+
* guessing a zone would silently move the instant.
|
|
41
|
+
*/
|
|
42
|
+
export declare function parseLegacyDateTime(text: string): G9LocalDateTime;
|
|
43
|
+
/** `true` when `text` is a stored date/time {@link parseLegacyDateTime} accepts. */
|
|
44
|
+
export declare function isLegacyDateTimeText(text: string): boolean;
|
|
45
|
+
/** Writes a {@link G9LocalDateTime} in the given profile (default `provider`, the MAUI connection format). */
|
|
46
|
+
export declare function formatLegacyDateTime(value: G9LocalDateTime, profile?: G9DateTimeWriteProfile): string;
|
|
47
|
+
/** Chronological order of two values: negative, zero or positive. */
|
|
48
|
+
export declare function compareLocalDateTime(a: G9LocalDateTime, b: G9LocalDateTime): number;
|
|
49
|
+
/** `true` when both values are the same instant of wall time (tick precision). */
|
|
50
|
+
export declare function localDateTimeEquals(a: G9LocalDateTime, b: G9LocalDateTime): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* The wall-clock fields of an epoch-millisecond instant at a fixed UTC offset (default 0 = UTC, MAUI's
|
|
53
|
+
* `G9SystemClock`). Pure arithmetic: no `Date`, no `Intl`, no time-zone database.
|
|
54
|
+
*/
|
|
55
|
+
export declare function localDateTimeFromEpochMs(epochMs: number, offsetMinutes?: number): G9LocalDateTime;
|
|
56
|
+
/**
|
|
57
|
+
* The epoch milliseconds of a wall-clock value read at a fixed UTC offset (default UTC). Sub-millisecond ticks are
|
|
58
|
+
* truncated: use the value itself when 100 ns precision matters.
|
|
59
|
+
*/
|
|
60
|
+
export declare function localDateTimeToEpochMs(value: G9LocalDateTime, offsetMinutes?: number): number;
|
|
61
|
+
//# sourceMappingURL=local-date-time.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-date-time.d.ts","sourceRoot":"","sources":["../../src/values/local-date-time.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,WAAW,eAAe;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IACrB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,6DAA6D;IAC7D,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;CACxB;AAED;;;;;GAKG;AACH,MAAM,MAAM,sBAAsB,GAAG,UAAU,GAAG,MAAM,CAAC;AASzD,2DAA2D;AAC3D,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAG/D;AAMD,uGAAuG;AACvG,wBAAgB,mBAAmB,CAAC,MAAM,EAAE;IAC1C,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,GAAG,EAAE,MAAM,CAAC;IACZ,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,GAAG,eAAe,CAsBlB;AAED,qFAAqF;AACrF,eAAO,MAAM,eAAe,EAAE,eAAoE,CAAC;AAInG;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,GAAG,eAAe,CAajE;AAED,oFAAoF;AACpF,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAO1D;AAQD,8GAA8G;AAC9G,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,eAAe,EAAE,OAAO,GAAE,sBAAmC,GAAG,MAAM,CAOjH;AAED,qEAAqE;AACrE,wBAAgB,oBAAoB,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,eAAe,GAAG,MAAM,CAUnF;AAED,kFAAkF;AAClF,wBAAgB,mBAAmB,CAAC,CAAC,EAAE,eAAe,EAAE,CAAC,EAAE,eAAe,GAAG,OAAO,CAEnF;AAwBD;;;GAGG;AACH,wBAAgB,wBAAwB,CAAC,OAAO,EAAE,MAAM,EAAE,aAAa,SAAI,GAAG,eAAe,CAe5F;AAED;;;GAGG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,eAAe,EAAE,aAAa,SAAI,GAAG,MAAM,CASxF"}
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
import { G9SqliteError } from "../errors.js";
|
|
2
|
+
const TICKS_PER_MS = 10000;
|
|
3
|
+
const MS_PER_DAY = 86400000;
|
|
4
|
+
function isLeap(year) {
|
|
5
|
+
return year % 4 === 0 && year % 100 !== 0 || year % 400 === 0;
|
|
6
|
+
}
|
|
7
|
+
function daysInMonth(year, month) {
|
|
8
|
+
if (2 === month) return isLeap(year) ? 29 : 28;
|
|
9
|
+
return 4 === month || 6 === month || 9 === month || 11 === month ? 30 : 31;
|
|
10
|
+
}
|
|
11
|
+
function inRange(value, min, max) {
|
|
12
|
+
return Number.isInteger(value) && value >= min && value <= max;
|
|
13
|
+
}
|
|
14
|
+
function createLocalDateTime(fields) {
|
|
15
|
+
var _fields_hour, _fields_minute, _fields_second, _fields_ticks;
|
|
16
|
+
const value = {
|
|
17
|
+
year: fields.year,
|
|
18
|
+
month: fields.month,
|
|
19
|
+
day: fields.day,
|
|
20
|
+
hour: null != (_fields_hour = fields.hour) ? _fields_hour : 0,
|
|
21
|
+
minute: null != (_fields_minute = fields.minute) ? _fields_minute : 0,
|
|
22
|
+
second: null != (_fields_second = fields.second) ? _fields_second : 0,
|
|
23
|
+
ticks: null != (_fields_ticks = fields.ticks) ? _fields_ticks : 0
|
|
24
|
+
};
|
|
25
|
+
if (!inRange(value.year, 1, 9999) || !inRange(value.month, 1, 12) || !inRange(value.day, 1, daysInMonth(value.year, value.month)) || !inRange(value.hour, 0, 23) || !inRange(value.minute, 0, 59) || !inRange(value.second, 0, 59) || !inRange(value.ticks, 0, 9999999)) throw new G9SqliteError('misuse', `Invalid date/time ${JSON.stringify(value)}.`);
|
|
26
|
+
return Object.freeze(value);
|
|
27
|
+
}
|
|
28
|
+
const G9_DATETIME_MIN = createLocalDateTime({
|
|
29
|
+
year: 1,
|
|
30
|
+
month: 1,
|
|
31
|
+
day: 1
|
|
32
|
+
});
|
|
33
|
+
const LEGACY = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})(?:\.(\d{1,7}))?$/;
|
|
34
|
+
function parseLegacyDateTime(text) {
|
|
35
|
+
var _m_;
|
|
36
|
+
const m = LEGACY.exec(text);
|
|
37
|
+
if (!m) throw new G9SqliteError('misuse', `'${text}' is not a stored date/time (yyyy-MM-dd HH:mm:ss[.fffffff]).`);
|
|
38
|
+
const fraction = null != (_m_ = m[7]) ? _m_ : '';
|
|
39
|
+
return createLocalDateTime({
|
|
40
|
+
year: Number(m[1]),
|
|
41
|
+
month: Number(m[2]),
|
|
42
|
+
day: Number(m[3]),
|
|
43
|
+
hour: Number(m[4]),
|
|
44
|
+
minute: Number(m[5]),
|
|
45
|
+
second: Number(m[6]),
|
|
46
|
+
ticks: fraction ? Number(`${fraction}0000000`.slice(0, 7)) : 0
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function isLegacyDateTimeText(text) {
|
|
50
|
+
try {
|
|
51
|
+
parseLegacyDateTime(text);
|
|
52
|
+
return true;
|
|
53
|
+
} catch {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
function pad(value, width) {
|
|
58
|
+
let s = String(value);
|
|
59
|
+
while(s.length < width)s = `0${s}`;
|
|
60
|
+
return s;
|
|
61
|
+
}
|
|
62
|
+
function formatLegacyDateTime(value, profile = 'provider') {
|
|
63
|
+
const date = `${pad(value.year, 4)}-${pad(value.month, 2)}-${pad(value.day, 2)}`;
|
|
64
|
+
const time = `${pad(value.hour, 2)}:${pad(value.minute, 2)}:${pad(value.second, 2)}`;
|
|
65
|
+
const fraction = pad(value.ticks, 7);
|
|
66
|
+
if ('sync' === profile) return `${date}T${time}.${fraction}`;
|
|
67
|
+
const trimmed = fraction.replace(/0+$/, '');
|
|
68
|
+
return trimmed ? `${date} ${time}.${trimmed}` : `${date} ${time}`;
|
|
69
|
+
}
|
|
70
|
+
function compareLocalDateTime(a, b) {
|
|
71
|
+
return a.year - b.year || a.month - b.month || a.day - b.day || a.hour - b.hour || a.minute - b.minute || a.second - b.second || a.ticks - b.ticks;
|
|
72
|
+
}
|
|
73
|
+
function localDateTimeEquals(a, b) {
|
|
74
|
+
return 0 === compareLocalDateTime(a, b);
|
|
75
|
+
}
|
|
76
|
+
function daysFromCivil(year, month, day) {
|
|
77
|
+
const y = month <= 2 ? year - 1 : year;
|
|
78
|
+
const era = Math.floor(y / 400);
|
|
79
|
+
const yoe = y - 400 * era;
|
|
80
|
+
const doy = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;
|
|
81
|
+
const doe = 365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;
|
|
82
|
+
return 146097 * era + doe - 719468;
|
|
83
|
+
}
|
|
84
|
+
function civilFromDays(days) {
|
|
85
|
+
const z = days + 719468;
|
|
86
|
+
const era = Math.floor(z / 146097);
|
|
87
|
+
const doe = z - 146097 * era;
|
|
88
|
+
const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);
|
|
89
|
+
const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));
|
|
90
|
+
const mp = Math.floor((5 * doy + 2) / 153);
|
|
91
|
+
const day = doy - Math.floor((153 * mp + 2) / 5) + 1;
|
|
92
|
+
const month = mp < 10 ? mp + 3 : mp - 9;
|
|
93
|
+
return {
|
|
94
|
+
year: yoe + 400 * era + (month <= 2 ? 1 : 0),
|
|
95
|
+
month,
|
|
96
|
+
day
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
function localDateTimeFromEpochMs(epochMs, offsetMinutes = 0) {
|
|
100
|
+
if (!Number.isFinite(epochMs)) throw new G9SqliteError('misuse', `Epoch ${epochMs} is not finite.`);
|
|
101
|
+
const shifted = Math.floor(epochMs) + 60000 * offsetMinutes;
|
|
102
|
+
const days = Math.floor(shifted / MS_PER_DAY);
|
|
103
|
+
const msOfDay = shifted - days * MS_PER_DAY;
|
|
104
|
+
const { year, month, day } = civilFromDays(days);
|
|
105
|
+
return createLocalDateTime({
|
|
106
|
+
year,
|
|
107
|
+
month,
|
|
108
|
+
day,
|
|
109
|
+
hour: Math.floor(msOfDay / 3600000),
|
|
110
|
+
minute: Math.floor(msOfDay / 60000) % 60,
|
|
111
|
+
second: Math.floor(msOfDay / 1000) % 60,
|
|
112
|
+
ticks: msOfDay % 1000 * TICKS_PER_MS
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
function localDateTimeToEpochMs(value, offsetMinutes = 0) {
|
|
116
|
+
const days = daysFromCivil(value.year, value.month, value.day);
|
|
117
|
+
const ms = days * MS_PER_DAY + 3600000 * value.hour + 60000 * value.minute + 1000 * value.second + Math.floor(value.ticks / TICKS_PER_MS);
|
|
118
|
+
return ms - 60000 * offsetMinutes;
|
|
119
|
+
}
|
|
120
|
+
export { G9_DATETIME_MIN, compareLocalDateTime, createLocalDateTime, daysInMonth, formatLegacyDateTime, isLegacyDateTimeText, localDateTimeEquals, localDateTimeFromEpochMs, localDateTimeToEpochMs, parseLegacyDateTime };
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=local-date-time.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"values/local-date-time.js","sources":["../../src/values/local-date-time.ts"],"sourcesContent":["import { G9SqliteError } from '../errors';\n\n/**\n * A calendar date and wall-clock time with .NET `DateTime` precision (100 ns ticks), no time zone and no offset: the\n * shape MAUI stored in TEXT columns (plan/specs/05-Persistence.md §2). Never converted through `Date` unless asked,\n * so no precision and no zone is invented.\n */\nexport interface G9LocalDateTime {\n readonly year: number;\n readonly month: number;\n readonly day: number;\n readonly hour: number;\n readonly minute: number;\n readonly second: number;\n /** 100-nanosecond ticks within the second, 0 … 9 999 999. */\n readonly ticks: number;\n}\n\n/**\n * How a {@link G9LocalDateTime} is written as text.\n * - `provider`: the MAUI connection provider's `yyyy-MM-dd HH:mm:ss.FFFFFFF` (space; trailing fraction zeros and an\n * all-zero fraction omitted). The default.\n * - `sync`: G9SyncData's `yyyy-MM-dd'T'HH:mm:ss.fffffff` (T; always seven digits).\n */\nexport type G9DateTimeWriteProfile = 'provider' | 'sync';\n\nconst TICKS_PER_MS = 10_000;\nconst MS_PER_DAY = 86_400_000;\n\nfunction isLeap(year: number): boolean {\n return (year % 4 === 0 && year % 100 !== 0) || year % 400 === 0;\n}\n\n/** Days in a month of the proleptic Gregorian calendar. */\nexport function daysInMonth(year: number, month: number): number {\n if (month === 2) return isLeap(year) ? 29 : 28;\n return month === 4 || month === 6 || month === 9 || month === 11 ? 30 : 31;\n}\n\nfunction inRange(value: number, min: number, max: number): boolean {\n return Number.isInteger(value) && value >= min && value <= max;\n}\n\n/** Validates and freezes a {@link G9LocalDateTime}. Throws `misuse` for an impossible date or time. */\nexport function createLocalDateTime(fields: {\n year: number;\n month: number;\n day: number;\n hour?: number;\n minute?: number;\n second?: number;\n ticks?: number;\n}): G9LocalDateTime {\n const value = {\n year: fields.year,\n month: fields.month,\n day: fields.day,\n hour: fields.hour ?? 0,\n minute: fields.minute ?? 0,\n second: fields.second ?? 0,\n ticks: fields.ticks ?? 0,\n };\n if (\n !inRange(value.year, 1, 9999) ||\n !inRange(value.month, 1, 12) ||\n !inRange(value.day, 1, daysInMonth(value.year, value.month)) ||\n !inRange(value.hour, 0, 23) ||\n !inRange(value.minute, 0, 59) ||\n !inRange(value.second, 0, 59) ||\n !inRange(value.ticks, 0, 9_999_999)\n ) {\n throw new G9SqliteError('misuse', `Invalid date/time ${JSON.stringify(value)}.`);\n }\n return Object.freeze(value);\n}\n\n/** .NET `default(DateTime)`: 0001-01-01 00:00:00. MAUI used it to mean \"not set\". */\nexport const G9_DATETIME_MIN: G9LocalDateTime = createLocalDateTime({ year: 1, month: 1, day: 1 });\n\nconst LEGACY = /^(\\d{4})-(\\d{2})-(\\d{2})[ T](\\d{2}):(\\d{2}):(\\d{2})(?:\\.(\\d{1,7}))?$/;\n\n/**\n * Reads both stored families losslessly: `yyyy-MM-dd HH:mm:ss[.F…]` (provider) and `yyyy-MM-ddTHH:mm:ss[.f…]`\n * (sync), with 0–7 fraction digits. Anything else (an offset, a `Z`, a date alone) is rejected with `misuse`:\n * guessing a zone would silently move the instant.\n */\nexport function parseLegacyDateTime(text: string): G9LocalDateTime {\n const m = LEGACY.exec(text);\n if (!m) throw new G9SqliteError('misuse', `'${text}' is not a stored date/time (yyyy-MM-dd HH:mm:ss[.fffffff]).`);\n const fraction = m[7] ?? '';\n return createLocalDateTime({\n year: Number(m[1]),\n month: Number(m[2]),\n day: Number(m[3]),\n hour: Number(m[4]),\n minute: Number(m[5]),\n second: Number(m[6]),\n ticks: fraction ? Number(`${fraction}0000000`.slice(0, 7)) : 0,\n });\n}\n\n/** `true` when `text` is a stored date/time {@link parseLegacyDateTime} accepts. */\nexport function isLegacyDateTimeText(text: string): boolean {\n try {\n parseLegacyDateTime(text);\n return true;\n } catch {\n return false;\n }\n}\n\nfunction pad(value: number, width: number): string {\n let s = String(value);\n while (s.length < width) s = `0${s}`;\n return s;\n}\n\n/** Writes a {@link G9LocalDateTime} in the given profile (default `provider`, the MAUI connection format). */\nexport function formatLegacyDateTime(value: G9LocalDateTime, profile: G9DateTimeWriteProfile = 'provider'): string {\n const date = `${pad(value.year, 4)}-${pad(value.month, 2)}-${pad(value.day, 2)}`;\n const time = `${pad(value.hour, 2)}:${pad(value.minute, 2)}:${pad(value.second, 2)}`;\n const fraction = pad(value.ticks, 7);\n if (profile === 'sync') return `${date}T${time}.${fraction}`;\n const trimmed = fraction.replace(/0+$/, '');\n return trimmed ? `${date} ${time}.${trimmed}` : `${date} ${time}`;\n}\n\n/** Chronological order of two values: negative, zero or positive. */\nexport function compareLocalDateTime(a: G9LocalDateTime, b: G9LocalDateTime): number {\n return (\n a.year - b.year ||\n a.month - b.month ||\n a.day - b.day ||\n a.hour - b.hour ||\n a.minute - b.minute ||\n a.second - b.second ||\n a.ticks - b.ticks\n );\n}\n\n/** `true` when both values are the same instant of wall time (tick precision). */\nexport function localDateTimeEquals(a: G9LocalDateTime, b: G9LocalDateTime): boolean {\n return compareLocalDateTime(a, b) === 0;\n}\n\n/** Days from 1970-01-01 to the given civil date (Howard Hinnant's algorithm; no `Date`). */\nfunction daysFromCivil(year: number, month: number, day: number): number {\n const y = month <= 2 ? year - 1 : year;\n const era = Math.floor(y / 400);\n const yoe = y - era * 400;\n const doy = Math.floor((153 * (month + (month > 2 ? -3 : 9)) + 2) / 5) + day - 1;\n const doe = yoe * 365 + Math.floor(yoe / 4) - Math.floor(yoe / 100) + doy;\n return era * 146097 + doe - 719468;\n}\n\nfunction civilFromDays(days: number): { year: number; month: number; day: number } {\n const z = days + 719468;\n const era = Math.floor(z / 146097);\n const doe = z - era * 146097;\n const yoe = Math.floor((doe - Math.floor(doe / 1460) + Math.floor(doe / 36524) - Math.floor(doe / 146096)) / 365);\n const doy = doe - (365 * yoe + Math.floor(yoe / 4) - Math.floor(yoe / 100));\n const mp = Math.floor((5 * doy + 2) / 153);\n const day = doy - Math.floor((153 * mp + 2) / 5) + 1;\n const month = mp < 10 ? mp + 3 : mp - 9;\n return { year: yoe + era * 400 + (month <= 2 ? 1 : 0), month, day };\n}\n\n/**\n * The wall-clock fields of an epoch-millisecond instant at a fixed UTC offset (default 0 = UTC, MAUI's\n * `G9SystemClock`). Pure arithmetic: no `Date`, no `Intl`, no time-zone database.\n */\nexport function localDateTimeFromEpochMs(epochMs: number, offsetMinutes = 0): G9LocalDateTime {\n if (!Number.isFinite(epochMs)) throw new G9SqliteError('misuse', `Epoch ${epochMs} is not finite.`);\n const shifted = Math.floor(epochMs) + offsetMinutes * 60_000;\n const days = Math.floor(shifted / MS_PER_DAY);\n const msOfDay = shifted - days * MS_PER_DAY;\n const { year, month, day } = civilFromDays(days);\n return createLocalDateTime({\n year,\n month,\n day,\n hour: Math.floor(msOfDay / 3_600_000),\n minute: Math.floor(msOfDay / 60_000) % 60,\n second: Math.floor(msOfDay / 1000) % 60,\n ticks: (msOfDay % 1000) * TICKS_PER_MS,\n });\n}\n\n/**\n * The epoch milliseconds of a wall-clock value read at a fixed UTC offset (default UTC). Sub-millisecond ticks are\n * truncated: use the value itself when 100 ns precision matters.\n */\nexport function localDateTimeToEpochMs(value: G9LocalDateTime, offsetMinutes = 0): number {\n const days = daysFromCivil(value.year, value.month, value.day);\n const ms =\n days * MS_PER_DAY +\n value.hour * 3_600_000 +\n value.minute * 60_000 +\n value.second * 1000 +\n Math.floor(value.ticks / TICKS_PER_MS);\n return ms - offsetMinutes * 60_000;\n}\n"],"names":["TICKS_PER_MS","MS_PER_DAY","isLeap","year","daysInMonth","month","inRange","value","min","max","Number","createLocalDateTime","fields","_fields_hour","_fields_minute","_fields_second","_fields_ticks","G9SqliteError","JSON","Object","G9_DATETIME_MIN","LEGACY","parseLegacyDateTime","text","_m_","m","fraction","isLegacyDateTimeText","pad","width","s","String","formatLegacyDateTime","profile","date","time","trimmed","compareLocalDateTime","a","b","localDateTimeEquals","daysFromCivil","day","y","era","Math","yoe","doy","doe","civilFromDays","days","z","mp","localDateTimeFromEpochMs","epochMs","offsetMinutes","shifted","msOfDay","localDateTimeToEpochMs","ms"],"mappings":";AA0BA,MAAMA,eAAe;AACrB,MAAMC,aAAa;AAEnB,SAASC,OAAOC,IAAY;IAC1B,OAAQA,OAAO,MAAM,KAAKA,OAAO,QAAQ,KAAMA,OAAO,QAAQ;AAChE;AAGO,SAASC,YAAYD,IAAY,EAAEE,KAAa;IACrD,IAAIA,AAAU,MAAVA,OAAa,OAAOH,OAAOC,QAAQ,KAAK;IAC5C,OAAOE,AAAU,MAAVA,SAAeA,AAAU,MAAVA,SAAeA,AAAU,MAAVA,SAAeA,AAAU,OAAVA,QAAe,KAAK;AAC1E;AAEA,SAASC,QAAQC,KAAa,EAAEC,GAAW,EAAEC,GAAW;IACtD,OAAOC,OAAO,SAAS,CAACH,UAAUA,SAASC,OAAOD,SAASE;AAC7D;AAGO,SAASE,oBAAoBC,MAQnC;QAKSC,cACEC,gBACAC,gBACDC;IAPT,MAAMT,QAAQ;QACZ,MAAMK,OAAO,IAAI;QACjB,OAAOA,OAAO,KAAK;QACnB,KAAKA,OAAO,GAAG;QACf,MAAM,QAAAC,CAAAA,eAAAA,OAAO,IAAI,AAAD,IAAVA,eAAe;QACrB,QAAQ,QAAAC,CAAAA,iBAAAA,OAAO,MAAM,AAAD,IAAZA,iBAAiB;QACzB,QAAQ,QAAAC,CAAAA,iBAAAA,OAAO,MAAM,AAAD,IAAZA,iBAAiB;QACzB,OAAO,QAAAC,CAAAA,gBAAAA,OAAO,KAAK,AAAD,IAAXA,gBAAgB;IACzB;IACA,IACE,CAACV,QAAQC,MAAM,IAAI,EAAE,GAAG,SACxB,CAACD,QAAQC,MAAM,KAAK,EAAE,GAAG,OACzB,CAACD,QAAQC,MAAM,GAAG,EAAE,GAAGH,YAAYG,MAAM,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AACS,WAAWC,AAAgB,QAAhBA;IACtC,MAAML,OAAOL,KAAK,KAAK,CAACW,UAAUvD;IAClC,MAAMwD,UAAUD,UAAUN,OAAOjD;IACjC,MAAM,EAAEE,IAAI,EAAEE,KAAK,EAAEqC,GAAG,EAAE,GAAGO,cAAcC;IAC3C,OAAOvC,oBAAoB;QACzBR;QACAE;QACAqC;QACA,MAAMG,KAAK,KAAK,CAACY,UAAU;QAC3B,QAAQZ,KAAK,KAAK,CAACY,UAAU,SAAU;QACvC,QAAQZ,KAAK,KAAK,CAACY,UAAU,QAAQ;QACrC,OAAQA,UAAU,OAAQzD;IAC5B;AACF;AAMO,SAAS0D,uBAAuBnD,KAAsB,EAAEgD,gBAAgB,CAAC;IAC9E,MAAML,OAAOT,cAAclC,MAAM,IAAI,EAAEA,MAAM,KAAK,EAAEA,MAAM,GAAG;IAC7D,MAAMoD,KACJT,OAAOjD,aACPM,AAAa,UAAbA,MAAM,IAAI,GACVA,AAAe,QAAfA,MAAM,MAAM,GACZA,AAAe,OAAfA,MAAM,MAAM,GACZsC,KAAK,KAAK,CAACtC,MAAM,KAAK,GAAGP;IAC3B,OAAO2D,KAAKJ,AAAgB,QAAhBA;AACd"}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type G9Int64 } from './int64.js';
|
|
2
|
+
/** An int64 outside ±(2^53 − 1), tagged so it is never mistaken for text. */
|
|
3
|
+
export interface G9Int64Tag {
|
|
4
|
+
readonly int64: G9Int64;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* A value crossing the driver boundary (plan/specs/05-Persistence.md §3). Integers inside ±(2^53 − 1) are plain
|
|
8
|
+
* numbers (the fast path); larger ones are {@link G9Int64Tag}s. `null` is SQL NULL. A BLOB is an `ArrayBuffer`
|
|
9
|
+
* (empty ≠ NULL ≠ empty text).
|
|
10
|
+
*/
|
|
11
|
+
export type G9SqlValue = null | number | string | ArrayBuffer | G9Int64Tag;
|
|
12
|
+
/** `true` for a {@link G9Int64Tag}. */
|
|
13
|
+
export declare function isInt64Tag(value: unknown): value is G9Int64Tag;
|
|
14
|
+
/** `true` when `value` is a valid {@link G9SqlValue}. `NaN` is not (SQLite stores it as NULL, silently). */
|
|
15
|
+
export declare function isSqlValue(value: unknown): value is G9SqlValue;
|
|
16
|
+
/**
|
|
17
|
+
* The driver-boundary form of an int64: a plain number when safe, a tag otherwise. Accepts a canonical
|
|
18
|
+
* {@link G9Int64}, any decimal integer string, or a safe integer.
|
|
19
|
+
*/
|
|
20
|
+
export declare function int64ToSqlValue(value: G9Int64 | string | number): number | G9Int64Tag;
|
|
21
|
+
/**
|
|
22
|
+
* Reads an integer {@link G9SqlValue} as a canonical {@link G9Int64}. Throws `misuse` for anything that is not an
|
|
23
|
+
* integer (a REAL with a fraction, text, a BLOB).
|
|
24
|
+
*/
|
|
25
|
+
export declare function int64FromSqlValue(value: G9SqlValue): G9Int64;
|
|
26
|
+
/** Copies the bytes of a typed array or buffer into a standalone `ArrayBuffer`. */
|
|
27
|
+
export declare function toArrayBuffer(bytes: ArrayBuffer | ArrayBufferView): ArrayBuffer;
|
|
28
|
+
/** A short, privacy-safe description of a value's kind for error messages. */
|
|
29
|
+
export declare function describeSqlValue(value: unknown): string;
|
|
30
|
+
/** Structural equality of two {@link G9SqlValue}s (BLOBs by bytes, int64 tags by value). */
|
|
31
|
+
export declare function sqlValueEquals(a: G9SqlValue, b: G9SqlValue): boolean;
|
|
32
|
+
/** Lower-case hex of a BLOB (diagnostics and SQL literals). */
|
|
33
|
+
export declare function bytesToHex(bytes: ArrayBuffer): string;
|
|
34
|
+
/** Parses hex (any case, even length) into an `ArrayBuffer`. */
|
|
35
|
+
export declare function hexToBytes(hex: string): ArrayBuffer;
|
|
36
|
+
//# sourceMappingURL=sql-value.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sql-value.d.ts","sourceRoot":"","sources":["../../src/values/sql-value.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,OAAO,EAAoC,MAAM,SAAS,CAAC;AAEzE,6EAA6E;AAC7E,MAAM,WAAW,UAAU;IACzB,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAC;CACzB;AAED;;;;GAIG;AACH,MAAM,MAAM,UAAU,GAAG,IAAI,GAAG,MAAM,GAAG,MAAM,GAAG,WAAW,GAAG,UAAU,CAAC;AAE3E,uCAAuC;AACvC,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAO9D;AAED,4GAA4G;AAC5G,wBAAgB,UAAU,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,UAAU,CAI9D;AAED;;;GAGG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,UAAU,CAGrF;AAED;;;GAGG;AACH,wBAAgB,iBAAiB,CAAC,KAAK,EAAE,UAAU,GAAG,OAAO,CAS5D;AAED,mFAAmF;AACnF,wBAAgB,aAAa,CAAC,KAAK,EAAE,WAAW,GAAG,eAAe,GAAG,WAAW,CAK/E;AAED,8EAA8E;AAC9E,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAQvD;AAUD,4FAA4F;AAC5F,wBAAgB,cAAc,CAAC,CAAC,EAAE,UAAU,EAAE,CAAC,EAAE,UAAU,GAAG,OAAO,CAQpE;AAID,+DAA+D;AAC/D,wBAAgB,UAAU,CAAC,KAAK,EAAE,WAAW,GAAG,MAAM,CAQrD;AAED,gEAAgE;AAChE,wBAAgB,UAAU,CAAC,GAAG,EAAE,MAAM,GAAG,WAAW,CAOnD"}
|