diff --git a/README.md b/README.md index 1ea474b2..4a1a4547 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ Highlights: - Strict mode for object structs to prevent excessive fields and many more built-in helpers - Works with plain JavaScript/TypeScript too! You don't need to use ReScript - The **fastest** composable validation library in the entire JavaScript ecosystem ([benchmark](https://moltar.github.io/typescript-runtime-type-benchmarks/)) -- Small and tree-shakable: [8.5kB minified + zipped](https://bundlephobia.com/package/rescript-struct) +- Small and tree-shakable: [9.5kB minified + zipped](https://bundlephobia.com/package/rescript-struct) Also, it has declarative API allowing you to use **rescript-struct** as a building block for other tools, such as: diff --git a/docs/js-usage.md b/docs/js-usage.md index 71dc9508..788a17e0 100644 --- a/docs/js-usage.md +++ b/docs/js-usage.md @@ -548,11 +548,11 @@ Besides the individual bundle size, the overall size of the library is also sign At the same time **rescript-struct** is the fastest composable validation library in the entire JavaScript ecosystem. This is achieved because of the JIT approach when an ultra optimized validator is created using `eval`. -| | rescript-struct@5.0.1 | Zod@3.22.2 | Valibot@0.18.0 | -| ---------------------------------------- | --------------------- | --------------- | -------------- | -| **Total size** (minified + gzipped) | 8.5 kB | 13.2 kB | 6.6 kB | -| **Example size** (minified + gzipped) | 6.08 kB | 12.8 kB | 965 B | -| **Performance** (Example parsing) | 1,030,417 ops/ms | 376 ops/ms | 24,034 ops/ms | -| **Eval-free** | ❌ | ✅ | ✅ | -| **Codegen-free** (Doesn't need compiler) | ✅ | ✅ | ✅ | -| **Ecosystem** | ⭐️ | ⭐️⭐️⭐️⭐️⭐️ | ⭐️⭐️ | +| | rescript-struct@5.1.0 (unreleased) | Zod@3.22.2 | Valibot@0.18.0 | +| ---------------------------------------- | ---------------------------------- | --------------- | -------------- | +| **Total size** (minified + gzipped) | 9.68 kB | 13.4 kB | 6.73 kB | +| **Example size** (minified + gzipped) | 5.92 kB | 12.8 kB | 965 B | +| **Performance** (Example parsing) | 1,030,417 ops/ms | 376 ops/ms | 24,034 ops/ms | +| **Eval-free** | ❌ | ✅ | ✅ | +| **Codegen-free** (Doesn't need compiler) | ✅ | ✅ | ✅ | +| **Ecosystem** | ⭐️ | ⭐️⭐️⭐️⭐️⭐️ | ⭐️⭐️ | diff --git a/package.json b/package.json index a372c0e6..635aafa4 100644 --- a/package.json +++ b/package.json @@ -38,7 +38,7 @@ "type": "module", "main": "./dist/S.js", "module": "./dist/S.mjs", - "types": "./src/S_JsApi.d.ts", + "types": "./src/S.d.ts", "files": [ "dist", "src/S_Core.res", @@ -46,7 +46,7 @@ "src/S_Core.bs.mjs", "src/S.res", "src/S.resi", - "src/S_JsApi.d.ts", + "src/S.d.ts", "RescriptStruct.gen.ts", "bsconfig.json" ], diff --git a/packages/artifacts/README.md b/packages/artifacts/README.md index 1ea474b2..4a1a4547 100644 --- a/packages/artifacts/README.md +++ b/packages/artifacts/README.md @@ -20,7 +20,7 @@ Highlights: - Strict mode for object structs to prevent excessive fields and many more built-in helpers - Works with plain JavaScript/TypeScript too! You don't need to use ReScript - The **fastest** composable validation library in the entire JavaScript ecosystem ([benchmark](https://moltar.github.io/typescript-runtime-type-benchmarks/)) -- Small and tree-shakable: [8.5kB minified + zipped](https://bundlephobia.com/package/rescript-struct) +- Small and tree-shakable: [9.5kB minified + zipped](https://bundlephobia.com/package/rescript-struct) Also, it has declarative API allowing you to use **rescript-struct** as a building block for other tools, such as: diff --git a/packages/artifacts/dist/S.js b/packages/artifacts/dist/S.js index 05e60205..346acc36 100644 --- a/packages/artifacts/dist/S.js +++ b/packages/artifacts/dist/S.js @@ -1,160 +1,26 @@ 'use strict'; -var Caml_option = require('rescript/lib/js/caml_option.js'); var S = require('./../src/S_Core.bs.js'); function _interopNamespaceDefault(e) { - var n = Object.create(null); - if (e) { - Object.keys(e).forEach(function (k) { - if (k !== 'default') { - var d = Object.getOwnPropertyDescriptor(e, k); - Object.defineProperty(n, k, d.get ? d : { - enumerable: true, - get: function () { return e[k]; } - }); - } - }); - } - n.default = e; - return Object.freeze(n); + var n = Object.create(null); + if (e) { + Object.keys(e).forEach(function (k) { + if (k !== 'default') { + var d = Object.getOwnPropertyDescriptor(e, k); + Object.defineProperty(n, k, d.get ? d : { + enumerable: true, + get: function () { return e[k]; } + }); + } + }); + } + n.default = e; + return Object.freeze(n); } -var Caml_option__namespace = /*#__PURE__*/_interopNamespaceDefault(Caml_option); var S__namespace = /*#__PURE__*/_interopNamespaceDefault(S); -// Generated by ReScript, PLEASE EDIT WITH CARE - -function toJsResult(result) { - if (result.TAG === "Ok") { - return { - success: true, - value: result._0 - }; - } else { - return { - success: false, - error: result._0 - }; - } -} - -function transform$1(struct, maybeParser, maybeSerializer) { - return S__namespace.transform(struct, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function refine$1(struct, refiner) { - return S__namespace.refine(struct, (function (s) { - return function (v) { - refiner(v, s); - }; - })); -} - -function noop(a) { - return a; -} - -function asyncParserRefine$1(struct, refine) { - return S__namespace.transform(struct, (function (s) { - return { - a: (function (v) { - return function () { - return refine(v, s).then(function () { - return v; - }); - }; - }), - s: noop - }; - })); -} - -function optional$1(struct, maybeOr) { - var struct$1 = S__namespace.option(struct); - if (maybeOr === undefined) { - return struct$1; - } - var or = Caml_option__namespace.valFromOption(maybeOr); - if (typeof or === "function") { - return S__namespace.$$Option.getOrWith(struct$1, or); - } else { - return S__namespace.$$Option.getOr(struct$1, or); - } -} - -function tuple$1(definer) { - if (typeof definer === "function") { - return S__namespace.tuple(definer); - } else { - return S__namespace.tuple(function (s) { - return definer.map(function (struct, idx) { - return s.i(idx, struct); - }); - }); - } -} - -function custom$1(name, maybeParser, maybeSerializer, param) { - return S__namespace.custom(name, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function object$1(definer) { - if (typeof definer === "function") { - return S__namespace.object(definer); - } else { - return S__namespace.object(function (s) { - var definition = {}; - var fieldNames = Object.keys(definer); - for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ - var fieldName = fieldNames[idx]; - var struct = definer[fieldName]; - definition[fieldName] = s.f(fieldName, struct); - } - return definition; - }); - } -} - -function parse$1(struct, data) { - return toJsResult(S__namespace.parseAnyWith(data, struct)); -} - -function parseOrThrow$1(struct, data) { - return S__namespace.parseAnyOrRaiseWith(data, struct); -} - -function parseAsync$1(struct, data) { - return S__namespace.parseAnyAsyncWith(data, struct).then(toJsResult); -} - -function serialize$1(struct, value) { - return toJsResult(S__namespace.serializeToUnknownWith(value, struct)); -} - -function serializeOrThrow$1(struct, value) { - return S__namespace.serializeToUnknownOrRaiseWith(value, struct); -} -/* S_Core-RescriptStruct Not a pure module */ - const Error = S__namespace.$$Error.$$class; const string = S__namespace.string; const boolean = S__namespace.bool; @@ -164,30 +30,30 @@ const json = S__namespace.json; const never = S__namespace.never; const unknown = S__namespace.unknown; const undefined$1 = S__namespace.unit; -const optional = optional$1; +const optional = S__namespace.js_optional; const nullable = S__namespace.$$null; const array = S__namespace.array; const record = S__namespace.dict; const jsonString = S__namespace.jsonString; const union = S__namespace.union; -const object = object$1; +const object = S__namespace.js_object; const Object$1 = S__namespace.$$Object; const String = S__namespace.$$String; const Number = S__namespace.Float; const Array = S__namespace.$$Array; -const custom = custom$1; +const custom = S__namespace.js_custom; const literal = S__namespace.literal; -const tuple = tuple$1; -const asyncParserRefine = asyncParserRefine$1; -const refine = refine$1; -const transform = transform$1; +const tuple = S__namespace.js_tuple; +const asyncParserRefine = S__namespace.js_asyncParserRefine; +const refine = S__namespace.js_refine; +const transform = S__namespace.js_transform; const description = S__namespace.description; const describe = S__namespace.describe; -const parse = parse$1; -const parseOrThrow = parseOrThrow$1; -const parseAsync = parseAsync$1; -const serialize = serialize$1; -const serializeOrThrow = serializeOrThrow$1; +const parse = S__namespace.js_parse; +const parseOrThrow = S__namespace.js_parseOrThrow; +const parseAsync = S__namespace.js_parseAsync; +const serialize = S__namespace.js_serialize; +const serializeOrThrow = S__namespace.js_serializeOrThrow; exports.Array = Array; exports.Error = Error; diff --git a/packages/artifacts/dist/S.mjs b/packages/artifacts/dist/S.mjs index b2b6b68c..903a8d86 100644 --- a/packages/artifacts/dist/S.mjs +++ b/packages/artifacts/dist/S.mjs @@ -1,138 +1,5 @@ -import * as Caml_option from 'rescript/lib/es6/caml_option.js'; import * as S from './../src/S_Core.bs.mjs'; -// Generated by ReScript, PLEASE EDIT WITH CARE - -function toJsResult(result) { - if (result.TAG === "Ok") { - return { - success: true, - value: result._0 - }; - } else { - return { - success: false, - error: result._0 - }; - } -} - -function transform$1(struct, maybeParser, maybeSerializer) { - return S.transform(struct, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function refine$1(struct, refiner) { - return S.refine(struct, (function (s) { - return function (v) { - refiner(v, s); - }; - })); -} - -function noop(a) { - return a; -} - -function asyncParserRefine$1(struct, refine) { - return S.transform(struct, (function (s) { - return { - a: (function (v) { - return function () { - return refine(v, s).then(function () { - return v; - }); - }; - }), - s: noop - }; - })); -} - -function optional$1(struct, maybeOr) { - var struct$1 = S.option(struct); - if (maybeOr === undefined) { - return struct$1; - } - var or = Caml_option.valFromOption(maybeOr); - if (typeof or === "function") { - return S.$$Option.getOrWith(struct$1, or); - } else { - return S.$$Option.getOr(struct$1, or); - } -} - -function tuple$1(definer) { - if (typeof definer === "function") { - return S.tuple(definer); - } else { - return S.tuple(function (s) { - return definer.map(function (struct, idx) { - return s.i(idx, struct); - }); - }); - } -} - -function custom$1(name, maybeParser, maybeSerializer, param) { - return S.custom(name, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function object$1(definer) { - if (typeof definer === "function") { - return S.object(definer); - } else { - return S.object(function (s) { - var definition = {}; - var fieldNames = Object.keys(definer); - for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ - var fieldName = fieldNames[idx]; - var struct = definer[fieldName]; - definition[fieldName] = s.f(fieldName, struct); - } - return definition; - }); - } -} - -function parse$1(struct, data) { - return toJsResult(S.parseAnyWith(data, struct)); -} - -function parseOrThrow$1(struct, data) { - return S.parseAnyOrRaiseWith(data, struct); -} - -function parseAsync$1(struct, data) { - return S.parseAnyAsyncWith(data, struct).then(toJsResult); -} - -function serialize$1(struct, value) { - return toJsResult(S.serializeToUnknownWith(value, struct)); -} - -function serializeOrThrow$1(struct, value) { - return S.serializeToUnknownOrRaiseWith(value, struct); -} -/* S_Core-RescriptStruct Not a pure module */ - const Error = S.$$Error.$$class; const string = S.string; const boolean = S.bool; @@ -142,29 +9,29 @@ const json = S.json; const never = S.never; const unknown = S.unknown; const undefined$1 = S.unit; -const optional = optional$1; +const optional = S.js_optional; const nullable = S.$$null; const array = S.array; const record = S.dict; const jsonString = S.jsonString; const union = S.union; -const object = object$1; +const object = S.js_object; const Object$1 = S.$$Object; const String = S.$$String; const Number = S.Float; const Array = S.$$Array; -const custom = custom$1; +const custom = S.js_custom; const literal = S.literal; -const tuple = tuple$1; -const asyncParserRefine = asyncParserRefine$1; -const refine = refine$1; -const transform = transform$1; +const tuple = S.js_tuple; +const asyncParserRefine = S.js_asyncParserRefine; +const refine = S.js_refine; +const transform = S.js_transform; const description = S.description; const describe = S.describe; -const parse = parse$1; -const parseOrThrow = parseOrThrow$1; -const parseAsync = parseAsync$1; -const serialize = serialize$1; -const serializeOrThrow = serializeOrThrow$1; +const parse = S.js_parse; +const parseOrThrow = S.js_parseOrThrow; +const parseAsync = S.js_parseAsync; +const serialize = S.js_serialize; +const serializeOrThrow = S.js_serializeOrThrow; export { Array, Error, Number, Object$1 as Object, String, array, asyncParserRefine, boolean, custom, describe, description, integer, json, jsonString, literal, never, nullable, number, object, optional, parse, parseAsync, parseOrThrow, record, refine, serialize, serializeOrThrow, string, transform, tuple, undefined$1 as undefined, union, unknown }; diff --git a/packages/artifacts/package.json b/packages/artifacts/package.json index 794e1e02..a2890df4 100644 --- a/packages/artifacts/package.json +++ b/packages/artifacts/package.json @@ -38,7 +38,7 @@ "type": "commonjs", "main": "./dist/S.js", "module": "./dist/S.mjs", - "types": "./src/S_JsApi.d.ts", + "types": "./src/S.d.ts", "files": [ "dist", "src/S_Core.res", @@ -46,7 +46,7 @@ "src/S_Core.bs.mjs", "src/S.res", "src/S.resi", - "src/S_JsApi.d.ts", + "src/S.d.ts", "RescriptStruct.gen.ts", "bsconfig.json" ], diff --git a/packages/artifacts/src/S_JsApi.d.ts b/packages/artifacts/src/S.d.ts similarity index 100% rename from packages/artifacts/src/S_JsApi.d.ts rename to packages/artifacts/src/S.d.ts diff --git a/src/S_JsApi.js b/packages/artifacts/src/S.js similarity index 57% rename from src/S_JsApi.js rename to packages/artifacts/src/S.js index 13ff092e..36eb1509 100644 --- a/src/S_JsApi.js +++ b/packages/artifacts/src/S.js @@ -1,4 +1,3 @@ -import * as S_Js from "./S_JsApi.bs.mjs"; import * as S from "./S_Core.bs.mjs"; export const Error = S.$$Error.$$class; @@ -10,27 +9,27 @@ export const json = S.json; export const never = S.never; export const unknown = S.unknown; export const undefined = S.unit; -export const optional = S_Js.optional; +export const optional = S.js_optional; export const nullable = S.$$null; export const array = S.array; export const record = S.dict; export const jsonString = S.jsonString; export const union = S.union; -export const object = S_Js.object; +export const object = S.js_object; export const Object = S.$$Object; export const String = S.$$String; export const Number = S.Float; export const Array = S.$$Array; -export const custom = S_Js.custom; +export const custom = S.js_custom; export const literal = S.literal; -export const tuple = S_Js.tuple; -export const asyncParserRefine = S_Js.asyncParserRefine; -export const refine = S_Js.refine; -export const transform = S_Js.transform; +export const tuple = S.js_tuple; +export const asyncParserRefine = S.js_asyncParserRefine; +export const refine = S.js_refine; +export const transform = S.js_transform; export const description = S.description; export const describe = S.describe; -export const parse = S_Js.parse; -export const parseOrThrow = S_Js.parseOrThrow; -export const parseAsync = S_Js.parseAsync; -export const serialize = S_Js.serialize; -export const serializeOrThrow = S_Js.serializeOrThrow; +export const parse = S.js_parse; +export const parseOrThrow = S.js_parseOrThrow; +export const parseAsync = S.js_parseAsync; +export const serialize = S.js_serialize; +export const serializeOrThrow = S.js_serializeOrThrow; diff --git a/packages/artifacts/src/S_Core.bs.js b/packages/artifacts/src/S_Core.bs.js index bc889efb..93a1f8fb 100644 --- a/packages/artifacts/src/S_Core.bs.js +++ b/packages/artifacts/src/S_Core.bs.js @@ -3413,6 +3413,135 @@ function tuple3(v0, v1, v2) { }); } +function toJsResult(result) { + if (result.TAG === "Ok") { + return { + success: true, + value: result._0 + }; + } else { + return { + success: false, + error: result._0 + }; + } +} + +function js_parse(struct, data) { + return toJsResult(parseAnyWith(data, struct)); +} + +function js_parseOrThrow(struct, data) { + return parseAnyOrRaiseWith(data, struct); +} + +function js_parseAsync(struct, data) { + return parseAnyAsyncWith(data, struct).then(toJsResult); +} + +function js_serialize(struct, value) { + return toJsResult(serializeToUnknownWith(value, struct)); +} + +function js_serializeOrThrow(struct, value) { + return serializeToUnknownOrRaiseWith(value, struct); +} + +function js_transform(struct, maybeParser, maybeSerializer) { + return transform$1(struct, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_refine(struct, refiner) { + return refine(struct, (function (s) { + return function (v) { + refiner(v, s); + }; + })); +} + +function noop$1(a) { + return a; +} + +function js_asyncParserRefine(struct, refine) { + return transform$1(struct, (function (s) { + return { + a: (function (v) { + return function () { + return refine(v, s).then(function () { + return v; + }); + }; + }), + s: noop$1 + }; + })); +} + +function js_optional(struct, maybeOr) { + var struct$1 = factory$1(struct); + if (maybeOr === undefined) { + return struct$1; + } + var or = Caml_option.valFromOption(maybeOr); + if (typeof or === "function") { + return getOrWith(struct$1, or); + } else { + return getOr(struct$1, or); + } +} + +function js_tuple(definer) { + if (typeof definer === "function") { + return factory$7(definer); + } else { + return factory$7(function (s) { + return definer.map(function (struct, idx) { + return s.i(idx, struct); + }); + }); + } +} + +function js_custom(name, maybeParser, maybeSerializer, param) { + return custom(name, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_object(definer) { + if (typeof definer === "function") { + return factory$3(definer); + } else { + return factory$3(function (s) { + var definition = {}; + var fieldNames = Object.keys(definer); + for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ + var fieldName = fieldNames[idx]; + var struct = definer[fieldName]; + definition[fieldName] = s.f(fieldName, struct); + } + return definition; + }); + } +} + var B; var parseWith = parseAnyWith; @@ -3548,4 +3677,18 @@ exports.tuple2 = tuple2; exports.tuple3 = tuple3; exports.union = union; exports.jsonString = jsonString; +exports.toJsResult = toJsResult; +exports.js_parse = js_parse; +exports.js_parseOrThrow = js_parseOrThrow; +exports.js_parseAsync = js_parseAsync; +exports.js_serialize = js_serialize; +exports.js_serializeOrThrow = js_serializeOrThrow; +exports.js_transform = js_transform; +exports.js_refine = js_refine; +exports.noop = noop$1; +exports.js_asyncParserRefine = js_asyncParserRefine; +exports.js_optional = js_optional; +exports.js_tuple = js_tuple; +exports.js_custom = js_custom; +exports.js_object = js_object; /* symbol Not a pure module */ diff --git a/packages/artifacts/src/S_Core.bs.mjs b/packages/artifacts/src/S_Core.bs.mjs index 6a19c066..a9ee3d68 100644 --- a/packages/artifacts/src/S_Core.bs.mjs +++ b/packages/artifacts/src/S_Core.bs.mjs @@ -3412,6 +3412,135 @@ function tuple3(v0, v1, v2) { }); } +function toJsResult(result) { + if (result.TAG === "Ok") { + return { + success: true, + value: result._0 + }; + } else { + return { + success: false, + error: result._0 + }; + } +} + +function js_parse(struct, data) { + return toJsResult(parseAnyWith(data, struct)); +} + +function js_parseOrThrow(struct, data) { + return parseAnyOrRaiseWith(data, struct); +} + +function js_parseAsync(struct, data) { + return parseAnyAsyncWith(data, struct).then(toJsResult); +} + +function js_serialize(struct, value) { + return toJsResult(serializeToUnknownWith(value, struct)); +} + +function js_serializeOrThrow(struct, value) { + return serializeToUnknownOrRaiseWith(value, struct); +} + +function js_transform(struct, maybeParser, maybeSerializer) { + return transform$1(struct, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_refine(struct, refiner) { + return refine(struct, (function (s) { + return function (v) { + refiner(v, s); + }; + })); +} + +function noop$1(a) { + return a; +} + +function js_asyncParserRefine(struct, refine) { + return transform$1(struct, (function (s) { + return { + a: (function (v) { + return function () { + return refine(v, s).then(function () { + return v; + }); + }; + }), + s: noop$1 + }; + })); +} + +function js_optional(struct, maybeOr) { + var struct$1 = factory$1(struct); + if (maybeOr === undefined) { + return struct$1; + } + var or = Caml_option.valFromOption(maybeOr); + if (typeof or === "function") { + return getOrWith(struct$1, or); + } else { + return getOr(struct$1, or); + } +} + +function js_tuple(definer) { + if (typeof definer === "function") { + return factory$7(definer); + } else { + return factory$7(function (s) { + return definer.map(function (struct, idx) { + return s.i(idx, struct); + }); + }); + } +} + +function js_custom(name, maybeParser, maybeSerializer, param) { + return custom(name, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_object(definer) { + if (typeof definer === "function") { + return factory$3(definer); + } else { + return factory$3(function (s) { + var definition = {}; + var fieldNames = Object.keys(definer); + for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ + var fieldName = fieldNames[idx]; + var struct = definer[fieldName]; + definition[fieldName] = s.f(fieldName, struct); + } + return definition; + }); + } +} + var B; var parseWith = parseAnyWith; @@ -3548,5 +3677,19 @@ export { tuple3 , union , jsonString , + toJsResult , + js_parse , + js_parseOrThrow , + js_parseAsync , + js_serialize , + js_serializeOrThrow , + js_transform , + js_refine , + noop$1 as noop, + js_asyncParserRefine , + js_optional , + js_tuple , + js_custom , + js_object , } /* symbol Not a pure module */ diff --git a/packages/artifacts/src/S_Core.res b/packages/artifacts/src/S_Core.res index ed192b36..5f2b11aa 100644 --- a/packages/artifacts/src/S_Core.res +++ b/packages/artifacts/src/S_Core.res @@ -22,6 +22,9 @@ module Stdlib = { @send external thenResolveWithCatch: (t<'a>, 'a => 'b, Js.Exn.t => 'b) => t<'b> = "then" + @send + external thenResolve: (t<'a>, 'a => 'b) => t<'b> = "then" + @val @scope("Promise") external resolve: 'a => t<'a> = "resolve" } @@ -3546,3 +3549,124 @@ let jsonString = JsonString.factory @send external name: t<'a> => string = "n" + +// ============= +// JS/TS API +// ============= + +type jsResult<'value> + +let toJsResult = (result: result<'value, error>): jsResult<'value> => { + switch result { + | Ok(value) => {"success": true, "value": value}->Obj.magic + | Error(error) => {"success": false, "error": error}->Obj.magic + } +} + +let js_parse = (struct, data) => { + data->parseAnyWith(struct)->toJsResult +} + +let js_parseOrThrow = (struct, data) => { + data->parseAnyOrRaiseWith(struct) +} + +let js_parseAsync = (struct, data) => { + data->parseAnyAsyncWith(struct)->Stdlib.Promise.thenResolve(toJsResult) +} + +let js_serialize = (struct, value) => { + value->serializeToUnknownWith(struct)->Obj.magic->toJsResult +} + +let js_serializeOrThrow = (struct, value) => { + value->serializeToUnknownOrRaiseWith(struct) +} + +let js_transform = (struct, ~parser as maybeParser=?, ~serializer as maybeSerializer=?) => { + struct->transform(s => { + { + parser: ?switch maybeParser { + | Some(parser) => Some(v => parser(v, s)) + | None => None + }, + serializer: ?switch maybeSerializer { + | Some(serializer) => Some(v => serializer(v, s)) + | None => None + }, + } + }) +} + +let js_refine = (struct, refiner) => { + struct->refine(s => { + v => refiner(v, s) + }) +} + +let noop = a => a +let js_asyncParserRefine = (struct, refine) => { + struct->transform(s => { + { + asyncParser: v => () => refine(v, s)->Stdlib.Promise.thenResolve(() => v), + serializer: noop, + } + }) +} + +let js_optional = (struct, maybeOr) => { + let struct = option(struct) + switch maybeOr { + | Some(or) if Js.typeof(or) === "function" => struct->Option.getOrWith(or->Obj.magic)->Obj.magic + | Some(or) => struct->Option.getOr(or->Obj.magic)->Obj.magic + | None => struct + } +} + +let js_tuple = definer => { + if Js.typeof(definer) === "function" { + let definer = definer->(Obj.magic: unknown => Tuple.ctx => 'a) + tuple(definer) + } else { + let structs = definer->(Obj.magic: unknown => array>) + tuple(s => { + structs->Js.Array2.mapi((struct, idx) => { + s.item(idx, struct) + }) + }) + } +} + +let js_custom = (~name, ~parser as maybeParser=?, ~serializer as maybeSerializer=?, ()) => { + custom(name, s => { + { + parser: ?switch maybeParser { + | Some(parser) => Some(v => parser(v, s)) + | None => None + }, + serializer: ?switch maybeSerializer { + | Some(serializer) => Some(v => serializer(v, s)) + | None => None + }, + } + }) +} + +let js_object = definer => { + if Js.typeof(definer) === "function" { + let definer = definer->(Obj.magic: unknown => Object.ctx => 'a) + object(definer) + } else { + let definer = definer->(Obj.magic: unknown => Js.Dict.t>) + object(s => { + let definition = Js.Dict.empty() + let fieldNames = definer->Js.Dict.keys + for idx in 0 to fieldNames->Js.Array2.length - 1 { + let fieldName = fieldNames->Js.Array2.unsafe_get(idx) + let struct = definer->Js.Dict.unsafeGet(fieldName) + definition->Js.Dict.set(fieldName, s.field(fieldName, struct)) + } + definition + }) + } +} diff --git a/packages/artifacts/src/S_JsApi.bs.js b/packages/artifacts/src/S_JsApi.bs.js deleted file mode 100644 index a9f00a93..00000000 --- a/packages/artifacts/src/S_JsApi.bs.js +++ /dev/null @@ -1,148 +0,0 @@ -// Generated by ReScript, PLEASE EDIT WITH CARE -'use strict'; - -var Caml_option = require("rescript/lib/js/caml_option.js"); -var S_Core$RescriptStruct = require("./S_Core.bs.js"); - -function toJsResult(result) { - if (result.TAG === "Ok") { - return { - success: true, - value: result._0 - }; - } else { - return { - success: false, - error: result._0 - }; - } -} - -function transform(struct, maybeParser, maybeSerializer) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function refine(struct, refiner) { - return S_Core$RescriptStruct.refine(struct, (function (s) { - return function (v) { - refiner(v, s); - }; - })); -} - -function noop(a) { - return a; -} - -function asyncParserRefine(struct, refine) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - a: (function (v) { - return function () { - return refine(v, s).then(function () { - return v; - }); - }; - }), - s: noop - }; - })); -} - -function optional(struct, maybeOr) { - var struct$1 = S_Core$RescriptStruct.option(struct); - if (maybeOr === undefined) { - return struct$1; - } - var or = Caml_option.valFromOption(maybeOr); - if (typeof or === "function") { - return S_Core$RescriptStruct.$$Option.getOrWith(struct$1, or); - } else { - return S_Core$RescriptStruct.$$Option.getOr(struct$1, or); - } -} - -function tuple(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.tuple(definer); - } else { - return S_Core$RescriptStruct.tuple(function (s) { - return definer.map(function (struct, idx) { - return s.i(idx, struct); - }); - }); - } -} - -function custom(name, maybeParser, maybeSerializer, param) { - return S_Core$RescriptStruct.custom(name, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function object(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.object(definer); - } else { - return S_Core$RescriptStruct.object(function (s) { - var definition = {}; - var fieldNames = Object.keys(definer); - for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ - var fieldName = fieldNames[idx]; - var struct = definer[fieldName]; - definition[fieldName] = s.f(fieldName, struct); - } - return definition; - }); - } -} - -function parse(struct, data) { - return toJsResult(S_Core$RescriptStruct.parseAnyWith(data, struct)); -} - -function parseOrThrow(struct, data) { - return S_Core$RescriptStruct.parseAnyOrRaiseWith(data, struct); -} - -function parseAsync(struct, data) { - return S_Core$RescriptStruct.parseAnyAsyncWith(data, struct).then(toJsResult); -} - -function serialize(struct, value) { - return toJsResult(S_Core$RescriptStruct.serializeToUnknownWith(value, struct)); -} - -function serializeOrThrow(struct, value) { - return S_Core$RescriptStruct.serializeToUnknownOrRaiseWith(value, struct); -} - -exports.optional = optional; -exports.tuple = tuple; -exports.custom = custom; -exports.asyncParserRefine = asyncParserRefine; -exports.refine = refine; -exports.transform = transform; -exports.object = object; -exports.parse = parse; -exports.parseOrThrow = parseOrThrow; -exports.parseAsync = parseAsync; -exports.serialize = serialize; -exports.serializeOrThrow = serializeOrThrow; -/* S_Core-RescriptStruct Not a pure module */ diff --git a/packages/artifacts/src/S_JsApi.bs.mjs b/packages/artifacts/src/S_JsApi.bs.mjs deleted file mode 100644 index 9f229630..00000000 --- a/packages/artifacts/src/S_JsApi.bs.mjs +++ /dev/null @@ -1,149 +0,0 @@ -// Generated by ReScript, PLEASE EDIT WITH CARE - -import * as Caml_option from "rescript/lib/es6/caml_option.js"; -import * as S_Core$RescriptStruct from "./S_Core.bs.mjs"; - -function toJsResult(result) { - if (result.TAG === "Ok") { - return { - success: true, - value: result._0 - }; - } else { - return { - success: false, - error: result._0 - }; - } -} - -function transform(struct, maybeParser, maybeSerializer) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function refine(struct, refiner) { - return S_Core$RescriptStruct.refine(struct, (function (s) { - return function (v) { - refiner(v, s); - }; - })); -} - -function noop(a) { - return a; -} - -function asyncParserRefine(struct, refine) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - a: (function (v) { - return function () { - return refine(v, s).then(function () { - return v; - }); - }; - }), - s: noop - }; - })); -} - -function optional(struct, maybeOr) { - var struct$1 = S_Core$RescriptStruct.option(struct); - if (maybeOr === undefined) { - return struct$1; - } - var or = Caml_option.valFromOption(maybeOr); - if (typeof or === "function") { - return S_Core$RescriptStruct.$$Option.getOrWith(struct$1, or); - } else { - return S_Core$RescriptStruct.$$Option.getOr(struct$1, or); - } -} - -function tuple(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.tuple(definer); - } else { - return S_Core$RescriptStruct.tuple(function (s) { - return definer.map(function (struct, idx) { - return s.i(idx, struct); - }); - }); - } -} - -function custom(name, maybeParser, maybeSerializer, param) { - return S_Core$RescriptStruct.custom(name, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function object(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.object(definer); - } else { - return S_Core$RescriptStruct.object(function (s) { - var definition = {}; - var fieldNames = Object.keys(definer); - for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ - var fieldName = fieldNames[idx]; - var struct = definer[fieldName]; - definition[fieldName] = s.f(fieldName, struct); - } - return definition; - }); - } -} - -function parse(struct, data) { - return toJsResult(S_Core$RescriptStruct.parseAnyWith(data, struct)); -} - -function parseOrThrow(struct, data) { - return S_Core$RescriptStruct.parseAnyOrRaiseWith(data, struct); -} - -function parseAsync(struct, data) { - return S_Core$RescriptStruct.parseAnyAsyncWith(data, struct).then(toJsResult); -} - -function serialize(struct, value) { - return toJsResult(S_Core$RescriptStruct.serializeToUnknownWith(value, struct)); -} - -function serializeOrThrow(struct, value) { - return S_Core$RescriptStruct.serializeToUnknownOrRaiseWith(value, struct); -} - -export { - optional , - tuple , - custom , - asyncParserRefine , - refine , - transform , - object , - parse , - parseOrThrow , - parseAsync , - serialize , - serializeOrThrow , -} -/* S_Core-RescriptStruct Not a pure module */ diff --git a/packages/artifacts/src/S_JsApi.res b/packages/artifacts/src/S_JsApi.res deleted file mode 100644 index 27f18c42..00000000 --- a/packages/artifacts/src/S_JsApi.res +++ /dev/null @@ -1,132 +0,0 @@ -@@uncurried - -module Obj = { - external magic: 'a => 'b = "%identity" -} - -module Stdlib = { - module Promise = { - type t<+'a> = promise<'a> - - @send - external thenResolve: (t<'a>, 'a => 'b) => t<'b> = "then" - } -} - -type jsResult<'value> - -let toJsResult = (result: result<'value, S_Core.error>): jsResult<'value> => { - switch result { - | Ok(value) => {"success": true, "value": value}->Obj.magic - | Error(error) => {"success": false, "error": error}->Obj.magic - } -} - -let transform = (struct, ~parser as maybeParser=?, ~serializer as maybeSerializer=?) => { - struct->S_Core.transform(s => { - { - parser: ?switch maybeParser { - | Some(parser) => Some(v => parser(v, s)) - | None => None - }, - serializer: ?switch maybeSerializer { - | Some(serializer) => Some(v => serializer(v, s)) - | None => None - }, - } - }) -} - -let refine = (struct, refiner) => { - struct->S_Core.refine(s => { - v => refiner(v, s) - }) -} - -let noop = a => a -let asyncParserRefine = (struct, refine) => { - struct->S_Core.transform(s => { - { - asyncParser: v => () => refine(v, s)->Stdlib.Promise.thenResolve(() => v), - serializer: noop, - } - }) -} - -let optional = (struct, maybeOr) => { - let struct = S_Core.option(struct) - switch maybeOr { - | Some(or) if Js.typeof(or) === "function" => - struct->S_Core.Option.getOrWith(or->Obj.magic)->Obj.magic - | Some(or) => struct->S_Core.Option.getOr(or->Obj.magic)->Obj.magic - | None => struct - } -} - -let tuple = definer => { - if Js.typeof(definer) === "function" { - let definer = definer->(Obj.magic: unknown => S_Core.Tuple.ctx => 'a) - S_Core.tuple(definer) - } else { - let structs = definer->(Obj.magic: unknown => array>) - S_Core.tuple(s => { - structs->Js.Array2.mapi((struct, idx) => { - s.item(idx, struct) - }) - }) - } -} - -let custom = (~name, ~parser as maybeParser=?, ~serializer as maybeSerializer=?, ()) => { - S_Core.custom(name, s => { - { - parser: ?switch maybeParser { - | Some(parser) => Some(v => parser(v, s)) - | None => None - }, - serializer: ?switch maybeSerializer { - | Some(serializer) => Some(v => serializer(v, s)) - | None => None - }, - } - }) -} - -let object = definer => { - if Js.typeof(definer) === "function" { - let definer = definer->(Obj.magic: unknown => S_Core.Object.ctx => 'a) - S_Core.object(definer) - } else { - let definer = definer->(Obj.magic: unknown => Js.Dict.t>) - S_Core.object(s => { - let definition = Js.Dict.empty() - let fieldNames = definer->Js.Dict.keys - for idx in 0 to fieldNames->Js.Array2.length - 1 { - let fieldName = fieldNames->Js.Array2.unsafe_get(idx) - let struct = definer->Js.Dict.unsafeGet(fieldName) - definition->Js.Dict.set(fieldName, s.field(fieldName, struct)) - } - definition - }) - } -} - -let parse = (struct, data) => { - data->S_Core.parseAnyWith(struct)->toJsResult -} - -let parseOrThrow = (struct, data) => { - data->S_Core.parseAnyOrRaiseWith(struct) -} - -let parseAsync = (struct, data) => { - data->S_Core.parseAnyAsyncWith(struct)->Stdlib.Promise.thenResolve(toJsResult) -} - -let serialize = (struct, value) => { - value->S_Core.serializeToUnknownWith(struct)->Obj.magic->toJsResult -} - -let serializeOrThrow = (struct, value) => { - value->S_Core.serializeToUnknownOrRaiseWith(struct) -} diff --git a/packages/artifacts/src/S_JsApi.resi b/packages/artifacts/src/S_JsApi.resi deleted file mode 100644 index 552d451d..00000000 --- a/packages/artifacts/src/S_JsApi.resi +++ /dev/null @@ -1,34 +0,0 @@ -@@uncurried - -type jsResult<'value> - -let optional: (S_Core.t<'v>, option) => S_Core.t> - -let tuple: unknown => S_Core.t> - -let custom: ( - ~name: string, - ~parser: (unknown, S_Core.effectCtx<'output>) => 'output=?, - ~serializer: ('output, S_Core.effectCtx<'output>) => 'input=?, - unit, -) => S_Core.t<'output> - -let asyncParserRefine: ( - S_Core.t<'output>, - ('output, S_Core.effectCtx<'output>) => promise, -) => S_Core.t<'output> -let refine: (S_Core.t<'output>, ('output, S_Core.effectCtx<'output>) => unit) => S_Core.t<'output> - -let transform: ( - S_Core.t<'output>, - ~parser: ('output, S_Core.effectCtx<'transformed>) => 'transformed=?, - ~serializer: ('transformed, S_Core.effectCtx<'transformed>) => 'output=?, -) => S_Core.t<'transformed> - -let object: unknown => S_Core.t> - -let parse: (S_Core.t<'output>, 'a) => jsResult<'output> -let parseOrThrow: (S_Core.t<'output>, 'a) => 'output -let parseAsync: (S_Core.t<'output>, 'a) => promise> -let serialize: (S_Core.t<'output>, 'output) => jsResult<'input> -let serializeOrThrow: (S_Core.t<'output>, 'output) => unknown diff --git a/packages/prepack/src/Prepack.bs.mjs b/packages/prepack/src/Prepack.bs.mjs index b5269f3d..fcc5a7dd 100644 --- a/packages/prepack/src/Prepack.bs.mjs +++ b/packages/prepack/src/Prepack.bs.mjs @@ -23,7 +23,7 @@ var sourePaths = [ "RescriptStruct.gen.ts" ]; -var jsInputPath = Path.join(artifactsPath, "src/S_JsApi.js"); +var jsInputPath = Path.join(artifactsPath, "src/S.js"); function update(json, path, value) { var dict = Core__JSON.Decode.object(json); diff --git a/packages/prepack/src/Prepack.res b/packages/prepack/src/Prepack.res index dae63c6a..e7f239e4 100644 --- a/packages/prepack/src/Prepack.res +++ b/packages/prepack/src/Prepack.res @@ -8,7 +8,7 @@ let sourePaths = [ "README.md", "RescriptStruct.gen.ts", ] -let jsInputPath = NodeJs.Path.join2(artifactsPath, "src/S_JsApi.js") +let jsInputPath = NodeJs.Path.join2(artifactsPath, "src/S.js") module Stdlib = { module Dict = { diff --git a/packages/tests/src/benchmark/comparison.js b/packages/tests/src/benchmark/comparison.js index cfc22dbf..c8e08ec7 100644 --- a/packages/tests/src/benchmark/comparison.js +++ b/packages/tests/src/benchmark/comparison.js @@ -1,7 +1,7 @@ import B from "benchmark"; import { z } from "zod"; import * as V from "valibot"; -import * as S from "rescript-struct/src/S_JsApi.js"; +import * as S from "rescript-struct/src/S.js"; new B.Suite() .add("rescript-struct", () => { diff --git a/packages/tests/src/core/S_JsApi_test.ts b/packages/tests/src/core/S_JsApi_test.ts index decd5c94..a196667f 100644 --- a/packages/tests/src/core/S_JsApi_test.ts +++ b/packages/tests/src/core/S_JsApi_test.ts @@ -1,7 +1,7 @@ import test from "ava"; import { expectType, TypeEqual } from "ts-expect"; -import * as S from "../../../../src/S_JsApi.js"; +import * as S from "../../../../src/S.js"; test("Successfully parses string", (t) => { const struct = S.string; diff --git a/packages/tests/src/genType/GenType_test_type.ts b/packages/tests/src/genType/GenType_test_type.ts index eb5ce9bc..51216116 100644 --- a/packages/tests/src/genType/GenType_test_type.ts +++ b/packages/tests/src/genType/GenType_test_type.ts @@ -1,6 +1,6 @@ import { expectType, TypeEqual } from "ts-expect"; -import * as S from "../../../../src/S_JsApi.js"; +import * as S from "../../../../src/S.js"; import * as GenType from "./GenType.gen"; expectType>>( diff --git a/src/S_JsApi.d.ts b/src/S.d.ts similarity index 100% rename from src/S_JsApi.d.ts rename to src/S.d.ts diff --git a/packages/artifacts/src/S_JsApi.js b/src/S.js similarity index 57% rename from packages/artifacts/src/S_JsApi.js rename to src/S.js index 13ff092e..36eb1509 100644 --- a/packages/artifacts/src/S_JsApi.js +++ b/src/S.js @@ -1,4 +1,3 @@ -import * as S_Js from "./S_JsApi.bs.mjs"; import * as S from "./S_Core.bs.mjs"; export const Error = S.$$Error.$$class; @@ -10,27 +9,27 @@ export const json = S.json; export const never = S.never; export const unknown = S.unknown; export const undefined = S.unit; -export const optional = S_Js.optional; +export const optional = S.js_optional; export const nullable = S.$$null; export const array = S.array; export const record = S.dict; export const jsonString = S.jsonString; export const union = S.union; -export const object = S_Js.object; +export const object = S.js_object; export const Object = S.$$Object; export const String = S.$$String; export const Number = S.Float; export const Array = S.$$Array; -export const custom = S_Js.custom; +export const custom = S.js_custom; export const literal = S.literal; -export const tuple = S_Js.tuple; -export const asyncParserRefine = S_Js.asyncParserRefine; -export const refine = S_Js.refine; -export const transform = S_Js.transform; +export const tuple = S.js_tuple; +export const asyncParserRefine = S.js_asyncParserRefine; +export const refine = S.js_refine; +export const transform = S.js_transform; export const description = S.description; export const describe = S.describe; -export const parse = S_Js.parse; -export const parseOrThrow = S_Js.parseOrThrow; -export const parseAsync = S_Js.parseAsync; -export const serialize = S_Js.serialize; -export const serializeOrThrow = S_Js.serializeOrThrow; +export const parse = S.js_parse; +export const parseOrThrow = S.js_parseOrThrow; +export const parseAsync = S.js_parseAsync; +export const serialize = S.js_serialize; +export const serializeOrThrow = S.js_serializeOrThrow; diff --git a/src/S_Core.bs.mjs b/src/S_Core.bs.mjs index 6a19c066..a9ee3d68 100644 --- a/src/S_Core.bs.mjs +++ b/src/S_Core.bs.mjs @@ -3412,6 +3412,135 @@ function tuple3(v0, v1, v2) { }); } +function toJsResult(result) { + if (result.TAG === "Ok") { + return { + success: true, + value: result._0 + }; + } else { + return { + success: false, + error: result._0 + }; + } +} + +function js_parse(struct, data) { + return toJsResult(parseAnyWith(data, struct)); +} + +function js_parseOrThrow(struct, data) { + return parseAnyOrRaiseWith(data, struct); +} + +function js_parseAsync(struct, data) { + return parseAnyAsyncWith(data, struct).then(toJsResult); +} + +function js_serialize(struct, value) { + return toJsResult(serializeToUnknownWith(value, struct)); +} + +function js_serializeOrThrow(struct, value) { + return serializeToUnknownOrRaiseWith(value, struct); +} + +function js_transform(struct, maybeParser, maybeSerializer) { + return transform$1(struct, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_refine(struct, refiner) { + return refine(struct, (function (s) { + return function (v) { + refiner(v, s); + }; + })); +} + +function noop$1(a) { + return a; +} + +function js_asyncParserRefine(struct, refine) { + return transform$1(struct, (function (s) { + return { + a: (function (v) { + return function () { + return refine(v, s).then(function () { + return v; + }); + }; + }), + s: noop$1 + }; + })); +} + +function js_optional(struct, maybeOr) { + var struct$1 = factory$1(struct); + if (maybeOr === undefined) { + return struct$1; + } + var or = Caml_option.valFromOption(maybeOr); + if (typeof or === "function") { + return getOrWith(struct$1, or); + } else { + return getOr(struct$1, or); + } +} + +function js_tuple(definer) { + if (typeof definer === "function") { + return factory$7(definer); + } else { + return factory$7(function (s) { + return definer.map(function (struct, idx) { + return s.i(idx, struct); + }); + }); + } +} + +function js_custom(name, maybeParser, maybeSerializer, param) { + return custom(name, (function (s) { + return { + p: maybeParser !== undefined ? (function (v) { + return maybeParser(v, s); + }) : undefined, + s: maybeSerializer !== undefined ? (function (v) { + return maybeSerializer(v, s); + }) : undefined + }; + })); +} + +function js_object(definer) { + if (typeof definer === "function") { + return factory$3(definer); + } else { + return factory$3(function (s) { + var definition = {}; + var fieldNames = Object.keys(definer); + for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ + var fieldName = fieldNames[idx]; + var struct = definer[fieldName]; + definition[fieldName] = s.f(fieldName, struct); + } + return definition; + }); + } +} + var B; var parseWith = parseAnyWith; @@ -3548,5 +3677,19 @@ export { tuple3 , union , jsonString , + toJsResult , + js_parse , + js_parseOrThrow , + js_parseAsync , + js_serialize , + js_serializeOrThrow , + js_transform , + js_refine , + noop$1 as noop, + js_asyncParserRefine , + js_optional , + js_tuple , + js_custom , + js_object , } /* symbol Not a pure module */ diff --git a/src/S_Core.res b/src/S_Core.res index ed192b36..5f2b11aa 100644 --- a/src/S_Core.res +++ b/src/S_Core.res @@ -22,6 +22,9 @@ module Stdlib = { @send external thenResolveWithCatch: (t<'a>, 'a => 'b, Js.Exn.t => 'b) => t<'b> = "then" + @send + external thenResolve: (t<'a>, 'a => 'b) => t<'b> = "then" + @val @scope("Promise") external resolve: 'a => t<'a> = "resolve" } @@ -3546,3 +3549,124 @@ let jsonString = JsonString.factory @send external name: t<'a> => string = "n" + +// ============= +// JS/TS API +// ============= + +type jsResult<'value> + +let toJsResult = (result: result<'value, error>): jsResult<'value> => { + switch result { + | Ok(value) => {"success": true, "value": value}->Obj.magic + | Error(error) => {"success": false, "error": error}->Obj.magic + } +} + +let js_parse = (struct, data) => { + data->parseAnyWith(struct)->toJsResult +} + +let js_parseOrThrow = (struct, data) => { + data->parseAnyOrRaiseWith(struct) +} + +let js_parseAsync = (struct, data) => { + data->parseAnyAsyncWith(struct)->Stdlib.Promise.thenResolve(toJsResult) +} + +let js_serialize = (struct, value) => { + value->serializeToUnknownWith(struct)->Obj.magic->toJsResult +} + +let js_serializeOrThrow = (struct, value) => { + value->serializeToUnknownOrRaiseWith(struct) +} + +let js_transform = (struct, ~parser as maybeParser=?, ~serializer as maybeSerializer=?) => { + struct->transform(s => { + { + parser: ?switch maybeParser { + | Some(parser) => Some(v => parser(v, s)) + | None => None + }, + serializer: ?switch maybeSerializer { + | Some(serializer) => Some(v => serializer(v, s)) + | None => None + }, + } + }) +} + +let js_refine = (struct, refiner) => { + struct->refine(s => { + v => refiner(v, s) + }) +} + +let noop = a => a +let js_asyncParserRefine = (struct, refine) => { + struct->transform(s => { + { + asyncParser: v => () => refine(v, s)->Stdlib.Promise.thenResolve(() => v), + serializer: noop, + } + }) +} + +let js_optional = (struct, maybeOr) => { + let struct = option(struct) + switch maybeOr { + | Some(or) if Js.typeof(or) === "function" => struct->Option.getOrWith(or->Obj.magic)->Obj.magic + | Some(or) => struct->Option.getOr(or->Obj.magic)->Obj.magic + | None => struct + } +} + +let js_tuple = definer => { + if Js.typeof(definer) === "function" { + let definer = definer->(Obj.magic: unknown => Tuple.ctx => 'a) + tuple(definer) + } else { + let structs = definer->(Obj.magic: unknown => array>) + tuple(s => { + structs->Js.Array2.mapi((struct, idx) => { + s.item(idx, struct) + }) + }) + } +} + +let js_custom = (~name, ~parser as maybeParser=?, ~serializer as maybeSerializer=?, ()) => { + custom(name, s => { + { + parser: ?switch maybeParser { + | Some(parser) => Some(v => parser(v, s)) + | None => None + }, + serializer: ?switch maybeSerializer { + | Some(serializer) => Some(v => serializer(v, s)) + | None => None + }, + } + }) +} + +let js_object = definer => { + if Js.typeof(definer) === "function" { + let definer = definer->(Obj.magic: unknown => Object.ctx => 'a) + object(definer) + } else { + let definer = definer->(Obj.magic: unknown => Js.Dict.t>) + object(s => { + let definition = Js.Dict.empty() + let fieldNames = definer->Js.Dict.keys + for idx in 0 to fieldNames->Js.Array2.length - 1 { + let fieldName = fieldNames->Js.Array2.unsafe_get(idx) + let struct = definer->Js.Dict.unsafeGet(fieldName) + definition->Js.Dict.set(fieldName, s.field(fieldName, struct)) + } + definition + }) + } +} diff --git a/src/S_JsApi.bs.mjs b/src/S_JsApi.bs.mjs deleted file mode 100644 index 9f229630..00000000 --- a/src/S_JsApi.bs.mjs +++ /dev/null @@ -1,149 +0,0 @@ -// Generated by ReScript, PLEASE EDIT WITH CARE - -import * as Caml_option from "rescript/lib/es6/caml_option.js"; -import * as S_Core$RescriptStruct from "./S_Core.bs.mjs"; - -function toJsResult(result) { - if (result.TAG === "Ok") { - return { - success: true, - value: result._0 - }; - } else { - return { - success: false, - error: result._0 - }; - } -} - -function transform(struct, maybeParser, maybeSerializer) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function refine(struct, refiner) { - return S_Core$RescriptStruct.refine(struct, (function (s) { - return function (v) { - refiner(v, s); - }; - })); -} - -function noop(a) { - return a; -} - -function asyncParserRefine(struct, refine) { - return S_Core$RescriptStruct.transform(struct, (function (s) { - return { - a: (function (v) { - return function () { - return refine(v, s).then(function () { - return v; - }); - }; - }), - s: noop - }; - })); -} - -function optional(struct, maybeOr) { - var struct$1 = S_Core$RescriptStruct.option(struct); - if (maybeOr === undefined) { - return struct$1; - } - var or = Caml_option.valFromOption(maybeOr); - if (typeof or === "function") { - return S_Core$RescriptStruct.$$Option.getOrWith(struct$1, or); - } else { - return S_Core$RescriptStruct.$$Option.getOr(struct$1, or); - } -} - -function tuple(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.tuple(definer); - } else { - return S_Core$RescriptStruct.tuple(function (s) { - return definer.map(function (struct, idx) { - return s.i(idx, struct); - }); - }); - } -} - -function custom(name, maybeParser, maybeSerializer, param) { - return S_Core$RescriptStruct.custom(name, (function (s) { - return { - p: maybeParser !== undefined ? (function (v) { - return maybeParser(v, s); - }) : undefined, - s: maybeSerializer !== undefined ? (function (v) { - return maybeSerializer(v, s); - }) : undefined - }; - })); -} - -function object(definer) { - if (typeof definer === "function") { - return S_Core$RescriptStruct.object(definer); - } else { - return S_Core$RescriptStruct.object(function (s) { - var definition = {}; - var fieldNames = Object.keys(definer); - for(var idx = 0 ,idx_finish = fieldNames.length; idx < idx_finish; ++idx){ - var fieldName = fieldNames[idx]; - var struct = definer[fieldName]; - definition[fieldName] = s.f(fieldName, struct); - } - return definition; - }); - } -} - -function parse(struct, data) { - return toJsResult(S_Core$RescriptStruct.parseAnyWith(data, struct)); -} - -function parseOrThrow(struct, data) { - return S_Core$RescriptStruct.parseAnyOrRaiseWith(data, struct); -} - -function parseAsync(struct, data) { - return S_Core$RescriptStruct.parseAnyAsyncWith(data, struct).then(toJsResult); -} - -function serialize(struct, value) { - return toJsResult(S_Core$RescriptStruct.serializeToUnknownWith(value, struct)); -} - -function serializeOrThrow(struct, value) { - return S_Core$RescriptStruct.serializeToUnknownOrRaiseWith(value, struct); -} - -export { - optional , - tuple , - custom , - asyncParserRefine , - refine , - transform , - object , - parse , - parseOrThrow , - parseAsync , - serialize , - serializeOrThrow , -} -/* S_Core-RescriptStruct Not a pure module */ diff --git a/src/S_JsApi.res b/src/S_JsApi.res deleted file mode 100644 index 27f18c42..00000000 --- a/src/S_JsApi.res +++ /dev/null @@ -1,132 +0,0 @@ -@@uncurried - -module Obj = { - external magic: 'a => 'b = "%identity" -} - -module Stdlib = { - module Promise = { - type t<+'a> = promise<'a> - - @send - external thenResolve: (t<'a>, 'a => 'b) => t<'b> = "then" - } -} - -type jsResult<'value> - -let toJsResult = (result: result<'value, S_Core.error>): jsResult<'value> => { - switch result { - | Ok(value) => {"success": true, "value": value}->Obj.magic - | Error(error) => {"success": false, "error": error}->Obj.magic - } -} - -let transform = (struct, ~parser as maybeParser=?, ~serializer as maybeSerializer=?) => { - struct->S_Core.transform(s => { - { - parser: ?switch maybeParser { - | Some(parser) => Some(v => parser(v, s)) - | None => None - }, - serializer: ?switch maybeSerializer { - | Some(serializer) => Some(v => serializer(v, s)) - | None => None - }, - } - }) -} - -let refine = (struct, refiner) => { - struct->S_Core.refine(s => { - v => refiner(v, s) - }) -} - -let noop = a => a -let asyncParserRefine = (struct, refine) => { - struct->S_Core.transform(s => { - { - asyncParser: v => () => refine(v, s)->Stdlib.Promise.thenResolve(() => v), - serializer: noop, - } - }) -} - -let optional = (struct, maybeOr) => { - let struct = S_Core.option(struct) - switch maybeOr { - | Some(or) if Js.typeof(or) === "function" => - struct->S_Core.Option.getOrWith(or->Obj.magic)->Obj.magic - | Some(or) => struct->S_Core.Option.getOr(or->Obj.magic)->Obj.magic - | None => struct - } -} - -let tuple = definer => { - if Js.typeof(definer) === "function" { - let definer = definer->(Obj.magic: unknown => S_Core.Tuple.ctx => 'a) - S_Core.tuple(definer) - } else { - let structs = definer->(Obj.magic: unknown => array>) - S_Core.tuple(s => { - structs->Js.Array2.mapi((struct, idx) => { - s.item(idx, struct) - }) - }) - } -} - -let custom = (~name, ~parser as maybeParser=?, ~serializer as maybeSerializer=?, ()) => { - S_Core.custom(name, s => { - { - parser: ?switch maybeParser { - | Some(parser) => Some(v => parser(v, s)) - | None => None - }, - serializer: ?switch maybeSerializer { - | Some(serializer) => Some(v => serializer(v, s)) - | None => None - }, - } - }) -} - -let object = definer => { - if Js.typeof(definer) === "function" { - let definer = definer->(Obj.magic: unknown => S_Core.Object.ctx => 'a) - S_Core.object(definer) - } else { - let definer = definer->(Obj.magic: unknown => Js.Dict.t>) - S_Core.object(s => { - let definition = Js.Dict.empty() - let fieldNames = definer->Js.Dict.keys - for idx in 0 to fieldNames->Js.Array2.length - 1 { - let fieldName = fieldNames->Js.Array2.unsafe_get(idx) - let struct = definer->Js.Dict.unsafeGet(fieldName) - definition->Js.Dict.set(fieldName, s.field(fieldName, struct)) - } - definition - }) - } -} - -let parse = (struct, data) => { - data->S_Core.parseAnyWith(struct)->toJsResult -} - -let parseOrThrow = (struct, data) => { - data->S_Core.parseAnyOrRaiseWith(struct) -} - -let parseAsync = (struct, data) => { - data->S_Core.parseAnyAsyncWith(struct)->Stdlib.Promise.thenResolve(toJsResult) -} - -let serialize = (struct, value) => { - value->S_Core.serializeToUnknownWith(struct)->Obj.magic->toJsResult -} - -let serializeOrThrow = (struct, value) => { - value->S_Core.serializeToUnknownOrRaiseWith(struct) -} diff --git a/src/S_JsApi.resi b/src/S_JsApi.resi deleted file mode 100644 index 552d451d..00000000 --- a/src/S_JsApi.resi +++ /dev/null @@ -1,34 +0,0 @@ -@@uncurried - -type jsResult<'value> - -let optional: (S_Core.t<'v>, option) => S_Core.t> - -let tuple: unknown => S_Core.t> - -let custom: ( - ~name: string, - ~parser: (unknown, S_Core.effectCtx<'output>) => 'output=?, - ~serializer: ('output, S_Core.effectCtx<'output>) => 'input=?, - unit, -) => S_Core.t<'output> - -let asyncParserRefine: ( - S_Core.t<'output>, - ('output, S_Core.effectCtx<'output>) => promise, -) => S_Core.t<'output> -let refine: (S_Core.t<'output>, ('output, S_Core.effectCtx<'output>) => unit) => S_Core.t<'output> - -let transform: ( - S_Core.t<'output>, - ~parser: ('output, S_Core.effectCtx<'transformed>) => 'transformed=?, - ~serializer: ('transformed, S_Core.effectCtx<'transformed>) => 'output=?, -) => S_Core.t<'transformed> - -let object: unknown => S_Core.t> - -let parse: (S_Core.t<'output>, 'a) => jsResult<'output> -let parseOrThrow: (S_Core.t<'output>, 'a) => 'output -let parseAsync: (S_Core.t<'output>, 'a) => promise> -let serialize: (S_Core.t<'output>, 'output) => jsResult<'input> -let serializeOrThrow: (S_Core.t<'output>, 'output) => unknown diff --git a/wallaby.conf.js b/wallaby.conf.js index 37869c06..1323d6aa 100644 --- a/wallaby.conf.js +++ b/wallaby.conf.js @@ -9,8 +9,7 @@ export default () => ({ "package.json", "src/S.bs.mjs", "src/S_Core.bs.mjs", - "src/S_JsApi.bs.mjs", - "src/S_JsApi.js", + "src/S.js", "packages/tests/src/utils/U.bs.mjs", ], tests,