Skip to main content
Home

latest

A snappy, type-safe JSON parser for all JavaScript environments.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
8 months ago (2.0.1)

๐Ÿš€ snap-parse

A snappy, type-safe JSON parser for all JavaScript environments.

JSR Score JSR Version

โœจ 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 objects
  • numbers (default: true): Parse numeric strings as numbers
  • special (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.

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@openjs/snap-parse

Import symbol

import * as snap_parse from "@openjs/snap-parse";
or

Import directly with a jsr specifier

import * as snap_parse from "jsr:@openjs/snap-parse";

Add Package

pnpm i jsr:@openjs/snap-parse
or (using pnpm 10.8 or older)
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
or (using Yarn 4.8 or older)
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";