parse-json-object
Advanced tools
| import { type JSONValue } from "types-json"; | ||
| import type { ZodSchema } from "zod"; | ||
| export type ValidateFunction<T extends JSONValue> = (value: JSONValue) => value is T; | ||
| export declare function parse<T extends JSONValue>(text: string | undefined, validator: ValidateFunction<T> | ZodSchema<T>): T | undefined; | ||
| export declare function parseJSONValue(text?: string): JSONValue | undefined; | ||
| export declare function parseJSONObject(text?: string): Record<string, JSONValue> | undefined; | ||
| export declare function parseJSONArray(text?: string): JSONValue[] | undefined; | ||
| export declare function parseString(text?: string): string | undefined; | ||
| export type { JSONArray, JSONObject, JSONValue } from "types-json"; | ||
| //# sourceMappingURL=index.d.ts.map |
| {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAoD,KAAK,SAAS,EAAE,MAAM,YAAY,CAAC;AAC9F,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,KAAK,CAAC;AAGrC,MAAM,MAAM,gBAAgB,CAAC,CAAC,SAAS,SAAS,IAAI,CAAC,KAAK,EAAE,SAAS,KAAK,KAAK,IAAI,CAAC,CAAC;AAErF,wBAAgB,KAAK,CAAC,CAAC,SAAS,SAAS,EACvC,IAAI,EAAE,MAAM,GAAG,SAAS,EACxB,SAAS,EAAE,gBAAgB,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,GAC5C,CAAC,GAAG,SAAS,CAef;AAED,wBAAgB,cAAc,CAAC,IAAI,CAAC,EAAE,MAAM,yBAE3C;AAED,wBAAgB,eAAe,CAAC,IAAI,CAAC,EAAE,MAAM,yCAE5C;AAED,wBAAgB,cAAc,CAAC,IAAI,CAAC,EAAE,MAAM,2BAE3C;AAED,wBAAgB,WAAW,CAAC,IAAI,CAAC,EAAE,MAAM,sBAExC;AAED,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC"} |
| import { isJSONArray, isJSONObject, isJSONValue, isString } from "types-json"; | ||
| import { validate } from "is-zod"; | ||
| export function parse(text, validator) { | ||
| if (text) { | ||
| try { | ||
| const json = JSON.parse(text); | ||
| if (typeof validator === "function") { | ||
| return validator(json) ? json : undefined; | ||
| } | ||
| else { | ||
| return validate(validator)(json) ? json : undefined; | ||
| } | ||
| } | ||
| catch { | ||
| return undefined; | ||
| } | ||
| } | ||
| else { | ||
| return undefined; | ||
| } | ||
| } | ||
| export function parseJSONValue(text) { | ||
| return parse(text, isJSONValue); | ||
| } | ||
| export function parseJSONObject(text) { | ||
| return parse(text, isJSONObject); | ||
| } | ||
| export function parseJSONArray(text) { | ||
| return parse(text, isJSONArray); | ||
| } | ||
| export function parseString(text) { | ||
| return parse(text, isString); | ||
| } | ||
| //# sourceMappingURL=index.js.map |
| {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,YAAY,EAAE,WAAW,EAAE,QAAQ,EAAkB,MAAM,YAAY,CAAC;AAE9F,OAAO,EAAE,QAAQ,EAAE,MAAM,QAAQ,CAAC;AAIlC,MAAM,UAAU,KAAK,CACnB,IAAwB,EACxB,SAA6C;IAE7C,IAAG,IAAI,EAAE,CAAC;QACR,IAAI,CAAC;YACH,MAAM,IAAI,GAAc,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;YACzC,IAAG,OAAO,SAAS,KAAK,UAAU,EAAE,CAAC;gBACnC,OAAO,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;YAC5C,CAAC;iBAAM,CAAC;gBACN,OAAO,QAAQ,CAAC,SAAS,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC;YACtD,CAAC;QACH,CAAC;QAAC,MAAK,CAAC;YACN,OAAO,SAAS,CAAC;QACnB,CAAC;IACH,CAAC;SAAM,CAAC;QACN,OAAO,SAAS,CAAC;IACnB,CAAC;AACH,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,IAAa;IAC1C,OAAO,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,eAAe,CAAC,IAAa;IAC3C,OAAO,KAAK,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;AACnC,CAAC;AAED,MAAM,UAAU,cAAc,CAAC,IAAa;IAC1C,OAAO,KAAK,CAAC,IAAI,EAAE,WAAW,CAAC,CAAC;AAClC,CAAC;AAED,MAAM,UAAU,WAAW,CAAC,IAAa;IACvC,OAAO,KAAK,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;AAC/B,CAAC"} |
+44
-26
| { | ||
| "name": "parse-json-object", | ||
| "version": "3.0.1", | ||
| "version": "4.0.0", | ||
| "description": "Parse a typed JSON object", | ||
| "license": "MIT", | ||
| "private": false, | ||
| "author": "Connor White", | ||
| "contributors": [ | ||
| "Connor White" | ||
| ], | ||
| "homepage": "https://connorwhite.dev/github/parse-json-object", | ||
| "author": { | ||
| "name": "Connor White", | ||
| "email": "oss@bconnorwhite.com", | ||
| "url": "https://bconnorwhite.com" | ||
| }, | ||
| "homepage": "https://github.bconnorwhite.com/parse-json-object#readme", | ||
| "repository": { | ||
@@ -25,37 +26,54 @@ "type": "git", | ||
| "type": "module", | ||
| "main": ".auto/build/index.js", | ||
| "exports": "./.auto/build/index.js", | ||
| "types": ".auto/build/index.d.ts", | ||
| "main": "./build/index.js", | ||
| "exports": { | ||
| ".": "./build/index.js" | ||
| }, | ||
| "types": "./build/index.d.ts", | ||
| "files": [ | ||
| ".auto/build/**/!(tsconfig.tsbuildinfo)" | ||
| "build/**/!(tsconfig.tsbuildinfo)" | ||
| ], | ||
| "engines": { | ||
| "node": "^14.13.1 || >=16.0.0" | ||
| "bun": "^1.3.0", | ||
| "node": "^24.0.0 || ^26.0.0" | ||
| }, | ||
| "scripts": { | ||
| "build": "auto build", | ||
| "dev": "auto dev", | ||
| "lint": "auto lint", | ||
| "prepack": "yarn build", | ||
| "release": "auto release", | ||
| "test": "auto test", | ||
| "typecheck": "auto typecheck" | ||
| "build": "tsc --project ./tsconfig.json", | ||
| "lint": "npm run lint:package && npm run lint:source", | ||
| "lint:package": "npmPkgJsonLint .", | ||
| "lint:source": "ESLINT_USE_FLAT_CONFIG=false eslint src test --ext .ts", | ||
| "prepack": "npm run build", | ||
| "release": "npm publish", | ||
| "test": "npm run test:node && npm run test:bun && npm run test:browser", | ||
| "test:browser": "vitest run --config ./vitest.config.mjs --project browser", | ||
| "test:bun": "bun test ./test/index.test.ts", | ||
| "test:node": "vitest run --config ./vitest.config.mjs --project node", | ||
| "typecheck": "npm run build && tsc --project ./test/tsconfig.json" | ||
| }, | ||
| "dependencies": { | ||
| "is-zod": "^1.0.0", | ||
| "types-json": "^4.0.1" | ||
| "is-zod": "^2.0.1", | ||
| "types-json": "^5.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "autorepo": "^0.1.9", | ||
| "@bconnorwhite/eslint-config": "^1.1.0", | ||
| "@bconnorwhite/tsconfig": "^1.0.0", | ||
| "@types/node": "^24.3.0", | ||
| "@typescript-eslint/eslint-plugin": "^8.56.1", | ||
| "@typescript-eslint/parser": "^8.56.1", | ||
| "@vitest/browser": "^3.2.4", | ||
| "eslint": "^9.39.4", | ||
| "eslint-plugin-import": "^2.32.0", | ||
| "eslint-plugin-json": "^4.0.1", | ||
| "npm-package-json-lint": "^9.1.0", | ||
| "npm-package-json-lint-config-bconnorwhite": "^1.0.0", | ||
| "playwright": "^1.58.2", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^3.2.4", | ||
| "zod": "^3.20.2" | ||
| }, | ||
| "eslintConfig": { | ||
| "extends": "@autosoft/eslint-config" | ||
| "extends": "@bconnorwhite/eslint-config" | ||
| }, | ||
| "jest": { | ||
| "preset": "@autosoft/jest-preset" | ||
| }, | ||
| "npmpackagejsonlint": { | ||
| "extends": "npm-package-json-lint-config-auto" | ||
| "extends": "npm-package-json-lint-config-bconnorwhite/oss" | ||
| } | ||
| } |
+50
-40
@@ -10,5 +10,2 @@ <!--BEGIN HEADER--> | ||
| </a> | ||
| <a href="https://coveralls.io/github/bconnorwhite/parse-json-object?branch=master"> | ||
| <img alt="Coverage Status" src="https://img.shields.io/coveralls/github/bconnorwhite/parse-json-object.svg?branch=master"> | ||
| </a> | ||
| </div> | ||
@@ -20,29 +17,39 @@ | ||
| <br /> | ||
| _If I should maintain this repo, please ⭐️_ | ||
| <a href="https://github.com/bconnorwhite/parse-json-object"> | ||
| <img align="right" alt="GitHub stars" src="https://img.shields.io/github/stars/bconnorwhite/parse-json-object?label=%E2%AD%90%EF%B8%8F&style=social"> | ||
| </a> | ||
| _DM me on [Twitter](https://twitter.com/bconnorwhite) if you have questions or suggestions._ | ||
| <a href="https://twitter.com/bconnorwhite"> | ||
| <img align="right" alt="Twitter" src="https://img.shields.io/twitter/url?label=%40bconnorwhite&style=social&url=https%3A%2F%2Ftwitter.com%2Fbconnorwhite"> | ||
| </a> | ||
| --- | ||
| <!--END HEADER--> | ||
| - Returns `undefined` if unable to parse | ||
| - Returns value if successful | ||
| <!-- BEGIN INSTALLATION --> | ||
| ## Installation | ||
| <details open> | ||
| <summary> | ||
| <a href="https://www.npmjs.com/package/parse-json-object"> | ||
| <img src="https://img.shields.io/badge/npm-CB3837?logo=npm&logoColor=white" alt="NPM" /> | ||
| </a> | ||
| </summary> | ||
| ```sh | ||
| yarn add parse-json-object | ||
| npm install parse-json-object | ||
| ``` | ||
| </details> | ||
| <details> | ||
| <summary> | ||
| <a href="https://yarnpkg.com/package/parse-json-object"> | ||
| <img src="https://img.shields.io/badge/yarn-2C8EBB?logo=yarn&logoColor=white" alt="Yarn" /> | ||
| </a> | ||
| </summary> | ||
| ```sh | ||
| npm install parse-json-object | ||
| yarn add parse-json-object | ||
| ``` | ||
| </details> | ||
| <details> | ||
| <summary> | ||
| <img src="https://img.shields.io/badge/pnpm-F69220?logo=pnpm&logoColor=white" alt="PNPM" /> | ||
| </summary> | ||
| ```sh | ||
@@ -52,2 +59,19 @@ pnpm add parse-json-object | ||
| </details> | ||
| <details> | ||
| <summary> | ||
| <img src="https://img.shields.io/badge/bun-EE81C3?logo=bun&logoColor=white" alt="Bun" /> | ||
| </summary> | ||
| ```sh | ||
| bun add parse-json-object | ||
| ``` | ||
| </details> | ||
| <!-- END INSTALLATION --> | ||
| - Returns `undefined` if unable to parse | ||
| - Returns value if successful | ||
| ## Usage | ||
@@ -99,6 +123,10 @@ | ||
| <!--BEGIN FOOTER--> | ||
| <br /> | ||
| ## Related Packages: | ||
| - [stringify-json-object](https://www.npmjs.com/package/stringify-json-object): Stringify and format a JSON object | ||
| - [types-json](https://www.npmjs.com/package/types-json): Type checking for JSON objects | ||
| <!--BEGIN FOOTER--> | ||
| <h2 id="dependencies">Dependencies<a href="https://www.npmjs.com/package/parse-json-object?activeTab=dependencies"><img align="right" alt="dependencies" src="https://img.shields.io/librariesio/release/npm/parse-json-object.svg"></a></h2> | ||
@@ -109,23 +137,5 @@ | ||
| <br /> | ||
| <h3>Dev Dependencies</h3> | ||
| - [autorepo](https://www.npmjs.com/package/autorepo): Autorepo abstracts away your dev dependencies, providing a single command to run all of your scripts. | ||
| - [zod](https://www.npmjs.com/package/zod): TypeScript-first schema declaration and validation library with static type inference | ||
| <br /> | ||
| <h2 id="license">License <a href="https://opensource.org/licenses/MIT"><img align="right" alt="license" src="https://img.shields.io/npm/l/parse-json-object.svg"></a></h2> | ||
| [MIT](https://opensource.org/licenses/MIT) | ||
| [MIT](https://opensource.org/licenses/MIT) - _MIT License_ | ||
| <!--END FOOTER--> | ||
| <br /> | ||
| ## Related Packages: | ||
| - [stringify-json-object](https://www.npmjs.com/package/stringify-json-object): Stringify and format a JSON object | ||
| - [types-json](https://www.npmjs.com/package/types-json): Type checking for JSON objects |
| import { JSONValue } from "types-json"; | ||
| import z from "zod"; | ||
| export type ValidateFunction<T extends JSONValue> = (value: JSONValue) => value is T; | ||
| export declare function parse<T extends JSONValue>(text: string | undefined, validator: ValidateFunction<T> | z.ZodSchema<T>): T | undefined; | ||
| export declare function parseJSONValue(text?: string): JSONValue | undefined; | ||
| export declare function parseJSONObject(text?: string): import("types-json").JSONObject | undefined; | ||
| export declare function parseJSONArray(text?: string): import("types-json").JSONArray | undefined; | ||
| export declare function parseString(text?: string): string | undefined; | ||
| export type { JSONValue, JSONObject, JSONArray } from "types-json"; |
| import { isJSONObject, isJSONValue, isJSONArray, isString } from "types-json"; | ||
| import { validate } from "is-zod"; | ||
| export function parse(text, validator) { | ||
| if (text) { | ||
| try { | ||
| const json = JSON.parse(text); | ||
| if (typeof validator === "function") { | ||
| return validator(json) ? json : undefined; | ||
| } else { | ||
| return validate(validator)(json) ? json : undefined; | ||
| } | ||
| } catch (err) { | ||
| return undefined; | ||
| } | ||
| } else { | ||
| return undefined; | ||
| } | ||
| } | ||
| export function parseJSONValue(text) { | ||
| return parse(text, isJSONValue); | ||
| } | ||
| export function parseJSONObject(text) { | ||
| return parse(text, isJSONObject); | ||
| } | ||
| export function parseJSONArray(text) { | ||
| return parse(text, isJSONArray); | ||
| } | ||
| export function parseString(text) { | ||
| return parse(text, isString); | ||
| } |
-46
| ## [3.0.1](https://github.com/bconnorwhite/parse-json-object/compare/v3.0.0...v3.0.1) (2023-01-29) | ||
| ### Bug Fixes | ||
| * adjust types ([83320fc](https://github.com/bconnorwhite/parse-json-object/commit/83320fc17389109e4c407a4f1d28af7a18990c4a)) | ||
| * reexport types-json types ([1e2f64c](https://github.com/bconnorwhite/parse-json-object/commit/1e2f64cf7faf842771b37bf8cd0c17c7d0f0ca61)) | ||
| # [3.0.0](https://github.com/bconnorwhite/parse-json-object/compare/v2.0.1...v3.0.0) (2023-01-29) | ||
| ## [2.0.1](https://github.com/bconnorwhite/parse-json-object/compare/v2.0.0...v2.0.1) (2020-10-04) | ||
| # [2.0.0](https://github.com/bconnorwhite/parse-json-object/compare/v1.1.0...v2.0.0) (2020-09-10) | ||
| # [1.1.0](https://github.com/bconnorwhite/parse-json-object/compare/v1.0.6...v1.1.0) (2020-09-07) | ||
| ## [1.0.6](https://github.com/bconnorwhite/parse-json-object/compare/v1.0.5...v1.0.6) (2020-09-07) | ||
| ## [1.0.5](https://github.com/bconnorwhite/parse-json-object/compare/v1.0.4...v1.0.5) (2020-09-03) | ||
| ## [1.0.4](https://github.com/bconnorwhite/parse-json-object/compare/v1.0.2...v1.0.4) (2020-09-02) | ||
| ## [1.0.2](https://github.com/bconnorwhite/parse-json-object/compare/v1.0.1...v1.0.2) (2020-08-28) | ||
| ## 1.0.1 (2020-08-28) | ||
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
9200
16.26%6
20%42
7.69%0
-100%137
7.87%15
650%+ Added
+ Added
- Removed
- Removed
Updated
Updated