Giter Site home page Giter Site logo

seroval's Introduction

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

seroval's People

Contributors

lxsmnsyc avatar samualtnorman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

seroval's Issues

Edge Case: Mistake on isolating Map entries

const a = {
  d: {},
  map: new Map,
  b: {
    c: {
      foo: 'bar'
    }
  }
};

a.map.set(a, a.b.c);

a.b.c.parent = a.b;
a.b.c.d = a.d;

This causes a.b.c to be undefined because the origin reference, located at a.map, is deferred due to recursion.

((intermediate value) , $R[75].group , $R[163].group , $R[0]) is not a function

image

source:

'((self.$R=self.$R||{})["server-fn:11"]=[],($R=>$R[0]=[$R[1]={_id:"918f8823-823d-43b1-984e-9eb994e5c34b",userId:void 0,type:1,level:0,createDate:$R[2]=new Date("2024-07-30T07:28:30.968Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,name:"wfwefwe",childs:$R[3]=[$R[4]={_id:"d160bb9a-32ac-4edf-a4e6-9d4ff2e4b5e8",userId:void 0,type:2,level:0,createDate:$R[5]=new Date("2024-07-30T07:28:31.967Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[6]={_id:"9e245b81-c1be-4d7b-a342-dc89733c3575",userId:null,type:10001,level:0,createDate:$R[7]=new Date("2024-07-28T08:40:04.817Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"11111fwefwefw333333",typing_hideTextCharRange:$R[8]=[],typing_textGroup:$R[9]=[$R[10]=["1"],$R[11]=["1"],$R[12]=["1"],$R[13]=["1"],$R[14]=["1"],$R[15]=["f"],$R[16]=["w"],$R[17]=["e"],$R[18]=["f"],$R[19]=["w"],$R[20]=["e"],$R[21]=["f"],$R[22]=["w"],$R[23]=["3"],$R[24]=["3"],$R[25]=["3"],$R[26]=["3"],$R[27]=["3"],$R[28]=["3"]],typing_fillGap:null,typing_hideIndices:null,typing_hideKeyboard:null,typing_hideKeyboardTipsForHiddenContent:null,typing_hideTextGroupNote:null,typing_textAlign:null,typing_textGroupNote:null}},$R[29]={_id:"63cc4b24-0617-4b06-a25c-897506835e6f",userId:void 0,type:2,level:0,createDate:$R[30]=new Date("2024-07-30T07:28:34.623Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[31]={_id:"0cd41ea5-bbdb-43c5-ae3c-dc3320083957",userId:null,type:10001,level:0,createDate:$R[32]=new Date("2024-07-29T13:24:43.459Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"wdqwefwefw",typing_hideTextCharRange:$R[33]=[],typing_textGroup:$R[34]=[$R[35]=["w"],$R[36]=["d"],$R[37]=["q"],$R[38]=["w"],$R[39]=["e"],$R[40]=["f"],$R[41]=["w"],$R[42]=["e"],$R[43]=["f"],$R[44]=["w"]]}},$R[45]={_id:"30fbc539-a69d-4f32-b570-de0632ce7f71",userId:void 0,type:2,level:0,createDate:$R[46]=new Date("2024-07-30T07:28:32.575Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[47]={_id:"d1955fbc-942d-4bb8-b02c-e32790210dcf",userId:null,type:10001,level:0,createDate:$R[48]=new Date("2024-07-28T22:45:46.947Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"rgergergerg",typing_hideTextCharRange:$R[49]=[],typing_textGroup:$R[50]=[$R[51]=["r"],$R[52]=["g"],$R[53]=["e"],$R[54]=["r"],$R[55]=["g"],$R[56]=["e"],$R[57]=["r"],$R[58]=["g"],$R[59]=["e"],$R[60]=["r"],$R[61]=["g"]],typing_fillGap:null,typing_hideIndices:null,typing_hideKeyboard:null,typing_hideKeyboardTipsForHiddenContent:null,typing_hideTextGroupNote:null,typing_textAlign:null,typing_textGroupNote:null}},$R[62]={_id:"77529a84-5651-46b6-9cd7-e3fa8b3e424b",userId:void 0,type:2,level:0,createDate:$R[63]=new Date("2024-07-30T07:28:33.136Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[64]={id:"422372c5-c24b-47bb-8c39-4b64c5226309",userId:null,type:10001,level:0,createDate:$R[65]=new Date("2024-07-29T13:21:41.403Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"dqwdqwd",typing_hideTextCharRange:$R[66]=[],typing_textGroup:$R[67]=[$R[68]=["d"],$R[69]=["q"],$R[70]=["w"],$R[71]=["d"],$R[72]=["q"],$R[73]=["w"],$R[74]=["d"]]}},$R[75]={id:"69cea096-4a92-46bc-ae81-c3a365c42bef",userId:void 0,type:2,level:0,createDate:$R[76]=new Date("2024-07-30T07:51:06.100Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[77]={id:"0cd41ea5-bbdb-43c5-ae3c-dc3320083957",userId:null,type:10001,level:0,createDate:$R[78]=new Date("2024-07-29T13:24:43.459Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"wdqwefwefw",typing_hideTextCharRange:$R[79]=[],typing_textGroup:$R[80]=[$R[81]=["w"],$R[82]=["d"],$R[83]=["q"],$R[84]=["w"],$R[85]=["e"],$R[86]=["f"],$R[87]=["w"],$R[88]=["e"],$R[89]=["f"],$R[90]=["w"]]}}],childIds:$R[91]=["d160bb9a-32ac-4edf-a4e6-9d4ff2e4b5e8","63cc4b24-0617-4b06-a25c-897506835e6f","30fbc539-a69d-4f32-b570-de0632ce7f71","77529a84-5651-46b6-9cd7-e3fa8b3e424b","69cea096-4a92-46bc-ae81-c3a365c42bef"]},$R[92]={id:"f7c3b926-7aef-404f-94f1-7989ec0a7c55",userId:null,type:1,level:0,createDate:$R[93]=new Date("2024-07-30T04:24:31.170Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,name:"等待对方由55555",childIds:$R[94]=["a30c1940-c5d0-452d-8997-73cb20a00d3b","159971ff-f7be-461c-97c5-fabeebc5588a","27819545-5d26-4ead-b159-97b5eccadcad","93bf3a66-1f1e-4cc9-978c-302b703418de"],childs:$R[95]=[$R[96]={_id:"a30c1940-c5d0-452d-8997-73cb20a00d3b",userId:void 0,type:2,level:0,createDate:$R[97]=new Date("2024-07-30T07:08:56.138Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[98]={_id:"9e245b81-c1be-4d7b-a342-dc89733c3575",userId:null,type:10001,level:0,createDate:$R[99]=new Date("2024-07-28T08:40:04.817Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"11111fwefwefw333333",typing_hideTextCharRange:$R[100]=[],typing_textGroup:$R[101]=[$R[102]=["1"],$R[103]=["1"],$R[104]=["1"],$R[105]=["1"],$R[106]=["1"],$R[107]=["f"],$R[108]=["w"],$R[109]=["e"],$R[110]=["f"],$R[111]=["w"],$R[112]=["e"],$R[113]=["f"],$R[114]=["w"],$R[115]=["3"],$R[116]=["3"],$R[117]=["3"],$R[118]=["3"],$R[119]=["3"],$R[120]=["3"]],typing_fillGap:null,typing_hideIndices:null,typing_hideKeyboard:null,typing_hideKeyboardTipsForHiddenContent:null,typing_hideTextGroupNote:null,typing_textAlign:null,typing_textGroupNote:null}},$R[121]={_id:"159971ff-f7be-461c-97c5-fabeebc5588a",userId:void 0,type:2,level:0,createDate:$R[122]=new Date("2024-07-30T07:08:57.522Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[123]={_id:"d1955fbc-942d-4bb8-b02c-e32790210dcf",userId:null,type:10001,level:0,createDate:$R[124]=new Date("2024-07-28T22:45:46.947Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"rgergergerg",typing_hideTextCharRange:$R[125]=[],typing_textGroup:$R[126]=[$R[127]=["r"],$R[128]=["g"],$R[129]=["e"],$R[130]=["r"],$R[131]=["g"],$R[132]=["e"],$R[133]=["r"],$R[134]=["g"],$R[135]=["e"],$R[136]=["r"],$R[137]=["g"]],typing_fillGap:null,typing_hideIndices:null,typing_hideKeyboard:null,typing_hideKeyboardTipsForHiddenContent:null,typing_hideTextGroupNote:null,typing_textAlign:null,typing_textGroupNote:null}},$R[138]={_id:"27819545-5d26-4ead-b159-97b5eccadcad",userId:void 0,type:2,level:0,createDate:$R[139]=new Date("2024-07-30T05:44:19.951Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[140]={_id:"9e245b81-c1be-4d7b-a342-dc89733c3575",userId:null,type:10001,level:0,createDate:$R[141]=new Date("2024-07-28T08:40:04.817Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,typing_text:"11111fwefwefw333333",typing_hideTextCharRange:$R[142]=[],typing_textGroup:$R[143]=[$R[144]=["1"],$R[145]=["1"],$R[146]=["1"],$R[147]=["1"],$R[148]=["1"],$R[149]=["f"],$R[150]=["w"],$R[151]=["e"],$R[152]=["f"],$R[153]=["w"],$R[154]=["e"],$R[155]=["f"],$R[156]=["w"],$R[157]=["3"],$R[158]=["3"],$R[159]=["3"],$R[160]=["3"],$R[161]=["3"],$R[162]=["3"]],typing_fillGap:null,typing_hideIndices:null,typing_hideKeyboard:null,typing_hideKeyboardTipsForHiddenContent:null,typing_hideTextGroupNote:null,typing_textAlign:null,typing_textGroupNote:null}},$R[163]={_id:"93bf3a66-1f1e-4cc9-978c-302b703418de",userId:void 0,type:2,level:0,createDate:$R[164]=new Date("2024-07-30T07:08:59.386Z"),runNum:0,finishNum:0,finishTime:0,scoreNum:0,score:0,access:1,target:$R[123]}]}],$R[75].group=$R[62].group=$R[45].group=$R[29].group=$R[4].group=$R[1],$R[163].group=$R[138].group=$R[121].group=$R[96].group=$R[92],$R[0])($R["server-fn:11"]))'

