@peerigon/typescript-toolkit 3.0.0 โ 4.0.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/result/result.d.ts +5 -4
- package/dist/result/result.d.ts.map +1 -1
- package/dist/result/result.js +11 -8
- package/dist/result/result.js.map +1 -1
- package/dist/unwrap/unwrap.d.ts +2 -2
- package/dist/unwrap/unwrap.js.map +1 -1
- package/package.json +18 -12
- package/dist/main.d.ts +0 -6
- package/dist/main.d.ts.map +0 -1
- package/dist/main.js +0 -6
- package/dist/main.js.map +0 -1
package/dist/result/result.d.ts
CHANGED
|
@@ -94,19 +94,20 @@ declare const pending: <const Data = undefined>({ data, createdAt, }?: Pick<Part
|
|
|
94
94
|
/**
|
|
95
95
|
* Creates a result in the success state.
|
|
96
96
|
*
|
|
97
|
-
* @param
|
|
97
|
+
* @param data - The data to store in the result
|
|
98
98
|
* @param options.createdAt - The date when the result was created
|
|
99
99
|
* @returns The successful result
|
|
100
100
|
*/
|
|
101
|
-
declare const success: <const Data>(
|
|
101
|
+
declare const success: <const Data>(data: Data, { createdAt }?: Partial<ResultMetadata>) => Result.Success<Data>;
|
|
102
102
|
/**
|
|
103
103
|
* Creates a result in the error state.
|
|
104
104
|
*
|
|
105
|
-
* @param
|
|
105
|
+
* @param givenError - The error to store in the result
|
|
106
106
|
* @param options.data - Potentially stale data from a previous result
|
|
107
|
+
* @param options.createdAt - The date when the result was created
|
|
107
108
|
* @returns The failed result
|
|
108
109
|
*/
|
|
109
|
-
declare const error: <const GivenError extends Error, const Data = never>(
|
|
110
|
+
declare const error: <const GivenError extends Error, const Data = never>(givenError: GivenError, { data, createdAt, }?: Pick<Partial<Result.Error<GivenError, Data>>, "data"> & Partial<ResultMetadata>) => Result.Error<GivenError, Data>;
|
|
110
111
|
/**
|
|
111
112
|
* Calls the function and returns it as result.
|
|
112
113
|
* If the function throws an Error, an error result is returned.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/result/result.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"result.d.ts","sourceRoot":"","sources":["../../src/result/result.ts"],"names":[],"mappings":"AAYA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,MAAM,CAChB,IAAI,GAAG,OAAO,EACd,UAAU,SAAS,YAAY,GAAG,YAAY,IAE5C,MAAM,CAAC,OAAO,CAAC,IAAI,GAAG,SAAS,CAAC,GAChC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,GACpB,MAAM,CAAC,KAAK,CAAC,UAAU,EAAE,IAAI,GAAG,SAAS,CAAC,CAAC;AAE/C,eAAO,MAAM,MAAM;;;;;;CAMT,CAAC;AAEX,yBAAiB,MAAM,CAAC;IACtB,UAAiB,MAAM,CAAC;QACtB,KAAY,OAAO,GAAG,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;QACnD,KAAY,OAAO,GAAG,OAAO,MAAM,CAAC,MAAM,CAAC,OAAO,CAAC;QACnD,KAAY,KAAK,GAAG,OAAO,MAAM,CAAC,MAAM,CAAC,KAAK,CAAC;KAChD;IAED,KAAY,MAAM,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,GAAG,MAAM,CAAC,KAAK,CAAC;IAEpE;;;OAGG;IACH,KAAY,OAAO,CAAC,IAAI,GAAG,SAAS,IAAI,QAAQ,CAAC;QAC/C,MAAM,EAAE,MAAM,CAAC,OAAO,CAAC;QACvB,gCAAgC;QAChC,SAAS,EAAE,KAAK,CAAC;QACjB,oCAAoC;QACpC,OAAO,EAAE,KAAK,CAAC;QACf,yCAAyC;QACzC,SAAS,EAAE,IAAI,CAAC;QAChB,oDAAoD;QACpD,IAAI,EAAE,IAAI,CAAC;QACX,KAAK,EAAE,IAAI,CAAC;KACb,CAAC,CAAC;IAEH;;OAEG;IACH,KAAY,OAAO,CAAC,IAAI,GAAG,OAAO,IAAI,QAAQ,CAAC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AAC,CACC,KAAK,EAAE,OAAO,CAAC,WAAW,EAAE;QAAE,MAAM,EAAE,MAAM,CAAC,MAAM,CAAC,KAAK,CAAA;KAAE,CAAC,CAAC,OAAO,CAAC,KAClE,UAAU,CAAC,GAChB,UAAU,CAAC;IACf,IAAI,EAAE,CAAC,CAAC,MAAM,EAAE,WAAW,KAAK,UAAU,CAAC,GAAG,UAAU,CAAC;CAC1D,CAAC;AAEF,KAAK,aAAa,CAAC,WAAW,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,IAAI;IAClE,IAAI,EAAE,CAAC,UAAU,EACf,QAAQ,EAAE,YAAY,CAAC,WAAW,EAAE,UAAU,CAAC,KAC5C,UAAU,CAAC;CACjB,CAAC;AAEF,KAAK,QAAQ,GAAG;IACd,CAAC,WAAW,SAAS,MAAM,GAAG,IAAI,GAAG,SAAS,EAC5C,WAAW,EAAE,WAAW,GACvB,aAAa,CAAC,WAAW,CAAC,CAAC;IAC9B,IAAI,EAAE,OAAO,IAAI,CAAC;IAClB,SAAS,EAAE,OAAO,SAAS,CAAC;IAC5B,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,OAAO,EAAE,OAAO,OAAO,CAAC;IACxB,KAAK,EAAE,OAAO,KAAK,CAAC;IACpB,QAAQ,EAAE,OAAO,WAAW,CAAC;CAC9B,CAAC;AAEF,eAAO,MAAM,MAAM,EAAE,QAkEpB,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAE3C,KAAK,YAAY,GAAG,KAAK,CAAC;AAE1B,KAAK,cAAc,GAAG;IACpB,2CAA2C;IAC3C,SAAS,EAAE,IAAI,CAAC;CACjB,CAAC"}
|
package/dist/result/result.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
// We're using manual prototype inheritance here, so `this` outside of a class is expected.
|
|
2
|
+
/* eslint-disable unicorn/no-this-outside-of-class */
|
|
1
3
|
import { createPrototype } from "../lib/create-prototype.js";
|
|
2
4
|
import { stringify } from "../lib/string.js";
|
|
3
5
|
import { symbolOfResult } from "./result.lib.js";
|
|
@@ -46,11 +48,11 @@ const pendingPrototype = createPrototype({
|
|
|
46
48
|
/**
|
|
47
49
|
* Creates a result in the success state.
|
|
48
50
|
*
|
|
49
|
-
* @param
|
|
51
|
+
* @param data - The data to store in the result
|
|
50
52
|
* @param options.createdAt - The date when the result was created
|
|
51
53
|
* @returns The successful result
|
|
52
54
|
*/
|
|
53
|
-
const success = (
|
|
55
|
+
const success = (data, { createdAt = new Date() } = {}) => {
|
|
54
56
|
const successResult = Object.create(successPrototype, {
|
|
55
57
|
data: { value: data, enumerable: true },
|
|
56
58
|
});
|
|
@@ -72,11 +74,12 @@ const successPrototype = createPrototype({
|
|
|
72
74
|
/**
|
|
73
75
|
* Creates a result in the error state.
|
|
74
76
|
*
|
|
75
|
-
* @param
|
|
77
|
+
* @param givenError - The error to store in the result
|
|
76
78
|
* @param options.data - Potentially stale data from a previous result
|
|
79
|
+
* @param options.createdAt - The date when the result was created
|
|
77
80
|
* @returns The failed result
|
|
78
81
|
*/
|
|
79
|
-
const error = (
|
|
82
|
+
const error = (givenError, { data, createdAt = new Date(), } = {}) => {
|
|
80
83
|
const errorResult = Object.create(errorPrototype, {
|
|
81
84
|
data: { value: data, enumerable: true },
|
|
82
85
|
error: { value: givenError, enumerable: true },
|
|
@@ -106,11 +109,11 @@ const errorPrototype = createPrototype({
|
|
|
106
109
|
*/
|
|
107
110
|
const from = (fn) => {
|
|
108
111
|
try {
|
|
109
|
-
return success(
|
|
112
|
+
return success(fn());
|
|
110
113
|
}
|
|
111
114
|
catch (caughtError) {
|
|
112
115
|
if (isError(caughtError)) {
|
|
113
|
-
return error(
|
|
116
|
+
return error(caughtError);
|
|
114
117
|
}
|
|
115
118
|
throw caughtError;
|
|
116
119
|
}
|
|
@@ -125,11 +128,11 @@ const from = (fn) => {
|
|
|
125
128
|
*/
|
|
126
129
|
const fromAsync = async (fn) => {
|
|
127
130
|
try {
|
|
128
|
-
return success(
|
|
131
|
+
return success(await fn());
|
|
129
132
|
}
|
|
130
133
|
catch (caughtError) {
|
|
131
134
|
if (isError(caughtError)) {
|
|
132
|
-
return error(
|
|
135
|
+
return error(caughtError);
|
|
133
136
|
}
|
|
134
137
|
throw caughtError;
|
|
135
138
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result/result.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAiCjD,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,MAAM,EAAE;QACN,OAAO,EAAE,SAAS;QAClB,OAAO,EAAE,SAAS;QAClB,KAAK,EAAE,OAAO;KACf;CACO,CAAC;AAyEX,MAAM,WAAW,GAAG,CAAO,MAAoB,EAAE,QAAwB,EAAE,EAAE;IAC3E,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE;QAC5C,KAAK,EAAE,QAAQ;QACf,UAAU,EAAE,KAAK;KAClB,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAO,MAAoB,EAAkB,EAAE;IACjE,OAAQ,MAAgD,CAAC,cAAc,CAAE,CAAC;AAC5E,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,OAAO,GAAG,CAAyB,EACvC,IAAI,EACJ,SAAS,GAAG,IAAI,IAAI,EAAE,MAEI,EAAE,EAAwB,EAAE;IACtD,MAAM,aAAa,GAAyB,MAAM,CAAC,MAAM,CAAC,gBAAgB,EAAE;QAC1E,IAAI,EAAE,EAAE,KAAK,EAAE,IAAI,EAAE,UAAU,EAAE,IAAI,EAAE;KACxC,CAAC,CAAC;IAEH,WAAW,CAAC,aAAa,EAAE,EAAE,SAAS,EAAE,CAAC,CAAC;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AC,CAAE,QAAQ,CAAC,OAAsB,CAAC;YACvC,CAAC;YACD,IACE,WAAW,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO;gBAC7C,SAAS,IAAI,QAAQ,EACrB,CAAC;gBACD,OAAO,OAAO,QAAQ,CAAC,OAAO,KAAK,UAAU;oBAC3C,CAAC,CACG,QAAQ,CAAC,OAIV,CAAC,WAAW,CAAC,IAAI,CAAC;oBACrB,CAAC,CAAE,QAAQ,CAAC,OAAsB,CAAC;YACvC,CAAC;YACD,IACE,WAAW,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,KAAK;gBAC3C,OAAO,IAAI,QAAQ,EACnB,CAAC;gBACD,OAAO,OAAO,QAAQ,CAAC,KAAK,KAAK,UAAU;oBACzC,CAAC,CACG,QAAQ,CAAC,KAIV,CAAC,WAAW,CAAC,KAAK,CAAC;oBACtB,CAAC,CAAE,QAAQ,CAAC,KAAoB,CAAC;YACrC,CAAC;YACD,OAAO,OAAO,QAAQ,CAAC,IAAI,KAAK,UAAU;gBACxC,CAAC,CACG,QAAQ,CAAC,IAIV,CAAC,WAAW,CAAC;gBAChB,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC,EACD;IACE,IAAI;IACJ,SAAS;IACT,OAAO;IACP,OAAO;IACP,KAAK;IACL,QAAQ,EAAE,WAAW;CACtB,CACF,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC","sourcesContent":["import { createPrototype } from \"../lib/create-prototype.ts\";\nimport { stringify } from \"../lib/string.ts\";\nimport { symbolOfResult } from \"./result.lib.ts\";\n\n// Namespaces are only used to group related types together.\n/* eslint-disable @typescript-eslint/no-namespace */\n\n// This module uses prototypes to create objects for the Result.Pending, Result.Success and Result.Error types.\n// This way unimportant properties won't show up in the debugger and we keep the memory footprint low.\n\n/**\n * Represents some async data that could be in the following states:\n *\n * - initial (not implemented, see below)\n * - pending\n * - success\n * - error\n *\n * This type is inspired by https://rametta.org/posts/elm-remote-data/ and has been\n * designed to be aligned with tanstack query's useQuery() result.\n * For instance, it can be used by a React component that wants to handle asynchronous\n * data but does not want to call useQuery directly.\n *\n * The initial state has not been implemented because it's not compatible\n * with tanstack query. If the initial state is needed,\n * it can be represented as Result | undefined\n */\nexport type Result<\n Data = unknown,\n GivenError extends GenericError = GenericError,\n> =\n | Result.Pending<Data | undefined>\n | Result.Success<Data>\n | Result.Error<GivenError, Data | undefined>;\n\nexport const Result = {\n Status: {\n Pending: \"pending\",\n Success: \"success\",\n Error: \"error\",\n },\n} as const;\n\nexport namespace Result {\n export namespace Status {\n export type Pending = typeof Result.Status.Pending;\n export type Success = typeof Result.Status.Success;\n export type Error = typeof Result.Status.Error;\n }\n\n export type Status = Status.Pending | Status.Success | Status.Error;\n\n /**\n * Represents a pending result where data is being loaded.\n * Pending might have stale data, but there's new data inflight.\n */\n export type Pending<Data = undefined> = Readonly<{\n status: Status.Pending;\n /** Is true when there's data */\n isSuccess: false;\n /** Is true when there's an error */\n isError: false;\n /** Is true when there's no result yet */\n isPending: true;\n /** Potentially stale data from a previous result */\n data: Data;\n error: null;\n }>;\n\n /**\n * Represents a successful result that holds some data.\n */\n export type Success<Data = unknown> = Readonly<{\n status: Status.Success;\n /** Is true when there's data */\n isSuccess: true;\n /** Is true when there's an error */\n isError: false;\n /** Is true when there's no result yet */\n isPending: false;\n data: Data;\n error: null;\n }>;\n\n /**\n * Represents a failed result that holds an error.\n * Might also contain stale data from a previous result.\n */\n export type Error<\n GivenError extends GenericError = GenericError,\n Data = undefined,\n > = Readonly<{\n status: Status.Error;\n /** Is true when there's data */\n isSuccess: false;\n /** Is true when there's an error */\n isError: true;\n /** Is true when there's no result yet */\n isPending: false;\n /** Potentially stale data from a previous result */\n data: Data;\n /** The error that occurred */\n error: GivenError;\n }>;\n\n /**\n * Represents a synchronous result that can be either success or error (no pending state).\n */\n export type Sync<\n Data = unknown,\n GivenError extends GenericError = GenericError,\n > = Success<Data> | Error<GivenError, Data>;\n}\n\nconst setMetadata = <Data>(result: Result<Data>, metadata: ResultMetadata) => {\n Object.defineProperty(result, symbolOfResult, {\n value: metadata,\n enumerable: false,\n });\n};\n\nconst getMetadata = <Data>(result: Result<Data>): ResultMetadata => {\n return (result as { [symbolOfResult]?: ResultMetadata })[symbolOfResult]!;\n};\n\n/**\n * Creates a result in the pending state.\n *\n * @param options.data - Potentially stale data from a previous result\n * @param options.createdAt - The date when the result was created\n * @returns The pending result\n */\nconst pending = <const Data = undefined>({\n data,\n createdAt = new Date(),\n}: Pick<Partial<Result.Pending<Data>>, \"data\"> &\n Partial<ResultMetadata> = {}): Result.Pending<Data> => {\n const pendingResult: Result.Pending<Data> = Object.create(pendingPrototype, {\n data: { value: data, enumerable: true },\n });\n\n setMetadata(pendingResult, { createdAt });\n\n return pendingResult;\n};\n\nconst pendingPrototype: Result.Pending = createPrototype(\n {\n status: Result.Status.Pending,\n isSuccess: false,\n isError: false,\n isPending: true,\n data: undefined,\n error: null,\n } as const,\n {\n toString(this: Result.Pending<unknown>) {\n return `Result.Pending(${\n this.data === undefined ? \"\" : stringify(this.data)\n })`;\n },\n },\n);\n\n/**\n * Creates a result in the success state.\n *\n * @param options.data - The data to store in the result\n * @param options.createdAt - The date when the result was created\n * @returns The successful result\n */\nconst success = <const Data>({\n data,\n createdAt = new Date(),\n}: Pick<Result.Success<Data>, \"data\"> &\n Partial<ResultMetadata>): Result.Success<Data> => {\n const successResult: Result.Success<Data> = Object.create(successPrototype, {\n data: { value: data, enumerable: true },\n });\n\n setMetadata(successResult, { createdAt });\n\n return successResult;\n};\n\nconst successPrototype: Result.Success<undefined> = createPrototype(\n {\n status: Result.Status.Success,\n isSuccess: true,\n isError: false,\n isPending: false,\n data: undefined,\n error: null,\n } as const,\n {\n toString(this: Result.Success) {\n return `Result.Success(${stringify(this.data)})`;\n },\n },\n);\n\n/**\n * Creates a result in the error state.\n *\n * @param options.error - The error to store in the result\n * @param options.data - Potentially stale data from a previous result\n * @returns The failed result\n */\nconst error = <const GivenError extends Error, const Data = never>({\n error: givenError,\n data,\n createdAt = new Date(),\n}: Pick<Result.Error<GivenError, Data>, \"error\"> &\n Pick<Partial<Result.Error<GivenError, Data>>, \"data\"> &\n Partial<ResultMetadata>): Result.Error<GivenError, Data> => {\n const errorResult: Result.Error<GivenError, Data> = Object.create(\n errorPrototype,\n {\n data: { value: data, enumerable: true },\n error: { value: givenError, enumerable: true },\n },\n );\n\n setMetadata(errorResult, { createdAt });\n\n return errorResult;\n};\n\nconst errorPrototype: Result.Error = createPrototype(\n {\n status: Result.Status.Error,\n isSuccess: false,\n isError: true,\n isPending: false,\n data: undefined,\n error: new Error(\"Default error\"),\n } as const,\n {\n toString(this: Result.Error) {\n return `Result.Error(${stringify(this.error.message)})`;\n },\n },\n);\n\n/**\n * Calls the function and returns it as result.\n * If the function throws an Error, an error result is returned.\n * If the function throws anything else, it is rethrown.\n *\n * @param fn - The function to call\n * @returns The result of the function\n */\nconst from = <Data>(fn: () => Data): Result.Sync<Data> => {\n try {\n return success({ data: fn() });\n } catch (caughtError) {\n if (isError(caughtError)) {\n return error<GenericError>({ error: caughtError });\n }\n\n throw caughtError;\n }\n};\n\n/**\n * Calls and awaits the async function and returns it as result.\n * If the function rejects the promise, an error result is returned.\n * If the function rejects with anything else, the rejection is rethrown.\n *\n * @param fn - The async function to call and await\n * @returns The result of the async function\n */\nconst fromAsync = async <Data>(\n fn: () => Promise<Data>,\n): Promise<Result.Sync<Data>> => {\n try {\n return success({ data: await fn() });\n } catch (caughtError) {\n if (isError(caughtError)) {\n return error<GenericError>({ error: caughtError });\n }\n\n throw caughtError;\n }\n};\n\n/**\n * Checks if the given value is an Error. Doesn't use instanceof so that\n * DOMException and errors from a different realm can be checked as well.\n */\nconst isError = (error: unknown): error is GenericError => {\n if (\"isError\" in Error && typeof Error.isError === \"function\") {\n const result: unknown = Error.isError(error);\n\n if (typeof result === \"boolean\") {\n return result;\n }\n }\n\n return (\n typeof error === \"object\" &&\n error !== null &&\n \"name\" in error &&\n typeof error.name === \"string\" &&\n \"message\" in error &&\n typeof error.message === \"string\" &&\n \"stack\" in error &&\n typeof error.stack === \"string\"\n );\n};\n\ntype CaseHandlers<\n GivenResult extends Result | null | undefined,\n ReturnType = unknown,\n> = {\n pending?:\n | ((\n maybeData: Extract<\n GivenResult,\n { status: Result.Status.Pending }\n >[\"data\"],\n ) => ReturnType)\n | ReturnType;\n success?:\n | ((\n data: Extract<GivenResult, { status: Result.Status.Success }>[\"data\"],\n ) => ReturnType)\n | ReturnType;\n error?:\n | ((\n error: Extract<GivenResult, { status: Result.Status.Error }>[\"error\"],\n ) => ReturnType)\n | ReturnType;\n else: ((result: GivenResult) => ReturnType) | ReturnType;\n};\n\ntype ResultWrapper<GivenResult extends Result | null | undefined> = {\n case: <ReturnType>(\n handlers: CaseHandlers<GivenResult, ReturnType>,\n ) => ReturnType;\n};\n\ntype ResultFn = {\n <GivenResult extends Result | null | undefined>(\n givenResult: GivenResult,\n ): ResultWrapper<GivenResult>;\n from: typeof from;\n fromAsync: typeof fromAsync;\n pending: typeof pending;\n success: typeof success;\n error: typeof error;\n metadata: typeof getMetadata;\n};\n\nexport const result: ResultFn = Object.assign(\n <GivenResult extends Result | null | undefined>(\n givenResult: GivenResult,\n ): ResultWrapper<GivenResult> => {\n return {\n case: <ReturnType>(\n handlers: CaseHandlers<GivenResult, ReturnType>,\n ): ReturnType => {\n if (\n givenResult?.status === Result.Status.Pending &&\n \"pending\" in handlers\n ) {\n return typeof handlers.pending === \"function\"\n ? (\n handlers.pending as Extract<\n typeof handlers.pending,\n (maybeData: unknown) => ReturnType\n >\n )(givenResult.data)\n : (handlers.pending as ReturnType);\n }\n if (\n givenResult?.status === Result.Status.Success &&\n \"success\" in handlers\n ) {\n return typeof handlers.success === \"function\"\n ? (\n handlers.success as Extract<\n typeof handlers.success,\n (data: unknown) => ReturnType\n >\n )(givenResult.data)\n : (handlers.success as ReturnType);\n }\n if (\n givenResult?.status === Result.Status.Error &&\n \"error\" in handlers\n ) {\n return typeof handlers.error === \"function\"\n ? (\n handlers.error as Extract<\n typeof handlers.error,\n (error: unknown) => ReturnType\n >\n )(givenResult.error)\n : (handlers.error as ReturnType);\n }\n return typeof handlers.else === \"function\"\n ? (\n handlers.else as Extract<\n typeof handlers.else,\n (result: unknown) => ReturnType\n >\n )(givenResult)\n : handlers.else;\n },\n };\n },\n {\n from,\n fromAsync,\n pending,\n success,\n error,\n metadata: getMetadata,\n },\n);\n\nexport { isResult } from \"./result.lib.ts\";\n\ntype GenericError = Error;\n\ntype ResultMetadata = {\n /** The date when the result was created */\n createdAt: Date;\n};\n"]}
|
|
1
|
+
{"version":3,"file":"result.js","sourceRoot":"","sources":["../../src/result/result.ts"],"names":[],"mappings":"AAAA,2FAA2F;AAC3F,qDAAqD;AACrD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AAiCjD,MAAM,CAAC,MAAM,MAAM,GAAG;IACpB,MAAM,EAAE;QACN,OAAO,EAAE,SAAS;QAClB,OAAO,EAAE,SAAS;QAClB,KAAK,EAAE,OAAO;KACf;CACO,CAAC;AAyEX,MAAM,WAAW,GAAG,CAAO,MAAoB,EAAE,QAAwB,EAAE,EAAE;IAC3E,MAAM,CAAC,cAAc,CAAC,MAAM,EAAE,cAAc,EAAE;QAC5C,KAAK,EAAE,QAAQ;QACf,UAAU,EAAE,KAAK;KAClB,CAAC,CAAC;AACL,CAAC,CAAC;AAEF,MAAM,WAAW,GAAG,CAAO,MAAoB,EAAkB,EAAE;IACjE,OAAQ,MAAgD,CAAC,cAAc,CAAE,CAAC;AAC5E,CAAC,CAAC;AAEF;;;;;;GAMG;AACH,MAAM,OAAO,GAAG,CAAyB,EACvC,IAAI,EACJ,SAAS,GAAG,IAAI,IAAI,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QAAQ,EACrB,CAAC;gBACD,OAAO,OAAO,QAAQ,CAAC,OAAO,KAAK,UAAU;oBAC3C,CAAC,CACG,QAAQ,CAAC,OAIV,CAAC,WAAW,CAAC,IAAI,CAAC;oBACrB,CAAC,CAAE,QAAQ,CAAC,OAAsB,CAAC;YACvC,CAAC;YACD,IACE,WAAW,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,OAAO;gBAC7C,SAAS,IAAI,QAAQ,EACrB,CAAC;gBACD,OAAO,OAAO,QAAQ,CAAC,OAAO,KAAK,UAAU;oBAC3C,CAAC,CACG,QAAQ,CAAC,OAIV,CAAC,WAAW,CAAC,IAAI,CAAC;oBACrB,CAAC,CAAE,QAAQ,CAAC,OAAsB,CAAC;YACvC,CAAC;YACD,IACE,WAAW,EAAE,MAAM,KAAK,MAAM,CAAC,MAAM,CAAC,KAAK;gBAC3C,OAAO,IAAI,QAAQ,EACnB,CAAC;gBACD,OAAO,OAAO,QAAQ,CAAC,KAAK,KAAK,UAAU;oBACzC,CAAC,CACG,QAAQ,CAAC,KAIV,CAAC,WAAW,CAAC,KAAK,CAAC;oBACtB,CAAC,CAAE,QAAQ,CAAC,KAAoB,CAAC;YACrC,CAAC;YACD,OAAO,OAAO,QAAQ,CAAC,IAAI,KAAK,UAAU;gBACxC,CAAC,CACG,QAAQ,CAAC,IAIV,CAAC,WAAW,CAAC;gBAChB,CAAC,CAAC,QAAQ,CAAC,IAAI,CAAC;QACpB,CAAC;KACF,CAAC;AACJ,CAAC,EACD;IACE,IAAI;IACJ,SAAS;IACT,OAAO;IACP,OAAO;IACP,KAAK;IACL,QAAQ,EAAE,WAAW;CACtB,CACF,CAAC;AAEF,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC","sourcesContent":["// We're using manual prototype inheritance here, so `this` outside of a class is expected.\n/* eslint-disable unicorn/no-this-outside-of-class */\nimport { createPrototype } from \"../lib/create-prototype.ts\";\nimport { stringify } from \"../lib/string.ts\";\nimport { symbolOfResult } from \"./result.lib.ts\";\n\n// Namespaces are only used to group related types together.\n/* eslint-disable @typescript-eslint/no-namespace */\n\n// This module uses prototypes to create objects for the Result.Pending, Result.Success and Result.Error types.\n// This way unimportant properties won't show up in the debugger and we keep the memory footprint low.\n\n/**\n * Represents some async data that could be in the following states:\n *\n * - initial (not implemented, see below)\n * - pending\n * - success\n * - error\n *\n * This type is inspired by https://rametta.org/posts/elm-remote-data/ and has been\n * designed to be aligned with tanstack query's useQuery() result.\n * For instance, it can be used by a React component that wants to handle asynchronous\n * data but does not want to call useQuery directly.\n *\n * The initial state has not been implemented because it's not compatible\n * with tanstack query. If the initial state is needed,\n * it can be represented as Result | undefined\n */\nexport type Result<\n Data = unknown,\n GivenError extends GenericError = GenericError,\n> =\n | Result.Pending<Data | undefined>\n | Result.Success<Data>\n | Result.Error<GivenError, Data | undefined>;\n\nexport const Result = {\n Status: {\n Pending: \"pending\",\n Success: \"success\",\n Error: \"error\",\n },\n} as const;\n\nexport namespace Result {\n export namespace Status {\n export type Pending = typeof Result.Status.Pending;\n export type Success = typeof Result.Status.Success;\n export type Error = typeof Result.Status.Error;\n }\n\n export type Status = Status.Pending | Status.Success | Status.Error;\n\n /**\n * Represents a pending result where data is being loaded.\n * Pending might have stale data, but there's new data inflight.\n */\n export type Pending<Data = undefined> = Readonly<{\n status: Status.Pending;\n /** Is true when there's data */\n isSuccess: false;\n /** Is true when there's an error */\n isError: false;\n /** Is true when there's no result yet */\n isPending: true;\n /** Potentially stale data from a previous result */\n data: Data;\n error: null;\n }>;\n\n /**\n * Represents a successful result that holds some data.\n */\n export type Success<Data = unknown> = Readonly<{\n status: Status.Success;\n /** Is true when there's data */\n isSuccess: true;\n /** Is true when there's an error */\n isError: false;\n /** Is true when there's no result yet */\n isPending: false;\n data: Data;\n error: null;\n }>;\n\n /**\n * Represents a failed result that holds an error.\n * Might also contain stale data from a previous result.\n */\n export type Error<\n GivenError extends GenericError = GenericError,\n Data = undefined,\n > = Readonly<{\n status: Status.Error;\n /** Is true when there's data */\n isSuccess: false;\n /** Is true when there's an error */\n isError: true;\n /** Is true when there's no result yet */\n isPending: false;\n /** Potentially stale data from a previous result */\n data: Data;\n /** The error that occurred */\n error: GivenError;\n }>;\n\n /**\n * Represents a synchronous result that can be either success or error (no pending state).\n */\n export type Sync<\n Data = unknown,\n GivenError extends GenericError = GenericError,\n > = Success<Data> | Error<GivenError, Data>;\n}\n\nconst setMetadata = <Data>(result: Result<Data>, metadata: ResultMetadata) => {\n Object.defineProperty(result, symbolOfResult, {\n value: metadata,\n enumerable: false,\n });\n};\n\nconst getMetadata = <Data>(result: Result<Data>): ResultMetadata => {\n return (result as { [symbolOfResult]?: ResultMetadata })[symbolOfResult]!;\n};\n\n/**\n * Creates a result in the pending state.\n *\n * @param options.data - Potentially stale data from a previous result\n * @param options.createdAt - The date when the result was created\n * @returns The pending result\n */\nconst pending = <const Data = undefined>({\n data,\n createdAt = new Date(),\n}: Pick<Partial<Result.Pending<Data>>, \"data\"> &\n Partial<ResultMetadata> = {}): Result.Pending<Data> => {\n const pendingResult: Result.Pending<Data> = Object.create(pendingPrototype, {\n data: { value: data, enumerable: true },\n });\n\n setMetadata(pendingResult, { createdAt });\n\n return pendingResult;\n};\n\nconst pendingPrototype: Result.Pending = createPrototype(\n {\n status: Result.Status.Pending,\n isSuccess: false,\n isError: false,\n isPending: true,\n data: undefined,\n error: null,\n } as const,\n {\n toString(this: Result.Pending<unknown>) {\n return `Result.Pending(${\n this.data === undefined ? \"\" : stringify(this.data)\n })`;\n },\n },\n);\n\n/**\n * Creates a result in the success state.\n *\n * @param data - The data to store in the result\n * @param options.createdAt - The date when the result was created\n * @returns The successful result\n */\nconst success = <const Data>(\n data: Data,\n { createdAt = new Date() }: Partial<ResultMetadata> = {},\n): Result.Success<Data> => {\n const successResult: Result.Success<Data> = Object.create(successPrototype, {\n data: { value: data, enumerable: true },\n });\n\n setMetadata(successResult, { createdAt });\n\n return successResult;\n};\n\nconst successPrototype: Result.Success<undefined> = createPrototype(\n {\n status: Result.Status.Success,\n isSuccess: true,\n isError: false,\n isPending: false,\n data: undefined,\n error: null,\n } as const,\n {\n toString(this: Result.Success) {\n return `Result.Success(${stringify(this.data)})`;\n },\n },\n);\n\n/**\n * Creates a result in the error state.\n *\n * @param givenError - The error to store in the result\n * @param options.data - Potentially stale data from a previous result\n * @param options.createdAt - The date when the result was created\n * @returns The failed result\n */\nconst error = <const GivenError extends Error, const Data = never>(\n givenError: GivenError,\n {\n data,\n createdAt = new Date(),\n }: Pick<Partial<Result.Error<GivenError, Data>>, \"data\"> &\n Partial<ResultMetadata> = {},\n): Result.Error<GivenError, Data> => {\n const errorResult: Result.Error<GivenError, Data> = Object.create(\n errorPrototype,\n {\n data: { value: data, enumerable: true },\n error: { value: givenError, enumerable: true },\n },\n );\n\n setMetadata(errorResult, { createdAt });\n\n return errorResult;\n};\n\nconst errorPrototype: Result.Error = createPrototype(\n {\n status: Result.Status.Error,\n isSuccess: false,\n isError: true,\n isPending: false,\n data: undefined,\n error: new Error(\"Default error\"),\n } as const,\n {\n toString(this: Result.Error) {\n return `Result.Error(${stringify(this.error.message)})`;\n },\n },\n);\n\n/**\n * Calls the function and returns it as result.\n * If the function throws an Error, an error result is returned.\n * If the function throws anything else, it is rethrown.\n *\n * @param fn - The function to call\n * @returns The result of the function\n */\nconst from = <Data>(fn: () => Data): Result.Sync<Data> => {\n try {\n return success(fn());\n } catch (caughtError) {\n if (isError(caughtError)) {\n return error<GenericError>(caughtError);\n }\n\n throw caughtError;\n }\n};\n\n/**\n * Calls and awaits the async function and returns it as result.\n * If the function rejects the promise, an error result is returned.\n * If the function rejects with anything else, the rejection is rethrown.\n *\n * @param fn - The async function to call and await\n * @returns The result of the async function\n */\nconst fromAsync = async <Data>(\n fn: () => Promise<Data>,\n): Promise<Result.Sync<Data>> => {\n try {\n return success(await fn());\n } catch (caughtError) {\n if (isError(caughtError)) {\n return error<GenericError>(caughtError);\n }\n\n throw caughtError;\n }\n};\n\n/**\n * Checks if the given value is an Error. Doesn't use instanceof so that\n * DOMException and errors from a different realm can be checked as well.\n */\nconst isError = (error: unknown): error is GenericError => {\n if (\"isError\" in Error && typeof Error.isError === \"function\") {\n const result: unknown = Error.isError(error);\n\n if (typeof result === \"boolean\") {\n return result;\n }\n }\n\n return (\n typeof error === \"object\" &&\n error !== null &&\n \"name\" in error &&\n typeof error.name === \"string\" &&\n \"message\" in error &&\n typeof error.message === \"string\" &&\n \"stack\" in error &&\n typeof error.stack === \"string\"\n );\n};\n\ntype CaseHandlers<\n GivenResult extends Result | null | undefined,\n ReturnType = unknown,\n> = {\n pending?:\n | ((\n maybeData: Extract<\n GivenResult,\n { status: Result.Status.Pending }\n >[\"data\"],\n ) => ReturnType)\n | ReturnType;\n success?:\n | ((\n data: Extract<GivenResult, { status: Result.Status.Success }>[\"data\"],\n ) => ReturnType)\n | ReturnType;\n error?:\n | ((\n error: Extract<GivenResult, { status: Result.Status.Error }>[\"error\"],\n ) => ReturnType)\n | ReturnType;\n else: ((result: GivenResult) => ReturnType) | ReturnType;\n};\n\ntype ResultWrapper<GivenResult extends Result | null | undefined> = {\n case: <ReturnType>(\n handlers: CaseHandlers<GivenResult, ReturnType>,\n ) => ReturnType;\n};\n\ntype ResultFn = {\n <GivenResult extends Result | null | undefined>(\n givenResult: GivenResult,\n ): ResultWrapper<GivenResult>;\n from: typeof from;\n fromAsync: typeof fromAsync;\n pending: typeof pending;\n success: typeof success;\n error: typeof error;\n metadata: typeof getMetadata;\n};\n\nexport const result: ResultFn = Object.assign(\n <GivenResult extends Result | null | undefined>(\n givenResult: GivenResult,\n ): ResultWrapper<GivenResult> => {\n return {\n case: <ReturnType>(\n handlers: CaseHandlers<GivenResult, ReturnType>,\n ): ReturnType => {\n if (\n givenResult?.status === Result.Status.Pending &&\n \"pending\" in handlers\n ) {\n return typeof handlers.pending === \"function\"\n ? (\n handlers.pending as Extract<\n typeof handlers.pending,\n (maybeData: unknown) => ReturnType\n >\n )(givenResult.data)\n : (handlers.pending as ReturnType);\n }\n if (\n givenResult?.status === Result.Status.Success &&\n \"success\" in handlers\n ) {\n return typeof handlers.success === \"function\"\n ? (\n handlers.success as Extract<\n typeof handlers.success,\n (data: unknown) => ReturnType\n >\n )(givenResult.data)\n : (handlers.success as ReturnType);\n }\n if (\n givenResult?.status === Result.Status.Error &&\n \"error\" in handlers\n ) {\n return typeof handlers.error === \"function\"\n ? (\n handlers.error as Extract<\n typeof handlers.error,\n (error: unknown) => ReturnType\n >\n )(givenResult.error)\n : (handlers.error as ReturnType);\n }\n return typeof handlers.else === \"function\"\n ? (\n handlers.else as Extract<\n typeof handlers.else,\n (result: unknown) => ReturnType\n >\n )(givenResult)\n : handlers.else;\n },\n };\n },\n {\n from,\n fromAsync,\n pending,\n success,\n error,\n metadata: getMetadata,\n },\n);\n\nexport { isResult } from \"./result.lib.ts\";\n\ntype GenericError = Error;\n\ntype ResultMetadata = {\n /** The date when the result was created */\n createdAt: Date;\n};\n"]}
|
package/dist/unwrap/unwrap.d.ts
CHANGED
|
@@ -17,10 +17,10 @@ import { type Result } from "../result/result.ts";
|
|
|
17
17
|
* const nullValue = unwrap(null, "default"); // "default"
|
|
18
18
|
*
|
|
19
19
|
* // With Result types
|
|
20
|
-
* const success = result.success(
|
|
20
|
+
* const success = result.success("success");
|
|
21
21
|
* const data = unwrap(success); // "success"
|
|
22
22
|
*
|
|
23
|
-
* const error = result.error(
|
|
23
|
+
* const error = result.error(new Error("failed"));
|
|
24
24
|
* const fallback = unwrap(error, "default"); // "default"
|
|
25
25
|
*
|
|
26
26
|
* // With Promise.allSettled results
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"unwrap.js","sourceRoot":"","sources":["../../src/unwrap/unwrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAe,MAAM,qBAAqB,CAAC;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AAM,CAAC;IAC1B,CAAC;IAED,OAAO,UAAU,CAAC;AACpB,CAAC;AAED,MAAM,sBAAsB,GAAG,CAC7B,KAAc,EAC0B,EAAE,CAC1C,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,QAAQ,IAAI,KAAK;IACjB,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,IAAI,KAAK,CAAC;QACjD,CAAC,KAAK,CAAC,MAAM,KAAK,UAAU,IAAI,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC;AAExD,MAAM,WAAW,GAAG,iBAAiB,CAAC;AACtC,MAAM,yBAAyB,GAAG,CAAC,UAAmB,EAAE,EAAE,CACxD,GAAG,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,4CAA4C,CAAC","sourcesContent":["import { stringify } from \"../lib/string.ts\";\nimport { isResult } from \"../result/result.lib.ts\";\nimport { type Result } from \"../result/result.ts\";\n\n/**\n * Unwrap a value from Result or nullable types, returning the underlying value or throwing an error.\n *\n * This function safely extracts values from wrapped types like Result,\n * handling null/undefined values and providing fallback mechanisms.\n *\n * @param maybeValue - The value to unwrap (can be a plain value or Result)\n * @param fallback - Optional fallback value to return instead of throwing\n * @returns The unwrapped value or fallback\n * @throws {TypeError} When the value cannot be unwrapped and no fallback is provided\n *\n * @example\n * ```ts\n * // Basic unwrapping\n * const value = unwrap(\"hello\"); // \"hello\"\n * const nullValue = unwrap(null, \"default\"); // \"default\"\n *\n * // With Result types\n * const success = result.success(
|
|
1
|
+
{"version":3,"file":"unwrap.js","sourceRoot":"","sources":["../../src/unwrap/unwrap.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAC;AACnD,OAAO,EAAe,MAAM,qBAAqB,CAAC;AAkDlD,+DAA+D;AAC/D,MAAM,UAAU,MAAM,CACpB,UAA2E,EAC3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AED,MAAM,sBAAsB,GAAG,CAC7B,KAAc,EAC0B,EAAE,CAC1C,OAAO,KAAK,KAAK,QAAQ;IACzB,KAAK,KAAK,IAAI;IACd,QAAQ,IAAI,KAAK;IACjB,CAAC,CAAC,KAAK,CAAC,MAAM,KAAK,WAAW,IAAI,OAAO,IAAI,KAAK,CAAC;QACjD,CAAC,KAAK,CAAC,MAAM,KAAK,UAAU,IAAI,QAAQ,IAAI,KAAK,CAAC,CAAC,CAAC;AAExD,MAAM,WAAW,GAAG,iBAAiB,CAAC;AACtC,MAAM,yBAAyB,GAAG,CAAC,UAAmB,EAAE,EAAE,CACxD,GAAG,WAAW,GAAG,MAAM,CAAC,UAAU,CAAC,4CAA4C,CAAC","sourcesContent":["import { stringify } from \"../lib/string.ts\";\nimport { isResult } from \"../result/result.lib.ts\";\nimport { type Result } from \"../result/result.ts\";\n\n/**\n * Unwrap a value from Result or nullable types, returning the underlying value or throwing an error.\n *\n * This function safely extracts values from wrapped types like Result,\n * handling null/undefined values and providing fallback mechanisms.\n *\n * @param maybeValue - The value to unwrap (can be a plain value or Result)\n * @param fallback - Optional fallback value to return instead of throwing\n * @returns The unwrapped value or fallback\n * @throws {TypeError} When the value cannot be unwrapped and no fallback is provided\n *\n * @example\n * ```ts\n * // Basic unwrapping\n * const value = unwrap(\"hello\"); // \"hello\"\n * const nullValue = unwrap(null, \"default\"); // \"default\"\n *\n * // With Result types\n * const success = result.success(\"success\");\n * const data = unwrap(success); // \"success\"\n *\n * const error = result.error(new Error(\"failed\"));\n * const fallback = unwrap(error, \"default\"); // \"default\"\n *\n * // With Promise.allSettled results\n * const settled = { status: \"fulfilled\", value: \"done\" } as const;\n * unwrap(settled); // \"done\"\n * ```\n */\nexport function unwrap<Value, GivenError extends Error>(\n maybeValue: Value | Result<Value, GivenError> | PromiseFulfilledResult<Value>,\n): Value;\nexport function unwrap<Value>(\n maybeValue: Result.Success<Value> | Result.Pending<Value>,\n fallback: unknown,\n): Value;\nexport function unwrap<Value, GivenError extends Error, const Fallback>(\n maybeValue: Result.Error<GivenError, Value>,\n fallback: Fallback,\n): Fallback;\nexport function unwrap<const Fallback>(\n maybeValue: PromiseRejectedResult,\n fallback: Fallback,\n): Fallback;\nexport function unwrap<Value, GivenError extends Error, const Fallback>(\n maybeValue: Value | Result<Value, GivenError> | PromiseSettledResult<Value>,\n fallback: Fallback,\n): NonNullable<Value> | Fallback;\n// eslint-disable-next-line prefer-arrow/prefer-arrow-functions\nexport function unwrap<Value, GivenError extends Error, Fallback>(\n maybeValue: Value | Result<Value, GivenError> | PromiseSettledResult<Value>,\n fallback?: Value,\n): Value | Fallback {\n const hasFallback = arguments.length > 1;\n\n if (maybeValue === null || maybeValue === undefined) {\n if (hasFallback) return fallback!;\n throw new TypeError(`${errorPrefix}Value is ${stringify(maybeValue)}`);\n }\n\n if (isResult(maybeValue)) {\n // Handle pending state\n if (\"isPending\" in maybeValue && maybeValue.isPending) {\n if (maybeValue.data === undefined) {\n if (hasFallback) return fallback!;\n throw new TypeError(typeErrorMessageForResult(maybeValue), {\n cause: maybeValue,\n });\n }\n return maybeValue.data;\n }\n\n // Handle error state\n if (maybeValue.isError) {\n if (hasFallback) return fallback!;\n throw new TypeError(typeErrorMessageForResult(maybeValue), {\n cause: maybeValue,\n });\n }\n\n // Handle success state\n return maybeValue.data;\n }\n\n if (isPromiseSettledResult(maybeValue)) {\n if (maybeValue.status === \"fulfilled\") {\n return maybeValue.value;\n }\n\n if (hasFallback) return fallback!;\n throw maybeValue.reason;\n }\n\n return maybeValue;\n}\n\nconst isPromiseSettledResult = (\n value: unknown,\n): value is PromiseSettledResult<unknown> =>\n typeof value === \"object\" &&\n value !== null &&\n \"status\" in value &&\n ((value.status === \"fulfilled\" && \"value\" in value) ||\n (value.status === \"rejected\" && \"reason\" in value));\n\nconst errorPrefix = \"Cannot unwrap: \";\nconst typeErrorMessageForResult = (maybeValue: unknown) =>\n `${errorPrefix}${String(maybeValue)} is not a success and there is no fallback`;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerigon/typescript-toolkit",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "4.0.0",
|
|
4
4
|
"description": "๐งโจ Tiny helpers for TypeScript applications",
|
|
5
5
|
"keywords": [],
|
|
6
6
|
"homepage": "https://github.com/peerigon/typescript-toolkit#readme",
|
|
@@ -16,7 +16,6 @@
|
|
|
16
16
|
"sideEffects": false,
|
|
17
17
|
"type": "module",
|
|
18
18
|
"exports": {
|
|
19
|
-
".": "./dist/main.js",
|
|
20
19
|
"./assert": "./dist/assert/assert.js",
|
|
21
20
|
"./dedupe": "./dist/dedupe/dedupe.js",
|
|
22
21
|
"./emitter": "./dist/emitter/emitter.js",
|
|
@@ -27,7 +26,6 @@
|
|
|
27
26
|
"./signals": "./dist/signals/signals.js",
|
|
28
27
|
"./unwrap": "./dist/unwrap/unwrap.js"
|
|
29
28
|
},
|
|
30
|
-
"main": "./dist/main.js",
|
|
31
29
|
"files": [
|
|
32
30
|
"dist",
|
|
33
31
|
"README.md"
|
|
@@ -42,6 +40,7 @@
|
|
|
42
40
|
"test:jsr": "jsr publish --dry-run --allow-dirty",
|
|
43
41
|
"test:build": "run-s build test:build:*",
|
|
44
42
|
"test:build:size": "size-limit",
|
|
43
|
+
"test:build:publishable": "verify-publishable",
|
|
45
44
|
"vitest": "vitest",
|
|
46
45
|
"build": "run-s build:*",
|
|
47
46
|
"build:clear": "rimraf dist",
|
|
@@ -54,26 +53,33 @@
|
|
|
54
53
|
},
|
|
55
54
|
"devDependencies": {
|
|
56
55
|
"@djankies/vitest-mcp": "0.5.1",
|
|
57
|
-
"@eslint/mcp": "0.3.
|
|
58
|
-
"@peerigon/configs": "^
|
|
56
|
+
"@eslint/mcp": "0.3.7",
|
|
57
|
+
"@peerigon/configs": "^18.1.2",
|
|
59
58
|
"@secretlint/secretlint-rule-preset-recommend": "^13.0.2",
|
|
60
59
|
"@size-limit/preset-small-lib": "^12.1.0",
|
|
61
60
|
"@tanstack/query-core": "^5.100.11",
|
|
62
|
-
"@types/node": "^
|
|
63
|
-
"@vitest/coverage-v8": "^4.1.
|
|
61
|
+
"@types/node": "^26.0.1",
|
|
62
|
+
"@vitest/coverage-v8": "^4.1.9",
|
|
64
63
|
"eslint": "^9.39.2",
|
|
65
64
|
"husky": "^9.1.7",
|
|
66
65
|
"jsr": "^0.14.3",
|
|
67
|
-
"lint-staged": "^17.0.
|
|
68
|
-
"npm-run-all2": "^
|
|
66
|
+
"lint-staged": "^17.0.8",
|
|
67
|
+
"npm-run-all2": "^9.0.2",
|
|
69
68
|
"pin-github-action": "^3.4.0",
|
|
70
|
-
"prettier": "^3.
|
|
69
|
+
"prettier": "^3.9.0",
|
|
71
70
|
"rimraf": "^6.0.1",
|
|
72
71
|
"secretlint": "^13.0.2",
|
|
73
72
|
"semantic-release": "^25.0.3",
|
|
74
73
|
"ts-lsp-mcp": "0.1.3",
|
|
75
|
-
"typescript": "^
|
|
76
|
-
"vitest": "^4.1.
|
|
74
|
+
"typescript": "^6.0.3",
|
|
75
|
+
"vitest": "^4.1.9"
|
|
76
|
+
},
|
|
77
|
+
"devEngines": {
|
|
78
|
+
"runtime": {
|
|
79
|
+
"name": "node",
|
|
80
|
+
"version": "24.18.0",
|
|
81
|
+
"onFail": "error"
|
|
82
|
+
}
|
|
77
83
|
},
|
|
78
84
|
"publishConfig": {
|
|
79
85
|
"access": "public",
|
package/dist/main.d.ts
DELETED
package/dist/main.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"main.d.ts","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC"}
|
package/dist/main.js
DELETED
package/dist/main.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"main.js","sourceRoot":"","sources":["../src/main.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,kBAAkB,CAAC;AACjC,cAAc,gBAAgB,CAAC","sourcesContent":["export * from \"./assert/assert.ts\";\nexport * from \"./dedupe/dedupe.ts\";\nexport * from \"./enums/enums.ts\";\nexport * from \"./match/match.ts\";\nexport * from \"./need/need.ts\";\n"]}
|