@nlozgachev/pipelined 0.62.0 → 0.64.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.
@@ -277,9 +277,9 @@ var memoize = (f, options) => {
277
277
  const result = f(a);
278
278
  cache.set(key, result);
279
279
  if (maxSize !== void 0 && cache.size > maxSize) {
280
- const firstKey = cache.keys().next().value;
281
- if (firstKey !== void 0) {
282
- cache.delete(firstKey);
280
+ for (const k of cache.keys()) {
281
+ cache.delete(k);
282
+ break;
283
283
  }
284
284
  }
285
285
  return result;
@@ -389,32 +389,151 @@ pipe.try = (f, onError) => (a) => {
389
389
  var import_node_util = require("util");
390
390
 
391
391
  // src/Types/Brand.ts
392
- var Brand;
393
- ((Brand2) => {
394
- Brand2.wrap = () => (value) => value;
395
- Brand2.unwrap = (branded) => branded;
396
- })(Brand || (Brand = {}));
392
+ var Brand = {
393
+ /**
394
+ * Returns a constructor that wraps a value of type T in brand K.
395
+ * The resulting function performs an unchecked cast — only use when the raw
396
+ * value is known to satisfy the brand's invariants.
397
+ *
398
+ * @example
399
+ * ```ts
400
+ * type PositiveNumber = Brand<"PositiveNumber", number>;
401
+ * const toPositiveNumber = Brand.wrap<"PositiveNumber", number>();
402
+ *
403
+ * const n: PositiveNumber = toPositiveNumber(42);
404
+ * ```
405
+ */
406
+ wrap: () => (value) => value,
407
+ /**
408
+ * Strips the brand and returns the underlying value.
409
+ * Since Brand<K, T> extends T this is rarely needed, but can improve readability.
410
+ *
411
+ * @example
412
+ * ```ts
413
+ * type UserId = Brand<"UserId", string>;
414
+ * const toUserId = Brand.wrap<"UserId", string>();
415
+ * const userId: UserId = toUserId("user-123");
416
+ * const raw: string = Brand.unwrap(userId); // "user-123"
417
+ * ```
418
+ */
419
+ unwrap: (branded) => branded
420
+ };
397
421
 
398
422
  // src/Types/Duration.ts
399
- var Duration;
400
- ((Duration2) => {
401
- const wrap = Brand.wrap();
402
- Duration2.milliseconds = (ms) => wrap(ms);
403
- Duration2.seconds = (s) => wrap(s * 1e3);
404
- Duration2.minutes = (m) => wrap(m * 60 * 1e3);
405
- Duration2.hours = (h) => wrap(h * 60 * 60 * 1e3);
406
- Duration2.days = (d) => wrap(d * 24 * 60 * 60 * 1e3);
407
- let to;
408
- ((to2) => {
409
- to2.milliseconds = (d) => Brand.unwrap(d);
410
- to2.seconds = (d) => Brand.unwrap(d) / 1e3;
411
- to2.minutes = (d) => Brand.unwrap(d) / (60 * 1e3);
412
- to2.hours = (d) => Brand.unwrap(d) / (60 * 60 * 1e3);
413
- to2.days = (d) => Brand.unwrap(d) / (24 * 60 * 60 * 1e3);
414
- })(to = Duration2.to || (Duration2.to = {}));
415
- Duration2.add = (other) => (self) => wrap(Brand.unwrap(self) + Brand.unwrap(other));
416
- Duration2.subtract = (other) => (self) => wrap(Brand.unwrap(self) - Brand.unwrap(other));
417
- })(Duration || (Duration = {}));
423
+ var wrap = Brand.wrap();
424
+ var Duration = {
425
+ /**
426
+ * Creates a Duration from milliseconds.
427
+ *
428
+ * @example
429
+ * ```ts
430
+ * Duration.milliseconds(500); // 500ms Duration
431
+ * ```
432
+ */
433
+ milliseconds: (ms) => wrap(ms),
434
+ /**
435
+ * Creates a Duration from seconds.
436
+ *
437
+ * @example
438
+ * ```ts
439
+ * Duration.seconds(2); // 2000ms Duration
440
+ * ```
441
+ */
442
+ seconds: (s) => wrap(s * 1e3),
443
+ /**
444
+ * Creates a Duration from minutes.
445
+ *
446
+ * @example
447
+ * ```ts
448
+ * Duration.minutes(5); // 300000ms Duration
449
+ * ```
450
+ */
451
+ minutes: (m) => wrap(m * 60 * 1e3),
452
+ /**
453
+ * Creates a Duration from hours.
454
+ *
455
+ * @example
456
+ * ```ts
457
+ * Duration.hours(1); // 3600000ms Duration
458
+ * ```
459
+ */
460
+ hours: (h) => wrap(h * 60 * 60 * 1e3),
461
+ /**
462
+ * Creates a Duration from days.
463
+ *
464
+ * @example
465
+ * ```ts
466
+ * Duration.days(1); // 86400000ms Duration
467
+ * ```
468
+ */
469
+ days: (d) => wrap(d * 24 * 60 * 60 * 1e3),
470
+ // --- to ---
471
+ to: {
472
+ /**
473
+ * Converts a Duration back to raw milliseconds.
474
+ *
475
+ * @example
476
+ * ```ts
477
+ * Duration.to.milliseconds(Duration.seconds(2)); // 2000
478
+ * ```
479
+ */
480
+ milliseconds: (d) => Brand.unwrap(d),
481
+ /**
482
+ * Converts a Duration to seconds.
483
+ *
484
+ * @example
485
+ * ```ts
486
+ * Duration.to.seconds(Duration.milliseconds(2500)); // 2.5
487
+ * ```
488
+ */
489
+ seconds: (d) => Brand.unwrap(d) / 1e3,
490
+ /**
491
+ * Converts a Duration to minutes.
492
+ *
493
+ * @example
494
+ * ```ts
495
+ * Duration.to.minutes(Duration.seconds(120)); // 2
496
+ * ```
497
+ */
498
+ minutes: (d) => Brand.unwrap(d) / (60 * 1e3),
499
+ /**
500
+ * Converts a Duration to hours.
501
+ *
502
+ * @example
503
+ * ```ts
504
+ * Duration.to.hours(Duration.minutes(90)); // 1.5
505
+ * ```
506
+ */
507
+ hours: (d) => Brand.unwrap(d) / (60 * 60 * 1e3),
508
+ /**
509
+ * Converts a Duration to days.
510
+ *
511
+ * @example
512
+ * ```ts
513
+ * Duration.to.days(Duration.hours(36)); // 1.5
514
+ * ```
515
+ */
516
+ days: (d) => Brand.unwrap(d) / (24 * 60 * 60 * 1e3)
517
+ },
518
+ /**
519
+ * Adds two Durations together.
520
+ *
521
+ * @example
522
+ * ```ts
523
+ * pipe(Duration.seconds(1), Duration.add(Duration.milliseconds(500))); // 1500ms
524
+ * ```
525
+ */
526
+ add: (other) => (self) => wrap(Brand.unwrap(self) + Brand.unwrap(other)),
527
+ /**
528
+ * Subtracts the other Duration from this one.
529
+ *
530
+ * @example
531
+ * ```ts
532
+ * pipe(Duration.seconds(1), Duration.subtract(Duration.milliseconds(500))); // 500ms
533
+ * ```
534
+ */
535
+ subtract: (other) => (self) => wrap(Brand.unwrap(self) - Brand.unwrap(other))
536
+ };
418
537
 
419
538
  // src/Composition/tap.ts
420
539
  function tap(f) {
@@ -423,83 +542,85 @@ function tap(f) {
423
542
  return a;
424
543
  };
425
544
  }
426
- ((tap2) => {
427
- tap2.log = (options) => (a) => {
428
- const logger = options?.logger ?? console.log;
429
- const formatter = options?.formatter ?? ((val) => {
430
- try {
431
- return typeof val === "object" && val !== null ? JSON.stringify(val) : String(val);
432
- } catch {
433
- return String(val);
434
- }
435
- });
436
- const formatted = formatter(a);
437
- if (options?.label !== void 0) {
438
- logger(`[${options.label}]: ${formatted}`);
439
- } else {
440
- logger(formatted);
441
- }
442
- return a;
443
- };
444
- tap2.inspect = (options) => (a) => {
445
- const label = options?.label;
446
- const depth = options?.depth ?? null;
447
- const colors = options?.colors ?? true;
448
- let formatted;
449
- if (typeof import_node_util.inspect === "function") {
450
- formatted = (0, import_node_util.inspect)(a, { depth, colors });
451
- } else {
452
- try {
453
- formatted = JSON.stringify(a, null, 2);
454
- } catch {
455
- formatted = String(a);
456
- }
545
+ var log = (options) => (a) => {
546
+ const logger = options?.logger ?? console.log;
547
+ const formatter = options?.formatter ?? ((val) => {
548
+ try {
549
+ return typeof val === "object" && val !== null ? JSON.stringify(val) : String(val);
550
+ } catch {
551
+ return String(val);
552
+ }
553
+ });
554
+ const formatted = formatter(a);
555
+ if (options?.label !== void 0) {
556
+ logger(`[${options.label}]: ${formatted}`);
557
+ } else {
558
+ logger(formatted);
559
+ }
560
+ return a;
561
+ };
562
+ var inspect = (options) => (a) => {
563
+ const label = options?.label;
564
+ const depth = options?.depth ?? null;
565
+ const colors = options?.colors ?? true;
566
+ let formatted;
567
+ if (typeof import_node_util.inspect === "function") {
568
+ formatted = (0, import_node_util.inspect)(a, { depth, colors });
569
+ } else {
570
+ try {
571
+ formatted = JSON.stringify(a, null, 2);
572
+ } catch {
573
+ formatted = String(a);
457
574
  }
458
- if (label !== void 0) {
459
- console.log(`[${label}]: ${formatted}`);
575
+ }
576
+ if (label !== void 0) {
577
+ console.log(`[${label}]: ${formatted}`);
578
+ } else {
579
+ console.log(formatted);
580
+ }
581
+ return a;
582
+ };
583
+ var async3 = (fn, options) => (a) => {
584
+ const onError = options?.onError ?? console.error;
585
+ Promise.resolve(fn(a)).catch((err) => {
586
+ onError(err);
587
+ });
588
+ return a;
589
+ };
590
+ var time = (fn, config) => (a) => {
591
+ const start = performance.now();
592
+ const triggerFinish = (duration) => {
593
+ if (config.label !== void 0) {
594
+ console.log(`[${config.label}]: ${Duration.to.milliseconds(duration)}ms`);
460
595
  } else {
461
- console.log(formatted);
596
+ config.onFinish(duration);
462
597
  }
463
- return a;
464
- };
465
- tap2.async = (fn, options) => (a) => {
466
- const onError = options?.onError ?? console.error;
467
- Promise.resolve(fn(a)).catch((err) => {
468
- onError(err);
469
- });
470
- return a;
471
598
  };
472
- tap2.time = (fn, config) => (a) => {
473
- const start = performance.now();
474
- const triggerFinish = (duration) => {
475
- if (config.label !== void 0) {
476
- console.log(`[${config.label}]: ${Duration.to.milliseconds(duration)}ms`);
477
- } else {
478
- config.onFinish(duration);
479
- }
480
- };
481
- try {
482
- const res = fn(a);
483
- if (res !== null && (typeof res === "object" || typeof res === "function") && typeof res.then === "function") {
484
- res.then(() => {
485
- const duration = Duration.milliseconds(performance.now() - start);
486
- triggerFinish(duration);
487
- }, () => {
488
- const duration = Duration.milliseconds(performance.now() - start);
489
- triggerFinish(duration);
490
- });
491
- } else {
599
+ try {
600
+ const res = fn(a);
601
+ if (res !== null && (typeof res === "object" || typeof res === "function") && typeof res.then === "function") {
602
+ res.then(() => {
492
603
  const duration = Duration.milliseconds(performance.now() - start);
493
604
  triggerFinish(duration);
494
- }
495
- } catch (err) {
605
+ }, () => {
606
+ const duration = Duration.milliseconds(performance.now() - start);
607
+ triggerFinish(duration);
608
+ });
609
+ } else {
496
610
  const duration = Duration.milliseconds(performance.now() - start);
497
611
  triggerFinish(duration);
498
- throw err;
499
612
  }
500
- return a;
501
- };
502
- })(tap || (tap = {}));
613
+ } catch (err) {
614
+ const duration = Duration.milliseconds(performance.now() - start);
615
+ triggerFinish(duration);
616
+ throw err;
617
+ }
618
+ return a;
619
+ };
620
+ tap.log = log;
621
+ tap.inspect = inspect;
622
+ tap.async = async3;
623
+ tap.time = time;
503
624
 
504
625
  // src/Composition/uncurry.ts
505
626
  function uncurry(f) {
@@ -1,5 +1,5 @@
1
- import { A as Awaitable, T as Thenable } from './InternalTypes-DuK_XpTi.cjs';
2
- import { D as Duration } from './Duration-B8joKzro.cjs';
1
+ import { A as Awaitable, T as Thenable } from './InternalTypes-GFn4RTwD.cjs';
2
+ import { D as Duration } from './Duration-DeyxG6VQ.cjs';
3
3
 
4
4
  /**
5
5
  * Composes functions from right to left, returning a new function.
@@ -829,6 +829,12 @@ interface pipe {
829
829
  * @see {@link Maybe.tap} for Maybe-specific tap that only runs on Some
830
830
  */
831
831
  declare function tap<A>(f: (a: A) => void): (a: A) => A;
832
+ declare namespace tap {
833
+ var log: <A>(options?: tap.LogOptions<A>) => (a: A) => A;
834
+ var inspect: <A>(options?: tap.InspectOptions) => (a: A) => A;
835
+ var async: <A>(fn: (a: A) => Thenable<unknown>, options?: tap.AsyncOptions) => (a: A) => A;
836
+ var time: <A>(fn: (a: A) => unknown, config: tap.TimeConfig) => (a: A) => A;
837
+ }
832
838
  declare namespace tap {
833
839
  /**
834
840
  * Configuration options for {@link tap.log}.
@@ -888,80 +894,6 @@ declare namespace tap {
888
894
  onFinish: (duration: Duration) => void;
889
895
  label?: never;
890
896
  };
891
- /**
892
- * Logs the piped value to the console or a custom logger, returning the value unchanged.
893
- *
894
- * @example
895
- * ```ts
896
- * pipe(
897
- * 42,
898
- * tap.log(), // logs: 42
899
- * tap.log({ label: "Count" }) // logs: [Count]: 42
900
- * );
901
- * ```
902
- */
903
- const log: <A>(options?: LogOptions<A>) => (a: A) => A;
904
- /**
905
- * Performs a deep structured inspect formatting on the piped value, returning it unchanged.
906
- * In Node.js environments, this utilizes Node's `node:util` `inspect` utility.
907
- *
908
- * @example
909
- * ```ts
910
- * pipe(
911
- * { user: { name: "Alice", details: { age: 30 } } },
912
- * tap.inspect({ label: "User Object", depth: 2 })
913
- * );
914
- * ```
915
- */
916
- const inspect: <A>(options?: InspectOptions) => (a: A) => A;
917
- /**
918
- * Triggers a fire-and-forget asynchronous side effect in the background,
919
- * returning the piped value immediately and synchronously.
920
- * Any errors thrown by the async function are caught and forwarded to `onError`.
921
- *
922
- * @example
923
- * ```ts
924
- * const user = { id: 1 };
925
- * const saveToDatabase = async (u: typeof user) => {};
926
- * const logError = (err: unknown) => console.error(err);
927
- *
928
- * pipe(
929
- * user,
930
- * tap.async(async (u) => {
931
- * await saveToDatabase(u);
932
- * }, { onError: (err) => logError(err) })
933
- * );
934
- * ```
935
- */
936
- const async: <A>(fn: (a: A) => Thenable<unknown>, options?: AsyncOptions) => (a: A) => A;
937
- /**
938
- * Runs a function and measures its execution duration, returning the value unchanged.
939
- * Supports both synchronous and asynchronous functions. If the timed function returns
940
- * a Promise, duration measurement resolves asynchronously upon resolution/rejection.
941
- *
942
- * @example
943
- * ```ts
944
- * const data = [1, 2, 3];
945
- * const processData = (d: typeof data) => d.map(n => n * 2);
946
- * const fetchData = async (d: typeof data) => d.length;
947
- * const metrics = { histogram: (name: string, ms: number) => {} };
948
- *
949
- * // Time a synchronous computation
950
- * pipe(
951
- * data,
952
- * tap.time(processData, { label: "sync-process" })
953
- * );
954
- *
955
- * // Time an asynchronous fetch with custom metrics callback
956
- * pipe(
957
- * data,
958
- * tap.time(fetchData, {
959
- * onFinish: (dur) => metrics.histogram("api.time", Duration.to.milliseconds(dur))
960
- * })
961
- * );
962
- * ```
963
- */
964
- const time: <A>(fn: (a: A) => unknown, config: TimeConfig) => (a: A) => A;
965
897
  }
966
898
 
967
899
  /**
@@ -1,5 +1,5 @@
1
- import { A as Awaitable, T as Thenable } from './InternalTypes-LdhLQx3N.js';
2
- import { D as Duration } from './Duration-B8joKzro.js';
1
+ import { A as Awaitable, T as Thenable } from './InternalTypes-CCXa8Kvr.js';
2
+ import { D as Duration } from './Duration-DeyxG6VQ.js';
3
3
 
4
4
  /**
5
5
  * Composes functions from right to left, returning a new function.
@@ -829,6 +829,12 @@ interface pipe {
829
829
  * @see {@link Maybe.tap} for Maybe-specific tap that only runs on Some
830
830
  */
831
831
  declare function tap<A>(f: (a: A) => void): (a: A) => A;
832
+ declare namespace tap {
833
+ var log: <A>(options?: tap.LogOptions<A>) => (a: A) => A;
834
+ var inspect: <A>(options?: tap.InspectOptions) => (a: A) => A;
835
+ var async: <A>(fn: (a: A) => Thenable<unknown>, options?: tap.AsyncOptions) => (a: A) => A;
836
+ var time: <A>(fn: (a: A) => unknown, config: tap.TimeConfig) => (a: A) => A;
837
+ }
832
838
  declare namespace tap {
833
839
  /**
834
840
  * Configuration options for {@link tap.log}.
@@ -888,80 +894,6 @@ declare namespace tap {
888
894
  onFinish: (duration: Duration) => void;
889
895
  label?: never;
890
896
  };
891
- /**
892
- * Logs the piped value to the console or a custom logger, returning the value unchanged.
893
- *
894
- * @example
895
- * ```ts
896
- * pipe(
897
- * 42,
898
- * tap.log(), // logs: 42
899
- * tap.log({ label: "Count" }) // logs: [Count]: 42
900
- * );
901
- * ```
902
- */
903
- const log: <A>(options?: LogOptions<A>) => (a: A) => A;
904
- /**
905
- * Performs a deep structured inspect formatting on the piped value, returning it unchanged.
906
- * In Node.js environments, this utilizes Node's `node:util` `inspect` utility.
907
- *
908
- * @example
909
- * ```ts
910
- * pipe(
911
- * { user: { name: "Alice", details: { age: 30 } } },
912
- * tap.inspect({ label: "User Object", depth: 2 })
913
- * );
914
- * ```
915
- */
916
- const inspect: <A>(options?: InspectOptions) => (a: A) => A;
917
- /**
918
- * Triggers a fire-and-forget asynchronous side effect in the background,
919
- * returning the piped value immediately and synchronously.
920
- * Any errors thrown by the async function are caught and forwarded to `onError`.
921
- *
922
- * @example
923
- * ```ts
924
- * const user = { id: 1 };
925
- * const saveToDatabase = async (u: typeof user) => {};
926
- * const logError = (err: unknown) => console.error(err);
927
- *
928
- * pipe(
929
- * user,
930
- * tap.async(async (u) => {
931
- * await saveToDatabase(u);
932
- * }, { onError: (err) => logError(err) })
933
- * );
934
- * ```
935
- */
936
- const async: <A>(fn: (a: A) => Thenable<unknown>, options?: AsyncOptions) => (a: A) => A;
937
- /**
938
- * Runs a function and measures its execution duration, returning the value unchanged.
939
- * Supports both synchronous and asynchronous functions. If the timed function returns
940
- * a Promise, duration measurement resolves asynchronously upon resolution/rejection.
941
- *
942
- * @example
943
- * ```ts
944
- * const data = [1, 2, 3];
945
- * const processData = (d: typeof data) => d.map(n => n * 2);
946
- * const fetchData = async (d: typeof data) => d.length;
947
- * const metrics = { histogram: (name: string, ms: number) => {} };
948
- *
949
- * // Time a synchronous computation
950
- * pipe(
951
- * data,
952
- * tap.time(processData, { label: "sync-process" })
953
- * );
954
- *
955
- * // Time an asynchronous fetch with custom metrics callback
956
- * pipe(
957
- * data,
958
- * tap.time(fetchData, {
959
- * onFinish: (dur) => metrics.histogram("api.time", Duration.to.milliseconds(dur))
960
- * })
961
- * );
962
- * ```
963
- */
964
- const time: <A>(fn: (a: A) => unknown, config: TimeConfig) => (a: A) => A;
965
897
  }
966
898
 
967
899
  /**