Better serialization for `Blob` and `File`

Currently Blob and File relies on ArrayBuffer serialization, which means it uses the Uint8Array indirectly. This serialization form is accurate, but is probably too large for consideration. Blob accepts Uint8Array, ArrayBuffer, DataView and string, so it's probably ideal if the string format can be utilized instead.

Inside of getCrossReferenceHeader script, getting error Uncaught SyntaxError: Unexpected identifier '_$HY'

I am trying Solid-js 1.8.3 and renderToStringAsync, and I got a hydration script injected with an error like below:

                               | Uncaught SyntaxError: Unexpected identifier '_$HY'
                               V
(self.$R=self.$R||{})["s2-"]=[]_$HY.r["s2-0-0-0-0-0-0"]=($R=>($R[0]=_$.P()))($R["s2-"]);($R=>(_$.Ps($R[0],!0)))($R["s2-"]);

After I added the semicolon here, the hydration script could be parsed by the browser:

(self.$R = self.$R || {})["s2-"] = [];
_$HY.r["s2-0-0-0-0-0-0"] = (($R) => ($R[0] = _$.P()))($R["s2-"]);
(($R) => _$.Ps($R[0], !0))($R["s2-"]);

Maybe there is a missing semicolon here? I noticed that the id == null has a semicolon included but the id != null case does not have a semicolon at the end.

