zerde 0.1.1 → 0.1.3
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 +18 -12
- package/dist/index.js +1 -1
- package/package.json +1 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
2
|
-
import * as neverthrow4 from "neverthrow";
|
|
1
|
+
import * as neverthrow0$1 from "neverthrow";
|
|
3
2
|
import * as neverthrow0 from "neverthrow";
|
|
3
|
+
import * as neverthrow1 from "neverthrow";
|
|
4
4
|
import { ResultAsync } from "neverthrow";
|
|
5
5
|
import { ParseOptions as ParseOptions$1 } from "jsonc-parser";
|
|
6
6
|
import { StandardSchemaV1 } from "@standard-schema/spec";
|
|
@@ -21,6 +21,7 @@ declare class EnhancedError<T = unknown> extends Error {
|
|
|
21
21
|
declare class ParseError extends EnhancedError {}
|
|
22
22
|
type ParseOptions = {
|
|
23
23
|
json: ParseJSONOptions;
|
|
24
|
+
format?: string;
|
|
24
25
|
};
|
|
25
26
|
declare const defaultParseOptions: {
|
|
26
27
|
readonly json: {
|
|
@@ -29,9 +30,14 @@ declare const defaultParseOptions: {
|
|
|
29
30
|
readonly disallowComments: true;
|
|
30
31
|
};
|
|
31
32
|
};
|
|
32
|
-
declare const parseIt: (stringifiedContent: string,
|
|
33
|
-
|
|
34
|
-
|
|
33
|
+
declare const parseIt: (stringifiedContent: string, parseOptions: ParseOptions) => neverthrow0$1.Result<unknown, ParseError>;
|
|
34
|
+
//#endregion
|
|
35
|
+
//#region src/schema.d.ts
|
|
36
|
+
interface StringSchema extends StandardSchemaV1<string> {
|
|
37
|
+
type: "string";
|
|
38
|
+
message: string;
|
|
39
|
+
}
|
|
40
|
+
declare function parseString(message?: string): StringSchema;
|
|
35
41
|
//#endregion
|
|
36
42
|
//#region src/stringify.d.ts
|
|
37
43
|
declare class StringifyError extends EnhancedError {}
|
|
@@ -43,7 +49,7 @@ declare const defaultStringifyOptions: {
|
|
|
43
49
|
};
|
|
44
50
|
declare const stringifyIt: (unknownContent: unknown, formatAndStringifyOptions: Partial<StringifyOptions & {
|
|
45
51
|
format: string;
|
|
46
|
-
}>) =>
|
|
52
|
+
}>) => neverthrow0.Result<string, StringifyError>;
|
|
47
53
|
//#endregion
|
|
48
54
|
//#region src/typeHelpers.d.ts
|
|
49
55
|
type ObjectKeys<T extends object> = `${Exclude<keyof T, symbol>}`;
|
|
@@ -56,7 +62,7 @@ declare class ValidationError extends EnhancedError {
|
|
|
56
62
|
readonly issues: ReadonlyArray<StandardSchemaV1.Issue>;
|
|
57
63
|
constructor(issues: ReadonlyArray<StandardSchemaV1.Issue>);
|
|
58
64
|
}
|
|
59
|
-
declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<
|
|
65
|
+
declare const validateIt: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema) => ResultAsync<StandardSchemaV1.InferOutput<Schema>, ValidationError>;
|
|
60
66
|
//#endregion
|
|
61
67
|
//#region src/zerde.d.ts
|
|
62
68
|
type ZerdeOptions = Partial<{
|
|
@@ -75,11 +81,11 @@ declare class Zerde {
|
|
|
75
81
|
readonly json: 0;
|
|
76
82
|
};
|
|
77
83
|
constructor(options?: ZerdeOptions);
|
|
78
|
-
parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) =>
|
|
79
|
-
stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) =>
|
|
84
|
+
parse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow1.ResultAsync<StandardSchemaV1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
85
|
+
stringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
|
|
80
86
|
}
|
|
81
87
|
declare const zerde: Zerde;
|
|
82
|
-
declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) =>
|
|
83
|
-
declare const zstringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) =>
|
|
88
|
+
declare const zparse: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, parseOptions?: string | Partial<ParseOptions>) => neverthrow1.ResultAsync<StandardSchemaV1.InferOutput<Schema>, ParseError | ValidationError>;
|
|
89
|
+
declare const zstringify: <Schema extends StandardSchemaV1>(unknownContent: unknown, schema: Schema, stringifyOptions?: string | Partial<StringifyOptions>) => neverthrow1.ResultAsync<string, StringifyError | ValidationError>;
|
|
84
90
|
//#endregion
|
|
85
|
-
export { EnhancedError, type ErrorInfo, ParseError, type ParseOptions, Prettify, StringifyError, type StringifyOptions, ValidationError, Zerde, type ZerdeOptions, defaultParseOptions, defaultStringifyOptions, objectEntries, objectKeys, parseIt, stringifyIt, validateIt, zerde, zparse, zstringify };
|
|
91
|
+
export { EnhancedError, type ErrorInfo, ParseError, type ParseOptions, Prettify, type StringSchema, StringifyError, type StringifyOptions, ValidationError, Zerde, type ZerdeOptions, defaultParseOptions, defaultStringifyOptions, objectEntries, objectKeys, parseIt, parseString, stringifyIt, validateIt, zerde, zparse, zstringify };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{ResultAsync as e,fromThrowable as t}from"neverthrow";import{parse as n}from"jsonc-parser";import{configure as r}from"safe-stable-stringify";const i={allowEmptyContent:!0,allowTrailingComma:!0,disallowComments:!0};function a(e,t={}){let r={...t,...i},a=[],o=n(e,a,r);if(a.length>0)throw SyntaxError(`Could not parse JSON`,{cause:a});return o}const o=0,s=r({bigint:!1,circularValue:
|
|
1
|
+
import{ResultAsync as e,fromThrowable as t}from"neverthrow";import{parse as n}from"jsonc-parser";import{configure as r}from"safe-stable-stringify";const i={allowEmptyContent:!0,allowTrailingComma:!0,disallowComments:!0};function a(e,t={}){let r={...t,...i},a=[],o=n(e,a,r);if(a.length>0)throw SyntaxError(`Could not parse JSON`,{cause:a});return o}const o=0,s=r({bigint:!1,circularValue:TypeError,deterministic:!0,strict:!0});function c(e,t){let n=s(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}const l=Object.keys,u=Object.entries;var d=class extends Error{constructor(e,t){super(e,{cause:t.cause}),this.name=this.constructor.name}};const f=[`.`,`/`,`\\`,`+`];function p(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(...f.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 u(r)){let r=`Can handle ${e}: ${i}`;t(r,()=>{n(p(i)).toEqual(`json`)})}})}var m=class extends d{};const h={json:i},g=t((e,t)=>{let{format:n,...r}=t,i=l(r).sort().at(0)??``,o=p(n??i);return o===`json`?a(e,r.json):e},e=>new m(`Could not parse content`,{cause:e}));function _(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 v=class extends d{};const y={json:o},b=t((e,t)=>{if(typeof e==`string`)return e;let{format:n,...r}=t,i=p(n??``);return c(e,r.json)},e=>new v(`Could not stringify content`,{cause:e}));var x=class extends d{issues;constructor(e){super(`Content was not valid`,{cause:e}),this.issues=e}};const S=e.fromThrowable(async(e,t)=>{let n=await t[`~standard`].validate(e);if(n.issues)throw new x(n.issues);return n.value},e=>e);var C=class{parseOptions=h;stringifyOptions=y;constructor(e={}){this.parseOptions={...e.parse,...h}}parse=(e,t,n)=>{if(typeof e==`string`){let r=typeof n==`string`?{format:n}:n,i={...this.parseOptions,...r};return g(e,i).asyncAndThen(e=>S(e,t))}return S(e,t)};stringify=(e,t,n)=>{let r=typeof n==`string`?{format:n}:n,i={...this.stringifyOptions,...r};return S(e,t).andThen(e=>b(e,i))}};const w=new C,T=w.parse,E=w.stringify;export{d as EnhancedError,m as ParseError,v as StringifyError,x as ValidationError,C as Zerde,h as defaultParseOptions,y as defaultStringifyOptions,u as objectEntries,l as objectKeys,g as parseIt,_ as parseString,b as stringifyIt,S as validateIt,w as zerde,T as zparse,E as zstringify};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "zerde",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.3",
|
|
4
4
|
"description": "parsing, and stringifying combined with schema validation",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"parse",
|
|
@@ -41,8 +41,6 @@
|
|
|
41
41
|
},
|
|
42
42
|
"scripts": {
|
|
43
43
|
"build": "lefthook run build",
|
|
44
|
-
"lint": "biome check --error-on-warnings --colors=force --write src",
|
|
45
|
-
"test": "vitest run",
|
|
46
44
|
"test:watch": "vitest"
|
|
47
45
|
},
|
|
48
46
|
"dependencies": {
|