Skip to content

Commit

Permalink
rename to safeDestr
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Jun 12, 2023
1 parent c0a0c80 commit a29e137
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 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, destrSafe } = require("destr");
const { destr, safeDestr } = require("destr");

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

### Deno

```js
import { destr, destrSafe } from "https://deno.land/x/destr/src/index.ts";
import { destr, safeDestr } 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

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)
When using `safeDestr` 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"
destrSafe("[foo");
safeDestr("[foo");

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

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

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

module.exports = destr;
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export function destr<T = unknown>(value: any, options: Options = {}): T {
}
}

export function destrSafe<T = unknown>(value: any, options: Options = {}): T {
export function safeDestr<T = unknown>(value: any, options: Options = {}): T {
return destr<T>(value, { ...options, strict: true });
}

Expand Down
6 changes: 3 additions & 3 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, destrSafe } from "../src";
import { destr, safeDestr } from "../src";

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

it("throws an error if it's a invalid JSON texts with destrSafe", () => {
it("throws an error if it's a invalid JSON texts with safeDestr", () => {
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(() => destrSafe(testCase.input)).toThrowError(
expect(() => safeDestr(testCase.input)).toThrowError(
testCase.output || ""
);
}
Expand Down

0 comments on commit a29e137

Please sign in to comment.