kind-adt 0.2.5 → 0.2.6

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.
Files changed (2) hide show
  1. package/index.js +139 -131
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -49,13 +49,12 @@ export function make(variants) {
49
49
  throw new TypeError(
50
50
  `Expected ${variants.map((tag) => "`" + tag + "(...)`").join("/")}, but got \`${stringify(adt)}\``
51
51
  );
52
- return entriesOf(adt)
53
- .filter(
54
- ([key]) =>
55
- key.startsWith("_") && !Object.is(Number(key.slice(1)), NaN)
56
- )
57
- .sort(([a], [b]) => Number(a.slice(1)) - Number(b.slice(1)))
58
- .map(([, value]) => value);
52
+ const result = [];
53
+ for (const key in adt)
54
+ if (Object.prototype.hasOwnProperty.call(adt, key))
55
+ if (key.startsWith("_") && !isNaN(Number(key.slice(1))))
56
+ result[key.slice(1)] = adt[key];
57
+ return result;
59
58
  }
60
59
  : unwrap,
61
60
 
@@ -169,12 +168,12 @@ export function make(variants) {
169
168
  * @returns {Array}
170
169
  */
171
170
  export function unwrap(adt) {
172
- return entriesOf(adt)
173
- .filter(
174
- ([key]) => key.startsWith("_") && !Object.is(Number(key.slice(1)), NaN)
175
- )
176
- .sort(([a], [b]) => Number(a.slice(1)) - Number(b.slice(1)))
177
- .map(([, value]) => value);
171
+ const result = [];
172
+ for (const key in adt)
173
+ if (Object.prototype.hasOwnProperty.call(adt, key))
174
+ if (key.startsWith("_") && !isNaN(Number(key.slice(1))))
175
+ result[key.slice(1)] = adt[key];
176
+ return result;
178
177
  }
179
178
 
180
179
  /**
@@ -185,13 +184,13 @@ export function unwrap(adt) {
185
184
  * @param {Object<string, Function>} cases The cases to match.
186
185
  * @returns {*}
187
186
  */
188
- function _match(adt, cases) {
187
+ const _match = (adt, cases) => {
189
188
  if (adt != null && cases[adt._tag]) return cases[adt._tag](...unwrap(adt));
190
189
  if (cases._) return cases._(adt);
191
190
  throw new Error(
192
191
  `No case found for \`${stringify(adt)}\`. Consider adding a catch-all case (\`_\`) if needed`
193
192
  );
194
- }
193
+ };
195
194
 
196
195
  /*********************
197
196
  * Utility functions *
@@ -214,48 +213,14 @@ function _match(adt, cases) {
214
213
  * matchesPrefix("is你好", "is"); // => true
215
214
  * ```
216
215
  */
217
- function matchesPrefix(str, prefix) {
218
- return (
219
- str.startsWith(prefix) &&
220
- str.length > prefix.length &&
221
- str[prefix.length] === str[prefix.length].toUpperCase()
222
- );
223
- }
216
+ const matchesPrefix = (str, prefix) =>
217
+ str.startsWith(prefix) &&
218
+ str.length > prefix.length &&
219
+ str[prefix.length] === str[prefix.length].toUpperCase();
224
220
 
225
221
  /****************************
226
222
  * Common utility functions *
227
223
  ****************************/
228
- /**
229
- * A polyfill for `Array#flatMap` to support pre ES2019 environments.
230
- * @param arr The array to flatten.
231
- * @param fn The function to call on each element of the array.
232
- * @returns
233
- */
234
- function flatMap(arr, fn) {
235
- const result = [];
236
- for (let i = 0; i < arr.length; i++) {
237
- const value = fn(arr[i], i, arr);
238
- if (Array.isArray(value)) Array.prototype.push.apply(result, value);
239
- else result.push(value);
240
- }
241
- return result;
242
- }
243
-
244
- /**
245
- * Get the entries of an object.
246
- * @private
247
- *
248
- * @param {*} obj The object to get the entries from.
249
- * @returns {Array}
250
- */
251
- function entriesOf(obj) {
252
- const entries = [];
253
- for (const key in obj)
254
- if (Object.prototype.hasOwnProperty.call(obj, key))
255
- entries.push([key, obj[key]]);
256
- return entries;
257
- }
258
-
259
224
  /**
260
225
  * Change the name of a function for better debugging experience.
261
226
  * @private
@@ -368,7 +333,7 @@ const stringify = (value) => {
368
333
  */
369
334
  function inspect({ ancestors, c, level, trailingComma }, expand) {
370
335
  const fields = unwrap(this);
371
- const fieldKeys = fields.map((_, i) => `_${i}`);
336
+ const fieldKeys = fields.map((_, i) => "_" + i);
372
337
 
373
338
  let body = expand(this, {
374
339
  level,
@@ -457,61 +422,86 @@ function inspect({ ancestors, c, level, trailingComma }, expand) {
457
422
 
458
423
  return (
459
424
  fields.length ?
460
- variant(
461
- sequence([
462
- text(c.cyan(this._tag) + "("),
463
- ...flatMap(fields, (field, i, arr) =>
464
- i !== arr.length - 1 ? [expand(field), text(", ")]
465
- : trailingComma === "always" ? [expand(field), text(",")]
466
- : expand(field)
467
- ),
468
- ...(body.type === "text" ? [text(")")] : [text(") "), body]),
469
- ]),
470
- body.type === "text" ?
471
- shouldCollapse ?
472
- sequence([text(c.cyan(this._tag) + "("), firstFieldNode, text(")")])
473
- : between(
474
- fields.map((field, i, arr) =>
475
- trailingComma !== "none" || i !== arr.length - 1 ?
476
- pair(expand(field), text(","))
477
- : expand(field)
478
- ),
479
- text(c.cyan(this._tag) + "("),
480
- text(")")
481
- )
482
- : pair(
425
+ {
426
+ type: "variant",
427
+ inline: {
428
+ type: "sequence",
429
+ values: [
430
+ { type: "text", value: c.cyan(this._tag) + "(" },
431
+ ...fields
432
+ .map((field, i, arr) =>
433
+ i !== arr.length - 1 ?
434
+ [expand(field), { type: "text", value: ", " }]
435
+ : trailingComma === "always" ?
436
+ [expand(field), { type: "text", value: "," }]
437
+ : [expand(field)]
438
+ )
439
+ .reduce((acc, val) => acc.concat(val), []),
440
+ ...(body.type === "text" ?
441
+ [{ type: "text", value: ")" }]
442
+ : [{ type: "text", value: ") " }, body]),
443
+ ],
444
+ },
445
+ wrap:
446
+ body.type === "text" ?
483
447
  shouldCollapse ?
484
- sequence([
485
- text(c.cyan(this._tag) + "("),
486
- firstFieldNode,
487
- text(") "),
488
- ])
489
- : between(
490
- fields.map((field, i, arr) =>
448
+ {
449
+ type: "sequence",
450
+ values: [
451
+ { type: "text", value: c.cyan(this._tag) + "(" },
452
+ firstFieldNode,
453
+ { type: "text", value: ")" },
454
+ ],
455
+ }
456
+ : {
457
+ type: "between",
458
+ values: fields.map((field, i, arr) =>
491
459
  trailingComma !== "none" || i !== arr.length - 1 ?
492
- pair(expand(field), text(","))
460
+ {
461
+ type: "sequence",
462
+ values: [expand(field), { type: "text", value: "," }],
463
+ }
493
464
  : expand(field)
494
465
  ),
495
- text(c.cyan(this._tag) + "("),
496
- text(") ")
497
- ),
498
- body
499
- )
500
- )
501
- : body.type === "text" ? text(c.cyan(this._tag))
502
- : pair(text(c.cyan(this._tag) + " "), body)
466
+ open: { type: "text", value: c.cyan(this._tag) + "(" },
467
+ close: { type: "text", value: ")" },
468
+ }
469
+ : {
470
+ type: "sequence",
471
+ values: [
472
+ shouldCollapse ?
473
+ {
474
+ type: "sequence",
475
+ values: [
476
+ { type: "text", value: c.cyan(this._tag) + "(" },
477
+ firstFieldNode,
478
+ { type: "text", value: ") " },
479
+ ],
480
+ }
481
+ : {
482
+ type: "between",
483
+ values: fields.map((field, i, arr) =>
484
+ trailingComma !== "none" || i !== arr.length - 1 ?
485
+ {
486
+ type: "sequence",
487
+ values: [expand(field), { type: "text", value: "," }],
488
+ }
489
+ : expand(field)
490
+ ),
491
+ open: { type: "text", value: c.cyan(this._tag) + "(" },
492
+ close: { type: "text", value: ") " },
493
+ },
494
+ body,
495
+ ],
496
+ },
497
+ }
498
+ : body.type === "text" ? { type: "text", value: c.cyan(this._tag) }
499
+ : {
500
+ type: "sequence",
501
+ values: [{ type: "text", value: c.cyan(this._tag) + " " }, body],
502
+ }
503
503
  );
504
504
  }
505
- const text = (value) => ({ type: "text", value });
506
- const variant = (inline, wrap) => ({ type: "variant", inline, wrap });
507
- const sequence = (values) => ({ type: "sequence", values });
508
- const pair = (left, right) => sequence([left, right]);
509
- const between = (values, open, close) => ({
510
- type: "between",
511
- values,
512
- open,
513
- close,
514
- });
515
505
 
516
506
  export const PipeableProto = {
517
507
  pipe(...fs) {
@@ -548,34 +538,52 @@ export const PipeableProto = {
548
538
  }
549
539
  },
550
540
  };
551
- export function Pipeable() {}
552
- Pipeable.prototype = PipeableProto;
553
- Pipeable.prototype.constructor = Pipeable;
554
-
555
- export const PipeableFunctionProto = Object.assign({}, PipeableProto);
556
- Object.setPrototypeOf(PipeableFunctionProto, Function.prototype);
557
-
558
- export const ADTProto = Object.create(PipeableProto);
559
- ADTProto[Symbol.for("showify.inspect.custom")] = inspect;
560
- ADTProto[Symbol.for("nodejs.util.inspect.custom")] = function inspect() {
561
- return unwrap(this).reduce(
562
- (acc, cur, i) => {
563
- acc["_" + i] = cur;
564
- return acc;
565
- },
566
- { _tag: this._tag }
567
- );
568
- };
569
- export function ADT() {}
570
- ADT.prototype = ADTProto;
571
- ADT.prototype.constructor = ADT;
572
541
 
573
- export const ADTConstructorProto = Object.create(PipeableFunctionProto);
574
- ADTConstructorProto.toJSON = function toJSON() {
575
- return { _tag: this._tag };
576
- };
577
- ADTConstructorProto[Symbol.for("nodejs.util.inspect.custom")] =
578
- function inspect() {
542
+ /** @type {*} */
543
+ export const Pipeable = /* @__PURE__ */ (() => {
544
+ function Pipeable() {}
545
+ Pipeable.prototype = PipeableProto;
546
+ Pipeable.prototype.constructor = Pipeable;
547
+ return Pipeable;
548
+ })();
549
+
550
+ export const PipeableFunctionProto = /* @__PURE__ */ (() => {
551
+ const PipeableFunctionProto = Object.assign({}, PipeableProto);
552
+ return Object.setPrototypeOf(PipeableFunctionProto, Function.prototype);
553
+ })();
554
+
555
+ export const ADTProto = /* @__PURE__ */ (() => {
556
+ const ADTProto = Object.create(PipeableProto);
557
+ ADTProto[Symbol.for("showify.inspect.custom")] = inspect;
558
+ ADTProto[Symbol.for("nodejs.util.inspect.custom")] = function inspect() {
559
+ return unwrap(this).reduce(
560
+ (acc, cur, i) => {
561
+ acc["_" + i] = cur;
562
+ return acc;
563
+ },
564
+ { _tag: this._tag }
565
+ );
566
+ };
567
+ return ADTProto;
568
+ })();
569
+
570
+ /** @type {*} */
571
+ export const ADT = /* @__PURE__ */ (() => {
572
+ function ADT() {}
573
+ ADT.prototype = ADTProto;
574
+ ADT.prototype.constructor = ADT;
575
+ return ADT;
576
+ })();
577
+
578
+ export const ADTConstructorProto = /* @__PURE__ */ (() => {
579
+ const ADTConstructorProto = Object.create(PipeableFunctionProto);
580
+ ADTConstructorProto.toJSON = function toJSON() {
579
581
  return { _tag: this._tag };
580
582
  };
581
- ADTConstructorProto[Symbol.for("showify.inspect.custom")] = inspect;
583
+ ADTConstructorProto[Symbol.for("nodejs.util.inspect.custom")] =
584
+ function inspect() {
585
+ return { _tag: this._tag };
586
+ };
587
+ ADTConstructorProto[Symbol.for("showify.inspect.custom")] = inspect;
588
+ return ADTConstructorProto;
589
+ })();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "kind-adt",
3
- "version": "0.2.5",
3
+ "version": "0.2.6",
4
4
  "description": "🪴 The kind of ADTs you can count on in TypeScript",
5
5
  "keywords": [
6
6
  "typescript",