zerde 0.1.3 → 0.1.4
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/dist/index.d.ts +57 -31
- package/dist/index.js +1 -1
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,28 @@
|
|
|
1
|
+
import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
|
|
2
|
+
import { CreateNodeOptions, DocumentOptions, ParseOptions as ParseOptions$2, SchemaOptions, ToJSOptions, ToStringOptions } from "yaml";
|
|
1
3
|
import * as neverthrow0$1 from "neverthrow";
|
|
2
4
|
import * as neverthrow0 from "neverthrow";
|
|
3
5
|
import * as neverthrow1 from "neverthrow";
|
|
4
6
|
import { ResultAsync } from "neverthrow";
|
|
5
|
-
import {
|
|
6
|
-
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
7
|
+
import { StandardSchemaV1, StandardSchemaV1 as StandardSchemaV1$1 } from "@standard-schema/spec";
|
|
7
8
|
|
|
8
9
|
//#region src/formats/json.d.ts
|
|
9
10
|
type ParseJSONOptions = Partial<ParseOptions$1>;
|
|
11
|
+
declare const defaultParseJSONOptions: {
|
|
12
|
+
allowEmptyContent: false;
|
|
13
|
+
allowTrailingComma: false;
|
|
14
|
+
disallowComments: true;
|
|
15
|
+
};
|
|
16
|
+
declare function parseJSON(stringifiedContent: string, parseOptions?: ParseJSONOptions): unknown;
|
|
10
17
|
type StringifyJSONOptions = string | number;
|
|
18
|
+
declare const defaultStringifyJSONOptions: 0;
|
|
19
|
+
declare function stringifyJSON(unknownContent: unknown, stringifyOptions?: StringifyJSONOptions): string;
|
|
20
|
+
//#endregion
|
|
21
|
+
//#region src/formats/yaml.d.ts
|
|
22
|
+
type ParseYAMLOptions = DocumentOptions & ParseOptions$2 & SchemaOptions & ToJSOptions;
|
|
23
|
+
declare function parseYAML(stringifiedContent: string, parseOptions?: ParseYAMLOptions): unknown;
|
|
24
|
+
type StringifyYAMLOptions = CreateNodeOptions & DocumentOptions & ParseOptions$2 & SchemaOptions & ToStringOptions;
|
|
25
|
+
declare function stringifyYAML(unknownContent: unknown, stringifyOptions?: StringifyYAMLOptions): string;
|
|
11
26
|
//#endregion
|
|
12
27
|
//#region src/utils.d.ts
|
|
13
28
|
type ErrorInfo<T> = T & {
|
|
@@ -16,24 +31,31 @@ type ErrorInfo<T> = T & {
|
|
|
16
31
|
declare class EnhancedError<T = unknown> extends Error {
|
|
17
32
|
constructor(message: string, info: ErrorInfo<T>);
|
|
18
33
|
}
|
|
34
|
+
declare function extractFormatSuffix(format: string): string;
|
|
19
35
|
//#endregion
|
|
20
36
|
//#region src/parse.d.ts
|
|
21
37
|
declare class ParseError extends EnhancedError {}
|
|
22
|
-
type ParseOptions = {
|
|
38
|
+
type ParseOptions = Partial<{
|
|
23
39
|
json: ParseJSONOptions;
|
|
24
|
-
|
|
25
|
-
|
|
40
|
+
yaml: ParseYAMLOptions;
|
|
41
|
+
format: string;
|
|
42
|
+
}>;
|
|
26
43
|
declare const defaultParseOptions: {
|
|
27
44
|
readonly json: {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
45
|
+
allowEmptyContent: false;
|
|
46
|
+
allowTrailingComma: false;
|
|
47
|
+
disallowComments: true;
|
|
31
48
|
};
|
|
49
|
+
readonly yaml: {};
|
|
32
50
|
};
|
|
33
|
-
declare const parseIt: (stringifiedContent: string, parseOptions:
|
|
51
|
+
declare const parseIt: (stringifiedContent: string, parseOptions: Partial<{
|
|
52
|
+
json: ParseJSONOptions;
|
|
53
|
+
yaml: ParseYAMLOptions;
|
|
54
|
+
format: string;
|
|
55
|
+
}>) => neverthrow0$1.Result<unknown, ParseError>;
|
|
34
56
|
//#endregion
|
|
35
57
|
//#region src/schema.d.ts
|
|
36
|
-
interface StringSchema extends StandardSchemaV1<string> {
|
|
58
|
+
interface StringSchema extends StandardSchemaV1$1<string> {
|
|
37
59
|
type: "string";
|
|
38
60
|
message: string;
|
|
39
61
|
}
|
|
@@ -41,13 +63,18 @@ declare function parseString(message?: string): StringSchema;
|
|
|
41
63
|
//#endregion
|
|
42
64
|
//#region src/stringify.d.ts
|
|
43
65
|
declare class StringifyError extends EnhancedError {}
|
|
44
|
-
type StringifyOptions = {
|
|
66
|
+
type StringifyOptions = Partial<{
|
|
45
67
|
json: StringifyJSONOptions;
|
|
46
|
-
|
|
68
|
+
yaml: StringifyYAMLOptions;
|
|
69
|
+
}>;
|
|
47
70
|
declare const defaultStringifyOptions: {
|
|
48
71
|
readonly json: 0;
|
|
72
|
+
readonly yaml: {};
|
|
49
73
|
};
|
|
50
|
-
declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<
|
|
74
|
+
declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<Partial<{
|
|
75
|
+
json: StringifyJSONOptions;
|
|
76
|
+
yaml: StringifyYAMLOptions;
|
|
77
|
+
}> & {
|
|
51
78
|
format: string;
|
|
52
79
|
}>) => neverthrow0.Result<string, StringifyError>;
|
|
53
80
|
//#endregion
|
|
@@ -59,10 +86,10 @@ type Prettify<T> = { [K in keyof T]: T[K] } & {};
|
|
|
59
86
|
//#endregion
|
|
60
87
|
//#region src/validate.d.ts
|
|
61
88
|
declare class ValidationError extends EnhancedError {
|
|
62
|
-
readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
|
|
63
|
-
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
|
|
89
|
+
readonly issues: ReadonlyArray<StandardSchemaV1$1.Issue>;
|
|
90
|
+
constructor(issues: ReadonlyArray<StandardSchemaV1$1.Issue>);
|
|
64
91
|
}
|
|
65
|
-
declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1.InferOutput<Schema>, ValidationError>;
|
|
92
|
+
declare const validateIt: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ValidationError>;
|
|
66
93
|
//#endregion
|
|
67
94
|
//#region src/zerde.d.ts
|
|
68
95
|
type ZerdeOptions = Partial<{
|
|
@@ -70,22 +97,21 @@ type ZerdeOptions = Partial<{
|
|
|
70
97
|
stringify: StringifyOptions;
|
|
71
98
|
}>;
|
|
72
99
|
declare class Zerde {
|
|
73
|
-
parseOptions:
|
|
74
|
-
|
|
75
|
-
readonly allowEmptyContent: true;
|
|
76
|
-
readonly allowTrailingComma: true;
|
|
77
|
-
readonly disallowComments: true;
|
|
78
|
-
};
|
|
79
|
-
};
|
|
80
|
-
stringifyOptions: {
|
|
81
|
-
readonly json: 0;
|
|
82
|
-
};
|
|
100
|
+
parseOptions: ParseOptions;
|
|
101
|
+
stringifyOptions: StringifyOptions;
|
|
83
102
|
constructor(options?: ZerdeOptions);
|
|
84
|
-
parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string |
|
|
85
|
-
stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string |
|
|
103
|
+
parse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | ParseOptions) => neverthrow1.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
104
|
+
stringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | StringifyOptions) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
|
|
86
105
|
}
|
|
87
106
|
declare const zerde: Zerde;
|
|
88
|
-
declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<
|
|
89
|
-
|
|
107
|
+
declare const zparse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<{
|
|
108
|
+
json: ParseJSONOptions;
|
|
109
|
+
yaml: ParseYAMLOptions;
|
|
110
|
+
format: string;
|
|
111
|
+
}> | undefined) => neverthrow1.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
112
|
+
declare const zstringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<{
|
|
113
|
+
json: StringifyJSONOptions;
|
|
114
|
+
yaml: StringifyYAMLOptions;
|
|
115
|
+
}> | undefined) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
|
|
90
116
|
//#endregion
|
|
91
|
-
export { EnhancedError,
|
|
117
|
+
export { EnhancedError, ErrorInfo, ParseError, ParseJSONOptions, ParseOptions, ParseYAMLOptions, Prettify, type StandardSchemaV1, StringSchema, StringifyError, StringifyJSONOptions, StringifyOptions, StringifyYAMLOptions, ValidationError, Zerde, ZerdeOptions, defaultParseJSONOptions, defaultParseOptions, defaultStringifyJSONOptions, defaultStringifyOptions, extractFormatSuffix, objectEntries, objectKeys, parseIt, parseJSON, parseString, parseYAML, stringifyIt, stringifyJSON, stringifyYAML, validateIt, zerde, zparse, zstringify };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{parse as e}from"jsonc-parser";import{configure as t}from"safe-stable-stringify";import{parse as n,stringify as r}from"yaml";import{ResultAsync as i,fromThrowable as a}from"neverthrow";const o={allowEmptyContent:!1,allowTrailingComma:!1,disallowComments:!0};function s(t,n={}){let r={...n,...o},i=[],a=e(t,i,r);if(i.length>0)throw SyntaxError(`Could not parse JSON`,{cause:i});return a}const c=0,l=t({bigint:!1,circularValue:TypeError,deterministic:!0,strict:!0});function u(e,t){let n=l(e,null,t);if(n===void 0)throw SyntaxError(`safe-stable-stringify should have thrown an error if trying to stringify an invalid json object`);return n}function d(e,t={}){return n(e,t)}function f(e,t={}){return r(e,{sortMapEntries:!0,...t})}const p=Object.keys,m=Object.entries;var h=class extends Error{constructor(e,t){super(e,{cause:t.cause}),this.name=this.constructor.name}};const g=[`.`,`/`,`\\`,`+`];function _(e){let t=e.trim().toLowerCase();if(!t)return t;let n=t.indexOf(`;`);n>0&&(t=t.slice(0,n));let r=Math.max(...g.map(e=>t.lastIndexOf(e)));return r<0?t:t.slice(r+1)}if(import.meta.vitest){let{describe:e,it:t,expect:n}=import.meta.vitest,r={lowercase:`json`,uppercase:`JSON`,mixedcase:`jSoN`,extension:`.json`,file:`file.json`,"file with path":`some/path/to/file.json`,"file with a windows path":`some\\path\\to\\file.json`,"basic media type":`application/json`,"complex media type":`application/customFormat+json`,"media type with charset":`application/customFormat+json; charset=utf-8`};e(`extractFormatSuffix`,()=>{for(let[e,i]of m(r)){let r=`Can handle ${e}: ${i}`;t(r,()=>{n(_(i)).toEqual(`json`)})}})}var v=class extends h{};const y={json:o,yaml:{}},b=a((e,t)=>{let{format:n,...r}=t,i=p(r).sort().at(0)??``,a=_(n??i);return a===`yaml`||a===`yml`?d(e,r.yaml):a===`json`?s(e,r.json):e},e=>new v(`Could not parse content`,{cause:e}));function x(e=`Invalid type`){return{type:`string`,message:e,"~standard":{version:1,vendor:`StandardSchema`,validate(t){return typeof t==`string`?{value:t}:{issues:[{message:e}]}}}}}var S=class extends h{};const C={json:c,yaml:{}},w=a((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=_(n??``);return i===`yaml`||i===`yml`?f(e,r.yaml):u(e,r.json)},e=>new S(`Could not stringify content`,{cause:e}));var T=class extends h{issues;constructor(e){super(`Content was not valid`,{cause:e}),this.issues=e}};const E=i.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues)throw new T(n.issues);return n.value},e=>e);var D=class{parseOptions;stringifyOptions;constructor(e={}){this.parseOptions={...y,...e.parse},this.stringifyOptions={...C,...e.stringify}}parse=(e,t,n)=>{if(typeof e==`string`){let r=typeof n==`string`?{format:n}:n,i={...this.parseOptions,...r};return b(e,i).asyncAndThen(e=>E(e,t))}return E(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return E(e,t).andThen(e=>w(e,i))}};const O=new D,k=O.parse,A=O.stringify;export{h as EnhancedError,v as ParseError,S as StringifyError,T as ValidationError,D as Zerde,o as defaultParseJSONOptions,y as defaultParseOptions,c as defaultStringifyJSONOptions,C as defaultStringifyOptions,_ as extractFormatSuffix,m as objectEntries,p as objectKeys,b as parseIt,s as parseJSON,x as parseString,d as parseYAML,w as stringifyIt,u as stringifyJSON,f as stringifyYAML,E as validateIt,O as zerde,k as zparse,A as zstringify};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zerde",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "parsing, and stringifying combined with schema validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"parse",
|
|
@@ -47,6 +47,7 @@
|
|
|
47
47
|
"@standard-schema/spec": "^1.0.0",
|
|
48
48
|
"jsonc-parser": "^3.3.1",
|
|
49
49
|
"neverthrow": "^8.2.0",
|
|
50
|
-
"safe-stable-stringify": "^2.5.0"
|
|
50
|
+
"safe-stable-stringify": "^2.5.0",
|
|
51
|
+
"yaml": "^2.8.2"
|
|
51
52
|
}
|
|
52
53
|
}
|