export function getCrossReferenceHeader(id?: string): string {
  if (id == null) {
    return `self.${GLOBAL_CONTEXT_REFERENCES}=self.${GLOBAL_CONTEXT_REFERENCES}||[];`;
  }
- return `(self.${GLOBAL_CONTEXT_REFERENCES}=self.${GLOBAL_CONTEXT_REFERENCES}||{})["${serializeString(id)}"]=[]`;
+ return `(self.${GLOBAL_CONTEXT_REFERENCES}=self.${GLOBAL_CONTEXT_REFERENCES}||{})["${serializeString(id)}"]=[];`;
}

Combined types

Objects that are iterable, Promise-like, null constructor and Object constructor are currently exclusive from one another. Maybe there's a way to serialize them in a way that the entire behavior is combined.

Allow opting out of serialization

If possible, it'll be pretty convenient to mark some value to be not serialized. Imagine this arbitrary API:

import { serialize, NoSerialization } from "seroval";

console.log(serialize({
  title: "My Article",
  load: new NoSerialization(() => import("./my-article.mdx")),
})) // { title: "My Article" }

In this case, without NoSerialization, the load property should've filtered first not to make any serialization error, since anonymous functions are not serializable. However, because it's marked as NoSerialization, Seroval can just skip the serialization and remove the property.

serialise date to number instead of string

surely it's better to serialise a date to the format new Date(1682085024851) instead of new Date("2023-04-21T13:50:24.851Z")?

the number is smaller and the string needs parsing.

I might be wrong though and there's something I'm missing. I'm curious if that's the case.

Thanks.

serialize 0x prefix string object key as number

Encountered this while using SolidStart.

When object string key contains 0x prefix it gets serialised as hex number.

For context, address identifiers in crypto such as Ethereum are commonly hex addresses.

import { deserialize, serialize } from "seroval"

const original = {
  "0x1": 2
}

const output = serialize(original)
console.log("output", output)
// output ({0x1:2})

const result = deserialize(output)
console.log("deserialize", result)
// deserialize { '1': 2 }

Feature: JSON string serializer

Add toJSONString and fromJSONString to skip the use of JSON.stringify and JSON.parse in userland (also because JSON.stringify has some bottleneck)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.