nalloc 0.0.1 → 0.0.2
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/README.md +124 -38
- package/build/index.cjs +12 -68
- package/build/index.cjs.map +1 -1
- package/build/index.d.ts +1 -4
- package/build/index.js +1 -3
- package/build/index.js.map +1 -1
- package/build/iter.cjs +105 -0
- package/build/iter.cjs.map +1 -0
- package/build/iter.d.ts +61 -0
- package/build/iter.js +78 -0
- package/build/iter.js.map +1 -0
- package/build/option.cjs +19 -5
- package/build/option.cjs.map +1 -1
- package/build/option.d.ts +22 -1
- package/build/option.js +14 -6
- package/build/option.js.map +1 -1
- package/build/result.cjs +125 -54
- package/build/result.cjs.map +1 -1
- package/build/result.d.ts +83 -53
- package/build/result.js +100 -38
- package/build/result.js.map +1 -1
- package/build/safe.cjs +34 -15
- package/build/safe.cjs.map +1 -1
- package/build/safe.d.ts +4 -27
- package/build/safe.js +3 -14
- package/build/safe.js.map +1 -1
- package/build/types.cjs +38 -7
- package/build/types.cjs.map +1 -1
- package/build/types.d.ts +26 -4
- package/build/types.js +23 -7
- package/build/types.js.map +1 -1
- package/build/unsafe.cjs +14 -61
- package/build/unsafe.cjs.map +1 -1
- package/build/unsafe.d.ts +2 -27
- package/build/unsafe.js +2 -9
- package/build/unsafe.js.map +1 -1
- package/package.json +13 -16
- package/src/__tests__/index.ts +42 -0
- package/src/__tests__/iter.ts +218 -0
- package/src/__tests__/option.ts +48 -19
- package/src/__tests__/result.ts +286 -91
- package/src/__tests__/result.types.ts +3 -22
- package/src/__tests__/safe.ts +9 -15
- package/src/__tests__/unsafe.ts +11 -12
- package/src/index.ts +1 -18
- package/src/iter.ts +129 -0
- package/src/option.ts +36 -7
- package/src/result.ts +216 -113
- package/src/safe.ts +5 -42
- package/src/types.ts +52 -14
- package/src/unsafe.ts +2 -47
- package/build/devtools.cjs +0 -79
- package/build/devtools.cjs.map +0 -1
- package/build/devtools.d.ts +0 -82
- package/build/devtools.js +0 -43
- package/build/devtools.js.map +0 -1
- package/build/testing.cjs +0 -111
- package/build/testing.cjs.map +0 -1
- package/build/testing.d.ts +0 -85
- package/build/testing.js +0 -81
- package/build/testing.js.map +0 -1
- package/src/__tests__/tooling.ts +0 -86
- package/src/devtools.ts +0 -97
- package/src/testing.ts +0 -159
package/build/safe.cjs
CHANGED
|
@@ -9,6 +9,9 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
get Iter () {
|
|
13
|
+
return _itercjs;
|
|
14
|
+
},
|
|
12
15
|
get Option () {
|
|
13
16
|
return _optioncjs;
|
|
14
17
|
},
|
|
@@ -18,19 +21,47 @@ _export(exports, {
|
|
|
18
21
|
get err () {
|
|
19
22
|
return _typescjs.err;
|
|
20
23
|
},
|
|
24
|
+
get isErr () {
|
|
25
|
+
return _typescjs.isErr;
|
|
26
|
+
},
|
|
27
|
+
get isNone () {
|
|
28
|
+
return _typescjs.isNone;
|
|
29
|
+
},
|
|
30
|
+
get isOk () {
|
|
31
|
+
return _typescjs.isOk;
|
|
32
|
+
},
|
|
33
|
+
get isSome () {
|
|
34
|
+
return _typescjs.isSome;
|
|
35
|
+
},
|
|
36
|
+
get isSync () {
|
|
37
|
+
return _typescjs.isSync;
|
|
38
|
+
},
|
|
39
|
+
get isThenable () {
|
|
40
|
+
return _typescjs.isThenable;
|
|
41
|
+
},
|
|
21
42
|
get none () {
|
|
22
43
|
return _typescjs.none;
|
|
23
44
|
},
|
|
24
45
|
get ok () {
|
|
25
|
-
return ok;
|
|
46
|
+
return _typescjs.ok;
|
|
47
|
+
},
|
|
48
|
+
get safeTry () {
|
|
49
|
+
return _resultcjs.safeTry;
|
|
50
|
+
},
|
|
51
|
+
get safeTryAsync () {
|
|
52
|
+
return _resultcjs.safeTryAsync;
|
|
26
53
|
},
|
|
27
54
|
get some () {
|
|
28
|
-
return some;
|
|
55
|
+
return _typescjs.some;
|
|
56
|
+
},
|
|
57
|
+
get unwrap () {
|
|
58
|
+
return _resultcjs.unwrap;
|
|
29
59
|
}
|
|
30
60
|
});
|
|
31
61
|
const _typescjs = require("./types.cjs");
|
|
32
|
-
const _optioncjs = /*#__PURE__*/ _interop_require_wildcard(require("./option.cjs"));
|
|
33
62
|
const _resultcjs = /*#__PURE__*/ _interop_require_wildcard(require("./result.cjs"));
|
|
63
|
+
const _optioncjs = /*#__PURE__*/ _interop_require_wildcard(require("./option.cjs"));
|
|
64
|
+
const _itercjs = /*#__PURE__*/ _interop_require_wildcard(require("./iter.cjs"));
|
|
34
65
|
function _getRequireWildcardCache(nodeInterop) {
|
|
35
66
|
if (typeof WeakMap !== "function") return null;
|
|
36
67
|
var cacheBabelInterop = new WeakMap();
|
|
@@ -72,17 +103,5 @@ function _interop_require_wildcard(obj, nodeInterop) {
|
|
|
72
103
|
}
|
|
73
104
|
return newObj;
|
|
74
105
|
}
|
|
75
|
-
function some(value) {
|
|
76
|
-
if (value === null || value === undefined) {
|
|
77
|
-
throw new TypeError('some() requires a non-nullable value');
|
|
78
|
-
}
|
|
79
|
-
return (0, _typescjs.some)(value);
|
|
80
|
-
}
|
|
81
|
-
function ok(value) {
|
|
82
|
-
if ((0, _typescjs.isErr)(value)) {
|
|
83
|
-
throw new TypeError('ok() cannot wrap an Err value');
|
|
84
|
-
}
|
|
85
|
-
return (0, _typescjs.ok)(value);
|
|
86
|
-
}
|
|
87
106
|
|
|
88
107
|
//# sourceMappingURL=safe.cjs.map
|
package/build/safe.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/safe.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/safe.ts"],"sourcesContent":["export { some, none, ok, err, isOk, isErr, isSome, isNone, isThenable, isSync } from './types.js';\nexport type {\n Some,\n None,\n Ok,\n Err,\n Option as OptionType,\n OptionValue,\n IsOption,\n InferSome,\n Result as ResultType,\n ResultValue,\n ResultErrorType,\n IsResult,\n InferErr,\n MaybePromise,\n} from './types.js';\nexport { safeTry, safeTryAsync, unwrap } from './result.js';\nexport * as Option from './option.js';\nexport * as Result from './result.js';\nexport * as Iter from './iter.js';\n"],"names":["Iter","Option","Result","err","isErr","isNone","isOk","isSome","isSync","isThenable","none","ok","safeTry","safeTryAsync","some","unwrap"],"mappings":";;;;;;;;;;;QAoBYA;;;QAFAC;;;QACAC;;;QAnBaC;eAAAA,aAAG;;QAAQC;eAAAA,eAAK;;QAAUC;eAAAA,gBAAM;;QAA3BC;eAAAA,cAAI;;QAASC;eAAAA,gBAAM;;QAAsBC;eAAAA,gBAAM;;QAAlBC;eAAAA,oBAAU;;QAAtDC;eAAAA,cAAI;;QAAEC;eAAAA,YAAE;;QAiBdC;eAAAA,kBAAO;;QAAEC;eAAAA,uBAAY;;QAjBrBC;eAAAA,cAAI;;QAiBmBC;eAAAA,iBAAM;;;0BAjB+C;mEAiBvC;mEACtB;iEAEF"}
|
package/build/safe.d.ts
CHANGED
|
@@ -1,29 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export
|
|
4
|
-
export { none, err };
|
|
1
|
+
export { some, none, ok, err, isOk, isErr, isSome, isNone, isThenable, isSync } from './types.js';
|
|
2
|
+
export type { Some, None, Ok, Err, Option as OptionType, OptionValue, IsOption, InferSome, Result as ResultType, ResultValue, ResultErrorType, IsResult, InferErr, MaybePromise, } from './types.js';
|
|
3
|
+
export { safeTry, safeTryAsync, unwrap } from './result.js';
|
|
5
4
|
export * as Option from './option.js';
|
|
6
5
|
export * as Result from './result.js';
|
|
7
|
-
|
|
8
|
-
* Creates a Some value with runtime validation.
|
|
9
|
-
* Throws if the value is null or undefined.
|
|
10
|
-
* @param value - The value to wrap (must be non-nullable)
|
|
11
|
-
* @returns The value typed as Some
|
|
12
|
-
* @throws {TypeError} If value is null or undefined
|
|
13
|
-
* @example
|
|
14
|
-
* some(42) // Some(42)
|
|
15
|
-
* some(null) // throws TypeError
|
|
16
|
-
* some(undefined) // throws TypeError
|
|
17
|
-
*/
|
|
18
|
-
export declare function some<T>(value: T): Some<ValueType<T>>;
|
|
19
|
-
/**
|
|
20
|
-
* Creates an Ok value with runtime validation.
|
|
21
|
-
* Throws if the value is an Err (prevents accidental double-wrapping).
|
|
22
|
-
* @param value - The success value
|
|
23
|
-
* @returns The value typed as Ok
|
|
24
|
-
* @throws {TypeError} If value is an Err
|
|
25
|
-
* @example
|
|
26
|
-
* ok(42) // Ok(42)
|
|
27
|
-
* ok(err('fail')) // throws TypeError
|
|
28
|
-
*/
|
|
29
|
-
export declare function ok<T>(value: T): Ok<T>;
|
|
6
|
+
export * as Iter from './iter.js';
|
package/build/safe.js
CHANGED
|
@@ -1,18 +1,7 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
1
|
+
export { some, none, ok, err, isOk, isErr, isSome, isNone, isThenable, isSync } from "./types.js";
|
|
2
|
+
export { safeTry, safeTryAsync, unwrap } from "./result.js";
|
|
3
3
|
export * as Option from "./option.js";
|
|
4
4
|
export * as Result from "./result.js";
|
|
5
|
-
export
|
|
6
|
-
if (value === null || value === undefined) {
|
|
7
|
-
throw new TypeError('some() requires a non-nullable value');
|
|
8
|
-
}
|
|
9
|
-
return someUnsafe(value);
|
|
10
|
-
}
|
|
11
|
-
export function ok(value) {
|
|
12
|
-
if (isErr(value)) {
|
|
13
|
-
throw new TypeError('ok() cannot wrap an Err value');
|
|
14
|
-
}
|
|
15
|
-
return okUnsafe(value);
|
|
16
|
-
}
|
|
5
|
+
export * as Iter from "./iter.js";
|
|
17
6
|
|
|
18
7
|
//# sourceMappingURL=safe.js.map
|
package/build/safe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/safe.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/safe.ts"],"sourcesContent":["export { some, none, ok, err, isOk, isErr, isSome, isNone, isThenable, isSync } from './types.js';\nexport type {\n Some,\n None,\n Ok,\n Err,\n Option as OptionType,\n OptionValue,\n IsOption,\n InferSome,\n Result as ResultType,\n ResultValue,\n ResultErrorType,\n IsResult,\n InferErr,\n MaybePromise,\n} from './types.js';\nexport { safeTry, safeTryAsync, unwrap } from './result.js';\nexport * as Option from './option.js';\nexport * as Result from './result.js';\nexport * as Iter from './iter.js';\n"],"names":["some","none","ok","err","isOk","isErr","isSome","isNone","isThenable","isSync","safeTry","safeTryAsync","unwrap","Option","Result","Iter"],"mappings":"AAAA,SAASA,IAAI,EAAEC,IAAI,EAAEC,EAAE,EAAEC,GAAG,EAAEC,IAAI,EAAEC,KAAK,EAAEC,MAAM,EAAEC,MAAM,EAAEC,UAAU,EAAEC,MAAM,QAAQ,aAAa;AAiBlG,SAASC,OAAO,EAAEC,YAAY,EAAEC,MAAM,QAAQ,cAAc;AAC5D,OAAO,KAAKC,MAAM,MAAM,cAAc;AACtC,OAAO,KAAKC,MAAM,MAAM,cAAc;AACtC,OAAO,KAAKC,IAAI,MAAM,YAAY"}
|
package/build/types.cjs
CHANGED
|
@@ -9,6 +9,9 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
+
get EMPTY () {
|
|
13
|
+
return EMPTY;
|
|
14
|
+
},
|
|
12
15
|
get NONE () {
|
|
13
16
|
return NONE;
|
|
14
17
|
},
|
|
@@ -27,20 +30,33 @@ _export(exports, {
|
|
|
27
30
|
get isSome () {
|
|
28
31
|
return isSome;
|
|
29
32
|
},
|
|
33
|
+
get isSync () {
|
|
34
|
+
return isSync;
|
|
35
|
+
},
|
|
36
|
+
get isThenable () {
|
|
37
|
+
return isThenable;
|
|
38
|
+
},
|
|
30
39
|
get none () {
|
|
31
40
|
return none;
|
|
32
41
|
},
|
|
33
42
|
get ok () {
|
|
34
43
|
return ok;
|
|
35
44
|
},
|
|
45
|
+
get okUnchecked () {
|
|
46
|
+
return okUnchecked;
|
|
47
|
+
},
|
|
36
48
|
get optionOf () {
|
|
37
49
|
return optionOf;
|
|
38
50
|
},
|
|
39
51
|
get some () {
|
|
40
52
|
return some;
|
|
53
|
+
},
|
|
54
|
+
get someUnchecked () {
|
|
55
|
+
return someUnchecked;
|
|
41
56
|
}
|
|
42
57
|
});
|
|
43
58
|
const NONE = undefined;
|
|
59
|
+
const EMPTY = Object.freeze([]);
|
|
44
60
|
function isSome(opt) {
|
|
45
61
|
return opt !== null && opt !== undefined;
|
|
46
62
|
}
|
|
@@ -50,29 +66,44 @@ function isNone(opt) {
|
|
|
50
66
|
function optionOf(value) {
|
|
51
67
|
return isNone(value) ? NONE : value;
|
|
52
68
|
}
|
|
53
|
-
function
|
|
69
|
+
function someUnchecked(value) {
|
|
54
70
|
return value;
|
|
55
71
|
}
|
|
72
|
+
function some(value) {
|
|
73
|
+
if (value === null || value === undefined) {
|
|
74
|
+
throw new TypeError('some() requires a non-nullable value');
|
|
75
|
+
}
|
|
76
|
+
return someUnchecked(value);
|
|
77
|
+
}
|
|
56
78
|
const none = NONE;
|
|
57
79
|
const ERR_BRAND = Symbol.for('nalloc.ResultError');
|
|
58
80
|
function ResultErrorCtor(error) {
|
|
59
81
|
this.error = error;
|
|
60
82
|
}
|
|
61
83
|
ResultErrorCtor.prototype[ERR_BRAND] = true;
|
|
62
|
-
function hasErrBrand(value) {
|
|
63
|
-
return value?.[ERR_BRAND] === true;
|
|
64
|
-
}
|
|
65
84
|
function isOk(result) {
|
|
66
|
-
return !
|
|
85
|
+
return !result?.[ERR_BRAND];
|
|
67
86
|
}
|
|
68
87
|
function isErr(result) {
|
|
69
|
-
return
|
|
88
|
+
return result?.[ERR_BRAND] === true;
|
|
70
89
|
}
|
|
71
|
-
function
|
|
90
|
+
function okUnchecked(value) {
|
|
72
91
|
return value;
|
|
73
92
|
}
|
|
93
|
+
function ok(value) {
|
|
94
|
+
if (isErr(value)) {
|
|
95
|
+
throw new TypeError('ok() cannot wrap an Err value');
|
|
96
|
+
}
|
|
97
|
+
return okUnchecked(value);
|
|
98
|
+
}
|
|
74
99
|
function err(error) {
|
|
75
100
|
return new ResultErrorCtor(error);
|
|
76
101
|
}
|
|
102
|
+
function isThenable(value) {
|
|
103
|
+
return typeof value?.then === 'function';
|
|
104
|
+
}
|
|
105
|
+
function isSync(value) {
|
|
106
|
+
return typeof value?.then !== 'function';
|
|
107
|
+
}
|
|
77
108
|
|
|
78
109
|
//# sourceMappingURL=types.cjs.map
|
package/build/types.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/** Values that represent None (absence of a value). */\nexport type NoneValueType = null | undefined | void;\n\n/** Widens literal types to their base types for better type inference. */\nexport type Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T extends bigint\n ? bigint\n : T extends symbol\n ? symbol\n : T;\n\n/** Widens never to unknown, otherwise preserves the type. */\nexport type WidenNever<T> = [T] extends [never] ? unknown : T;\n\n/** Excludes None values from a type, leaving only valid Some values. */\nexport type ValueType<T> = Exclude<T, NoneValueType>;\n\nexport declare const SOME_BRAND: unique symbol;\n\n/** Represents a value that is present. The value itself is the Some - no wrapper object. */\nexport type Some<T> = ValueType<T> & { readonly [SOME_BRAND]: true };\n\n/** Represents the absence of a value (null or undefined). */\nexport type None = NoneValueType & { readonly [SOME_BRAND]: false };\n\n/** Constant representing None. Use this instead of null/undefined for clarity. */\nexport const NONE = undefined as None;\n\n/** A value that may or may not be present. Some(T) is the value itself; None is null/undefined. */\nexport type Option<T> = Some<ValueType<T>> | None;\n\n/** Extracts the value type from an Option type. */\nexport type OptionValue<TOption> = TOption extends Option<infer TValue> ? TValue : never;\n\n/** Type predicate: true if T is an Option type. */\nexport type IsOption<T> = T extends Option<any> ? true : false;\n\n/** Infers the Some type from a value, preserving the inner type. */\nexport type InferSome<T> = T extends Some<infer TValue> ? Some<ValueType<TValue>> : never;\n\n/**\n * Checks if an Option contains a value (is Some).\n * @param opt - The Option to check\n * @returns true if the Option is Some, false if None\n * @example\n * isSome(42) // true\n * isSome(null) // false\n * isSome(undefined) // false\n */\nexport function isSome<T>(opt: Some<T>): true;\nexport function isSome(opt: None): false;\nexport function isSome<T>(opt: unknown): opt is Some<T>;\nexport function isSome(opt: unknown): boolean {\n return opt !== null && opt !== undefined;\n}\n\n/**\n * Checks if an Option is None (absent).\n * @param opt - The Option to check\n * @returns true if the Option is None, false if Some\n * @example\n * isNone(null) // true\n * isNone(undefined) // true\n * isNone(42) // false\n */\nexport function isNone<T>(opt: Some<T>): false;\nexport function isNone(opt: None): true;\nexport function isNone(opt: unknown): opt is None;\nexport function isNone(opt: unknown): boolean {\n return opt === null || opt === undefined;\n}\n\n/**\n * Creates an Option from a nullable value. Returns None for null/undefined, Some otherwise.\n * @param value - The value to wrap\n * @returns Some(value) if value is non-null, None otherwise\n * @example\n * optionOf(42) // Some(42)\n * optionOf(null) // None\n * optionOf(undefined) // None\n */\nexport function optionOf(value: null): None;\nexport function optionOf(value: undefined): None;\nexport function optionOf<T>(value: T): T extends NoneValueType ? None : Some<T>;\nexport function optionOf<T>(value: T): Option<T> {\n return isNone(value as Option<T>) ? NONE : (value as Some<T>);\n}\n\n/**\n * Creates a Some value
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/** Values that represent None (absence of a value). */\nexport type NoneValueType = null | undefined | void;\n\n/** Widens literal types to their base types for better type inference. */\nexport type Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T extends bigint\n ? bigint\n : T extends symbol\n ? symbol\n : T;\n\n/** Widens never to unknown, otherwise preserves the type. */\nexport type WidenNever<T> = [T] extends [never] ? unknown : T;\n\n/** Excludes None values from a type, leaving only valid Some values. */\nexport type ValueType<T> = Exclude<T, NoneValueType>;\n\nexport declare const SOME_BRAND: unique symbol;\n\n/** Represents a value that is present. The value itself is the Some - no wrapper object. */\nexport type Some<T> = ValueType<T> & { readonly [SOME_BRAND]: true };\n\n/** Represents the absence of a value (null or undefined). */\nexport type None = NoneValueType & { readonly [SOME_BRAND]: false };\n\n/** Constant representing None. Use this instead of null/undefined for clarity. */\nexport const NONE = undefined as None;\n\n/** Shared frozen empty array to avoid allocations. */\nexport const EMPTY: readonly unknown[] = Object.freeze([]);\n\n/** A value that may or may not be present. Some(T) is the value itself; None is null/undefined. */\nexport type Option<T> = Some<ValueType<T>> | None;\n\n/** Extracts the value type from an Option type. */\nexport type OptionValue<TOption> = TOption extends Option<infer TValue> ? TValue : never;\n\n/** Type predicate: true if T is an Option type. */\nexport type IsOption<T> = T extends Option<any> ? true : false;\n\n/** Infers the Some type from a value, preserving the inner type. */\nexport type InferSome<T> = T extends Some<infer TValue> ? Some<ValueType<TValue>> : never;\n\n/**\n * Checks if an Option contains a value (is Some).\n * @param opt - The Option to check\n * @returns true if the Option is Some, false if None\n * @example\n * isSome(42) // true\n * isSome(null) // false\n * isSome(undefined) // false\n */\nexport function isSome<T>(opt: Some<T>): true;\nexport function isSome(opt: None): false;\nexport function isSome<T>(opt: unknown): opt is Some<T>;\nexport function isSome(opt: unknown): boolean {\n return opt !== null && opt !== undefined;\n}\n\n/**\n * Checks if an Option is None (absent).\n * @param opt - The Option to check\n * @returns true if the Option is None, false if Some\n * @example\n * isNone(null) // true\n * isNone(undefined) // true\n * isNone(42) // false\n */\nexport function isNone<T>(opt: Some<T>): false;\nexport function isNone(opt: None): true;\nexport function isNone(opt: unknown): opt is None;\nexport function isNone(opt: unknown): boolean {\n return opt === null || opt === undefined;\n}\n\n/**\n * Creates an Option from a nullable value. Returns None for null/undefined, Some otherwise.\n * @param value - The value to wrap\n * @returns Some(value) if value is non-null, None otherwise\n * @example\n * optionOf(42) // Some(42)\n * optionOf(null) // None\n * optionOf(undefined) // None\n */\nexport function optionOf(value: null): None;\nexport function optionOf(value: undefined): None;\nexport function optionOf<T>(value: T): T extends NoneValueType ? None : Some<T>;\nexport function optionOf<T>(value: T): Option<T> {\n return isNone(value as Option<T>) ? NONE : (value as Some<T>);\n}\n\nexport function someUnchecked<T>(value: T): Some<ValueType<T>> {\n return value as Some<ValueType<T>>;\n}\n\n/**\n * Creates a Some value with runtime validation.\n * @param value - The value to wrap\n * @returns The value typed as Some\n * @throws TypeError if value is null or undefined\n * @example\n * some(42) // Some(42)\n */\nexport function some<T>(value: ValueType<T>): Some<ValueType<T>> {\n if (value === null || value === undefined) {\n throw new TypeError('some() requires a non-nullable value');\n }\n return someUnchecked(value);\n}\n\n/** Constant representing None. Alias for NONE. */\nexport const none: None = NONE;\n\ndeclare const OK_BRAND: unique symbol;\n\n/** Represents a successful Result value. The value itself is the Ok - no wrapper object. */\nexport type Ok<T> = T & { readonly [OK_BRAND]: true };\n\nconst ERR_BRAND = Symbol.for('nalloc.ResultError');\n\ninterface ResultErrorShape<E> {\n readonly error: E;\n readonly [ERR_BRAND]: true;\n}\n\nfunction ResultErrorCtor<E>(this: ResultErrorShape<E>, error: E): void {\n (this as { error: E }).error = error;\n}\n(ResultErrorCtor.prototype as { [ERR_BRAND]: true })[ERR_BRAND] = true;\n\n/** The error wrapper type used internally. */\nexport type ResultError<E> = ResultErrorShape<E>;\n\n/** Represents a failed Result containing an error. */\nexport type Err<E> = ResultError<E>;\n\n/** A value that is either successful (Ok) or failed (Err). Ok is the value itself; Err wraps the error. */\nexport type Result<T, E> = Ok<T> | Err<E>;\n\n/** Extracts the success value type from a Result type. */\nexport type ResultValue<TResult> = TResult extends Result<infer TValue, any> ? TValue : never;\n\n/** Extracts the error type from a Result type. */\nexport type ResultErrorType<TResult> = TResult extends Result<any, infer TError> ? TError : never;\n\n/** Type predicate: true if T is a Result type. */\nexport type IsResult<T> = T extends Result<any, any> ? true : false;\n\n/** Infers the Err type from a value, preserving the error type. */\nexport type InferErr<T> = T extends Err<infer TError> ? Err<TError> : never;\n\n/**\n * Checks if a Result is Ok (successful).\n * @param result - The Result to check\n * @returns true if the Result is Ok, false if Err\n * @example\n * isOk(42) // true (Ok value)\n * isOk(err('fail')) // false\n */\nexport function isOk<T>(result: Ok<T>): true;\nexport function isOk<E>(result: Err<E>): false;\nexport function isOk<T, E>(result: Result<T, E>): result is Ok<T>;\nexport function isOk(result: unknown): boolean;\nexport function isOk(result: unknown): boolean {\n return !(result as Record<symbol, unknown>)?.[ERR_BRAND];\n}\n\n/**\n * Checks if a Result is Err (failed).\n * @param result - The Result to check\n * @returns true if the Result is Err, false if Ok\n * @example\n * isErr(err('fail')) // true\n * isErr(42) // false (Ok value)\n */\nexport function isErr<E>(result: Err<E>): true;\nexport function isErr(result: Ok<unknown>): false;\nexport function isErr<T, E>(result: Result<T, E>): result is Err<E>;\nexport function isErr(result: unknown): result is Err<unknown>;\nexport function isErr(result: unknown): boolean {\n return (result as Record<symbol, unknown>)?.[ERR_BRAND] === true;\n}\n\nexport function okUnchecked<T>(value: T): Ok<T> {\n return value as Ok<T>;\n}\n\n/**\n * Creates an Ok value with runtime validation.\n * @param value - The success value\n * @returns The value typed as Ok\n * @throws TypeError if value is an Err\n * @example\n * ok(42) // Ok(42)\n */\nexport function ok<T>(value: T): Ok<T> {\n if (isErr(value)) {\n throw new TypeError('ok() cannot wrap an Err value');\n }\n return okUnchecked(value);\n}\n\n/**\n * Creates an Err value wrapping an error.\n * @param error - The error value\n * @returns An Err containing the error\n * @example\n * err('something went wrong') // Err('something went wrong')\n * err(new Error('failed')) // Err(Error)\n */\nexport function err<E>(error: E): Err<E> {\n return new (ResultErrorCtor as unknown as new (error: E) => Err<E>)(error);\n}\n\n/** A value that may or may not be a Promise. */\nexport type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;\n\n/**\n * Checks if a value is a thenable (has a .then method).\n * @param value - The value to check\n * @returns true if value is a PromiseLike\n */\nexport function isThenable<T>(value: MaybePromise<T>): value is PromiseLike<T> {\n return typeof (value as PromiseLike<T>)?.then === 'function';\n}\n\n/**\n * Checks if a value is synchronous (not a thenable).\n * @param value - The value to check\n * @returns true if value is not a PromiseLike\n */\nexport function isSync<T>(value: MaybePromise<T>): value is T {\n return typeof (value as PromiseLike<T>)?.then !== 'function';\n}\n"],"names":["EMPTY","NONE","err","isErr","isNone","isOk","isSome","isSync","isThenable","none","ok","okUnchecked","optionOf","some","someUnchecked","undefined","Object","freeze","opt","value","TypeError","ERR_BRAND","Symbol","for","ResultErrorCtor","error","prototype","result","then"],"mappings":";;;;;;;;;;;QAkCaA;eAAAA;;QAHAC;eAAAA;;QAwLGC;eAAAA;;QA/BAC;eAAAA;;QA5GAC;eAAAA;;QA4FAC;eAAAA;;QA5GAC;eAAAA;;QAgLAC;eAAAA;;QATAC;eAAAA;;QA/GHC;eAAAA;;QAoFGC;eAAAA;;QAZAC;eAAAA;;QAhGAC;eAAAA;;QAgBAC;eAAAA;;QAZAC;eAAAA;;;AAjET,MAAMb,OAAOc;AAGb,MAAMf,QAA4BgB,OAAOC,MAAM,CAAC,EAAE;AA0BlD,SAASX,OAAOY,GAAY;IACjC,OAAOA,QAAQ,QAAQA,QAAQH;AACjC;AAcO,SAASX,OAAOc,GAAY;IACjC,OAAOA,QAAQ,QAAQA,QAAQH;AACjC;AAcO,SAASH,SAAYO,KAAQ;IAClC,OAAOf,OAAOe,SAAsBlB,OAAQkB;AAC9C;AAEO,SAASL,cAAiBK,KAAQ;IACvC,OAAOA;AACT;AAUO,SAASN,KAAQM,KAAmB;IACzC,IAAIA,UAAU,QAAQA,UAAUJ,WAAW;QACzC,MAAM,IAAIK,UAAU;IACtB;IACA,OAAON,cAAcK;AACvB;AAGO,MAAMV,OAAaR;AAO1B,MAAMoB,YAAYC,OAAOC,GAAG,CAAC;AAO7B,SAASC,gBAA8CC,KAAQ;IAC7D,AAAC,IAAI,CAAkBA,KAAK,GAAGA;AACjC;AACCD,gBAAgBE,SAAS,AAA0B,CAACL,UAAU,GAAG;AAmC3D,SAAShB,KAAKsB,MAAe;IAClC,OAAO,CAAEA,QAAoC,CAACN,UAAU;AAC1D;AAcO,SAASlB,MAAMwB,MAAe;IACnC,OAAO,AAACA,QAAoC,CAACN,UAAU,KAAK;AAC9D;AAEO,SAASV,YAAeQ,KAAQ;IACrC,OAAOA;AACT;AAUO,SAAST,GAAMS,KAAQ;IAC5B,IAAIhB,MAAMgB,QAAQ;QAChB,MAAM,IAAIC,UAAU;IACtB;IACA,OAAOT,YAAYQ;AACrB;AAUO,SAASjB,IAAOuB,KAAQ;IAC7B,OAAO,IAAKD,gBAAwDC;AACtE;AAUO,SAASjB,WAAcW,KAAsB;IAClD,OAAO,OAAQA,OAA0BS,SAAS;AACpD;AAOO,SAASrB,OAAUY,KAAsB;IAC9C,OAAO,OAAQA,OAA0BS,SAAS;AACpD"}
|
package/build/types.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ export type None = NoneValueType & {
|
|
|
17
17
|
};
|
|
18
18
|
/** Constant representing None. Use this instead of null/undefined for clarity. */
|
|
19
19
|
export declare const NONE: None;
|
|
20
|
+
/** Shared frozen empty array to avoid allocations. */
|
|
21
|
+
export declare const EMPTY: readonly unknown[];
|
|
20
22
|
/** A value that may or may not be present. Some(T) is the value itself; None is null/undefined. */
|
|
21
23
|
export type Option<T> = Some<ValueType<T>> | None;
|
|
22
24
|
/** Extracts the value type from an Option type. */
|
|
@@ -61,10 +63,12 @@ export declare function isNone(opt: unknown): opt is None;
|
|
|
61
63
|
export declare function optionOf(value: null): None;
|
|
62
64
|
export declare function optionOf(value: undefined): None;
|
|
63
65
|
export declare function optionOf<T>(value: T): T extends NoneValueType ? None : Some<T>;
|
|
66
|
+
export declare function someUnchecked<T>(value: T): Some<ValueType<T>>;
|
|
64
67
|
/**
|
|
65
|
-
* Creates a Some value
|
|
66
|
-
* @param value - The value to wrap
|
|
68
|
+
* Creates a Some value with runtime validation.
|
|
69
|
+
* @param value - The value to wrap
|
|
67
70
|
* @returns The value typed as Some
|
|
71
|
+
* @throws TypeError if value is null or undefined
|
|
68
72
|
* @example
|
|
69
73
|
* some(42) // Some(42)
|
|
70
74
|
*/
|
|
@@ -106,6 +110,7 @@ export type InferErr<T> = T extends Err<infer TError> ? Err<TError> : never;
|
|
|
106
110
|
export declare function isOk<T>(result: Ok<T>): true;
|
|
107
111
|
export declare function isOk<E>(result: Err<E>): false;
|
|
108
112
|
export declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
|
|
113
|
+
export declare function isOk(result: unknown): boolean;
|
|
109
114
|
/**
|
|
110
115
|
* Checks if a Result is Err (failed).
|
|
111
116
|
* @param result - The Result to check
|
|
@@ -115,12 +120,15 @@ export declare function isOk<T, E>(result: Result<T, E>): result is Ok<T>;
|
|
|
115
120
|
* isErr(42) // false (Ok value)
|
|
116
121
|
*/
|
|
117
122
|
export declare function isErr<E>(result: Err<E>): true;
|
|
118
|
-
export declare function isErr(result: Ok<
|
|
123
|
+
export declare function isErr(result: Ok<unknown>): false;
|
|
119
124
|
export declare function isErr<T, E>(result: Result<T, E>): result is Err<E>;
|
|
125
|
+
export declare function isErr(result: unknown): result is Err<unknown>;
|
|
126
|
+
export declare function okUnchecked<T>(value: T): Ok<T>;
|
|
120
127
|
/**
|
|
121
|
-
* Creates an Ok value
|
|
128
|
+
* Creates an Ok value with runtime validation.
|
|
122
129
|
* @param value - The success value
|
|
123
130
|
* @returns The value typed as Ok
|
|
131
|
+
* @throws TypeError if value is an Err
|
|
124
132
|
* @example
|
|
125
133
|
* ok(42) // Ok(42)
|
|
126
134
|
*/
|
|
@@ -134,4 +142,18 @@ export declare function ok<T>(value: T): Ok<T>;
|
|
|
134
142
|
* err(new Error('failed')) // Err(Error)
|
|
135
143
|
*/
|
|
136
144
|
export declare function err<E>(error: E): Err<E>;
|
|
145
|
+
/** A value that may or may not be a Promise. */
|
|
146
|
+
export type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;
|
|
147
|
+
/**
|
|
148
|
+
* Checks if a value is a thenable (has a .then method).
|
|
149
|
+
* @param value - The value to check
|
|
150
|
+
* @returns true if value is a PromiseLike
|
|
151
|
+
*/
|
|
152
|
+
export declare function isThenable<T>(value: MaybePromise<T>): value is PromiseLike<T>;
|
|
153
|
+
/**
|
|
154
|
+
* Checks if a value is synchronous (not a thenable).
|
|
155
|
+
* @param value - The value to check
|
|
156
|
+
* @returns true if value is not a PromiseLike
|
|
157
|
+
*/
|
|
158
|
+
export declare function isSync<T>(value: MaybePromise<T>): value is T;
|
|
137
159
|
export {};
|
package/build/types.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export const NONE = undefined;
|
|
2
|
+
export const EMPTY = Object.freeze([]);
|
|
2
3
|
export function isSome(opt) {
|
|
3
4
|
return opt !== null && opt !== undefined;
|
|
4
5
|
}
|
|
@@ -8,29 +9,44 @@ export function isNone(opt) {
|
|
|
8
9
|
export function optionOf(value) {
|
|
9
10
|
return isNone(value) ? NONE : value;
|
|
10
11
|
}
|
|
11
|
-
export function
|
|
12
|
+
export function someUnchecked(value) {
|
|
12
13
|
return value;
|
|
13
14
|
}
|
|
15
|
+
export function some(value) {
|
|
16
|
+
if (value === null || value === undefined) {
|
|
17
|
+
throw new TypeError('some() requires a non-nullable value');
|
|
18
|
+
}
|
|
19
|
+
return someUnchecked(value);
|
|
20
|
+
}
|
|
14
21
|
export const none = NONE;
|
|
15
22
|
const ERR_BRAND = Symbol.for('nalloc.ResultError');
|
|
16
23
|
function ResultErrorCtor(error) {
|
|
17
24
|
this.error = error;
|
|
18
25
|
}
|
|
19
26
|
ResultErrorCtor.prototype[ERR_BRAND] = true;
|
|
20
|
-
function hasErrBrand(value) {
|
|
21
|
-
return value?.[ERR_BRAND] === true;
|
|
22
|
-
}
|
|
23
27
|
export function isOk(result) {
|
|
24
|
-
return !
|
|
28
|
+
return !result?.[ERR_BRAND];
|
|
25
29
|
}
|
|
26
30
|
export function isErr(result) {
|
|
27
|
-
return
|
|
31
|
+
return result?.[ERR_BRAND] === true;
|
|
28
32
|
}
|
|
29
|
-
export function
|
|
33
|
+
export function okUnchecked(value) {
|
|
30
34
|
return value;
|
|
31
35
|
}
|
|
36
|
+
export function ok(value) {
|
|
37
|
+
if (isErr(value)) {
|
|
38
|
+
throw new TypeError('ok() cannot wrap an Err value');
|
|
39
|
+
}
|
|
40
|
+
return okUnchecked(value);
|
|
41
|
+
}
|
|
32
42
|
export function err(error) {
|
|
33
43
|
return new ResultErrorCtor(error);
|
|
34
44
|
}
|
|
45
|
+
export function isThenable(value) {
|
|
46
|
+
return typeof value?.then === 'function';
|
|
47
|
+
}
|
|
48
|
+
export function isSync(value) {
|
|
49
|
+
return typeof value?.then !== 'function';
|
|
50
|
+
}
|
|
35
51
|
|
|
36
52
|
//# sourceMappingURL=types.js.map
|
package/build/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/** Values that represent None (absence of a value). */\nexport type NoneValueType = null | undefined | void;\n\n/** Widens literal types to their base types for better type inference. */\nexport type Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T extends bigint\n ? bigint\n : T extends symbol\n ? symbol\n : T;\n\n/** Widens never to unknown, otherwise preserves the type. */\nexport type WidenNever<T> = [T] extends [never] ? unknown : T;\n\n/** Excludes None values from a type, leaving only valid Some values. */\nexport type ValueType<T> = Exclude<T, NoneValueType>;\n\nexport declare const SOME_BRAND: unique symbol;\n\n/** Represents a value that is present. The value itself is the Some - no wrapper object. */\nexport type Some<T> = ValueType<T> & { readonly [SOME_BRAND]: true };\n\n/** Represents the absence of a value (null or undefined). */\nexport type None = NoneValueType & { readonly [SOME_BRAND]: false };\n\n/** Constant representing None. Use this instead of null/undefined for clarity. */\nexport const NONE = undefined as None;\n\n/** A value that may or may not be present. Some(T) is the value itself; None is null/undefined. */\nexport type Option<T> = Some<ValueType<T>> | None;\n\n/** Extracts the value type from an Option type. */\nexport type OptionValue<TOption> = TOption extends Option<infer TValue> ? TValue : never;\n\n/** Type predicate: true if T is an Option type. */\nexport type IsOption<T> = T extends Option<any> ? true : false;\n\n/** Infers the Some type from a value, preserving the inner type. */\nexport type InferSome<T> = T extends Some<infer TValue> ? Some<ValueType<TValue>> : never;\n\n/**\n * Checks if an Option contains a value (is Some).\n * @param opt - The Option to check\n * @returns true if the Option is Some, false if None\n * @example\n * isSome(42) // true\n * isSome(null) // false\n * isSome(undefined) // false\n */\nexport function isSome<T>(opt: Some<T>): true;\nexport function isSome(opt: None): false;\nexport function isSome<T>(opt: unknown): opt is Some<T>;\nexport function isSome(opt: unknown): boolean {\n return opt !== null && opt !== undefined;\n}\n\n/**\n * Checks if an Option is None (absent).\n * @param opt - The Option to check\n * @returns true if the Option is None, false if Some\n * @example\n * isNone(null) // true\n * isNone(undefined) // true\n * isNone(42) // false\n */\nexport function isNone<T>(opt: Some<T>): false;\nexport function isNone(opt: None): true;\nexport function isNone(opt: unknown): opt is None;\nexport function isNone(opt: unknown): boolean {\n return opt === null || opt === undefined;\n}\n\n/**\n * Creates an Option from a nullable value. Returns None for null/undefined, Some otherwise.\n * @param value - The value to wrap\n * @returns Some(value) if value is non-null, None otherwise\n * @example\n * optionOf(42) // Some(42)\n * optionOf(null) // None\n * optionOf(undefined) // None\n */\nexport function optionOf(value: null): None;\nexport function optionOf(value: undefined): None;\nexport function optionOf<T>(value: T): T extends NoneValueType ? None : Some<T>;\nexport function optionOf<T>(value: T): Option<T> {\n return isNone(value as Option<T>) ? NONE : (value as Some<T>);\n}\n\n/**\n * Creates a Some value
|
|
1
|
+
{"version":3,"sources":["../src/types.ts"],"sourcesContent":["/** Values that represent None (absence of a value). */\nexport type NoneValueType = null | undefined | void;\n\n/** Widens literal types to their base types for better type inference. */\nexport type Widen<T> = T extends string\n ? string\n : T extends number\n ? number\n : T extends boolean\n ? boolean\n : T extends bigint\n ? bigint\n : T extends symbol\n ? symbol\n : T;\n\n/** Widens never to unknown, otherwise preserves the type. */\nexport type WidenNever<T> = [T] extends [never] ? unknown : T;\n\n/** Excludes None values from a type, leaving only valid Some values. */\nexport type ValueType<T> = Exclude<T, NoneValueType>;\n\nexport declare const SOME_BRAND: unique symbol;\n\n/** Represents a value that is present. The value itself is the Some - no wrapper object. */\nexport type Some<T> = ValueType<T> & { readonly [SOME_BRAND]: true };\n\n/** Represents the absence of a value (null or undefined). */\nexport type None = NoneValueType & { readonly [SOME_BRAND]: false };\n\n/** Constant representing None. Use this instead of null/undefined for clarity. */\nexport const NONE = undefined as None;\n\n/** Shared frozen empty array to avoid allocations. */\nexport const EMPTY: readonly unknown[] = Object.freeze([]);\n\n/** A value that may or may not be present. Some(T) is the value itself; None is null/undefined. */\nexport type Option<T> = Some<ValueType<T>> | None;\n\n/** Extracts the value type from an Option type. */\nexport type OptionValue<TOption> = TOption extends Option<infer TValue> ? TValue : never;\n\n/** Type predicate: true if T is an Option type. */\nexport type IsOption<T> = T extends Option<any> ? true : false;\n\n/** Infers the Some type from a value, preserving the inner type. */\nexport type InferSome<T> = T extends Some<infer TValue> ? Some<ValueType<TValue>> : never;\n\n/**\n * Checks if an Option contains a value (is Some).\n * @param opt - The Option to check\n * @returns true if the Option is Some, false if None\n * @example\n * isSome(42) // true\n * isSome(null) // false\n * isSome(undefined) // false\n */\nexport function isSome<T>(opt: Some<T>): true;\nexport function isSome(opt: None): false;\nexport function isSome<T>(opt: unknown): opt is Some<T>;\nexport function isSome(opt: unknown): boolean {\n return opt !== null && opt !== undefined;\n}\n\n/**\n * Checks if an Option is None (absent).\n * @param opt - The Option to check\n * @returns true if the Option is None, false if Some\n * @example\n * isNone(null) // true\n * isNone(undefined) // true\n * isNone(42) // false\n */\nexport function isNone<T>(opt: Some<T>): false;\nexport function isNone(opt: None): true;\nexport function isNone(opt: unknown): opt is None;\nexport function isNone(opt: unknown): boolean {\n return opt === null || opt === undefined;\n}\n\n/**\n * Creates an Option from a nullable value. Returns None for null/undefined, Some otherwise.\n * @param value - The value to wrap\n * @returns Some(value) if value is non-null, None otherwise\n * @example\n * optionOf(42) // Some(42)\n * optionOf(null) // None\n * optionOf(undefined) // None\n */\nexport function optionOf(value: null): None;\nexport function optionOf(value: undefined): None;\nexport function optionOf<T>(value: T): T extends NoneValueType ? None : Some<T>;\nexport function optionOf<T>(value: T): Option<T> {\n return isNone(value as Option<T>) ? NONE : (value as Some<T>);\n}\n\nexport function someUnchecked<T>(value: T): Some<ValueType<T>> {\n return value as Some<ValueType<T>>;\n}\n\n/**\n * Creates a Some value with runtime validation.\n * @param value - The value to wrap\n * @returns The value typed as Some\n * @throws TypeError if value is null or undefined\n * @example\n * some(42) // Some(42)\n */\nexport function some<T>(value: ValueType<T>): Some<ValueType<T>> {\n if (value === null || value === undefined) {\n throw new TypeError('some() requires a non-nullable value');\n }\n return someUnchecked(value);\n}\n\n/** Constant representing None. Alias for NONE. */\nexport const none: None = NONE;\n\ndeclare const OK_BRAND: unique symbol;\n\n/** Represents a successful Result value. The value itself is the Ok - no wrapper object. */\nexport type Ok<T> = T & { readonly [OK_BRAND]: true };\n\nconst ERR_BRAND = Symbol.for('nalloc.ResultError');\n\ninterface ResultErrorShape<E> {\n readonly error: E;\n readonly [ERR_BRAND]: true;\n}\n\nfunction ResultErrorCtor<E>(this: ResultErrorShape<E>, error: E): void {\n (this as { error: E }).error = error;\n}\n(ResultErrorCtor.prototype as { [ERR_BRAND]: true })[ERR_BRAND] = true;\n\n/** The error wrapper type used internally. */\nexport type ResultError<E> = ResultErrorShape<E>;\n\n/** Represents a failed Result containing an error. */\nexport type Err<E> = ResultError<E>;\n\n/** A value that is either successful (Ok) or failed (Err). Ok is the value itself; Err wraps the error. */\nexport type Result<T, E> = Ok<T> | Err<E>;\n\n/** Extracts the success value type from a Result type. */\nexport type ResultValue<TResult> = TResult extends Result<infer TValue, any> ? TValue : never;\n\n/** Extracts the error type from a Result type. */\nexport type ResultErrorType<TResult> = TResult extends Result<any, infer TError> ? TError : never;\n\n/** Type predicate: true if T is a Result type. */\nexport type IsResult<T> = T extends Result<any, any> ? true : false;\n\n/** Infers the Err type from a value, preserving the error type. */\nexport type InferErr<T> = T extends Err<infer TError> ? Err<TError> : never;\n\n/**\n * Checks if a Result is Ok (successful).\n * @param result - The Result to check\n * @returns true if the Result is Ok, false if Err\n * @example\n * isOk(42) // true (Ok value)\n * isOk(err('fail')) // false\n */\nexport function isOk<T>(result: Ok<T>): true;\nexport function isOk<E>(result: Err<E>): false;\nexport function isOk<T, E>(result: Result<T, E>): result is Ok<T>;\nexport function isOk(result: unknown): boolean;\nexport function isOk(result: unknown): boolean {\n return !(result as Record<symbol, unknown>)?.[ERR_BRAND];\n}\n\n/**\n * Checks if a Result is Err (failed).\n * @param result - The Result to check\n * @returns true if the Result is Err, false if Ok\n * @example\n * isErr(err('fail')) // true\n * isErr(42) // false (Ok value)\n */\nexport function isErr<E>(result: Err<E>): true;\nexport function isErr(result: Ok<unknown>): false;\nexport function isErr<T, E>(result: Result<T, E>): result is Err<E>;\nexport function isErr(result: unknown): result is Err<unknown>;\nexport function isErr(result: unknown): boolean {\n return (result as Record<symbol, unknown>)?.[ERR_BRAND] === true;\n}\n\nexport function okUnchecked<T>(value: T): Ok<T> {\n return value as Ok<T>;\n}\n\n/**\n * Creates an Ok value with runtime validation.\n * @param value - The success value\n * @returns The value typed as Ok\n * @throws TypeError if value is an Err\n * @example\n * ok(42) // Ok(42)\n */\nexport function ok<T>(value: T): Ok<T> {\n if (isErr(value)) {\n throw new TypeError('ok() cannot wrap an Err value');\n }\n return okUnchecked(value);\n}\n\n/**\n * Creates an Err value wrapping an error.\n * @param error - The error value\n * @returns An Err containing the error\n * @example\n * err('something went wrong') // Err('something went wrong')\n * err(new Error('failed')) // Err(Error)\n */\nexport function err<E>(error: E): Err<E> {\n return new (ResultErrorCtor as unknown as new (error: E) => Err<E>)(error);\n}\n\n/** A value that may or may not be a Promise. */\nexport type MaybePromise<T> = T | Promise<T> | PromiseLike<T>;\n\n/**\n * Checks if a value is a thenable (has a .then method).\n * @param value - The value to check\n * @returns true if value is a PromiseLike\n */\nexport function isThenable<T>(value: MaybePromise<T>): value is PromiseLike<T> {\n return typeof (value as PromiseLike<T>)?.then === 'function';\n}\n\n/**\n * Checks if a value is synchronous (not a thenable).\n * @param value - The value to check\n * @returns true if value is not a PromiseLike\n */\nexport function isSync<T>(value: MaybePromise<T>): value is T {\n return typeof (value as PromiseLike<T>)?.then !== 'function';\n}\n"],"names":["NONE","undefined","EMPTY","Object","freeze","isSome","opt","isNone","optionOf","value","someUnchecked","some","TypeError","none","ERR_BRAND","Symbol","for","ResultErrorCtor","error","prototype","isOk","result","isErr","okUnchecked","ok","err","isThenable","then","isSync"],"mappings":"AA+BA,OAAO,MAAMA,OAAOC,UAAkB;AAGtC,OAAO,MAAMC,QAA4BC,OAAOC,MAAM,CAAC,EAAE,EAAE;AA0B3D,OAAO,SAASC,OAAOC,GAAY;IACjC,OAAOA,QAAQ,QAAQA,QAAQL;AACjC;AAcA,OAAO,SAASM,OAAOD,GAAY;IACjC,OAAOA,QAAQ,QAAQA,QAAQL;AACjC;AAcA,OAAO,SAASO,SAAYC,KAAQ;IAClC,OAAOF,OAAOE,SAAsBT,OAAQS;AAC9C;AAEA,OAAO,SAASC,cAAiBD,KAAQ;IACvC,OAAOA;AACT;AAUA,OAAO,SAASE,KAAQF,KAAmB;IACzC,IAAIA,UAAU,QAAQA,UAAUR,WAAW;QACzC,MAAM,IAAIW,UAAU;IACtB;IACA,OAAOF,cAAcD;AACvB;AAGA,OAAO,MAAMI,OAAab,KAAK;AAO/B,MAAMc,YAAYC,OAAOC,GAAG,CAAC;AAO7B,SAASC,gBAA8CC,KAAQ;IAC7D,AAAC,IAAI,CAAkBA,KAAK,GAAGA;AACjC;AACCD,gBAAgBE,SAAS,AAA0B,CAACL,UAAU,GAAG;AAmClE,OAAO,SAASM,KAAKC,MAAe;IAClC,OAAO,CAAEA,QAAoC,CAACP,UAAU;AAC1D;AAcA,OAAO,SAASQ,MAAMD,MAAe;IACnC,OAAO,AAACA,QAAoC,CAACP,UAAU,KAAK;AAC9D;AAEA,OAAO,SAASS,YAAed,KAAQ;IACrC,OAAOA;AACT;AAUA,OAAO,SAASe,GAAMf,KAAQ;IAC5B,IAAIa,MAAMb,QAAQ;QAChB,MAAM,IAAIG,UAAU;IACtB;IACA,OAAOW,YAAYd;AACrB;AAUA,OAAO,SAASgB,IAAOP,KAAQ;IAC7B,OAAO,IAAKD,gBAAwDC;AACtE;AAUA,OAAO,SAASQ,WAAcjB,KAAsB;IAClD,OAAO,OAAQA,OAA0BkB,SAAS;AACpD;AAOA,OAAO,SAASC,OAAUnB,KAAsB;IAC9C,OAAO,OAAQA,OAA0BkB,SAAS;AACpD"}
|
package/build/unsafe.cjs
CHANGED
|
@@ -9,74 +9,27 @@ function _export(target, all) {
|
|
|
9
9
|
});
|
|
10
10
|
}
|
|
11
11
|
_export(exports, {
|
|
12
|
-
get Option () {
|
|
13
|
-
return _optioncjs;
|
|
14
|
-
},
|
|
15
|
-
get Result () {
|
|
16
|
-
return _resultcjs;
|
|
17
|
-
},
|
|
18
|
-
get err () {
|
|
19
|
-
return _typescjs.err;
|
|
20
|
-
},
|
|
21
|
-
get none () {
|
|
22
|
-
return _typescjs.none;
|
|
23
|
-
},
|
|
24
12
|
get ok () {
|
|
25
|
-
return
|
|
13
|
+
return _typescjs.okUnchecked;
|
|
26
14
|
},
|
|
27
15
|
get some () {
|
|
28
|
-
return
|
|
16
|
+
return _typescjs.someUnchecked;
|
|
29
17
|
}
|
|
30
18
|
});
|
|
19
|
+
_export_star(require("./safe.cjs"), exports);
|
|
31
20
|
const _typescjs = require("./types.cjs");
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
}
|
|
42
|
-
function _interop_require_wildcard(obj, nodeInterop) {
|
|
43
|
-
if (!nodeInterop && obj && obj.__esModule) {
|
|
44
|
-
return obj;
|
|
45
|
-
}
|
|
46
|
-
if (obj === null || typeof obj !== "object" && typeof obj !== "function") {
|
|
47
|
-
return {
|
|
48
|
-
default: obj
|
|
49
|
-
};
|
|
50
|
-
}
|
|
51
|
-
var cache = _getRequireWildcardCache(nodeInterop);
|
|
52
|
-
if (cache && cache.has(obj)) {
|
|
53
|
-
return cache.get(obj);
|
|
54
|
-
}
|
|
55
|
-
var newObj = {
|
|
56
|
-
__proto__: null
|
|
57
|
-
};
|
|
58
|
-
var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor;
|
|
59
|
-
for(var key in obj){
|
|
60
|
-
if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
61
|
-
var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null;
|
|
62
|
-
if (desc && (desc.get || desc.set)) {
|
|
63
|
-
Object.defineProperty(newObj, key, desc);
|
|
64
|
-
} else {
|
|
65
|
-
newObj[key] = obj[key];
|
|
66
|
-
}
|
|
21
|
+
function _export_star(from, to) {
|
|
22
|
+
Object.keys(from).forEach(function(k) {
|
|
23
|
+
if (k !== "default" && !Object.prototype.hasOwnProperty.call(to, k)) {
|
|
24
|
+
Object.defineProperty(to, k, {
|
|
25
|
+
enumerable: true,
|
|
26
|
+
get: function() {
|
|
27
|
+
return from[k];
|
|
28
|
+
}
|
|
29
|
+
});
|
|
67
30
|
}
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
if (cache) {
|
|
71
|
-
cache.set(obj, newObj);
|
|
72
|
-
}
|
|
73
|
-
return newObj;
|
|
74
|
-
}
|
|
75
|
-
function some(value) {
|
|
76
|
-
return value;
|
|
77
|
-
}
|
|
78
|
-
function ok(value) {
|
|
79
|
-
return value;
|
|
31
|
+
});
|
|
32
|
+
return from;
|
|
80
33
|
}
|
|
81
34
|
|
|
82
35
|
//# sourceMappingURL=unsafe.cjs.map
|
package/build/unsafe.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/unsafe.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/unsafe.ts"],"sourcesContent":["export * from './safe.js';\nexport { someUnchecked as some, okUnchecked as ok } from './types.js';\n"],"names":["ok","okUnchecked","some","someUnchecked"],"mappings":";;;;;;;;;;;QAC+CA;eAAfC,qBAAW;;QAAjBC;eAAjBC,uBAAa;;;qBADR;0BAC2C"}
|
package/build/unsafe.d.ts
CHANGED
|
@@ -1,27 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
export {
|
|
3
|
-
export type { Some, None, Ok, Err, Option as OptionType, OptionValue, IsOption, InferSome, Result as ResultType, ResultValue, ResultErrorType, IsResult, InferErr, } from './types.js';
|
|
4
|
-
export * as Option from './option.js';
|
|
5
|
-
export * as Result from './result.js';
|
|
6
|
-
/**
|
|
7
|
-
* Creates a Some value without runtime validation.
|
|
8
|
-
* Use when you are certain the value is non-nullable.
|
|
9
|
-
* For validated creation, use safe.some() instead.
|
|
10
|
-
* @param value - The value to wrap (assumed non-nullable)
|
|
11
|
-
* @returns The value typed as Some
|
|
12
|
-
* @example
|
|
13
|
-
* some(42) // Some(42)
|
|
14
|
-
* some(null) // Some(null) - no validation, may cause issues
|
|
15
|
-
*/
|
|
16
|
-
export declare function some<T>(value: T): Some<T>;
|
|
17
|
-
/**
|
|
18
|
-
* Creates an Ok value without runtime validation.
|
|
19
|
-
* Use when you are certain the value is not an Err.
|
|
20
|
-
* For validated creation, use safe.ok() instead.
|
|
21
|
-
* @param value - The success value (assumed not an Err)
|
|
22
|
-
* @returns The value typed as Ok
|
|
23
|
-
* @example
|
|
24
|
-
* ok(42) // Ok(42)
|
|
25
|
-
* ok(err('fail')) // Ok(Err) - no validation, may cause issues
|
|
26
|
-
*/
|
|
27
|
-
export declare function ok<T>(value: T): Ok<T>;
|
|
1
|
+
export * from './safe.js';
|
|
2
|
+
export { someUnchecked as some, okUnchecked as ok } from './types.js';
|
package/build/unsafe.js
CHANGED
|
@@ -1,11 +1,4 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export * as Result from "./result.js";
|
|
4
|
-
export function some(value) {
|
|
5
|
-
return value;
|
|
6
|
-
}
|
|
7
|
-
export function ok(value) {
|
|
8
|
-
return value;
|
|
9
|
-
}
|
|
1
|
+
export * from "./safe.js";
|
|
2
|
+
export { someUnchecked as some, okUnchecked as ok } from "./types.js";
|
|
10
3
|
|
|
11
4
|
//# sourceMappingURL=unsafe.js.map
|
package/build/unsafe.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/unsafe.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"sources":["../src/unsafe.ts"],"sourcesContent":["export * from './safe.js';\nexport { someUnchecked as some, okUnchecked as ok } from './types.js';\n"],"names":["someUnchecked","some","okUnchecked","ok"],"mappings":"AAAA,cAAc,YAAY;AAC1B,SAASA,iBAAiBC,IAAI,EAAEC,eAAeC,EAAE,QAAQ,aAAa"}
|