Stringify JS values
  • TypeScript 95.8%
  • HTML 3.9%
  • JavaScript 0.3%
Find a file
Repository files (latest commit first)
Filename Latest commit message Latest commit date
Balazs Horvath 4979e18e9c
Some checks failed
CI / build (push) Has been cancelled
chore: update package.json and tsconfig build scripts
2026-06-05 01:18:47 +02:00
.changeset 1.5.2 2026-04-04 01:47:51 +08:00
.github/workflows Fix node version 2024-12-22 02:23:07 +08:00
.vscode Bump 2025-03-14 11:06:09 +08:00
benchmark 1.5.2 2026-04-04 01:47:51 +08:00
docs 1.4.1 and 1.4.2 2026-01-21 08:54:43 +08:00
packages chore: update package.json and tsconfig build scripts 2026-06-05 01:18:47 +02:00
.gitignore init 2023-02-05 23:45:22 +08:00
.npmrc Create .npmrc 2024-08-01 14:45:27 +08:00
biome.json Fix function strings 2025-11-09 22:15:44 +08:00
LICENSE Update LICENSE 2025-02-19 22:45:07 +08:00
package.json Bump dependencies 2025-11-03 10:27:17 +08:00
pnpm-lock.yaml Update pnpm-lock.yaml 2026-01-23 15:01:13 +08:00
pnpm-workspace.yaml Bump 2025-03-14 11:06:09 +08:00
README.md Fix docs 2023-12-08 01:29:59 +08:00

seroval

Stringify JS values

NPM JavaScript Style Guide

Install

npm install --save seroval
yarn add seroval
pnpm add seroval

Usage

import { serialize } from 'seroval';

const object = {
  number: [Math.random(), -0, NaN, Infinity, -Infinity],
  string: ['hello world', '<script>Hello World</script>'],
  boolean: [true, false],
  null: null,
  undefined: undefined,
  bigint: 9007199254740991n,
  array: [,,,], // holes
  regexp: /[a-z0-9]+/i,
  date: new Date(),
  map: new Map([['hello', 'world']]),
  set: new Set(['hello', 'world']),
};

// self cyclic references
// recursive objects
object.self = object;
// recursive arrays
object.array.push(object.array);
// recursive maps
object.map.set('self', object.map);
// recursive sets
object.set.add(object.set);

// mutual cyclic references
object.array.push(object.map);
object.map.set('mutual', object.set);
object.set.add(object.array);

const result = serialize(object);
console.log(result);

Output (as a string):

((h,j,k,m,o)=>(o={number:[0.5337763749243287,-0,0/0,1/0,-1/0],string:["hello world","\x3Cscript>Hello World\x3C/script>"],boolean:[!0,!1],null:null,undefined:void 0,bigint:9007199254740991n,array:h=[,,,,k=(j=[],new Map([["hello","world"],["mutual",m=new Set(["hello","world"])]]))],regexp:/[a-z0-9]+/i,date:new Date("2023-12-07T17:28:57.909Z"),map:k,set:m},h[3]=h,k.set("self",k),m.add(m).add(h),o.self=o,o))()

// Formatted for readability
((h, j, k, m, o) => (
  (o = {
    number: [0.5337763749243287, -0, 0 / 0, 1 / 0, -1 / 0],
    string: ["hello world", "\x3Cscript>Hello World\x3C/script>"],
    boolean: [!0, !1],
    null: null,
    undefined: void 0,
    bigint: 9007199254740991n,
    array: (h = [
      ,
      ,
      ,
      ,
      (k =
        ((j = []),
        new Map([
          ["hello", "world"],
          ["mutual", (m = new Set(["hello", "world"]))],
        ]))),
    ]),
    regexp: /[a-z0-9]+/i,
    date: new Date("2023-12-07T17:28:57.909Z"),
    map: k,
    set: m,
  }),
  (h[3] = h),
  k.set("self", k),
  m.add(m).add(h),
  (o.self = o),
  o
))();

Docs

Sponsors

Sponsors

License

MIT © lxsmnsyc