@praha/byethrow 0.4.1 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/exports.cjs +24 -15
- package/dist/cjs/exports.d.ts +1 -0
- package/dist/cjs/functions/parse.cjs +46 -0
- package/dist/cjs/functions/parse.d.ts +37 -0
- package/dist/esm/exports.d.ts +1 -0
- package/dist/esm/exports.js +1 -0
- package/dist/esm/functions/parse.d.ts +37 -0
- package/dist/esm/functions/parse.js +12 -0
- package/package.json +4 -1
package/dist/cjs/exports.cjs
CHANGED
@@ -42,6 +42,9 @@ var __webpack_modules__ = {
|
|
42
42
|
"./functions/or-else": function(module) {
|
43
43
|
module.exports = require("./functions/or-else.cjs");
|
44
44
|
},
|
45
|
+
"./functions/parse": function(module) {
|
46
|
+
module.exports = require("./functions/parse.cjs");
|
47
|
+
},
|
45
48
|
"./functions/pipe": function(module) {
|
46
49
|
module.exports = require("./functions/pipe.cjs");
|
47
50
|
},
|
@@ -194,34 +197,40 @@ var __webpack_exports__ = {};
|
|
194
197
|
return _functions_or_else__WEBPACK_IMPORTED_MODULE_14__[key];
|
195
198
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
196
199
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
197
|
-
var
|
200
|
+
var _functions_parse__WEBPACK_IMPORTED_MODULE_15__ = __webpack_require__("./functions/parse");
|
201
|
+
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
202
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_parse__WEBPACK_IMPORTED_MODULE_15__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
203
|
+
return _functions_parse__WEBPACK_IMPORTED_MODULE_15__[key];
|
204
|
+
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
205
|
+
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
206
|
+
var _functions_pipe__WEBPACK_IMPORTED_MODULE_16__ = __webpack_require__("./functions/pipe");
|
198
207
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
199
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
200
|
-
return
|
208
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_pipe__WEBPACK_IMPORTED_MODULE_16__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
209
|
+
return _functions_pipe__WEBPACK_IMPORTED_MODULE_16__[key];
|
201
210
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
202
211
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
203
|
-
var
|
212
|
+
var _functions_succeed__WEBPACK_IMPORTED_MODULE_17__ = __webpack_require__("./functions/succeed");
|
204
213
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
205
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
206
|
-
return
|
214
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_succeed__WEBPACK_IMPORTED_MODULE_17__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
215
|
+
return _functions_succeed__WEBPACK_IMPORTED_MODULE_17__[key];
|
207
216
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
208
217
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
209
|
-
var
|
218
|
+
var _functions_try__WEBPACK_IMPORTED_MODULE_18__ = __webpack_require__("./functions/try");
|
210
219
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
211
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
212
|
-
return
|
220
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_try__WEBPACK_IMPORTED_MODULE_18__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
221
|
+
return _functions_try__WEBPACK_IMPORTED_MODULE_18__[key];
|
213
222
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
214
223
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
215
|
-
var
|
224
|
+
var _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__ = __webpack_require__("./functions/unwrap");
|
216
225
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
217
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
218
|
-
return
|
226
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
227
|
+
return _functions_unwrap__WEBPACK_IMPORTED_MODULE_19__[key];
|
219
228
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
220
229
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
221
|
-
var
|
230
|
+
var _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__ = __webpack_require__("./functions/unwrap-error");
|
222
231
|
var __WEBPACK_REEXPORT_OBJECT__ = {};
|
223
|
-
for(var __WEBPACK_IMPORT_KEY__ in
|
224
|
-
return
|
232
|
+
for(var __WEBPACK_IMPORT_KEY__ in _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__)if ("default" !== __WEBPACK_IMPORT_KEY__) __WEBPACK_REEXPORT_OBJECT__[__WEBPACK_IMPORT_KEY__] = (function(key) {
|
233
|
+
return _functions_unwrap_error__WEBPACK_IMPORTED_MODULE_20__[key];
|
225
234
|
}).bind(0, __WEBPACK_IMPORT_KEY__);
|
226
235
|
__webpack_require__.d(__webpack_exports__, __WEBPACK_REEXPORT_OBJECT__);
|
227
236
|
})();
|
package/dist/cjs/exports.d.ts
CHANGED
@@ -13,6 +13,7 @@ export * from './functions/is-success';
|
|
13
13
|
export * from './functions/map';
|
14
14
|
export * from './functions/map-error';
|
15
15
|
export * from './functions/or-else';
|
16
|
+
export * from './functions/parse';
|
16
17
|
export * from './functions/pipe';
|
17
18
|
export * from './functions/succeed';
|
18
19
|
export * from './functions/try';
|
@@ -0,0 +1,46 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __webpack_require__ = {};
|
3
|
+
(()=>{
|
4
|
+
__webpack_require__.d = (exports1, definition)=>{
|
5
|
+
for(var key in definition)if (__webpack_require__.o(definition, key) && !__webpack_require__.o(exports1, key)) Object.defineProperty(exports1, key, {
|
6
|
+
enumerable: true,
|
7
|
+
get: definition[key]
|
8
|
+
});
|
9
|
+
};
|
10
|
+
})();
|
11
|
+
(()=>{
|
12
|
+
__webpack_require__.o = (obj, prop)=>Object.prototype.hasOwnProperty.call(obj, prop);
|
13
|
+
})();
|
14
|
+
(()=>{
|
15
|
+
__webpack_require__.r = (exports1)=>{
|
16
|
+
if ('undefined' != typeof Symbol && Symbol.toStringTag) Object.defineProperty(exports1, Symbol.toStringTag, {
|
17
|
+
value: 'Module'
|
18
|
+
});
|
19
|
+
Object.defineProperty(exports1, '__esModule', {
|
20
|
+
value: true
|
21
|
+
});
|
22
|
+
};
|
23
|
+
})();
|
24
|
+
var __webpack_exports__ = {};
|
25
|
+
__webpack_require__.r(__webpack_exports__);
|
26
|
+
__webpack_require__.d(__webpack_exports__, {
|
27
|
+
parse: ()=>parse
|
28
|
+
});
|
29
|
+
const external_fail_cjs_namespaceObject = require("./fail.cjs");
|
30
|
+
const external_succeed_cjs_namespaceObject = require("./succeed.cjs");
|
31
|
+
const is_promise_cjs_namespaceObject = require("../internals/helpers/is-promise.cjs");
|
32
|
+
const parse = (schema, ...args)=>{
|
33
|
+
const apply = (input)=>{
|
34
|
+
const result = schema['~standard']['validate'](input);
|
35
|
+
if ((0, is_promise_cjs_namespaceObject.isPromise)(result)) throw new TypeError('Schema validation must be synchronous');
|
36
|
+
return result.issues ? (0, external_fail_cjs_namespaceObject.fail)(result.issues) : (0, external_succeed_cjs_namespaceObject.succeed)(result.value);
|
37
|
+
};
|
38
|
+
return args.length <= 0 ? apply : apply(args[0]);
|
39
|
+
};
|
40
|
+
exports.parse = __webpack_exports__.parse;
|
41
|
+
for(var __webpack_i__ in __webpack_exports__)if (-1 === [
|
42
|
+
"parse"
|
43
|
+
].indexOf(__webpack_i__)) exports[__webpack_i__] = __webpack_exports__[__webpack_i__];
|
44
|
+
Object.defineProperty(exports, '__esModule', {
|
45
|
+
value: true
|
46
|
+
});
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import type { Result } from '../result';
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
3
|
+
/**
|
4
|
+
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
5
|
+
* Returns a {@link Result} with the parsed value on success or validation errors on failure.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam S - The Standard Schema V1 compatible schema type.
|
9
|
+
* @returns A {@link Result} with the parsed value or validation errors.
|
10
|
+
*
|
11
|
+
* @example Basic Usage
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
* import { z } from 'zod';
|
15
|
+
*
|
16
|
+
* const schema = z.object({ name: z.string() });
|
17
|
+
* const result = Result.parse(schema, { name: 'John' });
|
18
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Curried Usage
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
* import { z } from 'zod';
|
25
|
+
*
|
26
|
+
* const schema = z.object({ name: z.string() });
|
27
|
+
* const parser = Result.parse(schema);
|
28
|
+
* const result = parser({ name: 'John' });
|
29
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* @category Utilities
|
33
|
+
*/
|
34
|
+
export declare const parse: {
|
35
|
+
<S extends StandardSchemaV1>(schema: S): (value: unknown) => Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
36
|
+
<S extends StandardSchemaV1>(schema: S, value: unknown): Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
37
|
+
};
|
package/dist/esm/exports.d.ts
CHANGED
@@ -13,6 +13,7 @@ export * from './functions/is-success';
|
|
13
13
|
export * from './functions/map';
|
14
14
|
export * from './functions/map-error';
|
15
15
|
export * from './functions/or-else';
|
16
|
+
export * from './functions/parse';
|
16
17
|
export * from './functions/pipe';
|
17
18
|
export * from './functions/succeed';
|
18
19
|
export * from './functions/try';
|
package/dist/esm/exports.js
CHANGED
@@ -13,6 +13,7 @@ export * from "./functions/is-success.js";
|
|
13
13
|
export * from "./functions/map.js";
|
14
14
|
export * from "./functions/map-error.js";
|
15
15
|
export * from "./functions/or-else.js";
|
16
|
+
export * from "./functions/parse.js";
|
16
17
|
export * from "./functions/pipe.js";
|
17
18
|
export * from "./functions/succeed.js";
|
18
19
|
export * from "./functions/try.js";
|
@@ -0,0 +1,37 @@
|
|
1
|
+
import type { Result } from '../result';
|
2
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
3
|
+
/**
|
4
|
+
* Parses a value using a {@link https://github.com/standard-schema/standard-schema Standard Schema} compatible schema.
|
5
|
+
* Returns a {@link Result} with the parsed value on success or validation errors on failure.
|
6
|
+
*
|
7
|
+
* @function
|
8
|
+
* @typeParam S - The Standard Schema V1 compatible schema type.
|
9
|
+
* @returns A {@link Result} with the parsed value or validation errors.
|
10
|
+
*
|
11
|
+
* @example Basic Usage
|
12
|
+
* ```ts
|
13
|
+
* import { Result } from '@praha/byethrow';
|
14
|
+
* import { z } from 'zod';
|
15
|
+
*
|
16
|
+
* const schema = z.object({ name: z.string() });
|
17
|
+
* const result = Result.parse(schema, { name: 'John' });
|
18
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
19
|
+
* ```
|
20
|
+
*
|
21
|
+
* @example Curried Usage
|
22
|
+
* ```ts
|
23
|
+
* import { Result } from '@praha/byethrow';
|
24
|
+
* import { z } from 'zod';
|
25
|
+
*
|
26
|
+
* const schema = z.object({ name: z.string() });
|
27
|
+
* const parser = Result.parse(schema);
|
28
|
+
* const result = parser({ name: 'John' });
|
29
|
+
* // Result.Result<{ name: string }, ReadonlyArray<StandardSchemaV1.Issue>>
|
30
|
+
* ```
|
31
|
+
*
|
32
|
+
* @category Utilities
|
33
|
+
*/
|
34
|
+
export declare const parse: {
|
35
|
+
<S extends StandardSchemaV1>(schema: S): (value: unknown) => Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
36
|
+
<S extends StandardSchemaV1>(schema: S, value: unknown): Result<StandardSchemaV1.InferOutput<S>, ReadonlyArray<StandardSchemaV1.Issue>>;
|
37
|
+
};
|
@@ -0,0 +1,12 @@
|
|
1
|
+
import { fail } from "./fail.js";
|
2
|
+
import { succeed } from "./succeed.js";
|
3
|
+
import { isPromise } from "../internals/helpers/is-promise.js";
|
4
|
+
const parse = (schema, ...args)=>{
|
5
|
+
const apply = (input)=>{
|
6
|
+
const result = schema['~standard']['validate'](input);
|
7
|
+
if (isPromise(result)) throw new TypeError('Schema validation must be synchronous');
|
8
|
+
return result.issues ? fail(result.issues) : succeed(result.value);
|
9
|
+
};
|
10
|
+
return args.length <= 0 ? apply : apply(args[0]);
|
11
|
+
};
|
12
|
+
export { parse };
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@praha/byethrow",
|
3
|
-
"version": "0.
|
3
|
+
"version": "0.5.0",
|
4
4
|
"description": "A lightweight, tree-shakable Result type package with a simple, consistent API designed",
|
5
5
|
"keywords": [
|
6
6
|
"javascript",
|
@@ -40,6 +40,9 @@
|
|
40
40
|
"LICENSE",
|
41
41
|
"README.md"
|
42
42
|
],
|
43
|
+
"dependencies": {
|
44
|
+
"@standard-schema/spec": "^1.0.0"
|
45
|
+
},
|
43
46
|
"devDependencies": {
|
44
47
|
"@rslib/core": "0.10.4",
|
45
48
|
"eslint": "9.30.1",
|