@nerd-bible/valio 0.0.12 → 0.1.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/.github/workflows/publish.yml +26 -0
- package/.github/workflows/test.yml +14 -0
- package/README.md +2 -2
- package/package.json +16 -17
- package/publish.ts +52 -0
- package/src/codecs.test.ts +4 -3
- package/src/codecs.ts +23 -24
- package/src/containers.test.ts +3 -2
- package/src/containers.ts +68 -39
- package/src/index.ts +4 -4
- package/src/pipe.ts +36 -22
- package/src/primitives.test.ts +5 -4
- package/src/primitives.ts +18 -12
- package/test/conllu.ts +226 -0
- package/test/gum.conllu +66 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +22 -0
- package/dist/codecs.d.ts +0 -12
- package/dist/codecs.d.ts.map +0 -1
- package/dist/codecs.js +0 -38
- package/dist/codecs.js.map +0 -1
- package/dist/containers.d.ts +0 -54
- package/dist/containers.d.ts.map +0 -1
- package/dist/containers.js +0 -193
- package/dist/containers.js.map +0 -1
- package/dist/index.d.ts +0 -5
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -5
- package/dist/index.js.map +0 -1
- package/dist/locales/en.d.ts +0 -2
- package/dist/locales/en.d.ts.map +0 -1
- package/dist/locales/en.js +0 -22
- package/dist/locales/en.js.map +0 -1
- package/dist/pipe.d.ts +0 -66
- package/dist/pipe.d.ts.map +0 -1
- package/dist/pipe.js +0 -150
- package/dist/pipe.js.map +0 -1
- package/dist/primitives.d.ts +0 -41
- package/dist/primitives.d.ts.map +0 -1
- package/dist/primitives.js +0 -89
- package/dist/primitives.js.map +0 -1
package/test/conllu.ts
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import * as z from "../src/index.ts";
|
|
4
|
+
import { test } from "node:test";
|
|
5
|
+
import expect from "expect";
|
|
6
|
+
|
|
7
|
+
const intId = z.codecs.number().gt(0);
|
|
8
|
+
const rowId = z.union([
|
|
9
|
+
// Normal integer like 1, 2
|
|
10
|
+
intId,
|
|
11
|
+
// Multiword token like 1-4 or 1.2-4.2
|
|
12
|
+
z.string().regex(/[1-9][0-9]*\.\d+-\d+\.\d+/),
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
const boolean = z.codecs.boolean({
|
|
16
|
+
true: /yes|true/,
|
|
17
|
+
false: /no|false/,
|
|
18
|
+
});
|
|
19
|
+
const primitive = z.union([z.string(), boolean, z.codecs.number()]);
|
|
20
|
+
|
|
21
|
+
function recordConllu(delims = { prop: "|", value: "=" }) {
|
|
22
|
+
return z.codecs.custom(z.string(), z.record(z.string(), primitive), {
|
|
23
|
+
decode(value: string, ctx: z.Context) {
|
|
24
|
+
let success = true;
|
|
25
|
+
const output: Record<string, z.Output<typeof primitive>> = {};
|
|
26
|
+
|
|
27
|
+
const length = ctx.jsonPath.length;
|
|
28
|
+
for (const cur of value.split(delims.prop)) {
|
|
29
|
+
// 14:nmod:into
|
|
30
|
+
const index = cur.indexOf(delims.value);
|
|
31
|
+
const k = index === -1 ? cur : cur.substring(0, index);
|
|
32
|
+
if (!k) continue;
|
|
33
|
+
const v = index === -1 ? "" : cur.substring(index + 1);
|
|
34
|
+
ctx.jsonPath[length] = k;
|
|
35
|
+
const decoded = primitive.decode(v, ctx);
|
|
36
|
+
if (decoded.success) output[k] = decoded.output;
|
|
37
|
+
else success = false;
|
|
38
|
+
}
|
|
39
|
+
ctx.jsonPath.length = length;
|
|
40
|
+
|
|
41
|
+
if (!success) return { success, errors: ctx.errors };
|
|
42
|
+
return { success, output };
|
|
43
|
+
},
|
|
44
|
+
encode(v: Record<string, z.Output<typeof primitive>>) {
|
|
45
|
+
const entries = Object.entries(v);
|
|
46
|
+
if (!entries.length) return { success: true, output: "_" };
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
success: true,
|
|
50
|
+
output: entries
|
|
51
|
+
.map(([k, v]) => `${k}${delims.value}${v}`)
|
|
52
|
+
.join(delims.prop),
|
|
53
|
+
};
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const word = z.object({
|
|
59
|
+
id: rowId,
|
|
60
|
+
form: z.string(),
|
|
61
|
+
lemma: z.string(),
|
|
62
|
+
upos: z.string(),
|
|
63
|
+
xpos: z.string(),
|
|
64
|
+
feats: recordConllu(),
|
|
65
|
+
head: z.codecs.number(),
|
|
66
|
+
deprel: z.string(),
|
|
67
|
+
deps: recordConllu({ prop: "|", value: ":" }),
|
|
68
|
+
//.pipe(z.record(z.codecs.number().gte(0), primitive)),
|
|
69
|
+
misc: recordConllu(),
|
|
70
|
+
});
|
|
71
|
+
const columns = Object.keys(word.shape) as (keyof z.Output<typeof word>)[];
|
|
72
|
+
|
|
73
|
+
const wordConllu = z.codecs.custom(z.string(), word, {
|
|
74
|
+
decode(v, ctx) {
|
|
75
|
+
const split = v
|
|
76
|
+
.split("\t")
|
|
77
|
+
// spec is unclear what a missing _ means
|
|
78
|
+
// the _ are there for readability in editors that don't show whitespace
|
|
79
|
+
.map((v) => (v === "_" ? "" : v));
|
|
80
|
+
|
|
81
|
+
const res = {} as any;
|
|
82
|
+
for (let i = 0; i < columns.length; i++)
|
|
83
|
+
res[columns[i] as (typeof columns)[number]] = split[i];
|
|
84
|
+
|
|
85
|
+
return word.decode(res, ctx);
|
|
86
|
+
},
|
|
87
|
+
encode(value) {
|
|
88
|
+
let output = "";
|
|
89
|
+
for (const c of columns) {
|
|
90
|
+
const v = value[c];
|
|
91
|
+
if (v === "") output += "_";
|
|
92
|
+
else {
|
|
93
|
+
const encoded = word.shape[c].encode(v as never);
|
|
94
|
+
if (!encoded.success) return encoded;
|
|
95
|
+
output += encoded.output;
|
|
96
|
+
}
|
|
97
|
+
if (c !== "misc") output += "\t";
|
|
98
|
+
}
|
|
99
|
+
return { success: true, output };
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const headers = z.record(z.string(), z.union([z.string(), z.undefined()])).pipe(
|
|
104
|
+
z
|
|
105
|
+
.object({
|
|
106
|
+
sent_id: z.string().minLength(1),
|
|
107
|
+
text: z.string().minLength(1),
|
|
108
|
+
})
|
|
109
|
+
.loose(),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
const headerPrefix = "# ";
|
|
113
|
+
const headersConllu = z.codecs.custom(z.string(), headers, {
|
|
114
|
+
decode(str, ctx) {
|
|
115
|
+
const kvs: Record<string, string | undefined> = {};
|
|
116
|
+
const lines = str.split(/\r?\n/);
|
|
117
|
+
for (let line of lines) {
|
|
118
|
+
line = line.substring(headerPrefix.length);
|
|
119
|
+
const i = line.indexOf("=");
|
|
120
|
+
if (i === -1) {
|
|
121
|
+
kvs[line] = undefined;
|
|
122
|
+
} else {
|
|
123
|
+
kvs[line.substring(0, i).trim()] = line.substring(i + 1).trim();
|
|
124
|
+
}
|
|
125
|
+
(ctx.jsonPath[0] as number) += 1;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
return headers.decode(kvs);
|
|
129
|
+
},
|
|
130
|
+
encode(obj) {
|
|
131
|
+
let output = "";
|
|
132
|
+
for (const k in obj) {
|
|
133
|
+
output += `# ${k}`;
|
|
134
|
+
if (obj[k] != null) output += ` = ${obj[k]}`;
|
|
135
|
+
output += "\n";
|
|
136
|
+
}
|
|
137
|
+
return { success: true, output };
|
|
138
|
+
},
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const sentence = z.object({ headers, words: z.array(word) });
|
|
142
|
+
const sentenceConllu = z.codecs.custom(z.string(), sentence, {
|
|
143
|
+
decode(str, ctx) {
|
|
144
|
+
const headersEnd = str.match(/^[^#]/m);
|
|
145
|
+
const output: z.Output<typeof sentence> = {
|
|
146
|
+
headers: {} as any,
|
|
147
|
+
words: [],
|
|
148
|
+
};
|
|
149
|
+
if (headersEnd?.index) {
|
|
150
|
+
const headerSection = str.substring(0, headersEnd.index - 1);
|
|
151
|
+
str = str.substring(headersEnd.index);
|
|
152
|
+
const decoded = headersConllu.decode(headerSection, ctx);
|
|
153
|
+
if (!decoded.success) return decoded;
|
|
154
|
+
output.headers = decoded.output;
|
|
155
|
+
}
|
|
156
|
+
const lines = str.split(/\r?\n/);
|
|
157
|
+
for (const line of lines) {
|
|
158
|
+
if (line) {
|
|
159
|
+
const decoded = wordConllu.decode(line, ctx);
|
|
160
|
+
if (!decoded.success) return decoded;
|
|
161
|
+
output.words.push(decoded.output);
|
|
162
|
+
}
|
|
163
|
+
(ctx.jsonPath[0] as number) += 1;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
return { success: true, output };
|
|
167
|
+
},
|
|
168
|
+
encode(sentence) {
|
|
169
|
+
const h = headersConllu.encode(sentence.headers!);
|
|
170
|
+
if (!h.success) return h;
|
|
171
|
+
let output = h.output;
|
|
172
|
+
for (const w of sentence.words!) {
|
|
173
|
+
const encoded = wordConllu.encode(w);
|
|
174
|
+
if (!encoded.success) return encoded;
|
|
175
|
+
output += encoded.output;
|
|
176
|
+
output += "\n";
|
|
177
|
+
}
|
|
178
|
+
return { success: true, output: output.trimEnd() };
|
|
179
|
+
},
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
// Makes sure syntax is followed and required fields are included.
|
|
183
|
+
const normal = z.codecs.custom(z.string(), z.array(sentence), {
|
|
184
|
+
decode(str, ctx) {
|
|
185
|
+
const output: z.Output<typeof sentence>[] = [];
|
|
186
|
+
const split = str.split(/\r?\n\r?\n/);
|
|
187
|
+
ctx.jsonPath[0] = 1;
|
|
188
|
+
for (const s of split) {
|
|
189
|
+
const decoded = sentenceConllu.decode(s, ctx);
|
|
190
|
+
if (!decoded.success) return decoded;
|
|
191
|
+
output.push(decoded.output);
|
|
192
|
+
ctx.jsonPath[0] += 2;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
return { success: true, output };
|
|
196
|
+
},
|
|
197
|
+
encode(sentences, ctx) {
|
|
198
|
+
let output = "";
|
|
199
|
+
for (const s of sentences) {
|
|
200
|
+
const encoded = sentenceConllu.encode(s, ctx);
|
|
201
|
+
if (!encoded.success) return encoded;
|
|
202
|
+
output += encoded.output;
|
|
203
|
+
output += "\n\n";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
return { success: true, output: output.trimEnd() };
|
|
207
|
+
},
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
test("parses and encodes gum", () => {
|
|
211
|
+
// const decoded = wordConllu.decode(
|
|
212
|
+
// "1 In in ADP IN _ 0 case 3:foo Verse=1|SourceMap=1",
|
|
213
|
+
// );
|
|
214
|
+
// if (decoded.success) {
|
|
215
|
+
// console.dir(decoded.output, { depth: null });
|
|
216
|
+
// console.log(wordConllu.encode(decoded.output).output)
|
|
217
|
+
// }
|
|
218
|
+
|
|
219
|
+
const text = readFileSync(join(import.meta.dirname, "gum.conllu"), "utf8");
|
|
220
|
+
const parsed = normal.decode(text);
|
|
221
|
+
expect(parsed.errors).toBeUndefined();
|
|
222
|
+
if (parsed.success) {
|
|
223
|
+
const reencoded = normal.encode(parsed.output);
|
|
224
|
+
expect(reencoded.errors).toBeUndefined();
|
|
225
|
+
}
|
|
226
|
+
});
|
package/test/gum.conllu
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# newdoc id = GUM_academic_exposure
|
|
2
|
+
# global.Entity = GRP-etype-infstat-salience-centering-minspan-link-identity
|
|
3
|
+
# meta::author = Kara Morgan-Short, Ingrid Finger, Sarah Grey, Michael T. Ullman
|
|
4
|
+
# meta::dateCollected = 2018-09-11
|
|
5
|
+
# meta::dateCreated = 2012-03-28
|
|
6
|
+
# meta::dateModified = 2012-03-28
|
|
7
|
+
# meta::genre = academic
|
|
8
|
+
# meta::salientEntities = 3 (5*), 56 (3*), 7 (2), 12 (2), 41 (2), 42 (2), 43 (2), 53 (2), 62 (2), 89 (2), 2 (1), 44 (1), 52 (1), 91 (1), 96 (1), 98 (1*), 116 (1)
|
|
9
|
+
# meta::sourceURL = https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0032974
|
|
10
|
+
# meta::speakerCount = 0
|
|
11
|
+
# meta::summary1 = (human1) This study shows that limited exposure to a second language (L2) after it is no longer being actively used generally causes attrition of L2 competence.
|
|
12
|
+
# meta::summary2 = (human2) Investigated the trends of six studies examining L2 possible attrition in classroom learned vs. immersion learned participants over time.
|
|
13
|
+
# meta::summary3 = (human3) Before reviewing the results of six similar studies, this paper presents a study that examines if a substantial period of no exposure following second language (L2) training leads to decreased proficiency and/or less native-like processes in adult learners, as well as if the outcome is influenced by classroom-like versus immersion-like contexts.
|
|
14
|
+
# meta::summary4 = (human4) This study tests the effect of limited exposure to second language after training on the neurocognition of L2 grammar in adult learners and describes previous studies on this topic that showed that in some cases limited exposure leads to attrition of L2 knowledge, but in other cases, there is either no attrition or even a gain in L2 performance.
|
|
15
|
+
# meta::summary5 = (human5) This section of a paper introduces the paper’s goal of examining the effect of substantial periods of no exposure on the neurocognition of adult-learned second language (L2) grammar, and discusses previous research findings on the effect of various periods of limited exposure following L2 training on L2 performance or knowledge.
|
|
16
|
+
# meta::title = Second Language Processing Shows Increased Native-Like Neural Responses after Months of No Exposure
|
|
17
|
+
# newpar
|
|
18
|
+
# newpar_block = head (1 s)
|
|
19
|
+
# sent_id = GUM_academic_exposure-1
|
|
20
|
+
# s_type = frag
|
|
21
|
+
# s_prominence = 2
|
|
22
|
+
# transition = establishment
|
|
23
|
+
# text = Introduction
|
|
24
|
+
1 Introduction introduction NOUN NN Number=Sing 0 root 0:root Discourse=organization-heading:1->29:3:grf-ly-|Entity=(1-abstract-new-nnnnn-cf1-1-sgl)|MSeg=Introduc-tion
|
|
25
|
+
|
|
26
|
+
# newpar
|
|
27
|
+
# newpar_block = p (8 s)
|
|
28
|
+
# sent_id = GUM_academic_exposure-2
|
|
29
|
+
# s_type = decl
|
|
30
|
+
# s_prominence = 3
|
|
31
|
+
# transition = null
|
|
32
|
+
# text = Research on adult-learned second language (L2) has provided considerable insight into the neurocognitive mechanisms underlying the learning and processing of L2 grammar [1]–[11].
|
|
33
|
+
1 Research research NOUN NN Number=Sing 12 nsubj 12:nsubj Discourse=context-background:2->14:2:sem-synym-4-8,108-109|Entity=(2-abstract-new-nnnns-cf2-1-coref
|
|
34
|
+
2 on on ADP IN _ 7 case 7:case _
|
|
35
|
+
3 adult adult NOUN NN Number=Sing 5 compound 5:compound Entity=(3-abstract-new-sssss-cf1-5-coref|SpaceAfter=No|XML=<w>
|
|
36
|
+
4 - - PUNCT HYPH _ 3 punct 3:punct SpaceAfter=No
|
|
37
|
+
5 learned learn VERB VBN Tense=Past|VerbForm=Part|Voice=Pass 7 amod 7:amod MSeg=learn-ed|XML=</w>
|
|
38
|
+
6 second second ADJ JJ Degree=Pos|NumForm=Word|NumType=Ord 7 amod 7:amod _
|
|
39
|
+
7 language language NOUN NN Number=Sing 1 nmod 1:nmod:on Entity=3)
|
|
40
|
+
8 ( ( PUNCT -LRB- _ 9 punct 9:punct Discourse=restatement-partial:3->2:0:sem-synym-4-8,10+grf-prn-9,11|SpaceAfter=No
|
|
41
|
+
9 L2 L2 NOUN NN Number=Sing 7 appos 7:appos Entity=(3-abstract-giv:act-sssss-cf1-1-appos)|SpaceAfter=No
|
|
42
|
+
10 ) ) PUNCT -RRB- _ 9 punct 9:punct Entity=2)
|
|
43
|
+
11 has have AUX VBZ Mood=Ind|Number=Sing|Person=3|Tense=Pres|VerbForm=Fin 12 aux 12:aux Discourse=same-unit_m:4->2:1:_|MSeg=ha-s
|
|
44
|
+
12 provided provide VERB VBN Tense=Past|VerbForm=Part 0 root 0:root MSeg=provid-ed
|
|
45
|
+
13 considerable considerable ADJ JJ Degree=Pos 14 amod 14:amod Entity=(4-abstract-new-nnnnn-cf4-2-sgl|MSeg=consider-able
|
|
46
|
+
14 insight insight NOUN NN Number=Sing 12 obj 12:obj MSeg=in-sigh-t
|
|
47
|
+
15 into into ADP IN _ 18 case 18:case MSeg=in-to
|
|
48
|
+
16 the the DET DT Definite=Def|PronType=Art 18 det 18:det Entity=(5-abstract-new-nnnnn-cf5-3-sgl
|
|
49
|
+
17 neurocognitive neurocognitive ADJ JJ Degree=Pos 18 amod 18:amod MSeg=neuro-cognit-ive
|
|
50
|
+
18 mechanisms mechanism NOUN NNS Number=Plur 14 nmod 14:nmod:into MSeg=mechan-ism-s
|
|
51
|
+
19 underlying underlie VERB VBG VerbForm=Ger 18 acl 18:acl Discourse=elaboration-attribute:5->4:0:syn-mdf-19+syn-nmn-20|MSeg=under-ly-ing
|
|
52
|
+
20 the the DET DT Definite=Def|PronType=Art 21 det 21:det Entity=(6-abstract-new-nnnnn-cf3-2,4-sgl
|
|
53
|
+
21 learning learning NOUN NN Number=Sing 19 obj 19:obj MSeg=learn-ing
|
|
54
|
+
22 and and CCONJ CC _ 23 cc 23:cc _
|
|
55
|
+
23 processing processing NOUN NN Number=Sing 21 conj 19:obj|21:conj:and MSeg=process-ing
|
|
56
|
+
24 of of ADP IN _ 26 case 26:case _
|
|
57
|
+
25 L2 L2 NOUN NN Number=Sing 26 compound 26:compound Entity=(7-abstract-new-nnnss-cf6-2-coref(3-abstract-giv:act-sssss-cf1-1-coref)
|
|
58
|
+
26 grammar grammar NOUN NN Number=Sing 21 nmod 21:nmod:of Entity=7)6)5)4)
|
|
59
|
+
27 [ [ PUNCT -LRB- _ 28 punct 28:punct Discourse=explanation-evidence:6->2:2:grf-prn-28,30,32,34|SpaceAfter=No|XML=<w>
|
|
60
|
+
28 1 1 NUM CD NumForm=Digit|NumType=Card 12 dep 12:dep Entity=(8-abstract-new-nnnnn-cf7-1-sgl)|SpaceAfter=No
|
|
61
|
+
29 ] ] PUNCT -RRB- _ 28 punct 28:punct SpaceAfter=No
|
|
62
|
+
30 – - SYM SYM _ 32 case 32:case SpaceAfter=No
|
|
63
|
+
31 [ [ PUNCT -LRB- _ 32 punct 32:punct SpaceAfter=No
|
|
64
|
+
32 11 11 NUM CD NumForm=Digit|NumType=Card 28 nmod 28:nmod:to Entity=(9-abstract-new-nnnnn-cf8-1-sgl)|SpaceAfter=No
|
|
65
|
+
33 ] ] PUNCT -RRB- _ 32 punct 32:punct SpaceAfter=No
|
|
66
|
+
34 . . PUNCT . _ 12 punct 12:punct XML=</w>
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"rootDir": "${configDir}/src",
|
|
4
|
+
"outDir": "${configDir}/dist",
|
|
5
|
+
|
|
6
|
+
// Target bundlers and latest versions of Node
|
|
7
|
+
"target": "esnext",
|
|
8
|
+
"module": "esnext",
|
|
9
|
+
"moduleResolution": "bundler",
|
|
10
|
+
// Only drop imports with `type` modifier
|
|
11
|
+
"verbatimModuleSyntax": true,
|
|
12
|
+
|
|
13
|
+
// Best practices
|
|
14
|
+
"strict": true,
|
|
15
|
+
"skipLibCheck": true,
|
|
16
|
+
|
|
17
|
+
// Node supports TS now
|
|
18
|
+
"allowImportingTsExtensions": true,
|
|
19
|
+
"rewriteRelativeImportExtensions": true
|
|
20
|
+
},
|
|
21
|
+
"include": ["${configDir}/src/**/*"]
|
|
22
|
+
}
|
package/dist/codecs.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import type { Context, Pipe, Result } from "./pipe";
|
|
2
|
-
import * as p from "./primitives";
|
|
3
|
-
export declare function custom<I, O>(input: Pipe<I, any>, output: Pipe<any, O>, codec: {
|
|
4
|
-
encode?(input: O, ctx: Context): Result<I>;
|
|
5
|
-
decode?(output: I, ctx: Context): Result<O>;
|
|
6
|
-
}): Pipe<I, O>;
|
|
7
|
-
export declare function number(parser?: (string: string) => number): p.Comparable<string | number | null | undefined, number>;
|
|
8
|
-
export declare function boolean(opts: {
|
|
9
|
-
true?: string[];
|
|
10
|
-
false?: string[];
|
|
11
|
-
}): Pipe<any, boolean>;
|
|
12
|
-
//# sourceMappingURL=codecs.d.ts.map
|
package/dist/codecs.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"codecs.d.ts","sourceRoot":"","sources":["../src/codecs.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,wBAAgB,MAAM,CAAC,CAAC,EAAE,CAAC,EAC1B,KAAK,EAAE,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,EACnB,MAAM,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EACpB,KAAK,EAAE;IACN,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;IAC3C,MAAM,CAAC,CAAC,MAAM,EAAE,CAAC,EAAE,GAAG,EAAE,OAAO,GAAG,MAAM,CAAC,CAAC,CAAC,CAAC;CAC5C,GACC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAMZ;AAED,wBAAgB,MAAM,CACrB,MAAM,6BAAoB,GACxB,CAAC,CAAC,UAAU,CAAC,MAAM,GAAG,MAAM,GAAG,IAAI,GAAG,SAAS,EAAE,MAAM,CAAC,CAkB1D;AAED,wBAAgB,OAAO,CAAC,IAAI,EAAE;IAC7B,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,KAAK,CAAC,EAAE,MAAM,EAAE,CAAC;CACjB,GAAG,IAAI,CAAC,GAAG,EAAE,OAAO,CAAC,CAWrB"}
|
package/dist/codecs.js
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import * as c from "./containers";
|
|
2
|
-
import * as p from "./primitives";
|
|
3
|
-
export function custom(input, output, codec) {
|
|
4
|
-
const res = output.clone();
|
|
5
|
-
res.i = input.i.clone();
|
|
6
|
-
res.i.transform = codec.decode;
|
|
7
|
-
res.o.transform = codec.encode;
|
|
8
|
-
return res;
|
|
9
|
-
}
|
|
10
|
-
export function number(parser = Number.parseFloat) {
|
|
11
|
-
return custom(c.union([p.string(), p.number(), p.null(), p.undefined()]), p.number(), {
|
|
12
|
-
decode(input, ctx) {
|
|
13
|
-
if (typeof input == "number")
|
|
14
|
-
return { success: true, output: input };
|
|
15
|
-
if (input == null || input.toLowerCase() == "nan")
|
|
16
|
-
return { success: true, output: Number.NaN };
|
|
17
|
-
const output = parser(input);
|
|
18
|
-
if (!Number.isNaN(output))
|
|
19
|
-
return { success: true, output };
|
|
20
|
-
ctx.pushErrorFmt("coerce", input, { expected: "number" });
|
|
21
|
-
return { success: false, errors: ctx.errors };
|
|
22
|
-
},
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
export function boolean(opts) {
|
|
26
|
-
return custom(p.any(), p.boolean(), {
|
|
27
|
-
decode(input) {
|
|
28
|
-
if (typeof input === "string") {
|
|
29
|
-
if (opts.true?.includes(input))
|
|
30
|
-
return { success: true, output: true };
|
|
31
|
-
if (opts.false?.includes(input))
|
|
32
|
-
return { success: true, output: false };
|
|
33
|
-
}
|
|
34
|
-
return { success: true, output: Boolean(input) };
|
|
35
|
-
},
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
//# sourceMappingURL=codecs.js.map
|
package/dist/codecs.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"codecs.js","sourceRoot":"","sources":["../src/codecs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,MAAM,UAAU,MAAM,CACrB,KAAmB,EACnB,MAAoB,EACpB,KAGC;IAED,MAAM,GAAG,GAAG,MAAM,CAAC,KAAK,EAAS,CAAC;IAClC,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;IACxB,GAAG,CAAC,CAAC,CAAC,SAAS,GAAG,KAAK,CAAC,MAAM,CAAC;IAC/B,GAAG,CAAC,CAAC,CAAC,SA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EAAE,CAAC;gBAC/B,IAAI,IAAI,CAAC,IAAI,EAAE,QAAQ,CAAC,KAAK,CAAC;oBAAE,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;gBACvE,IAAI,IAAI,CAAC,KAAK,EAAE,QAAQ,CAAC,KAAK,CAAC;oBAC9B,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC;YAC1C,CAAC;YACD,OAAO,EAAE,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QAClD,CAAC;KACD,CAAC,CAAC;AACJ,CAAC"}
|
package/dist/containers.d.ts
DELETED
|
@@ -1,54 +0,0 @@
|
|
|
1
|
-
import type { Input, Output, Result } from "./pipe";
|
|
2
|
-
import { type Context, Pipe } from "./pipe";
|
|
3
|
-
import * as p from "./primitives";
|
|
4
|
-
declare class ValioArray<T> extends p.Arrayish<any[], T[]> {
|
|
5
|
-
element: Pipe<any, T>;
|
|
6
|
-
constructor(element: Pipe<any, T>);
|
|
7
|
-
}
|
|
8
|
-
export declare function array<T>(element: Pipe<any, T>): ValioArray<T>;
|
|
9
|
-
declare class ValioRecord<K extends PropertyKey, V> extends Pipe<Record<any, any>, Record<K, V>> {
|
|
10
|
-
keyPipe: Pipe<any, K>;
|
|
11
|
-
valPipe: Pipe<any, V>;
|
|
12
|
-
constructor(keyPipe: Pipe<any, K>, valPipe: Pipe<any, V>);
|
|
13
|
-
}
|
|
14
|
-
export declare function record<K extends PropertyKey, V>(keyPipe: Pipe<any, K>, valPipe: Pipe<any, V>): ValioRecord<K, V>;
|
|
15
|
-
declare class Union<T extends Readonly<Pipe[]>> extends Pipe<Output<T[number]>, Output<T[number]>> {
|
|
16
|
-
options: T;
|
|
17
|
-
constructor(options: T);
|
|
18
|
-
}
|
|
19
|
-
export declare function union<T extends Readonly<Pipe[]>>(options: T): Union<T>;
|
|
20
|
-
type ObjectOutput<Shape extends Record<string, Pipe<any, any>>> = {
|
|
21
|
-
[K in keyof Shape]: Output<Shape[K]>;
|
|
22
|
-
};
|
|
23
|
-
type Mask<Keys extends PropertyKey> = {
|
|
24
|
-
[K in Keys]?: true;
|
|
25
|
-
};
|
|
26
|
-
type Identity<T> = T;
|
|
27
|
-
type Flatten<T> = Identity<{
|
|
28
|
-
[k in keyof T]: T[k];
|
|
29
|
-
}>;
|
|
30
|
-
type Extend<A extends Record<any, any>, B extends Record<any, any>> = Flatten<keyof A & keyof B extends never ? A & B : {
|
|
31
|
-
[K in keyof A as K extends keyof B ? never : K]: A[K];
|
|
32
|
-
} & {
|
|
33
|
-
[K in keyof B]: B[K];
|
|
34
|
-
}>;
|
|
35
|
-
declare class ValioObject<Shape extends Record<any, Pipe<any, any>>> extends Pipe<Record<any, any>, ObjectOutput<Shape>> {
|
|
36
|
-
shape: Shape;
|
|
37
|
-
isLoose: boolean;
|
|
38
|
-
constructor(shape: Shape, isLoose: boolean);
|
|
39
|
-
clone(): this;
|
|
40
|
-
protected transformInput(data: object, ctx: Context): Result<ObjectOutput<Shape>>;
|
|
41
|
-
protected typeCheckOutput(v: any): v is ObjectOutput<Shape>;
|
|
42
|
-
pick<M extends Mask<keyof Shape>>(mask: M): ValioObject<Flatten<Pick<Shape, Extract<keyof Shape, keyof M>>>>;
|
|
43
|
-
omit<M extends Mask<keyof Shape>>(mask: M): ValioObject<Flatten<Omit<Shape, Extract<keyof Shape, keyof M>>>>;
|
|
44
|
-
partial<M extends Mask<keyof Shape>>(mask: M): ValioObject<{
|
|
45
|
-
[k in keyof Shape]: k extends keyof M ? Pipe<Input<Shape[k]>, Output<Shape[k]> | undefined> : Shape[k];
|
|
46
|
-
}>;
|
|
47
|
-
extend<T extends Record<any, Pipe<any, any>>>(shape: T): ValioObject<Extend<Shape, T>>;
|
|
48
|
-
loose<T = any>(isLoose?: boolean): ValioObject<Shape & {
|
|
49
|
-
[k: string]: Pipe<T, T>;
|
|
50
|
-
}>;
|
|
51
|
-
}
|
|
52
|
-
export declare function object<Shape extends Record<any, Pipe<any, any>>>(shape: Shape, loose?: boolean): ValioObject<Shape>;
|
|
53
|
-
export {};
|
|
54
|
-
//# sourceMappingURL=containers.d.ts.map
|
package/dist/containers.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"containers.d.ts","sourceRoot":"","sources":["../src/containers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACpD,OAAO,EAAE,KAAK,OAAO,EAAY,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,cAAM,UAAU,CAAC,CAAC,CAAE,SAAQ,CAAC,CAAC,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,EAAE,CAAC;IAC9B,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;gBAArB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;CA6BxC;AACD,wBAAgB,KAAK,CAAC,CAAC,EAAE,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,GAAG,UAAU,CAAC,CAAC,CAAC,CAE7D;AAED,cAAM,WAAW,CAAC,CAAC,SAAS,WAAW,EAAE,CAAC,CAAE,SAAQ,IAAI,CACvD,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAChB,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CACZ;IAEQ,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IACrB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;gBADrB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC,EACrB,OAAO,EAAE,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;CA+C7B;AACD,wBAAgB,MAAM,CAAC,CAA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,CAAC,EAClC,IAAI,EAAE,CAAC,GACL,WAAW,CAAC;SACb,CAAC,IAAI,MAAM,KAAK,GAAG,CAAC,SAAS,MAAM,CAAC,GAClC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,GACnD,KAAK,CAAC,CAAC,CAAC;KACX,CAAC;IAWF,MAAM,CAAC,CAAC,SAAS,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC3C,KAAK,EAAE,CAAC,GACN,WAAW,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAMhC,KAAK,CAAC,CAAC,GAAG,GAAG,EACZ,OAAO,UAAO,GACZ,WAAW,CAAC,KAAK,GAAG;QAAE,CAAC,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAA;KAAE,CAAC;CAKnD;AACD,wBAAgB,MAAM,CAAC,KAAK,SAAS,MAAM,CAAC,GAAG,EAAE,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC,EAC/D,KAAK,EAAE,KAAK,EACZ,KAAK,UAAQ,GACX,WAAW,CAAC,KAAK,CAAC,CAEpB"}
|
package/dist/containers.js
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import { HalfPipe, Pipe } from "./pipe";
|
|
2
|
-
import * as p from "./primitives";
|
|
3
|
-
class ValioArray extends p.Arrayish {
|
|
4
|
-
element;
|
|
5
|
-
constructor(element) {
|
|
6
|
-
super(new HalfPipe("array", (v) => Array.isArray(v), (input, ctx) => {
|
|
7
|
-
const output = new Array(input.length);
|
|
8
|
-
let success = true;
|
|
9
|
-
const length = ctx.jsonPath.length;
|
|
10
|
-
for (let i = 0; i < input.length; i++) {
|
|
11
|
-
ctx.jsonPath[length] = i.toString();
|
|
12
|
-
const decoded = element.decode(input[i], ctx);
|
|
13
|
-
if (decoded.success)
|
|
14
|
-
output[i] = decoded.output;
|
|
15
|
-
else
|
|
16
|
-
success = false;
|
|
17
|
-
}
|
|
18
|
-
ctx.jsonPath.length = length;
|
|
19
|
-
if (!success)
|
|
20
|
-
return { success, errors: ctx.errors };
|
|
21
|
-
return { success, output };
|
|
22
|
-
}), new HalfPipe(`array<${element.o.name}>`, (v) => {
|
|
23
|
-
if (!Array.isArray(v))
|
|
24
|
-
return false;
|
|
25
|
-
for (const e of v)
|
|
26
|
-
if (!element.o.typeCheck(e))
|
|
27
|
-
return false;
|
|
28
|
-
return true;
|
|
29
|
-
}));
|
|
30
|
-
this.element = element;
|
|
31
|
-
}
|
|
32
|
-
}
|
|
33
|
-
export function array(element) {
|
|
34
|
-
return new ValioArray(element);
|
|
35
|
-
}
|
|
36
|
-
class ValioRecord extends Pipe {
|
|
37
|
-
keyPipe;
|
|
38
|
-
valPipe;
|
|
39
|
-
constructor(keyPipe, valPipe) {
|
|
40
|
-
super(new HalfPipe("object", (v) => Object.prototype.toString.call(v) == "[object Object]", (input, ctx) => {
|
|
41
|
-
const output = {};
|
|
42
|
-
let success = true;
|
|
43
|
-
const length = ctx.jsonPath.length;
|
|
44
|
-
for (const key in input) {
|
|
45
|
-
ctx.jsonPath[length] = key;
|
|
46
|
-
const decodedKey = keyPipe.decode(key, ctx);
|
|
47
|
-
if (decodedKey.success) {
|
|
48
|
-
const decodedVal = valPipe.decode(input[key], ctx);
|
|
49
|
-
if (decodedVal.success) {
|
|
50
|
-
output[decodedKey.output] = decodedVal.output;
|
|
51
|
-
}
|
|
52
|
-
else {
|
|
53
|
-
success = false;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
success = false;
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
ctx.jsonPath.length = length;
|
|
61
|
-
if (!success)
|
|
62
|
-
return { success, errors: ctx.errors };
|
|
63
|
-
return { success, output };
|
|
64
|
-
}), new HalfPipe(`record<${keyPipe.o.name},${valPipe.o.name}>`, (v) => {
|
|
65
|
-
if (Object.prototype.toString.call(v) != "[object Object]")
|
|
66
|
-
return false;
|
|
67
|
-
for (const k in v) {
|
|
68
|
-
// Keys will always be strings.
|
|
69
|
-
// if (!keyPipe.o.typeCheck(k)) return false;
|
|
70
|
-
if (!valPipe.o.typeCheck(v[k]))
|
|
71
|
-
return false;
|
|
72
|
-
}
|
|
73
|
-
return true;
|
|
74
|
-
}));
|
|
75
|
-
this.keyPipe = keyPipe;
|
|
76
|
-
this.valPipe = valPipe;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
export function record(keyPipe, valPipe) {
|
|
80
|
-
return new ValioRecord(keyPipe, valPipe);
|
|
81
|
-
}
|
|
82
|
-
class Union extends Pipe {
|
|
83
|
-
options;
|
|
84
|
-
constructor(options) {
|
|
85
|
-
const name = options.map((o) => o.o.name).join("|");
|
|
86
|
-
super(new HalfPipe(name, (v) => {
|
|
87
|
-
for (const f of options)
|
|
88
|
-
if (f.i.typeCheck(v))
|
|
89
|
-
return true;
|
|
90
|
-
return false;
|
|
91
|
-
}, (data, ctx) => {
|
|
92
|
-
const newCtx = ctx.clone();
|
|
93
|
-
for (const s in options) {
|
|
94
|
-
const decoded = options[s].decode(data, newCtx);
|
|
95
|
-
if (decoded.success)
|
|
96
|
-
return decoded;
|
|
97
|
-
}
|
|
98
|
-
Object.assign(ctx.errors, newCtx.errors);
|
|
99
|
-
return { success: false, errors: ctx.errors };
|
|
100
|
-
}), new HalfPipe(name, (v) => {
|
|
101
|
-
for (const f of options)
|
|
102
|
-
if (f.o.typeCheck(v))
|
|
103
|
-
return true;
|
|
104
|
-
return false;
|
|
105
|
-
}));
|
|
106
|
-
this.options = options;
|
|
107
|
-
}
|
|
108
|
-
}
|
|
109
|
-
export function union(options) {
|
|
110
|
-
return new Union(options);
|
|
111
|
-
}
|
|
112
|
-
class ValioObject extends Pipe {
|
|
113
|
-
shape;
|
|
114
|
-
isLoose;
|
|
115
|
-
constructor(shape, isLoose) {
|
|
116
|
-
super(new HalfPipe("object", (v) => Object.prototype.toString.call(v) == "[object Object]", (data, ctx) => this.transformInput(data, ctx)), new HalfPipe(`{${Object.entries(shape)
|
|
117
|
-
.map(([k, v]) => `${k}: ${v.o.name}`)
|
|
118
|
-
.join(",")}}`, (v) => this.typeCheckOutput(v)));
|
|
119
|
-
this.shape = shape;
|
|
120
|
-
this.isLoose = isLoose;
|
|
121
|
-
}
|
|
122
|
-
clone() {
|
|
123
|
-
return new ValioObject(this.shape, this.isLoose);
|
|
124
|
-
}
|
|
125
|
-
transformInput(data, ctx) {
|
|
126
|
-
const output = this.isLoose ? data : {};
|
|
127
|
-
let success = true;
|
|
128
|
-
const length = ctx.jsonPath.length;
|
|
129
|
-
// Always expect the shape since that's what typescript does.
|
|
130
|
-
for (const p in this.shape) {
|
|
131
|
-
ctx.jsonPath[length] = p;
|
|
132
|
-
const decoded = this.shape[p].decode(data[p], ctx);
|
|
133
|
-
if (decoded.success)
|
|
134
|
-
output[p] = decoded.output;
|
|
135
|
-
else {
|
|
136
|
-
success = false;
|
|
137
|
-
delete output[p];
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
ctx.jsonPath.length = length;
|
|
141
|
-
if (!success)
|
|
142
|
-
return { success, errors: ctx.errors };
|
|
143
|
-
return { success, output: output };
|
|
144
|
-
}
|
|
145
|
-
typeCheckOutput(v) {
|
|
146
|
-
if (Object.prototype.toString.call(v) != "[object Object]")
|
|
147
|
-
return false;
|
|
148
|
-
for (const s in this.shape)
|
|
149
|
-
if (!this.shape[s].o.typeCheck(v[s]))
|
|
150
|
-
return false;
|
|
151
|
-
return true;
|
|
152
|
-
}
|
|
153
|
-
pick(mask) {
|
|
154
|
-
const next = this.clone();
|
|
155
|
-
for (const k in next.shape) {
|
|
156
|
-
if (!mask[k])
|
|
157
|
-
delete next.shape[k];
|
|
158
|
-
}
|
|
159
|
-
return next;
|
|
160
|
-
}
|
|
161
|
-
omit(mask) {
|
|
162
|
-
const next = this.clone();
|
|
163
|
-
for (const k in next.shape) {
|
|
164
|
-
if (mask[k])
|
|
165
|
-
delete next.shape[k];
|
|
166
|
-
}
|
|
167
|
-
return next;
|
|
168
|
-
}
|
|
169
|
-
partial(mask) {
|
|
170
|
-
const next = this.clone();
|
|
171
|
-
for (const k in next.shape) {
|
|
172
|
-
if (mask[k]) {
|
|
173
|
-
// @ts-expect-error
|
|
174
|
-
next.shape[k] = union([next.shape[k], p.undefined()]);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return next;
|
|
178
|
-
}
|
|
179
|
-
extend(shape) {
|
|
180
|
-
const next = this.clone();
|
|
181
|
-
Object.assign(next.shape, shape);
|
|
182
|
-
return next;
|
|
183
|
-
}
|
|
184
|
-
loose(isLoose = true) {
|
|
185
|
-
const next = this.clone();
|
|
186
|
-
next.isLoose = isLoose;
|
|
187
|
-
return next;
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
export function object(shape, loose = false) {
|
|
191
|
-
return new ValioObject(shape, loose);
|
|
192
|
-
}
|
|
193
|
-
//# sourceMappingURL=containers.js.map
|
package/dist/containers.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"containers.js","sourceRoot":"","sources":["../src/containers.ts"],"names":[],"mappings":"AACA,OAAO,EAAgB,QAAQ,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AACtD,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAElC,MAAM,UAAc,SAAQ,CAAC,CAAC,QAAoB;IAC9B;IAAnB,YAAmB,OAAqB;QACvC,KAAK,CACJ,IAAI,QAAQ,CACX,OAAO,EACP,CAAC,CAAM,EAAc,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EACxC,CAAC,KAAY,EAAE,GAAY,EAAe,EAAE;YAC3C,MAAM,MAAM,GAAG,IAAI,KAAK,CAAI,KAAK,CAAC,MAAM,CAAC,CAAC;Y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,mBAAmB;gBACnB,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,SAAS,EAAE,CAAC,CAAC,CAAC;YACvD,CAAC;QACF,CAAC;QACD,OAAO,IAAW,CAAC;IACpB,CAAC;IAED,MAAM,CACL,KAAQ;QAER,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QAC1B,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACjC,OAAO,IAAW,CAAC;IACpB,CAAC;IAED,KAAK,CACJ,OAAO,GAAG,IAAI;QAEd,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,EAAE,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,OAAO,IAAW,CAAC;IACpB,CAAC;CACD;AACD,MAAM,UAAU,MAAM,CACrB,KAAY,EACZ,KAAK,GAAG,KAAK;IAEb,OAAO,IAAI,WAAW,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AACtC,CAAC"}
|
package/dist/index.d.ts
DELETED
package/dist/index.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,UAAU,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC"}
|