Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: use named destr export #69

Merged
merged 2 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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