Skip to content
This repository was archived by the owner on Dec 30, 2023. It is now read-only.

Commit b2341ed

Browse files
authored
fix(runtime/module): šŸ› Use a native Error instead of custom RuntimeError (#90)
1 parent c44e742 commit b2341ed

File tree

6 files changed

+32
-22
lines changed

6 files changed

+32
-22
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@terminal-nerds/snippets-runtime": minor
3+
---
4+
5+
Use native `Error` instead of custom `RuntimeError`

ā€Žpackages/array/source/schema/empty/empty.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ARRAY_SCHEMA, validateArray } from "../schema.ts";
1+
import { ARRAY_SCHEMA, validateArray } from "../native/native.ts";
22

33
export const EMPTY_ARRAY_SCHEMA = ARRAY_SCHEMA.length(0);
44

ā€Žpackages/array/source/schema/schema.test.ts renamed to ā€Žpackages/array/source/schema/native/native.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { returns, throws } from "@terminal-nerds/snippets-test/unit";
33
import { describe, expect, it } from "vitest";
44
import { ZodError } from "zod";
55

6-
import { validateArray } from "./schema.ts";
6+
import { validateArray } from "./native.ts";
77

88
describe("validateArray(value)", () => {
99
it(throws(ZodError).on(`non-array values`).samples(SAMPLE_PRIMITIVES), () => {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from "zod";
2+
3+
export const ARRAY_SCHEMA = z.array(z.any());
4+
5+
export type AnyArray = ReadonlyArray<unknown>;
6+
7+
export function validateArray(value: unknown): asserts value is AnyArray {
8+
ARRAY_SCHEMA.parse(value);
9+
}
10+
11+
export function validateArrays(...arrays: AnyArray[]): void {
12+
for (const currentArray of arrays) {
13+
validateArray(currentArray);
14+
}
15+
}
Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,5 @@
1-
import { z } from "zod";
2-
3-
export const ARRAY_SCHEMA = z.array(z.any());
4-
5-
export type AnyArray = ReadonlyArray<unknown>;
6-
7-
export function validateArray(value: unknown): asserts value is AnyArray {
8-
ARRAY_SCHEMA.parse(value);
9-
}
10-
11-
export function validateArrays(...arrays: AnyArray[]): void {
12-
for (const currentArray of arrays) {
13-
validateArray(currentArray);
14-
}
15-
}
16-
171
export * from "./bigint/bigint.ts";
182
export * from "./empty/empty.ts";
193
export * from "./float/float.ts";
204
export * from "./integer/integer.ts";
5+
export * from "./native/native.ts";
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1-
import { RuntimeError } from "@terminal-nerds/snippets-error/custom";
1+
/* import { RuntimeError } from "@terminal-nerds/snippets-error/custom"; */
22
import { resolveModule } from "local-pkg";
33

44
import { IN_BROWSER } from "../environment/environment.ts";
55

6-
/** @see {@link https://nodejs.org/api/modules.html#modules-commonjs-modules} CommonJS Module */
6+
/**
7+
* @see {@link https://nodejs.org/api/modules.html#modules-commonjs-modules} CommonJS Module
8+
*/
79
export const IN_CJS = typeof globalThis.require === "function";
810

9-
/** @see {@link https://nodejs.org/api/esm.html#modules-ecmascript-modules} ECMAScript Module */
11+
/**
12+
* @see {@link https://nodejs.org/api/esm.html#modules-ecmascript-modules} ECMAScript Module
13+
*/
1014
export const IN_ESM = !IN_CJS && Boolean(import.meta);
1115

1216
export function hasModule(name: string): boolean {
13-
if (IN_BROWSER) throw new RuntimeError(`You cannot check for module existence in the browser.`);
17+
/* if (IN_BROWSER) throw new RuntimeError(`You cannot check for module existence in the browser.`); */
18+
if (IN_BROWSER) throw new Error(`You cannot check for module existence in the browser.`);
1419

1520
return Boolean(resolveModule(name));
1621
}

0 commit comments

Comments
Ā (0)