From 50a8b95edbba3bba345d0e75d3b399bdb69aa801 Mon Sep 17 00:00:00 2001 From: pooya parsa Date: Mon, 12 Jun 2023 13:49:45 +0200 Subject: [PATCH] refactor!: use named `destr` export (#69) --- README.md | 13 +++++++++---- lib/index.cjs | 6 ++++++ package.json | 7 ++++--- src/index.ts | 7 +++---- test/index.test.ts | 2 +- 5 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 lib/index.cjs diff --git a/README.md b/README.md index cbad1e8..18efb6d 100644 --- a/README.md +++ b/README.md @@ -14,25 +14,30 @@ A faster, secure and convenient alternative for [`JSON.parse`](https://developer Install using npm or yarn: ```bash +# npm npm i destr -# or + +# yarn yarn add destr + +# pnpm +pnpm i destr ``` Import into your Node.js project: ```js // CommonJS -const destr = require("destr"); +const { destr } = require("destr"); // ESM -import destr from "destr"; +import { destr } from "destr"; ``` ### Deno ```js -import destr from "https://deno.land/x/destr/src/index.ts"; +import { destr } from "https://deno.land/x/destr/src/index.ts"; console.log(destr('{ "deno": "yay" }')); ``` diff --git a/lib/index.cjs b/lib/index.cjs new file mode 100644 index 0000000..40e9c78 --- /dev/null +++ b/lib/index.cjs @@ -0,0 +1,6 @@ +const { destr } = require("../dist/index.cjs"); + +// Allow mixed default and named exports +destr.destr = destr; + +module.exports = destr; diff --git a/package.json b/package.json index 37a1576..38b470f 100644 --- a/package.json +++ b/package.json @@ -9,14 +9,15 @@ ".": { "types": "./dist/index.d.ts", "import": "./dist/index.mjs", - "require": "./dist/index.cjs" + "require": "./lib/index.cjs" } }, - "main": "./dist/index.cjs", + "main": "./lib/index.cjs", "module": "./dist/index.mjs", "types": "./dist/index.d.ts", "files": [ - "dist" + "dist", + "lib" ], "scripts": { "bench": "pnpm build && node ./bench.cjs", diff --git a/src/index.ts b/src/index.ts index a688e24..bb8253b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -29,10 +29,7 @@ export type Options = { strict?: boolean; }; -export default function destr( - value: any, - options: Options = {} -): T { +export function destr(value: any, options: Options = {}): T { if (typeof value !== "string") { return value; } @@ -80,3 +77,5 @@ export default function destr( return value as T; } } + +export default destr; diff --git a/test/index.test.ts b/test/index.test.ts index 3a330b5..e9fc69e 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1,5 +1,5 @@ import { expect, it, describe, vi } from "vitest"; -import destr from "../src"; +import { destr } from "../src"; describe("destr", () => { it("returns the passed value if it's not a string", () => {