Skip to content

Commit

Permalink
feat: destrSafe util
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 12, 2023
1 parent 4572b40 commit c0a0c80
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,16 @@ Import into your Node.js project:

```js
// CommonJS
const { destr } = require("destr");
const { destr, destrSafe } = require("destr");

// ESM
import { destr } from "destr";
import { destr, destrSafe } from "destr";
```

### Deno

```js
import { destr } from "https://deno.land/x/destr/src/index.ts";
import { destr, destrSafe } from "https://deno.land/x/destr/src/index.ts";

console.log(destr('{ "deno": "yay" }'));
```
Expand Down Expand Up @@ -98,14 +98,14 @@ destr(input);

### Strict Mode

If `{ strict: true }` passed as second argument, `destr` will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)
When using `destrSafe` it will throw an error if the input is not a valid JSON string or parsing fails. (non string values and built-ins will be still returned as-is)

```js
// Returns "[foo"
destr("[foo");
destrSafe("[foo");

// Throws an error
destr("[foo", { strict: true });
destrSafe("[foo", { strict: true });
```

## Benchmarks
Expand Down
3 changes: 2 additions & 1 deletion lib/index.cjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const { destr } = require("../dist/index.cjs");
const { destr, destrSafe } = require("../dist/index.cjs");

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

module.exports = destr;
8 changes: 4 additions & 4 deletions 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, destrSafe } from "../src";

describe("destr", () => {
it("returns the passed value if it's not a string", () => {
Expand Down Expand Up @@ -107,7 +107,7 @@ describe("destr", () => {
}
});

it("returns the passed string if it's a invalid JSON text and `strict` option is set `false`", () => {
it("returns the passed string if it's a invalid JSON text by default", () => {
const testCases = [
{ input: "{ " },
{ input: "[ " },
Expand All @@ -121,7 +121,7 @@ describe("destr", () => {
}
});

it("throws an error if it's a invalid JSON texts and `strict` option is set `true`", () => {
it("throws an error if it's a invalid JSON texts with destrSafe", () => {
const testCases = [
{ input: "{ ", output: "Unexpected end of JSON input" },
{ input: "[ ", output: "Unexpected end of JSON input" },
Expand All @@ -131,7 +131,7 @@ describe("destr", () => {
];

for (const testCase of testCases) {
expect(() => destr(testCase.input, { strict: true })).toThrowError(
expect(() => destrSafe(testCase.input)).toThrowError(
testCase.output || ""
);
}
Expand Down

0 comments on commit c0a0c80

Please sign in to comment.