Skip to content

Commit

Permalink
Added jsdocs
Browse files Browse the repository at this point in the history
  • Loading branch information
eboody committed May 22, 2024
1 parent e82e13a commit a19e1e9
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 52 deletions.
2 changes: 1 addition & 1 deletion deno.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@eman/oxy",
"version": "0.1.3",
"version": "0.1.4",
"exports": "./main.ts",
"tasks": {
"dev": "deno run --watch index.ts"
Expand Down
20 changes: 20 additions & 0 deletions error_enum.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,44 @@
import type { Err } from './result.ts';

/**
* Represents an object where each key is associated with a function returning an Err type.
* @template ErrObject - The type of the error object.
*/
export type ErrorObject = {
[key: string]: (...args: any[]) => Err<{
readonly type: string;
readonly name: string;
readonly message: string;
}>;
};

/**
* Wraps an error object type, preserving the function signatures.
* @template T - The type of the error object.
*/
export type ErrorObjWrapper<T> = Record<
keyof T,
T[keyof T] extends (...args: any[]) => Err<infer R> ? (...args: any[]) => Err<R> : never
>;

/**
* Extracts the keys of an ErrorObjWrapper as a union type.
* @template T - The type of the error object wrapper.
*/
export type ErrorObjectNames<T> = keyof ErrorObjWrapper<T>;

/**
* Determines the error type for a given key in an error object.
* @template T - The key type.
* @template U - The error object type.
*/
export type ErrorType<T, U extends ErrorObject> = T extends keyof ErrorObjWrapper<U>
? ReturnType<U[T]>
: T extends undefined
? ReturnType<ErrorObjWrapper<U>[keyof ErrorObjWrapper<U>]>
: never;


// let one = { type: "one", name: "one", message: "one" } as const;
// import { err } from "./Result";
// const Blah = {
Expand Down
37 changes: 36 additions & 1 deletion json_rpc.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@

import { P } from "npm:ts-pattern@5.1.1";

/**
* Represents a successful JSON-RPC response.
* @template Data - The type of the result data.
*/
export type JsonRpcSuccess<Data> = P.infer<typeof JsonRpcSuccessPattern> & {
result: Data;
};

/**
* Represents an error JSON-RPC response.
* @template ErrorData - The type of the error data.
*/
export type JsonRpcError<ErrorData> = P.infer<typeof JsonRpcErrorPattern> & {
error: {
code: number;
Expand All @@ -12,13 +21,37 @@ export type JsonRpcError<ErrorData> = P.infer<typeof JsonRpcErrorPattern> & {
};
};

/**
* Checks if the given object is a JSON-RPC error response.
* @template T - The type of the error data.
* @param {object} obj - The object to check.
* @param {unknown} obj.jsonrpc - The JSON-RPC version.
* @param {unknown} obj.result - The result of the JSON-RPC call.
* @param {unknown} obj.id - The identifier of the JSON-RPC call.
* @param {unknown} obj.error - The error object of the JSON-RPC call.
* @returns {boolean} True if the object is a JSON-RPC error response, otherwise false.
*/
function isJsonRpcError<T>(obj: { jsonrpc?: unknown, result?: unknown, id?: unknown, error?: unknown }): obj is JsonRpcError<T> {
return obj["jsonrpc"] && obj["result"] && obj["error"] ? true : false;
}

/**
* Checks if the given object is a successful JSON-RPC response.
* @template T - The type of the result data.
* @param {object} obj - The object to check.
* @param {unknown} obj.jsonrpc - The JSON-RPC version.
* @param {unknown} obj.result - The result of the JSON-RPC call.
* @param {unknown} obj.id - The identifier of the JSON-RPC call.
* @param {unknown} obj.error - The error object of the JSON-RPC call.
* @returns {boolean} True if the object is a successful JSON-RPC response, otherwise false.
*/
function isJsonRpcSuccess<T>(obj: { jsonrpc?: unknown, result?: unknown, id?: unknown, error?: unknown }): obj is JsonRpcSuccess<T> {
return obj["jsonrpc"] && obj["id"] && obj["result"] ? true : false;
}

/**
* Pattern for matching successful JSON-RPC responses.
*/
export const JsonRpcSuccessPattern: {
readonly id: any;
readonly jsonrpc: any;
Expand All @@ -31,6 +64,9 @@ export const JsonRpcSuccessPattern: {
result: { data: P.any }
} as const;

/**
* Pattern for matching JSON-RPC error responses.
*/
export const JsonRpcErrorPattern: {
readonly id: any;
readonly jsonrpc: any;
Expand All @@ -53,4 +89,3 @@ export const JsonRpcErrorPattern: {
},
} as const;


68 changes: 45 additions & 23 deletions option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,42 @@ import { isMatching, P } from "npm:ts-pattern@5.1.1";

/**
* Represents an optional value, either `Some` containing a value or `None`.
* @template Thing - The type of the value.
*/
export type Option<Thing> = Some<Thing> | None;

/**
* Represents an optional value that contains a value.
* @template Thing - The type of the value.
*/
export type Some<Thing> = {
readonly isSome: true;
readonly data: Thing;
readonly isNone: false;
};

/**
* Represents an optional value that does not contain a value.
*/
export type None = {
readonly isSome: false;
readonly data: undefined;
readonly isNone: true;
};

/**
* Creates an instance of Some.
* @template Thing - The type of the value.
* @param {Thing} data - The value to be contained.
* @returns {Some<Thing>} An instance of Some.
*/
export const some = <Thing>(data: Thing): Some<Thing> => {
return { isSome: true, data, isNone: false } as Some<Thing>;
};
export type Some<Thing> = {
readonly isSome: true;
readonly data: Thing;
readonly isNone: false;
};

/**
* Represents a constant `Some` with any type of data.
*/
export const SOMETHING: {
readonly isSome: true;
readonly isNone: false;
Expand All @@ -32,15 +47,18 @@ export const SOMETHING: {
isNone: false,
data: P.select(),
} as const;
export const NOTHING: None = { isSome: false, isNone: true } as None;

// export const isSome = <Thing>(option: Option<Thing>): option is Some<Thing> => {
// return option.isSome;
// };
// export const isNone = <Thing>(option: Option<Thing>): option is Nothing => {
// return option.isNone;
// };
/**
* Represents a constant `None`.
*/
export const NOTHING: None = { isSome: false, isNone: true } as None;

/**
* Wraps a function call in a promise that resolves to an Option.
* @template F - The type of the function.
* @param {F} someFunction - The function to wrap.
* @returns {Promise<Option<NonNullable<Awaited<ReturnType<F>>>>>} A promise resolving to an Option.
*/
export function OptionOf<F extends (...args: any[]) => any>(
someFunction: F
): Promise<Option<NonNullable<Awaited<ReturnType<F>>>>> {
Expand All @@ -60,16 +78,30 @@ export function OptionOf<F extends (...args: any[]) => any>(
});
}

/**
* Represents the Some variant type.
* @template R - The type of the option.
*/
export type SomeVariant<R extends Option<unknown>> = R extends Some<unknown>
? R
: never;

/**
* Utility object for creating and managing Option types.
*/
export const Option = {
some,
SOMETHING,
NOTHING,
OptionOf,
};

/**
* Converts a value to an Option.
* @template T - The type of the value.
* @param {T} data - The value to convert.
* @returns {Option<T>} An Option containing the value.
*/
const optionOfThing = <T>(data: T): Option<T> => {
const thingIsNullish = data === null || data === undefined;

Expand All @@ -95,19 +127,9 @@ const optionOfThing = <T>(data: T): Option<T> => {
} else {
return some(data);
}
//
// I dont consider this kind of thing clean code despite its conciseness:
//
// const optionOfRes = (data: any) =>
// null || undefined ||
// !thing.length ||
// thing.is === "nothing" ||
// (typeof thing === "object" ? JSON.stringify(thing) === "{}" : true)
// ? Nothing
// : some(thing.is ? thing["thing"] : thing);
//
};


// This library provides a way to represent values that may or may not exist. It does this by introducing the Option type, which can be either a Some variant, representing a value that is present, or a Nothing variant, representing a value that is not present.
//
// Why this is useful?
Expand Down
Loading

0 comments on commit a19e1e9

Please sign in to comment.