Skip to content

Commit

Permalink
Merge pull request #1 from dldc-packages/migrate-to-deno-jsr
Browse files Browse the repository at this point in the history
Migrate to JSR
  • Loading branch information
etienne-dldc authored May 4, 2024
2 parents 6a3de87 + 8818d1e commit 497ca53
Show file tree
Hide file tree
Showing 18 changed files with 244 additions and 5,889 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Publish

on:
push:
branches:
- main

jobs:
publish:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write # The OIDC ID token is used for authentication with JSR.
steps:
- uses: actions/checkout@v4
- run: npx jsr publish
1 change: 0 additions & 1 deletion .node-version

This file was deleted.

1 change: 0 additions & 1 deletion .prettierignore

This file was deleted.

6 changes: 0 additions & 6 deletions .prettierrc.json

This file was deleted.

15 changes: 0 additions & 15 deletions .release-it.json

This file was deleted.

5 changes: 3 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"eslint.useFlatConfig": true
"deno.enable": true,
"editor.defaultFormatter": "denoland.vscode-deno",
"npm.autoDetect": "off"
}
11 changes: 11 additions & 0 deletions deno.jsonc
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "@dldc/erreur",
"version": "7.1.0",
"exports": "./mod.ts",
"imports": {
"$std/": "https://deno.land/[email protected]/"
},
"tasks": {
"test:watch": "deno test --watch"
}
}
39 changes: 39 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions eslint.config.js

This file was deleted.

59 changes: 37 additions & 22 deletions src/mod.ts → mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
const ERREUR_DATA = Symbol('ERREUR_DATA');
const ERREUR_DATA = Symbol("ERREUR_DATA");

export interface TReadonlyErreurStore<Data> {
[ERREUR_DATA]: Data;
has(error: any): boolean;
get(error: any): Data | undefined;
has(error: unknown): boolean;
get(error: unknown): Data | undefined;
}

export interface TErreurStore<Data> extends TReadonlyErreurStore<Data> {
Expand All @@ -20,9 +20,12 @@ export interface TVoidErreurStore extends TReadonlyErreurStore<true> {
setAndReturn(error: unknown): Error;
}

export type TErreurStoreBase = TErreurStore<any> | TVoidErreurStore;
export type TReadableErreurStoreBase = TReadonlyErreurStore<any>;
export type TReadableErreurStoreRecord = Record<string, TReadableErreurStoreBase>;
export type TErreurStoreBase = TErreurStore<unknown> | TVoidErreurStore;
export type TReadableErreurStoreBase = TReadonlyErreurStore<unknown>;
export type TReadableErreurStoreRecord = Record<
string,
TReadableErreurStoreBase
>;

export function createErreurStore<Data>(): TErreurStore<Data> {
return createErreurStoreInternal<Data>(undefined);
Expand All @@ -32,18 +35,21 @@ export function createVoidErreurStore(): TVoidErreurStore {
return createErreurStoreInternal<true>(true) as TVoidErreurStore;
}

function createErreurStoreInternal<Data>(defaultValue: any): TErreurStore<Data> {
function createErreurStoreInternal<Data>(
// deno-lint-ignore no-explicit-any
defaultValue: any,
): TErreurStore<Data> {
const storage = new WeakMap<Error, Data>();

return {
[ERREUR_DATA]: true as any,
[ERREUR_DATA]: true as Data,
set,
setAndThrow,
setAndReturn,
has,
get,
asReadonly: {
[ERREUR_DATA]: true as any,
[ERREUR_DATA]: true as Data,
has,
get,
},
Expand All @@ -58,10 +64,10 @@ function createErreurStoreInternal<Data>(defaultValue: any): TErreurStore<Data>

function set(error: Error, data: Data = defaultValue) {
if (!(error instanceof Error)) {
throw new Error('Error should be an instance of Error');
throw new Error("Error should be an instance of Error");
}
if (storage.has(error)) {
throw new Error('Error already exists in store');
throw new Error("Error already exists in store");
}
storage.set(error, data);
}
Expand All @@ -79,8 +85,10 @@ function createErreurStoreInternal<Data>(defaultValue: any): TErreurStore<Data>
}
}

export function matchFirstErreur<Stores extends readonly TReadableErreurStoreBase[]>(
error: any,
export function matchFirstErreur<
Stores extends readonly TReadableErreurStoreBase[],
>(
error: unknown,
stores: Stores,
): Stores[number][typeof ERREUR_DATA] | undefined {
if (!(error instanceof Error)) {
Expand All @@ -94,11 +102,15 @@ export function matchFirstErreur<Stores extends readonly TReadableErreurStoreBas
return undefined;
}

export type TMatchResult<StoresRec extends TReadableErreurStoreRecord> = {
[K in keyof StoresRec]: StoresRec[K][typeof ERREUR_DATA] | undefined;
};

export function matchErreurs<StoresRec extends TReadableErreurStoreRecord>(
error: Error,
stores: StoresRec,
): { [K in keyof StoresRec]: StoresRec[K][typeof ERREUR_DATA] | undefined } {
const result: any = {};
const result: TMatchResult<StoresRec> = {} as TMatchResult<StoresRec>;
if (!(error instanceof Error)) {
for (const key in stores) {
result[key] = undefined;
Expand All @@ -115,33 +127,36 @@ export function toError(value: unknown): Error {
if (value instanceof Error) {
return value;
}
if (typeof value === 'string') {
if (typeof value === "string") {
return new Error(value);
}
if (value === null) {
return new Error('ErrorFromNull');
return new Error("ErrorFromNull");
}
if (value === undefined) {
return new Error('ErrorFromUndefined');
return new Error("ErrorFromUndefined");
}
if (typeof value === 'object' && value !== null) {
if (typeof value === "object" && value !== null) {
// object
const message = `ErrorFromObject: ${objectToMessage(value)}`;
const cause = 'cause' in value && value.cause instanceof Error ? value.cause : undefined;
const cause = "cause" in value && value.cause instanceof Error
? value.cause
: undefined;
return new Error(message, { cause });
}
const typeCapitalized = (typeof value).charAt(0).toUpperCase() + (typeof value).slice(1);
const typeCapitalized = (typeof value).charAt(0).toUpperCase() +
(typeof value).slice(1);
const message = `ErrorFrom${typeCapitalized}: ${String(value)}`;
return new Error(message);
}

function objectToMessage(obj: object): string {
if ('message' in obj && typeof obj.message === 'string') {
if ("message" in obj && typeof obj.message === "string") {
return obj.message;
}
try {
return JSON.stringify(obj);
} catch (error) {
} catch (_error) {
return `[object]`;
}
}
67 changes: 0 additions & 67 deletions package.json

This file was deleted.

Loading

0 comments on commit 497ca53

Please sign in to comment.