@nextera.one/tps-standard 0.5.1 → 0.5.2

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/index.d.ts CHANGED
@@ -12,17 +12,35 @@
12
12
  * - Added structural anchors (bldg, floor, room, zone)
13
13
  * - Added geospatial cell systems (S2, H3, Plus Code, what3words)
14
14
  */
15
- export type CalendarCode = 'greg' | 'hij' | 'jul' | 'holo' | 'unix';
15
+ export declare const DefaultCalendars: {
16
+ readonly TPS: "tps";
17
+ readonly GREG: "greg";
18
+ readonly HIJ: "hij";
19
+ readonly JUL: "jul";
20
+ readonly HOLO: "holo";
21
+ readonly UNIX: "unix";
22
+ };
23
+ /**
24
+ * Specifies the direction of the time-component hierarchy when serializing or
25
+ * deserializing a TPS string. The default is `'descending'` (millennium → … →
26
+ * second), but `'ascending'` produces the reverse order.
27
+ */
28
+ export declare enum TimeOrder {
29
+ DESC = "desc",
30
+ ASC = "asc"
31
+ }
16
32
  export interface TPSComponents {
17
- calendar: CalendarCode;
18
- millennium?: number;
19
- century?: number;
20
- year?: number;
21
- month?: number;
22
- day?: number;
23
- hour?: number;
24
- minute?: number;
25
- second?: number;
33
+ calendar: string;
34
+ millennium: number;
35
+ century: number;
36
+ year: number;
37
+ month: number;
38
+ day: number;
39
+ hour: number;
40
+ minute: number;
41
+ second: number;
42
+ /** Sub-second precision (0–999). Encoded as the last `m` token. */
43
+ millisecond: number;
26
44
  unixSeconds?: number;
27
45
  latitude?: number;
28
46
  longitude?: number;
@@ -43,6 +61,8 @@ export interface TPSComponents {
43
61
  room?: string;
44
62
  /** Logical area within building */
45
63
  zone?: string;
64
+ /** Raw pre-@ space anchor (e.g. adm:city:SA:riyadh, node:api-1, net:ip4:203.0.113.10) */
65
+ spaceAnchor?: string;
46
66
  /** Technical missing data (e.g. server log without GPS) */
47
67
  isUnknownLocation?: boolean;
48
68
  /** Removed for legal/security reasons (e.g. GDPR) */
@@ -54,6 +74,7 @@ export interface TPSComponents {
54
74
  /** Verification hash appended to time (e.g. "sha256:8f3e2a...") */
55
75
  signature?: string;
56
76
  extensions?: Record<string, string>;
77
+ order?: TimeOrder;
57
78
  }
58
79
  /**
59
80
  * Interface for Calendar Driver plugins.
@@ -106,30 +127,30 @@ export interface TPSComponents {
106
127
  */
107
128
  export interface CalendarDriver {
108
129
  /** The calendar code this driver handles (e.g., 'hij', 'jul'). */
109
- readonly code: CalendarCode;
130
+ readonly code: string;
110
131
  /**
111
132
  * Human-readable name for this calendar (optional).
112
133
  * @example "Hijri (Islamic)"
113
134
  */
114
135
  readonly name?: string;
115
136
  /**
116
- * Converts a Gregorian Date to this calendar's components.
137
+ * Converts a Date to this calendar's components.
117
138
  * @param date - The Gregorian Date object.
118
139
  * @returns Partial TPS components for year, month, day, etc.
119
140
  */
120
- fromGregorian(date: Date): Partial<TPSComponents>;
141
+ getComponentsFromDate(date: Date): Partial<TPSComponents>;
121
142
  /**
122
- * Converts this calendar's components to a Gregorian Date.
143
+ * Converts this calendar's components to a Date.
123
144
  * @param components - Partial TPS components (year, month, day, etc.).
124
145
  * @returns A JavaScript Date object.
125
146
  */
126
- toGregorian(components: Partial<TPSComponents>): Date;
147
+ getDateFromComponents(components: Partial<TPSComponents>): Date;
127
148
  /**
128
149
  * Generates a TPS time string for this calendar from a Date.
129
150
  * @param date - The Gregorian Date object.
130
- * @returns A TPS time string (e.g., "T:hij.y1447.M07.d21...").
151
+ * @returns A TPS time string (e.g., "T:hij.y1447.m07.d21...").
131
152
  */
132
- fromDate(date: Date): string;
153
+ getFromDate(date: Date): string;
133
154
  /**
134
155
  * Parse a calendar-specific date string into TPS components.
135
156
  * This allows drivers to handle native date formats from external libraries.
@@ -147,7 +168,7 @@ export interface CalendarDriver {
147
168
  * driver.parseDate('1447-07-21 14:30:00'); // → { year: 1447, month: 7, day: 21, hour: 14, ... }
148
169
  * ```
149
170
  */
150
- parseDate?(input: string, format?: string): Partial<TPSComponents>;
171
+ parseDate(input: string, format?: string): Partial<TPSComponents>;
151
172
  /**
152
173
  * Format TPS components to a calendar-specific date string.
153
174
  * Inverse of parseDate().
@@ -162,7 +183,7 @@ export interface CalendarDriver {
162
183
  * driver.format({ year: 1447, month: 7, day: 21 }, 'short'); // → '21/7/1447'
163
184
  * ```
164
185
  */
165
- format?(components: Partial<TPSComponents>, format?: string): string;
186
+ format(components: Partial<TPSComponents>, format?: string): string;
166
187
  /**
167
188
  * Validate a calendar-specific date string or components.
168
189
  *
@@ -175,7 +196,7 @@ export interface CalendarDriver {
175
196
  * driver.validate({ year: 1447, month: 7, day: 31 }); // → false (Rajab has 30 days)
176
197
  * ```
177
198
  */
178
- validate?(input: string | Partial<TPSComponents>): boolean;
199
+ validate(input: string | Partial<TPSComponents>): boolean;
179
200
  /**
180
201
  * Get calendar metadata (month names, day names, etc.).
181
202
  * Useful for UI rendering.
@@ -186,7 +207,7 @@ export interface CalendarDriver {
186
207
  * // → ['Muharram', 'Safar', 'Rabi I', ...]
187
208
  * ```
188
209
  */
189
- getMetadata?(): CalendarMetadata;
210
+ getMetadata(): CalendarMetadata;
190
211
  }
191
212
  /**
192
213
  * Metadata about a calendar system.
@@ -221,9 +242,18 @@ export declare class TPS {
221
242
  * @param code - The calendar code.
222
243
  * @returns The driver or undefined.
223
244
  */
224
- static getDriver(code: CalendarCode): CalendarDriver | undefined;
245
+ static getDriver(code: string): CalendarDriver | undefined;
225
246
  private static readonly REGEX_URI;
226
247
  private static readonly REGEX_TIME;
248
+ /**
249
+ * SANITIZER: Normalises a raw TPS input string before validation.
250
+ *
251
+ * Pure string-based — no parsing into components, no regex beyond simple
252
+ * character checks, no re-serialisation via buildTimePart / toURI.
253
+ *
254
+ * Token ranks (descending): m(8) c(7) y(6) m(5) d(4) h(3) m(2) s(1) m(0)
255
+ */
256
+ static sanitizeTimeInput(input: string): string;
227
257
  static validate(input: string): boolean;
228
258
  static parse(input: string): TPSComponents | null;
229
259
  /**
@@ -236,10 +266,14 @@ export declare class TPS {
236
266
  * CONVERTER: Creates a TPS Time Object string from a JavaScript Date.
237
267
  * Supports plugin drivers for non-Gregorian calendars.
238
268
  * @param date - The JS Date object (defaults to Now).
239
- * @param calendar - The target calendar driver (default 'greg').
240
- * @returns Canonical string (e.g., "T:greg.m3.c1.y26...").
269
+ * @param calendar - The target calendar driver (default `"tps"`).
270
+ * @param opts - Optional parameters; for built-in calendars the only
271
+ * supported key is `order` which may be `'ascending'` or `'descending'`.
272
+ * @returns Canonical string (e.g., "T:tps.m3.c1.y26...").
241
273
  */
242
- static fromDate(date?: Date, calendar?: CalendarCode): string;
274
+ static fromDate(date?: Date, calendar?: string, opts?: {
275
+ order?: TimeOrder;
276
+ }): string;
243
277
  /**
244
278
  * CONVERTER: Converts a TPS string to a Date in a target calendar format.
245
279
  * Uses plugin drivers for cross-calendar conversion.
@@ -247,7 +281,7 @@ export declare class TPS {
247
281
  * @param targetCalendar - The target calendar code (e.g., 'hij').
248
282
  * @returns A TPS string in the target calendar, or null if invalid.
249
283
  */
250
- static to(targetCalendar: CalendarCode, tpsString: string): string | null;
284
+ static to(targetCalendar: string, tpsString: string): string | null;
251
285
  /**
252
286
  * CONVERTER: Reconstructs a JavaScript Date object from a TPS string.
253
287
  * Supports plugin drivers for non-Gregorian calendars.
@@ -257,7 +291,7 @@ export declare class TPS {
257
291
  static toDate(tpsString: string): Date | null;
258
292
  /**
259
293
  * Parse a calendar-specific date string into TPS components.
260
- * Requires the driver to implement the optional `parseDate` method.
294
+ * Requires the driver to implement `parseDate`.
261
295
  *
262
296
  * @param calendar - The calendar code (e.g., 'hij')
263
297
  * @param dateString - Date string in calendar-native format (e.g., '1447-07-21')
@@ -270,10 +304,10 @@ export declare class TPS {
270
304
  * // { calendar: 'hij', year: 1447, month: 7, day: 21 }
271
305
  *
272
306
  * const uri = TPS.toURI({ ...components, latitude: 31.95, longitude: 35.91 });
273
- * // "tps://31.95,35.91@T:hij.y1447.M07.d21"
307
+ * // "tps://31.95,35.91@T:hij.y1447.m07.d21"
274
308
  * ```
275
309
  */
276
- static parseCalendarDate(calendar: CalendarCode, dateString: string, format?: string): Partial<TPSComponents> | null;
310
+ static parseCalendarDate(calendar: string, dateString: string, format?: string): Partial<TPSComponents> | null;
277
311
  /**
278
312
  * Convert a calendar-specific date string directly to a TPS URI.
279
313
  * This is a convenience method that combines parseDate + toURI.
@@ -287,18 +321,18 @@ export declare class TPS {
287
321
  * ```ts
288
322
  * // With coordinates
289
323
  * TPS.fromCalendarDate('hij', '1447-07-21', { latitude: 31.95, longitude: 35.91 });
290
- * // "tps://31.95,35.91@T:hij.y1447.M07.d21"
324
+ * // "tps://31.95,35.91@T:hij.y1447.m07.d21"
291
325
  *
292
326
  * // With privacy flag
293
327
  * TPS.fromCalendarDate('hij', '1447-07-21', { isHiddenLocation: true });
294
- * // "tps://hidden@T:hij.y1447.M07.d21"
328
+ * // "tps://hidden@T:hij.y1447.m07.d21"
295
329
  *
296
330
  * // Without location
297
331
  * TPS.fromCalendarDate('hij', '1447-07-21');
298
- * // "tps://unknown@T:hij.y1447.M07.d21"
332
+ * // "tps://unknown@T:hij.y1447.m07.d21"
299
333
  * ```
300
334
  */
301
- static fromCalendarDate(calendar: CalendarCode, dateString: string, location?: {
335
+ static fromCalendarDate(calendar: string, dateString: string, location?: {
302
336
  latitude?: number;
303
337
  longitude?: number;
304
338
  altitude?: number;
@@ -308,7 +342,7 @@ export declare class TPS {
308
342
  }): string;
309
343
  /**
310
344
  * Format TPS components to a calendar-specific date string.
311
- * Requires the driver to implement the optional `format` method.
345
+ * Requires the driver to implement `format`.
312
346
  *
313
347
  * @param calendar - The calendar code
314
348
  * @param components - TPS components to format
@@ -317,21 +351,61 @@ export declare class TPS {
317
351
  *
318
352
  * @example
319
353
  * ```ts
320
- * const tps = TPS.parse('tps://unknown@T:hij.y1447.M07.d21');
354
+ * const tps = TPS.parse('tps://unknown@T:hij.y1447.m07.d21');
321
355
  * const formatted = TPS.formatCalendarDate('hij', tps);
322
356
  * // "1447-07-21"
323
357
  * ```
324
358
  */
325
- static formatCalendarDate(calendar: CalendarCode, components: Partial<TPSComponents>, format?: string): string;
359
+ static formatCalendarDate(calendar: string, components: Partial<TPSComponents>, format?: string): string;
360
+ /**
361
+ * Generate the canonical `T:` time string for a set of components. The
362
+ * `order` field (or `comp.order`) controls whether tokens are emitted in
363
+ * ascending or descending hierarchy; if undefined the default
364
+ * `'descending'` orientation is used.
365
+ *
366
+ * Drivers may ignore this helper and produce their own time strings if they
367
+ * implement custom ordering logic.
368
+ */
369
+ static buildTimePart(comp: TPSComponents): string;
370
+ /**
371
+ * Parse the *time* portion of a TPS string (optionally beginning with
372
+ * `T:`) into components and determine the component ordering. This helper
373
+ * accepts tokens in **any** sequence and will return an `order` value of
374
+ * `'ascending'` or `'descending'`.
375
+ *
376
+ * The caller is responsible for stripping off a leading signature or other
377
+ * trailer characters; this method will drop anything after `!`, `;`, `?` or
378
+ * `#`.
379
+ *
380
+ * ### `m`-token disambiguation
381
+ * All four of millennium (rank 8), month (rank 5), minute (rank 2) and
382
+ * millisecond (rank 0) share the single-character prefix `m`. They are told
383
+ * apart by their **position relative to the neighbouring tokens**. The
384
+ * algorithm is:
385
+ *
386
+ * 1. Pre-scan the non-`m` tokens (c, y, d, h, s) whose ranks are fixed to
387
+ * determine whether the string is ascending or descending.
388
+ * 2. While iterating, track `lastAssignedRank` – the rank of the most
389
+ * recently processed token (m or non-m).
390
+ * 3. When an `m` token is encountered, derive its rank from `lastAssignedRank`
391
+ * and the detected order:
392
+ * - **DESC** null → 8 (mill) | rank > 5 → 5 (month) | rank > 2 → 2 (min) | else → 0 (ms)
393
+ * - **ASC** null → 0 (ms) | rank < 2 → 2 (min) | rank < 5 → 5 (month) | else → 8 (mill)
394
+ *
395
+ * @param input - Time fragment (e.g. `"T:greg.m3.c1.y26"` or `"greg.m0.s25.m30"`)
396
+ */
397
+ static parseTimeString(input: string): {
398
+ components: Partial<TPSComponents>;
399
+ order: TimeOrder;
400
+ } | null;
326
401
  private static _mapGroupsToComponents;
327
- private static pad;
328
402
  }
329
403
  /**
330
404
  * Decoded result from TPSUID7RB binary format.
331
405
  */
332
406
  export type TPSUID7RBDecodeResult = {
333
407
  /** Version identifier */
334
- version: 'tpsuid7rb';
408
+ version: "tpsuid7rb";
335
409
  /** Epoch milliseconds (UTC) */
336
410
  epochMs: number;
337
411
  /** Whether the TPS payload was compressed */
@@ -370,7 +444,7 @@ export type TPSUID7RBEncodeOptions = {
370
444
  *
371
445
  * @example
372
446
  * ```ts
373
- * const tps = 'tps://31.95,35.91@T:greg.m3.c1.y26.M01.d09';
447
+ * const tps = 'tps://31.95,35.91@T:greg.m3.c1.y26.m01.d09';
374
448
  *
375
449
  * // Encode to binary
376
450
  * const bytes = TPSUID7RB.encodeBinary(tps);
@@ -444,6 +518,7 @@ export declare class TPSUID7RB {
444
518
  longitude?: number;
445
519
  altitude?: number;
446
520
  compress?: boolean;
521
+ order?: TimeOrder;
447
522
  }): string;
448
523
  /**
449
524
  * Generate a TPS string from a Date and optional location.
@@ -494,3 +569,61 @@ export declare class TPSUID7RB {
494
569
  /** Generate cryptographically secure random bytes */
495
570
  private static randomBytes;
496
571
  }
572
+ /**
573
+ * `TpsDate` is a Date-like wrapper with native TPS conversion helpers.
574
+ *
575
+ * It mirrors common JavaScript `Date` construction patterns:
576
+ * - `new TpsDate()`
577
+ * - `new TpsDate(ms)`
578
+ * - `new TpsDate(isoString)`
579
+ * - `new TpsDate(tpsString)`
580
+ * - `new TpsDate(year, monthIndex, day?, hour?, minute?, second?, ms?)`
581
+ */
582
+ export declare class TpsDate {
583
+ private readonly internal;
584
+ private getTpsComponents;
585
+ private getTpsFullYear;
586
+ constructor();
587
+ constructor(value: string | number | Date | TpsDate);
588
+ constructor(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, ms?: number);
589
+ private static looksLikeTPS;
590
+ static now(): number;
591
+ static parse(input: string): number;
592
+ static UTC(year: number, monthIndex: number, day?: number, hours?: number, minutes?: number, seconds?: number, ms?: number): number;
593
+ static fromTPS(tps: string): TpsDate;
594
+ toGregorianDate(): Date;
595
+ toDate(): Date;
596
+ toTPS(calendar?: string, opts?: {
597
+ order?: TimeOrder;
598
+ }): string;
599
+ toTPSURI(calendar?: string, opts?: {
600
+ order?: TimeOrder;
601
+ latitude?: number;
602
+ longitude?: number;
603
+ altitude?: number;
604
+ isUnknownLocation?: boolean;
605
+ isHiddenLocation?: boolean;
606
+ isRedactedLocation?: boolean;
607
+ }): string;
608
+ getTime(): number;
609
+ valueOf(): number;
610
+ toString(): string;
611
+ toISOString(): string;
612
+ toUTCString(): string;
613
+ toJSON(): string | null;
614
+ getFullYear(): number;
615
+ getUTCFullYear(): number;
616
+ getMonth(): number;
617
+ getUTCMonth(): number;
618
+ getDate(): number;
619
+ getUTCDate(): number;
620
+ getHours(): number;
621
+ getUTCHours(): number;
622
+ getMinutes(): number;
623
+ getUTCMinutes(): number;
624
+ getSeconds(): number;
625
+ getUTCSeconds(): number;
626
+ getMilliseconds(): number;
627
+ getUTCMilliseconds(): number;
628
+ [Symbol.toPrimitive](hint: string): string | number;
629
+ }