From a24af4b1a51853dff4fbcf1e6d77005d18f7bc8c Mon Sep 17 00:00:00 2001 From: "Jean M. Lescure" Date: Tue, 30 Apr 2024 13:54:01 -0600 Subject: [PATCH] fix: expression is not constructable error on nodenext --- README.md | 8 +++++- dist/short-unique-id.d.ts | 26 ++++++++++++++----- package.json | 2 +- .../require.spec.js} | 2 +- .../import.spec.ts} | 2 +- specs/esm/package.json | 3 +++ specs/esm/tsconfig.json | 6 +++++ specs/tsconfig.json | 6 ----- 8 files changed, 38 insertions(+), 17 deletions(-) rename specs/{require-cjs.spec.js => cjs/require.spec.js} (54%) rename specs/{import-esm.spec.ts => esm/import.spec.ts} (54%) create mode 100644 specs/esm/package.json create mode 100644 specs/esm/tsconfig.json delete mode 100644 specs/tsconfig.json diff --git a/README.md b/README.md index 3e629d6..0880d56 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ const isValid = uid.validate(uuid); console.log(`Is the UUID valid? ${isValid}`); -// ----------- +// --- // Validate the generated UUID against the provided dictionary const customDictionary = ['a', 'b', /* ... */]; @@ -225,6 +225,12 @@ console.log(uid.rnd()); // Sequential UUID console.log(uid.seq()); + +// --- + +// Legacy support (Node 16 or less) +// or if using "Node16" as "module" or "moduleResolution" in "tsconfig.json" +const uid = new ShortUniqueId.default(); ``` alternatively using destructuring assignment: diff --git a/dist/short-unique-id.d.ts b/dist/short-unique-id.d.ts index 79368f8..78e651e 100644 --- a/dist/short-unique-id.d.ts +++ b/dist/short-unique-id.d.ts @@ -1,13 +1,13 @@ /** * @packageDocumentation **/ -export interface ShortUniqueIdRanges { +declare interface ShortUniqueIdRanges { [k: string]: [number, number]; } -export interface ShortUniqueIdRangesMap { +declare interface ShortUniqueIdRangesMap { [k: string]: ShortUniqueIdRanges; } -export type ShortUniqueIdDefaultDictionaries = 'number' | 'alpha' | 'alpha_lower' | 'alpha_upper' | 'alphanum' | 'alphanum_lower' | 'alphanum_upper' | 'hex'; +declare type ShortUniqueIdDefaultDictionaries = 'number' | 'alpha' | 'alpha_lower' | 'alpha_upper' | 'alphanum' | 'alphanum_lower' | 'alphanum_upper' | 'hex'; /** * ```js * { @@ -20,7 +20,7 @@ export type ShortUniqueIdDefaultDictionaries = 'number' | 'alpha' | 'alpha_lower *
* @see {@link DEFAULT_OPTIONS} */ -export interface ShortUniqueIdOptions { +declare interface ShortUniqueIdOptions { /** User-defined character dictionary */ dictionary: string[] | ShortUniqueIdDefaultDictionaries; /** If true, sequentialUUID use the dictionary in the given order */ @@ -42,8 +42,8 @@ export interface ShortUniqueIdOptions { * the probability of generating a duplicate in 1,000,000 rounds * is ~0.00000002, or about 1 in 50,000,000. */ -export declare const DEFAULT_UUID_LENGTH: number; -export declare const DEFAULT_OPTIONS: ShortUniqueIdOptions; +declare const DEFAULT_UUID_LENGTH: number; +declare const DEFAULT_OPTIONS: ShortUniqueIdOptions; /** * Generate random or sequential UUID of any length. * @@ -100,7 +100,17 @@ export declare const DEFAULT_OPTIONS: ShortUniqueIdOptions; * * For more information take a look at the [ShortUniqueIdOptions type definition](/interfaces/shortuniqueidoptions.html). */ -export default class ShortUniqueId { +declare namespace ShortUniqueId { + export { + ShortUniqueIdRanges, + ShortUniqueIdRangesMap, + ShortUniqueIdDefaultDictionaries, + ShortUniqueIdOptions, + DEFAULT_UUID_LENGTH, + DEFAULT_OPTIONS + } +} +declare class ShortUniqueId { /** @hidden */ static default: typeof ShortUniqueId; counter: number; @@ -280,3 +290,5 @@ export default class ShortUniqueId { validate: (uid: string, dictionary?: string[] | ShortUniqueIdDefaultDictionaries) => boolean; constructor(argOptions?: Partial); } + +export = ShortUniqueId; diff --git a/package.json b/package.json index 9a1efcb..63e16a9 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "runkitExampleFilename": "./runkit.js", "scripts": { "test": "tsx ./src/test.ts", - "test:local": "tsx ./src/test.ts && tsx --tsconfig ./specs/tsconfig.json ./specs/import-esm.spec.ts && ./scripts/cjs-test", + "test:local": "tsx ./src/test.ts && tsx --tsconfig ./specs/esm/tsconfig.json ./specs/esm/import.spec.ts && ./scripts/cjs-test", "build": "./scripts/build", "docs": "./scripts/docs", "release": "release-it" diff --git a/specs/require-cjs.spec.js b/specs/cjs/require.spec.js similarity index 54% rename from specs/require-cjs.spec.js rename to specs/cjs/require.spec.js index 8097963..9e4d061 100644 --- a/specs/require-cjs.spec.js +++ b/specs/cjs/require.spec.js @@ -1,4 +1,4 @@ -const ShortUniqueId = require('../dist/short-unique-id'); +const ShortUniqueId = require('../../dist/short-unique-id'); const suid = new ShortUniqueId({length: 3}); diff --git a/specs/import-esm.spec.ts b/specs/esm/import.spec.ts similarity index 54% rename from specs/import-esm.spec.ts rename to specs/esm/import.spec.ts index 83c6802..ffa63e8 100644 --- a/specs/import-esm.spec.ts +++ b/specs/esm/import.spec.ts @@ -1,4 +1,4 @@ -import ShortUniqueId from '../dist/short-unique-id'; +import ShortUniqueId from '../../dist/short-unique-id.js'; const suid = new ShortUniqueId({length: 3}); diff --git a/specs/esm/package.json b/specs/esm/package.json new file mode 100644 index 0000000..4720025 --- /dev/null +++ b/specs/esm/package.json @@ -0,0 +1,3 @@ +{ + "type": "module" +} diff --git a/specs/esm/tsconfig.json b/specs/esm/tsconfig.json new file mode 100644 index 0000000..8328d27 --- /dev/null +++ b/specs/esm/tsconfig.json @@ -0,0 +1,6 @@ +{ + "compilerOptions": { + "module": "NodeNext", + "moduleResolution": "NodeNext" + } +} \ No newline at end of file diff --git a/specs/tsconfig.json b/specs/tsconfig.json deleted file mode 100644 index 8b6685b..0000000 --- a/specs/tsconfig.json +++ /dev/null @@ -1,6 +0,0 @@ -{ - "compilerOptions": { - "module": "Node16", - "moduleResolution": "node16" - } -} \ No newline at end of file