@thi.ng/date 2.8.10 → 3.0.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 CHANGED
@@ -71,7 +71,7 @@ For Node.js REPL:
71
71
  const date = await import("@thi.ng/date");
72
72
  ```
73
73
 
74
- Package sizes (brotli'd, pre-treeshake): ESM: 5.43 KB
74
+ Package sizes (brotli'd, pre-treeshake): ESM: 5.53 KB
75
75
 
76
76
  ## Dependencies
77
77
 
@@ -404,6 +404,7 @@ The following preset formatters are available:
404
404
  - `FMT_HHmmss` - `"21:42:07"`
405
405
  - `FMT_HHmmss_ALT` - `"214207"`
406
406
  - `FMT_hms` - `"9:42:07 PM"`
407
+ - `FMT_mmss` - `"42:07"`
407
408
  - `FMT_yyyy` - `"2020"` (4 digit year)
408
409
  - `FMT_MM` - `"12"` (2 digit month)
409
410
  - `FMT_ww` - `"52"` (2 digit week)
@@ -424,28 +425,50 @@ defFormat([FMT_yyyyMMdd, " @ ", FMT_HHmmss])();
424
425
 
425
426
  ### Timecodes
426
427
 
427
- For timebased media applications, the higher-order `defTimecode()` can be used
428
- to create a formatter for a given FPS (frames / second, in [1..1000] range),
429
- e.g. `HH:mm:ss:ff`. The returned function takes a single arg (time in
430
- milliseconds) and returns formatted string.
428
+ For timebased media applications, the configurable higher-order `defTimecode()`
429
+ can be used to create a formatter for a given FPS (frames per second, in
430
+ [1..1000] range), e.g. `HH:mm:ss:ff`. The returned function takes a single arg
431
+ (time in milliseconds) and returns a formatted string.
431
432
 
432
- The timecode considers days too, but only includes them in the result if the day
433
- part is non-zero. The 4 separators between each field can be customized via 2nd
434
- arg (default: all `:`).
433
+ The timecode considers days too, but by default only includes them in the result
434
+ if the day part is non-zero. The 4 separators between each field can be
435
+ customized via the `sep`
436
+ [option](https://docs.thi.ng/umbrella/date/interfaces/TimecodeOpts). The minute
437
+ and second parts are the only ones which will always be present. The visibility
438
+ of others can be configured. Depending on FPS, the frame part will be between
439
+ 2-4 digits (zero-padded).
435
440
 
436
441
  ```ts
437
442
  import { defTimecode, DAY, HOUR, MINUTE, SECOND } from "@thi.ng/date";
438
443
 
439
- const a = defTimecode(30);
440
- a(1*HOUR + 2*MINUTE + 3*SECOND + 4*1000/30)
441
- // "01:02:03:04"
444
+ // use 30 fps w/ default options
445
+ const fmt = defTimecode(30);
442
446
 
443
- a(DAY);
444
- // "01:00:00:00:00"
447
+ // day part omitted by default if zero
448
+ console.log(
449
+ fmt(HOUR + 2*MINUTE + 3*SECOND + 4*SECOND/30)
450
+ );
451
+ // 01:02:03:04
445
452
 
446
- const b = defTimecode(30, ["d ", "h ", "' ", '" ']);
447
- b(DAY + HOUR + 2*MINUTE + 3*SECOND + 999)
448
- // "01d 01h 02' 03" 29"
453
+ // ...but shown if needed
454
+ console.log(fmt(DAY));
455
+ // 01:00:00:00:00
456
+
457
+ // use custom seperators
458
+ const fmt2 = defTimecode(30, { sep: ["d ", "h ", "' ", '" '] });
459
+
460
+ console.log(
461
+ fmt2(DAY + 2*HOUR + 3*MINUTE + 4*SECOND + 999)
462
+ );
463
+ // 01d 02h 03' 04" 29
464
+
465
+ // only use `min:sec`
466
+ const fmt3 = defTimecode(30, { hour: false, frames: false });
467
+
468
+ console.log(
469
+ fmt3(12 * MINUTE + 34 * SECOND)
470
+ );
471
+ // 12:34
449
472
  ```
450
473
 
451
474
  ### Locales
@@ -492,9 +515,10 @@ fmt(dateTime());
492
515
  // Saturday 19 September 2020
493
516
  ```
494
517
 
