zerde 0.1.5 → 0.1.7
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 +25 -9
- package/dist/index.js +1 -1
- package/package.json +4 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
|
+
import { ErrorLike, isErrorLike, serializeError } from "serialize-error";
|
|
1
2
|
import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
|
|
2
3
|
import { CreateNodeOptions, DocumentOptions, ParseOptions as ParseOptions$2, SchemaOptions, ToJSOptions, ToStringOptions } from "yaml";
|
|
3
|
-
import * as
|
|
4
|
-
import * as neverthrow3 from "neverthrow";
|
|
4
|
+
import * as neverthrow0$1 from "neverthrow";
|
|
5
5
|
import * as neverthrow0 from "neverthrow";
|
|
6
|
+
import * as neverthrow1 from "neverthrow";
|
|
6
7
|
import { ResultAsync } from "neverthrow";
|
|
7
8
|
import { StandardSchemaV1, StandardSchemaV1 as StandardSchemaV1$1 } from "@standard-schema/spec";
|
|
8
9
|
|
|
@@ -24,6 +25,21 @@ declare function parseYAML(stringifiedContent: string, parseOptions?: ParseYAMLO
|
|
|
24
25
|
type StringifyYAMLOptions = CreateNodeOptions & DocumentOptions & ParseOptions$2 & SchemaOptions & ToStringOptions;
|
|
25
26
|
declare function stringifyYAML(unknownContent: unknown, stringifyOptions?: StringifyYAMLOptions): string;
|
|
26
27
|
//#endregion
|
|
28
|
+
//#region src/hashing.d.ts
|
|
29
|
+
declare const _HASH_: unique symbol;
|
|
30
|
+
type Base10Hash = number & {
|
|
31
|
+
readonly [_HASH_]: 10;
|
|
32
|
+
};
|
|
33
|
+
type Base16Hash = string & {
|
|
34
|
+
readonly [_HASH_]: 16;
|
|
35
|
+
};
|
|
36
|
+
type Base36Hash = string & {
|
|
37
|
+
readonly [_HASH_]: 36;
|
|
38
|
+
};
|
|
39
|
+
declare function hashBase10(unknownContent: unknown): Base10Hash;
|
|
40
|
+
declare function hashBase16(unknownContent: unknown): Base16Hash;
|
|
41
|
+
declare function hashBase36(unknownContent: unknown): Base36Hash;
|
|
42
|
+
//#endregion
|
|
27
43
|
//#region src/utils.d.ts
|
|
28
44
|
type ErrorInfo<T> = T & {
|
|
29
45
|
cause: unknown;
|
|
@@ -55,7 +71,7 @@ declare const parseIt: (stringifiedContent: string, parseOptions: Partial<{
|
|
|
55
71
|
json: ParseJSONOptions;
|
|
56
72
|
yaml: ParseYAMLOptions;
|
|
57
73
|
format: string;
|
|
58
|
-
}>) =>
|
|
74
|
+
}>) => neverthrow0$1.Result<unknown, ParseError>;
|
|
59
75
|
//#endregion
|
|
60
76
|
//#region src/schema.d.ts
|
|
61
77
|
interface StringSchema extends StandardSchemaV1$1<string> {
|
|
@@ -82,7 +98,7 @@ declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions:
|
|
|
82
98
|
yaml: StringifyYAMLOptions;
|
|
83
99
|
}> & {
|
|
84
100
|
format: string;
|
|
85
|
-
}>) =>
|
|
101
|
+
}>) => neverthrow0.Result<string, StringifyError>;
|
|
86
102
|
//#endregion
|
|
87
103
|
//#region src/typeHelpers.d.ts
|
|
88
104
|
type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
|
|
@@ -112,18 +128,18 @@ declare class Zerde {
|
|
|
112
128
|
parseOptions: ParseOptions;
|
|
113
129
|
stringifyOptions: StringifyOptions;
|
|
114
130
|
constructor(options?: ZerdeOptions);
|
|
115
|
-
parse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | ParseOptions) =>
|
|
116
|
-
stringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | StringifyOptions) =>
|
|
131
|
+
parse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | ParseOptions) => neverthrow1.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
132
|
+
stringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | StringifyOptions) => neverthrow1.ResultAsync<string, ValidationError | StringifyError>;
|
|
117
133
|
}
|
|
118
134
|
declare const zerde: Zerde;
|
|
119
135
|
declare const zparse: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<{
|
|
120
136
|
json: ParseJSONOptions;
|
|
121
137
|
yaml: ParseYAMLOptions;
|
|
122
138
|
format: string;
|
|
123
|
-
}> | undefined) =>
|
|
139
|
+
}> | undefined) => neverthrow1.ResultAsync<StandardSchemaV1$1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
124
140
|
declare const zstringify: <Schema extends StandardSchemaV1$1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<{
|
|
125
141
|
json: StringifyJSONOptions;
|
|
126
142
|
yaml: StringifyYAMLOptions;
|
|
127
|
-
}> | undefined) =>
|
|
143
|
+
}> | undefined) => neverthrow1.ResultAsync<string, ValidationError | StringifyError>;
|
|
128
144
|
//#endregion
|
|
129
|
-
export { EnhancedError, ErrorInfo, ParseError, ParseJSONOptions, ParseOptions, ParseYAMLOptions, Prettify, type StandardSchemaV1, StringSchema, StringifyError, StringifyJSONOptions, StringifyOptions, StringifyYAMLOptions, ValidationError, ValidationIssue, ValidationIssues, Zerde, ZerdeOptions, defaultParseJSONOptions, defaultParseOptions, defaultStringifyJSONOptions, defaultStringifyOptions, extractFormatSuffix, objectEntries, objectKeys, parseIt, parseJSON, parseString, parseYAML, stringifyIt, stringifyJSON, stringifyYAML, validateIt, zerde, zparse, zstringify };
|
|
145
|
+
export { Base10Hash, Base16Hash, Base36Hash, EnhancedError, ErrorInfo, type ErrorLike, ParseError, ParseJSONOptions, ParseOptions, ParseYAMLOptions, Prettify, type StandardSchemaV1, StringSchema, StringifyError, StringifyJSONOptions, StringifyOptions, StringifyYAMLOptions, ValidationError, ValidationIssue, ValidationIssues, Zerde, ZerdeOptions, defaultParseJSONOptions, defaultParseOptions, defaultStringifyJSONOptions, defaultStringifyOptions, extractFormatSuffix, hashBase10, hashBase16, hashBase36, isErrorLike, objectEntries, objectKeys, parseIt, parseJSON, parseString, parseYAML, serializeError, stringifyIt, stringifyJSON, stringifyYAML, validateIt, zerde, zparse, zstringify };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{isErrorLike as e,serializeError as t,serializeError as n}from"serialize-error";import{parse as r}from"jsonc-parser";import{configure as i}from"safe-stable-stringify";import{parse as a,stringify as o}from"yaml";import{ResultAsync as s,fromThrowable as c}from"neverthrow";const l={allowEmptyContent:!1,allowTrailingComma:!1,disallowComments:!0};function u(e,t){try{return JSON.parse(e)}catch{let n={...l,...t},i=[],a=r(e,i,n);if(i.length>0)throw SyntaxError(`Could not parse JSON`,{cause:i});return a}}const d=0,f=i({bigint:!1,circularValue:TypeError,deterministic:!0,strict:!0});function p(e,t){let r=Error.isError(e)?n(e):e,i=f(r,null,t);if(i===void 0)throw SyntaxError(`safe-stable-stringify should have thrown an error if trying to stringify an invalid json object`);return i}function m(e,t){return a(e,t)}function h(e,t){return o(e,{sortMapEntries:!0,...t})}const g=16,_=36;function v(e){let t=p(e),n=t.length,r=2166136261;for(let e=0;e<n;e++)r^=t.charCodeAt(e),r+=(r<<1)+(r<<4)+(r<<7)+(r<<8)+(r<<24);return r>>>0}function y(e){return v(e)}function b(e){return v(e).toString(g)}function x(e){return v(e).toString(_)}const S=Object.keys,C=Object.entries;var w=class extends Error{constructor(e,t){super(e,{cause:t.cause}),this.name=this.constructor.name}};const T=[`.`,`/`,`\\`,`+`];function E(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(...T.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 C(r)){let r=`Can handle ${e}: ${i}`;t(r,()=>{n(E(i)).toEqual(`json`)})}})}var D=class extends w{tag=`ParseError`;constructor(e){super(`Could not parse content`,{cause:e}),this.name=`ParseError`}};const O={json:l,yaml:{}},k=c((e,t)=>{let{format:n,...r}=t,i=S(r).sort().at(0)??``,a=E(n??i);return a===`yaml`||a===`yml`?m(e,r.yaml):a===`json`?u(e,r.json):e},e=>new D(e));function A(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 j=class extends w{tag=`StringifyError`;constructor(e){super(`Could not stringify content`,{cause:e}),this.name=`StringifyError`}};const M={json:d,yaml:{}},N=c((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=E(n??``);return i===`yaml`||i===`yml`?h(e,r.yaml):p(e,r.json)},e=>new j(e));var P=class extends w{tag=`ValidationError`;originalValue;issues;constructor(e,t){super(`Content was not valid`,{cause:void 0}),this.originalValue=e,this.issues=t,this.name=`ValidationError`}};const F=s.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues){let t=n.issues.map(t=>({...t,input:I(e,t)}));throw new P(e,t)}return n.value},e=>e);function I(e,t){let n=e;for(let e of t.path??[])n=n[e];return n}if(import.meta.vitest){let{describe:e,it:t,expect:n}=import.meta.vitest,r=[[`simpleObject`,[`hello`],{hello:`world`},`world`],[`nestedObject`,[`deeply`,`nested`,`object`],{deeply:{nested:{object:`someValue`}}},`someValue`],[`array`,[1],[`zero`,`one`],`one`],[`noPath`,void 0,{hello:`world`},{hello:`world`}]];e(`extractOriginalInput`,()=>{for(let[e,i,a,o]of r){let r={message:`something went wrong`,path:i};t(`Can handle ${e}`,()=>{n(I(a,r)).toEqual(o)})}})}var L=class{parseOptions;stringifyOptions;constructor(e={}){this.parseOptions={...O,...e.parse},this.stringifyOptions={...M,...e.stringify}}parse=(e,t,n)=>{if(typeof e==`string`){let r=typeof n==`string`?{format:n}:n,i={...this.parseOptions,...r};return k(e,i).asyncAndThen(e=>F(e,t))}return F(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return F(e,t).andThen(e=>N(e,i))}};const R=new L,z=R.parse,B=R.stringify;export{w as EnhancedError,D as ParseError,j as StringifyError,P as ValidationError,L as Zerde,l as defaultParseJSONOptions,O as defaultParseOptions,d as defaultStringifyJSONOptions,M as defaultStringifyOptions,E as extractFormatSuffix,y as hashBase10,b as hashBase16,x as hashBase36,e as isErrorLike,C as objectEntries,S as objectKeys,k as parseIt,u as parseJSON,A as parseString,m as parseYAML,t as serializeError,N as stringifyIt,p as stringifyJSON,h as stringifyYAML,F as validateIt,R as zerde,z as zparse,B as zstringify};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zerde",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.7",
|
|
4
4
|
"description": "parsing, and stringifying combined with schema validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"parse",
|
|
@@ -44,10 +44,11 @@
|
|
|
44
44
|
"test:watch": "vitest"
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
|
-
"@standard-schema/spec": "^1.
|
|
47
|
+
"@standard-schema/spec": "^1.1.0",
|
|
48
48
|
"jsonc-parser": "^3.3.1",
|
|
49
49
|
"neverthrow": "^8.2.0",
|
|
50
50
|
"safe-stable-stringify": "^2.5.0",
|
|
51
|
-
"
|
|
51
|
+
"serialize-error": "^13.0.1",
|
|
52
|
+
"yaml": "^2.8.3"
|
|
52
53
|
}
|
|
53
54
|
}
|