diff --git a/README.md b/README.md index 18efb6d..3b1929b 100644 --- a/README.md +++ b/README.md @@ -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" }')); ``` @@ -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 diff --git a/lib/index.cjs b/lib/index.cjs index 40e9c78..de84d27 100644 --- a/lib/index.cjs +++ b/lib/index.cjs @@ -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; diff --git a/test/index.test.ts b/test/index.test.ts index e9fc69e..621de75 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, destrSafe } from "../src"; describe("destr", () => { it("returns the passed value if it's not a string", () => { @@ -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: "[ " }, @@ -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" }, @@ -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 || "" ); }