source: imaps-frontend/node_modules/flatted/README.md@ d565449

main
Last change on this file since d565449 was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 4.5 KB
Line 
1# flatted
2
3[![Downloads](https://img.shields.io/npm/dm/flatted.svg)](https://www.npmjs.com/package/flatted) [![Coverage Status](https://coveralls.io/repos/github/WebReflection/flatted/badge.svg?branch=main)](https://coveralls.io/github/WebReflection/flatted?branch=main) [![Build Status](https://travis-ci.com/WebReflection/flatted.svg?branch=main)](https://travis-ci.com/WebReflection/flatted) [![License: ISC](https://img.shields.io/badge/License-ISC-yellow.svg)](https://opensource.org/licenses/ISC) ![WebReflection status](https://offline.report/status/webreflection.svg)
4
5![snow flake](./flatted.jpg)
6
7<sup>**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**</sup>
8
9A super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).
10
11Available also for **[PHP](./php/flatted.php)**.
12
13Available also for **[Python](./python/flatted.py)**.
14
15- - -
16
17## Announcement 📣
18
19There is a standard approach to recursion and more data-types than what JSON allows, and it's part of the [Structured Clone polyfill](https://github.com/ungap/structured-clone/#readme).
20
21Beside acting as a polyfill, its `@ungap/structured-clone/json` export provides both `stringify` and `parse`, and it's been tested for being faster than *flatted*, but its produced output is also smaller than *flatted* in general.
22
23The *@ungap/structured-clone* module is, in short, a drop in replacement for *flatted*, but it's not compatible with *flatted* specialized syntax.
24
25However, if recursion, as well as more data-types, are what you are after, or interesting for your projects/use cases, consider switching to this new module whenever you can 👍
26
27- - -
28
29```js
30npm i flatted
31```
32
33Usable via [CDN](https://unpkg.com/flatted) or as regular module.
34
35```js
36// ESM
37import {parse, stringify, toJSON, fromJSON} from 'flatted';
38
39// CJS
40const {parse, stringify, toJSON, fromJSON} = require('flatted');
41
42const a = [{}];
43a[0].a = a;
44a.push(a);
45
46stringify(a); // [["1","0"],{"a":"0"}]
47```
48
49## toJSON and fromJSON
50
51If you'd like to implicitly survive JSON serialization, these two helpers helps:
52
53```js
54import {toJSON, fromJSON} from 'flatted';
55
56class RecursiveMap extends Map {
57 static fromJSON(any) {
58 return new this(fromJSON(any));
59 }
60 toJSON() {
61 return toJSON([...this.entries()]);
62 }
63}
64
65const recursive = new RecursiveMap;
66const same = {};
67same.same = same;
68recursive.set('same', same);
69
70const asString = JSON.stringify(recursive);
71const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
72asMap.get('same') === asMap.get('same').same;
73// true
74```
75
76
77## Flatted VS JSON
78
79As it is for every other specialized format capable of serializing and deserializing circular data, you should never `JSON.parse(Flatted.stringify(data))`, and you should never `Flatted.parse(JSON.stringify(data))`.
80
81The only way this could work is to `Flatted.parse(Flatted.stringify(data))`, as it is also for _CircularJSON_ or any other, otherwise there's no granted data integrity.
82
83Also please note this project serializes and deserializes only data compatible with JSON, so that sockets, or anything else with internal classes different from those allowed by JSON standard, won't be serialized and unserialized as expected.
84
85
86### New in V1: Exact same JSON API
87
88 * Added a [reviver](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Syntax) parameter to `.parse(string, reviver)` and revive your own objects.
89 * Added a [replacer](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify#Syntax) and a `space` parameter to `.stringify(object, replacer, space)` for feature parity with JSON signature.
90
91
92### Compatibility
93All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
94
95
96### How does it work ?
97While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
98
99Once parsed, all indexes will be replaced through the flattened collection.
100
101<sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
102
103```js
104// logic example
105var a = [{one: 1}, {two: '2'}];
106a[0].a = a;
107// a is the main object, will be at index '0'
108// {one: 1} is the second object, index '1'
109// {two: '2'} the third, in '2', and it has a string
110// which will be found at index '3'
111
112Flatted.stringify(a);
113// [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
114// a[one,two] {one: 1, a} {two: '2'} '2'
115```
Note: See TracBrowser for help on using the repository browser.