@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.
@@ -222,9 +222,9 @@ var memoize = (f, options) => {
222
222
  const result = f(a);
223
223
  cache.set(key, result);
224
224
  if (maxSize !== void 0 && cache.size > maxSize) {
225
- const firstKey = cache.keys().next().value;
226
- if (firstKey !== void 0) {
227
- cache.delete(firstKey);
225
+ for (const k of cache.keys()) {
226
+ cache.delete(k);
227
+ break;
228
228
  }
229
229
  }
230
230
  return result;
@@ -334,32 +334,151 @@ pipe.try = (f, onError) => (a) => {
334
334
  import { inspect as nodeInspect } from "util";
335
335
 
336
336
  // src/Types/Brand.ts
337
- var Brand;
338
- ((Brand2) => {
339
- Brand2.wrap = () => (value) => value;
340
- Brand2.unwrap = (branded) => branded;
341
- })(Brand || (Brand = {}));
337
+ var Brand = {
338
+ /**
339
+ * Returns a constructor that wraps a value of type T in brand K.
340
+ * The resulting function performs an unchecked cast — only use when the raw
341
+ * value is known to satisfy the brand's invariants.
342
+ *
343
+ * @example
344
+ * ```ts
345
+ * type PositiveNumber = Brand<"PositiveNumber", number>;
346
+ * const toPositiveNumber = Brand.wrap<"PositiveNumber", number>();
347
+ *
348
+ * const n: PositiveNumber = toPositiveNumber(42);
349
+ * ```
350
+ */
351
+ wrap: () => (value) => value,
352
+ /**
353
+ * Strips the brand and returns the underlying value.
354
+ * Since Brand<K, T> extends T this is rarely needed, but can improve readability.
355
+ *
356
+ * @example
357
+ * ```ts
358
+ * type UserId = Brand<"UserId", string>;
359
+ * const toUserId = Brand.wrap<"UserId", string>();
360
+ * const userId: UserId = toUserId("user-123");
361
+ * const raw: string = Brand.unwrap(userId); // "user-123"
362
+ * ```
363
+ */
364
+ unwrap: (branded) => branded
365
+ };
342
366
 
343
367
  // src/Types/Duration.ts
344
- var Duration;
345
- ((Duration2) => {
346
- const wrap = Brand.wrap();
347
- Duration2.milliseconds = (ms) => wrap(ms);
348
- Duration2.seconds = (s) => wrap(s * 1e3);
349
- Duration2.minutes = (m) => wrap(m * 60 * 1e3);
350
- Duration2.hours = (h) => wrap(h * 60 * 60 * 1e3);
351
- Duration2.days = (d) => wrap(d * 24 * 60 * 60 * 1e3);
352
- let to;
353
- ((to2) => {
354
- to2.milliseconds = (d) => Brand.unwrap(d);
355
- to2.seconds = (d) => Brand.unwrap(d) / 1e3;
356
- to2.minutes = (d) => Brand.unwrap(d) / (60 * 1e3);
357
- to2.hours = (d) => Brand.unwrap(d) / (60 * 60 * 1e3);
358
- to2.days = (d) => Brand.unwrap(d) / (24 * 60 * 60 * 1e3);
359
- })(to = Duration2.to || (Duration2.to = {}));
360
- Duration2.add = (other) => (self) => wrap(Brand.unwrap(self) + Brand.unwrap(other));
361
- Duration2.subtract = (other) => (self) => wrap(Brand.unwrap(self) - Brand.unwrap(other));
362
- })(Duration || (Duration = {}));
368
+ var wrap = Brand.wrap();
369
+ var Duration = {
370
+ /**
371
+ * Creates a Duration from milliseconds.
372
+ *
373
+ * @example
374
+ * ```ts
375
+ * Duration.milliseconds(500); // 500ms Duration
376
+ * ```
377
+ */
378
+ milliseconds: (ms) => wrap(ms),
379
+ /**
380
+ * Creates a Duration from seconds.
381
+ *
382
+ * @example
383
+ * ```ts
384
+ * Duration.seconds(2); // 2000ms Duration
385
+ * ```
386
+ */
387
+ seconds: (s) => wrap(s * 1e3),
388
+ /**
389
+ * Creates a Duration from minutes.
390
+ *
391
+ * @example
392
+ * ```ts
393
+ * Duration.minutes(5); // 300000ms Duration
394
+ * ```
395
+ */
396
+ minutes: (m) => wrap(m * 60 * 1e3),
397
+ /**
398
+ * Creates a Duration from hours.
399
+ *
400
+ * @example
401
+ * ```ts
402
+ * Duration.hours(1); // 3600000ms Duration
403
+ * ```
404
+ */
405
+ hours: (h) => wrap(h * 60 * 60 * 1e3),
406
+ /**
407
+ * Creates a Duration from days.
408
+ *
409
+ * @example
410
+ * ```ts
411
+ * Duration.days(1); // 86400000ms Duration
412
+ * ```
413
+ */
414
+ days: (d) => wrap(d * 24 * 60 * 60 * 1e3),
415
+ // --- to ---
416
+ to: {
417
+ /**
418
+ * Converts a Duration back to raw milliseconds.
419
+ *
420
+ * @example
421
+ * ```ts
422
+ * Duration.to.milliseconds(Duration.seconds(2)); // 2000
423
+ * ```
424
+ */
425
+ milliseconds: (d) => Brand.unwrap(d),
426
+ /**
427
+ * Converts a Duration to seconds.
428
+ *
429
+ * @example
430
+ * ```ts
431
+ * Duration.to.seconds(Duration.milliseconds(2500)); // 2.5
432
+ * ```
433
+ */
434
+ seconds: (d) => Brand.unwrap(d) / 1e3,
435
+ /**
436
+ * Converts a Duration to minutes.
437
+ *
438
+ * @example
439
+ * ```ts
440
+ * Duration.to.minutes(Duration.seconds(120)); // 2
441
+ * ```
442
+ */
443
+ minutes: (d) => Brand.unwrap(d) / (60 * 1e3),
444
+ /**
445
+ * Converts a Duration to hours.
446
+ *
447
+ * @example
448
+ * ```ts
449
+ * Duration.to.hours(Duration.minutes(90)); // 1.5
450
+ * ```
451
+ */
452
+ hours: (d) => Brand.unwrap(d) / (60 * 60 * 1e3),
453
+ /**
454
+ * Converts a Duration to days.
455
+ *
456
+ * @example
457
+ * ```ts
458
+ * Duration.to.days(Duration.hours(36)); // 1.5
459
+ * ```
460
+ */
461
+ days: (d) => Brand.unwrap(d) / (24 * 60 * 60 * 1e3)
462
+ },
463
+ /**
464
+ * Adds two Durations together.
465
+ *
466
+ * @example
467
+ * ```ts
468
+ * pipe(Duration.seconds(1), Duration.add(Duration.milliseconds(500))); // 1500ms
469
+ * ```
470
+ */
471
+ add: (other) => (self) => wrap(Brand.unwrap(self) + Brand.unwrap(other)),
472
+ /**
473
+ * Subtracts the other Duration from this one.
474
+ *
475
+ * @example
476
+ * ```ts
477
+ * pipe(Duration.seconds(1), Duration.subtract(Duration.milliseconds(500))); // 500ms
478
+ * ```
479
+ */
480
+ subtract: (other) => (self) => wrap(Brand.unwrap(self) - Brand.unwrap(other))
481
+ };
363
482
 
364
483
  // src/Composition/tap.ts
365
484
  function tap(f) {
@@ -368,83 +487,85 @@ function tap(f) {
368
487
  return a;
369
488
  };
370
489
  }
371
- ((tap2) => {
372
- tap2.log = (options) => (a) => {
373
- const logger = options?.logger ?? console.log;
374
- const formatter = options?.formatter ?? ((val) => {
375
- try {
376
- return typeof val === "object" && val !== null ? JSON.stringify(val) : String(val);
377
- } catch {
378
- return String(val);
379
- }
380
- });
381
- const formatted = formatter(a);
382
- if (options?.label !== void 0) {
383
- logger(`[${options.label}]: ${formatted}`);
384
- } else {
385
- logger(formatted);
386
- }
387
- return a;
388
- };
389
- tap2.inspect = (options) => (a) => {
390
- const label = options?.label;
391
- const depth = options?.depth ?? null;
392
- const colors = options?.colors ?? true;
393
- let formatted;
394
- if (typeof nodeInspect === "function") {
395
- formatted = nodeInspect(a, { depth, colors });
396
- } else {
397
- try {
398
- formatted = JSON.stringify(a, null, 2);
399
- } catch {
400
- formatted = String(a);
401
- }
490
+ var log = (options) => (a) => {
491
+ const logger = options?.logger ?? console.log;
492
+ const formatter = options?.formatter ?? ((val) => {
493
+ try {
494
+ return typeof val === "object" && val !== null ? JSON.stringify(val) : String(val);
495
+ } catch {
496
+ return String(val);
497
+ }
498
+ });
499
+ const formatted = formatter(a);
500
+ if (options?.label !== void 0) {
501
+ logger(`[${options.label}]: ${formatted}`);
502
+ } else {
503
+ logger(formatted);
504
+ }
505
+ return a;
506
+ };
507
+ var inspect = (options) => (a) => {
508
+ const label = options?.label;
509
+ const depth = options?.depth ?? null;
510
+ const colors = options?.colors ?? true;
511
+ let formatted;
512
+ if (typeof nodeInspect === "function") {
513
+ formatted = nodeInspect(a, { depth, colors });
514
+ } else {
515
+ try {
516
+ formatted = JSON.stringify(a, null, 2);
517
+ } catch {
518
+ formatted = String(a);
402
519
  }
403
- if (label !== void 0) {
404
- console.log(`[${label}]: ${formatted}`);
520
+ }
521
+ if (label !== void 0) {
522
+ console.log(`[${label}]: ${formatted}`);
523
+ } else {
524
+ console.log(formatted);
525
+ }
526
+ return a;
527
+ };
528
+ var async3 = (fn, options) => (a) => {
529
+ const onError = options?.onError ?? console.error;
530
+ Promise.resolve(fn(a)).catch((err) => {
531
+ onError(err);
532
+ });
533
+ return a;
534
+ };
535
+ var time = (fn, config) => (a) => {
536
+ const start = performance.now();
537
+ const triggerFinish = (duration) => {
538
+ if (config.label !== void 0) {
539
+ console.log(`[${config.label}]: ${Duration.to.milliseconds(duration)}ms`);
405
540
  } else {
406
- console.log(formatted);
541
+ config.onFinish(duration);
407
542
  }
408
- return a;
409
- };
410
- tap2.async = (fn, options) => (a) => {
411
- const onError = options?.onError ?? console.error;
412
- Promise.resolve(fn(a)).catch((err) => {
413
- onError(err);
414
- });
415
- return a;
416
543
  };
417
- tap2.time = (fn, config) => (a) => {
418
- const start = performance.now();
419
- const triggerFinish = (duration) => {
420
- if (config.label !== void 0) {
421
- console.log(`[${config.label}]: ${Duration.to.milliseconds(duration)}ms`);
422
- } else {
423
- config.onFinish(duration);
424
- }
425
- };
426
- try {
427
- const res = fn(a);
428
- if (res !== null && (typeof res === "object" || typeof res === "function") && typeof res.then === "function") {
429
- res.then(() => {
430
- const duration = Duration.milliseconds(performance.now() - start);
431
- triggerFinish(duration);
432
- }, () => {
433
- const duration = Duration.milliseconds(performance.now() - start);
434
- triggerFinish(duration);
435
- });
436
- } else {
544
+ try {
545
+ const res = fn(a);
546
+ if (res !== null && (typeof res === "object" || typeof res === "function") && typeof res.then === "function") {
547
+ res.then(() => {
437
548
  const duration = Duration.milliseconds(performance.now() - start);
438
549
  triggerFinish(duration);
439
- }
440
- } catch (err) {
550
+ }, () => {
551
+ const duration = Duration.milliseconds(performance.now() - start);
552
+ triggerFinish(duration);
553
+ });
554
+ } else {
441
555
  const duration = Duration.milliseconds(performance.now() - start);
442
556
  triggerFinish(duration);
443
- throw err;
444
557
  }
445
- return a;
446
- };
447
- })(tap || (tap = {}));
558
+ } catch (err) {
559
+ const duration = Duration.milliseconds(performance.now() - start);
560
+ triggerFinish(duration);
561
+ throw err;
562
+ }
563
+ return a;
564
+ };
565
+ tap.log = log;
566
+ tap.inspect = inspect;
567
+ tap.async = async3;
568
+ tap.time = time;
448
569
 
449
570
  // src/Composition/uncurry.ts
450
571
  function uncurry(f) {