source: frontend/node_modules/flatted/README.md

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 3.9 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) [![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
15Available also for **[Go](./golang/README.md)**.
16
17- - -
18
19## ℹ️ JSON only values
20
21If you need anything more complex than values JSON understands, there 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).
22
23- - -
24
25```js
26npm i flatted
27```
28
29Usable via [CDN](https://unpkg.com/flatted) or as regular module.
30
31```js
32// ESM
33import {parse, stringify, toJSON, fromJSON} from 'flatted';
34
35// CJS
36const {parse, stringify, toJSON, fromJSON} = require('flatted');
37
38const a = [{}];
39a[0].a = a;
40a.push(a);
41
42stringify(a); // [["1","0"],{"a":"0"}]
43```
44
45## toJSON and fromJSON
46
47If you'd like to implicitly survive JSON serialization, these two helpers helps:
48
49```js
50import {toJSON, fromJSON} from 'flatted';
51
52class RecursiveMap extends Map {
53 static fromJSON(any) {
54 return new this(fromJSON(any));
55 }
56 toJSON() {
57 return toJSON([...this.entries()]);
58 }
59}
60
61const recursive = new RecursiveMap;
62const same = {};
63same.same = same;
64recursive.set('same', same);
65
66const asString = JSON.stringify(recursive);
67const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
68asMap.get('same') === asMap.get('same').same;
69// true
70```
71
72
73## Flatted VS JSON
74
75As 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))`.
76
77The 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.
78
79Also 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.
80
81
82### New in V1: Exact same JSON API
83
84 * 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.
85 * 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.
86
87
88### Compatibility
89All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
90
91
92### How does it work ?
93While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
94
95Once parsed, all indexes will be replaced through the flattened collection.
96
97<sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
98
99```js
100// logic example
101var a = [{one: 1}, {two: '2'}];
102a[0].a = a;
103// a is the main object, will be at index '0'
104// {one: 1} is the second object, index '1'
105// {two: '2'} the third, in '2', and it has a string
106// which will be found at index '3'
107
108Flatted.stringify(a);
109// [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
110// a[one,two] {one: 1, a} {two: '2'} '2'
111```
Note: See TracBrowser for help on using the repository browser.