Skip to content

Commit

Permalink
refactor!: use named destr export (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 12, 2023
1 parent 5be5732 commit 50a8b95
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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" }'));
```
Expand Down
6 changes: 6 additions & 0 deletions lib/index.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const { destr } = require("../dist/index.cjs");

// Allow mixed default and named exports
destr.destr = destr;

module.exports = destr;
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@ export type Options = {
strict?: boolean;
};

export default function destr<T = unknown>(
value: any,
options: Options = {}
): T {
export function destr<T = unknown>(value: any, options: Options = {}): T {
if (typeof value !== "string") {
return value;
}
Expand Down Expand Up @@ -80,3 +77,5 @@ export default function destr<T = unknown>(
return value as T;
}
}

export default destr;
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down

0 comments on commit 50a8b95

Please sign in to comment.