@nerd-bible/valio 0.0.11 → 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.
@@ -1,5 +1,6 @@
1
- import { expect, test } from "bun:test";
2
- import * as v from "./index";
1
+ import { test } from "node:test";
2
+ import expect from "expect";
3
+ import * as v from "./index.ts";
3
4
 
4
5
  test("number", () => {
5
6
  const schema = v.number();
@@ -12,7 +13,7 @@ test("number", () => {
12
13
  });
13
14
 
14
15
  test("custom validator", () => {
15
- const schema = v.number().refine((n) => n == 5, "eq", { n: 5 });
16
+ const schema = v.number().refine((n) => n === 5, "eq", { n: 5 });
16
17
 
17
18
  expect(schema.decode(5)).toEqual({ success: true, output: 5 });
18
19
  expect(schema.encode(3)).toEqual({
@@ -22,7 +23,7 @@ test("custom validator", () => {
22
23
  });
23
24
 
24
25
  test("custom context", () => {
25
- const schema = v.number().refine((n) => n == 5, "eq", { n: 5 });
26
+ const schema = v.number().refine((n) => n === 5, "eq", { n: 5 });
26
27
  class MyContext extends v.Context {
27
28
  errorFmt() {
28
29
  return "You done messed up";
package/src/primitives.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { HalfPipe, Pipe } from "./pipe";
1
+ import { HalfPipe, Pipe } from "./pipe.ts";
2
2
 
3
3
  function primitive<T>(name: string, typeCheck: (v: T) => v is T) {
4
4
  const half = new HalfPipe(name, typeCheck);
@@ -8,7 +8,7 @@ function primitive<T>(name: string, typeCheck: (v: T) => v is T) {
8
8
  export function boolean() {
9
9
  return primitive<boolean>(
10
10
  "boolean",
11
- (v): v is boolean => typeof v == "boolean",
11
+ (v): v is boolean => typeof v === "boolean",
12
12
  );
13
13
  }
14
14
 
@@ -16,12 +16,12 @@ export function boolean() {
16
16
  export function undefined() {
17
17
  return primitive<undefined>(
18
18
  "undefined",
19
- (v): v is undefined => typeof v == "undefined",
19
+ (v): v is undefined => typeof v === "undefined",
20
20
  );
21
21
  }
22
22
 
23
23
  export function any() {
24
- return primitive<any>("any", (v): v is any => true);
24
+ return primitive<any>("any", (_v): _v is any => true);
25
25
  }
26
26
 
27
27
  function null_() {
@@ -43,13 +43,13 @@ export class Comparable<I, O> extends Pipe<I, O> {
43
43
  return this.refine((v) => v <= n, "lte", { n });
44
44
  }
45
45
  eq(n: O) {
46
- return this.refine((v) => v == n, "eq", { n });
46
+ return this.refine((v) => v === n, "eq", { n });
47
47
  }
48
48
  }
49
49
 
50
50
  class ValioNumber extends Comparable<number, number> {
51
51
  constructor() {
52
- const half = new HalfPipe("number", (v) => typeof v == "number");
52
+ const half = new HalfPipe("number", (v) => typeof v === "number");
53
53
  super(half, half);
54
54
  }
55
55
  }
@@ -71,9 +71,9 @@ export class Arrayish<
71
71
  }
72
72
  }
73
73
 
74
- class ValioString extends Pipe<string, string> {
74
+ class ValioString extends Arrayish<string, string> {
75
75
  constructor() {
76
- const half = new HalfPipe("string", (v) => typeof v == "string");
76
+ const half = new HalfPipe("string", (v) => typeof v === "string");
77
77
  super(half, half);
78
78
  }
79
79
 
@@ -88,9 +88,12 @@ export function string(): ValioString {
88
88
  export type Lit = string | number | bigint | boolean | null | undefined;
89
89
 
90
90
  class ValioLiteral<T extends Lit> extends Pipe<T, T> {
91
- constructor(public literal: T) {
92
- const half = new HalfPipe(`${literal}`, (v): v is T => v == literal);
91
+ literal: T;
92
+
93
+ constructor(literal: T) {
94
+ const half = new HalfPipe(`${literal}`, (v): v is T => v === literal);
93
95
  super(half, half);
96
+ this.literal = literal;
94
97
  }
95
98
  }
96
99
  export function literal<T extends Lit>(literal: T) {
@@ -98,14 +101,17 @@ export function literal<T extends Lit>(literal: T) {
98
101
  }
99
102
 
100
103
  class ValioEnum<T extends Lit> extends Pipe<T, T> {
101
- constructor(public literals: T[]) {
104
+ literals: readonly T[];
105
+
106
+ constructor(literals: readonly T[]) {
102
107
  const half = new HalfPipe(`${literals.join(",")}`, (v: any): v is T =>
103
108
  literals.includes(v),
104
109
  );
105
110
  super(half, half);
111
+ this.literals = literals;
106
112
  }
107
113
  }
108
- function enum_<T extends Lit>(literals: T[]): ValioEnum<T> {
114
+ function enum_<T extends Lit>(literals: readonly T[]): ValioEnum<T> {
109
115
  return new ValioEnum(literals);
110
116
  }
111
117
  export { enum_ as enum };
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
+ });
@@ -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>
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["${configDir}/src/**/*.test.ts"]
4
+ }
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
@@ -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
@@ -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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AAC,IAGvB;IACA,OAAO,MAAM,CAAC,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,EAAE,EAAE;QACnC,MAAM,CAAC,KAAK;YACX,IAAI,OAAO,KAAK,KAAK,QAAQ,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"}
@@ -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
@@ -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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