@openjs/snap-parse@2.0.1
latest
A snappy, type-safe JSON parser for all JavaScript environments.
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers




JSR Score
100%
Published
8 months ago (2.0.1)
๐ snap-parse
A snappy, type-safe JSON parser for all JavaScript environments.
โจ Features
- ๐ฏ Type-safe parsing with TypeScript
- ๐ Cross-platform support (Browser, Node.js, Deno, Bun, Cloudflare)
- ๐ Automatic date parsing
- ๐ข Smart number conversion
- โก Special value handling (undefined, Infinity, NaN)
- ๐ก๏ธ Zero dependencies
- ๐ฆ Tiny bundle size
๐ฆ Installation
# Using npm npx jsr add @openjs/snap-parse # Using deno deno add jsr:@openjs/snap-parse # Using yarn yarn dlx jsr add @openjs/snap-parse # Using pnpm pnpm dlx jsr add @openjs/snap-parse # Using bun bunx jsr add @openjs/snap-parse
๐ Usage
import { parse, stringify } from "@openjs/snap-parse"; // Basic parsing parse("123"); // => 123 parse("true"); // => true parse('"hello"'); // => "hello" // Date parsing parse("2024-02-14T12:00:00Z"); // => Date object // Special values parse("undefined"); // => undefined parse("Infinity"); // => Infinity parse("NaN"); // => NaN // Complex objects const input = { number: "123", text: "hello", date: "2024-02-14T12:00:00Z", array: ["1", "2", "3"], nested: { special: "undefined" } }; parse(input); // => { // number: 123, // text: "hello", // date: Date object, // array: [1, 2, 3], // nested: { special: undefined } // } // Custom options parse(input, { numbers: false }); // Keep numbers as strings parse(input, { dates: false }); // Keep dates as strings parse(input, { special: false }); // Keep special values as strings // Stringify special values stringify({ inf: Infinity, nan: NaN, undef: undefined }); // => '{"inf":"Infinity","nan":"NaN","undef":"undefined"}'
โ๏ธ API
parse<T>(value: unknown, options?: ParseOptions): T
Parse a value into a JSON-compatible format.
Options
dates
(default:true
): Convert ISO date strings to Date objectsnumbers
(default:true
): Parse numeric strings as numbersspecial
(default:true
): Parse undefined and special values
stringify(value: unknown): string
Safely stringify a value to JSON, handling special values like undefined
, Infinity
, and NaN
.
๐งช Testing
deno test
๐ License
MIT
๐ฅ Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
Add Package
deno add jsr:@openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";
Import directly with a jsr specifier
import * as snap_parse from "jsr:@openjs/snap-parse";
Add Package
pnpm i jsr:@openjs/snap-parse
pnpm dlx jsr add @openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";
Add Package
yarn add jsr:@openjs/snap-parse
yarn dlx jsr add @openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";
Add Package
vlt install jsr:@openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";
Add Package
npx jsr add @openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";
Add Package
bunx jsr add @openjs/snap-parse
Import symbol
import * as snap_parse from "@openjs/snap-parse";