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.
- package/index.js +139 -131
- 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
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
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
|
-
|
|
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
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
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) =>
|
|
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
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
:
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
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
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
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
|
-
|
|
460
|
+
{
|
|
461
|
+
type: "sequence",
|
|
462
|
+
values: [expand(field), { type: "text", value: "," }],
|
|
463
|
+
}
|
|
493
464
|
: expand(field)
|
|
494
465
|
),
|
|
495
|
-
text
|
|
496
|
-
text
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
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
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
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("
|
|
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
|
+
})();
|