495
- Use [`withLocale()`](https://docs.thi.ng/umbrella/date/functions/withLocale.html)
496
- to only temporarily set a locale and execute a function with it, then
497
- automatically restoring the currently active locale.
518
+ Use
519
+ [`withLocale()`](https://docs.thi.ng/umbrella/date/functions/withLocale.html) to
520
+ only temporarily set a locale and execute a function with it, then automatically
521
+ restoring the currently active locale.
498
522
 
499
523
  ```ts
500
524
  import { dateTime, withLocale, FR_LONG } from "@thi.ng/date";
package/datetime.d.ts CHANGED
@@ -118,5 +118,5 @@ export declare const ensureDateTime: (x: MaybeDate, prec?: Precision) => DateTim
118
118
  *
119
119
  * @param x -
120
120
  */
121
- export declare const maybeIsDate: (x: any) => x is string | number | DateTime | Date;
121
+ export declare const maybeIsDate: (x: any) => x is string | number | Date | DateTime;
122
122
  //# sourceMappingURL=datetime.d.ts.map
package/format.d.ts CHANGED
@@ -65,6 +65,8 @@ export declare const FMT_HHmm: Formatter;
65
65
  /**
66
66
  * Format preset, e.g. `5:08 PM`
67
67
  */
68
+ export declare const FMT_hmm: Formatter;
69
+ /** @deprecated renamed to {@link FMT_hmm} */
68
70
  export declare const FMT_hm: Formatter;
69
71
  /**
70
72
  * Format preset, e.g. `17:08:01`
@@ -78,6 +80,10 @@ export declare const FMT_HHmmss_ALT: Formatter;
78
80
  * Format preset, e.g. `5:08:01 PM`
79
81
  */
80
82
  export declare const FMT_hms: Formatter;
83
+ /**
84
+ * Format preset, 2-digit minute and second only (mm:ss)
85
+ */
86
+ export declare const FMT_mmss: Formatter;
81
87
  /**
82
88
  * Format preset, e.g. `20200919-170801`
83
89
  */
package/format.js CHANGED
@@ -169,10 +169,12 @@ const FMT_MMMdyyyy = defFormat(["MMM", " ", "d", " ", "yyyy"]);
169
169
  const FMT_dMyyyy = defFormat(["d", "/DM", "M", "/MY", "yyyy"]);
170
170
  const FMT_dMMMyyyy = defFormat(["d", "/DM", "MMM", " ", "yyyy"]);
171
171
  const FMT_HHmm = defFormat(["HH", "/HM", "mm"]);
172
- const FMT_hm = defFormat(["h", "/HM", "mm", " ", "A"]);
172
+ const FMT_hmm = defFormat(["h", "/HM", "mm", " ", "A"]);
173
+ const FMT_hm = FMT_hmm;
173
174
  const FMT_HHmmss = defFormat(["HH", "/HM", "mm", "/MS", "ss"]);
174
175
  const FMT_HHmmss_ALT = defFormat(["HH", "mm", "ss"]);
175
176
  const FMT_hms = defFormat(["h", "/HM", "mm", "/MS", "ss", " ", "A"]);
177
+ const FMT_mmss = defFormat(["mm", "/MS", "ss"]);
176
178
  const FMT_yyyyMMdd_HHmmss = defFormat(
177
179
  ["yyyy", "MM", "dd", "-", "HH", "mm", "ss"]
178
180
  );
@@ -258,8 +260,10 @@ export {
258
260
  FMT_dMyyyy,
259
261
  FMT_dd,
260
262
  FMT_hm,
263
+ FMT_hmm,
261
264
  FMT_hms,
262
265
  FMT_mm,
266
+ FMT_mmss,
263
267
  FMT_ss,
264
268
  FMT_ww,
265
269
  FMT_yyyy,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thi.ng/date",
3
- "version": "2.8.10",
3
+ "version": "3.0.0",
4
4
  "description": "Datetime types, relative dates, math, iterators, composable formatters, locales",
5
5
  "type": "module",
6
6
  "module": "./index.js",
@@ -40,12 +40,12 @@
40
40
  "tool:tangle": "../../node_modules/.bin/tangle src/**/*.ts"
41
41
  },
42
42
  "dependencies": {
43
- "@thi.ng/api": "^8.12.26",
44
- "@thi.ng/checks": "^3.10.0",
45
- "@thi.ng/strings": "^3.12.8"
43
+ "@thi.ng/api": "^8.12.27",
44
+ "@thi.ng/checks": "^3.10.1",
45
+ "@thi.ng/strings": "^3.12.9"
46
46
  },
47
47
  "devDependencies": {
48
- "esbuild": "^0.28.0",
48
+ "esbuild": "^0.28.1",
49
49
  "typedoc": "^0.28.19",
50
50
  "typescript": "^6.0.3"
51
51
  },
@@ -135,5 +135,5 @@
135
135
  "thi.ng": {
136
136
  "year": 2020
137
137
  },
138
- "gitHead": "c47c56420bcae2e0477d252a497be44f08ca185a"
138
+ "gitHead": "febee81fd8b59e5e330a6e6521a2667ed5401a3d"
139
139
  }
package/timecode.d.ts CHANGED
@@ -1,37 +1,78 @@
1
1
  /**
2
- * Returns a time formatter for given FPS (frames / second, in `[1,1000]`
2
+ * Options for {@link defTimecode}.
3
+ */
4
+ export interface TimecodeOpts {
5
+ /**
6
+ * Timecode part separators in the following order: day, hour, minute,
7
+ * second. By default uses `:` for all.
8
+ */
9
+ sep: ArrayLike<string>;
10
+ /**
11
+ * If true (default: false), the day timecode part will always be included
12
+ * in output. If false, only if the value is non-zero.
13
+ */
14
+ day: boolean;
15
+ /**
16
+ * If true (default), the hour timecode part will always be included in
17
+ * output. If false, only if the value is non-zero (or if day is used).
18
+ */
19
+ hour: boolean;
20
+ /**
21
+ * If true (default), frame timecode part will be included.
22
+ */
23
+ frame: boolean;
24
+ }
25
+ /**
26
+ * Returns a time formatter for given `fps` (frames per second, in `[1,1000]`
3
27
  * range), e.g. `HH:mm:ss:ff`. The returned function takes a single arg (time in
4
- * milliseconds) and returns formatted string.
28
+ * milliseconds) and returns a formatted string.
5
29
  *
6
30
  * @remarks
7
- * The timecode considers days too, but only includes them in the result if the
8
- * day part is non-zero. The 4 separators between each field can be customized
9
- * via 2nd arg (default: all `:`).
31
+ * The timecode considers days too, but by default only includes them in the
32
+ * result if the day part is non-zero. The 4 separators between each field can
33
+ * be customized via the {@link TimecodeOpts.sep} option.
34
+ *
35
+ * The minute and second parts are the only ones which will always be present.
36
+ * The visibility of others can be configured.
37
+ *
38
+ * Depending on FPS, the frame part will be between 2-4 digits (zero-padded).
10
39
  *
11
40
  * @example
12
41
  * ```ts tangle:../export/timecode.ts
13
42
  * import { defTimecode, DAY, HOUR, MINUTE, SECOND } from "@thi.ng/date";
14
43
  *
44
+ * // use 30 fps w/ default options
15
45
  * const fmt = defTimecode(30);
16
46
  *
47
+ * // day part omitted by default if zero
17
48
  * console.log(
18
- * fmt(HOUR + 2*MINUTE + 3*SECOND + 4*1000/30)
49
+ * fmt(HOUR + 2*MINUTE + 3*SECOND + 4*SECOND/30)
19
50
  * );
20
- * // "01:02:03:04"
51
+ * // 01:02:03:04
21
52
  *
53
+ * // ...but shown if needed
22
54
  * console.log(fmt(DAY));
23
- * // "01:00:00:00:00"
55
+ * // 01:00:00:00:00
56
+ *
57
+ * // use custom seperators
58
+ * const fmt2 = defTimecode(30, { sep: ["d ", "h ", "' ", '" '] });
59
+ *
60
+ * console.log(
61
+ * fmt2(DAY + 2*HOUR + 3*MINUTE + 4*SECOND + 999)
62
+ * );
63
+ * // 01d 02h 03' 04" 29
24
64
  *
25
- * const fmt2 = defTimecode(30, ["d ", "h ", "' ", '" ']);
65
+ * // only use `min:sec`
66
+ * const fmt3 = defTimecode(30, { hour: false, frames: false });
26
67
  *
27
68
  * console.log(
28
- * fmt2(DAY + HOUR + 2 * MINUTE + 3 * SECOND + 999)
69
+ * fmt3(12 * MINUTE + 34 * SECOND)
29
70
  * );
30
- * // "01d 01h 02' 03" 29"
71
+ * // 12:34
31
72
  * ```
32
73
  *
33
74
  * @param fps -
34
- * @param sep -
75
+ * @param opts -
35
76
  */
36
- export declare const defTimecode: (fps: number, sep?: ArrayLike<string>) => (t: number) => string;
77
+ export declare const defTimecode: (fps: number, opts?: Partial<TimecodeOpts>) => (t: number) => string;
37
78
  //# sourceMappingURL=timecode.d.ts.map
package/timecode.js CHANGED
@@ -1,20 +1,23 @@
1
- import { Z2 } from "@thi.ng/strings/pad-left";
1
+ import { Z2, Z3, Z4 } from "@thi.ng/strings/pad-left";
2
2
  import { decomposeDuration } from "./duration.js";
3
- const defTimecode = (fps, sep = "::::") => {
3
+ const defTimecode = (fps, opts = {}) => {
4
+ const {
5
+ sep = "::::",
6
+ day = false,
7
+ hour = true,
8
+ frame: useFrame = true
9
+ } = opts;
4
10
  const frame = 1e3 / fps;
11
+ const $sep = (x) => sep[x] ?? ":";
12
+ const fmtFrame = fps < 100 ? Z2 : fps < 1e3 ? Z3 : Z4;
5
13
  return (t) => {
6
14
  const [_, __, d, h, m, s, ms] = decomposeDuration(t);
7
- const parts = [
8
- Z2(h),
9
- sep[1],
10
- Z2(m),
11
- sep[2],
12
- Z2(s),
13
- sep[3],
14
- Z2(ms / frame | 0)
15
- ];
16
- d > 0 && parts.unshift(Z2(d), sep[0]);
17
- return parts.join("");
15
+ let parts = "";
16
+ if (d || day) parts += Z2(d) + $sep(0);
17
+ if (h || d || day || hour) parts += Z2(h) + $sep(1);
18
+ parts += Z2(m) + $sep(2) + Z2(s);
19
+ if (useFrame) parts += $sep(3) + fmtFrame(ms / frame | 0);
20
+ return parts;
18
21
  };
19
22
  };
20
23
  export {