Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # flatted
  2. [![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=master)](https://coveralls.io/github/WebReflection/flatted?branch=master) [![Build Status](https://travis-ci.org/WebReflection/flatted.svg?branch=master)](https://travis-ci.org/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)
  3. ![snow flake](./flatted.jpg)
  4. <sup>**Social Media Photo by [Matt Seymour](https://unsplash.com/@mattseymour) on [Unsplash](https://unsplash.com/)**</sup>
  5. A super light (0.5K) and fast circular JSON parser, directly from the creator of [CircularJSON](https://github.com/WebReflection/circular-json/#circularjson).
  6. Now available also for **[PHP](./php/flatted.php)**.
  7. ```js
  8. npm i flatted
  9. ```
  10. Usable via [CDN](https://unpkg.com/flatted) or as regular module.
  11. ```js
  12. // ESM
  13. import {parse, stringify, toJSON, fromJSON} from 'flatted';
  14. // CJS
  15. const {parse, stringify, toJSON, fromJSON} = require('flatted');
  16. const a = [{}];
  17. a[0].a = a;
  18. a.push(a);
  19. stringify(a); // [["1","0"],{"a":"0"}]
  20. ```
  21. ## toJSON and from JSON
  22. If you'd like to implicitly survive JSON serialization, these two helpers helps:
  23. ```js
  24. import {toJSON, fromJSON} from 'flatted';
  25. class RecursiveMap extends Map {
  26. static fromJSON(any) {
  27. return new this(fromJSON(any));
  28. }
  29. toJSON() {
  30. return toJSON([...this.entries()]);
  31. }
  32. }
  33. const recursive = new RecursiveMap;
  34. const same = {};
  35. same.same = same;
  36. recursive.set('same', same);
  37. const asString = JSON.stringify(recursive);
  38. const asMap = RecursiveMap.fromJSON(JSON.parse(asString));
  39. asMap.get('same') === asMap.get('same').same;
  40. // true
  41. ```
  42. ## Flatted VS JSON
  43. As 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))`.
  44. The 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.
  45. Also 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.
  46. ### New in V1: Exact same JSON API
  47. * 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.
  48. * 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.
  49. ### Compatibility
  50. All ECMAScript engines compatible with `Map`, `Set`, `Object.keys`, and `Array.prototype.reduce` will work, even if polyfilled.
  51. ### How does it work ?
  52. While stringifying, all Objects, including Arrays, and strings, are flattened out and replaced as unique index. `*`
  53. Once parsed, all indexes will be replaced through the flattened collection.
  54. <sup><sub>`*` represented as string to avoid conflicts with numbers</sub></sup>
  55. ```js
  56. // logic example
  57. var a = [{one: 1}, {two: '2'}];
  58. a[0].a = a;
  59. // a is the main object, will be at index '0'
  60. // {one: 1} is the second object, index '1'
  61. // {two: '2'} the third, in '2', and it has a string
  62. // which will be found at index '3'
  63. Flatted.stringify(a);
  64. // [["1","2"],{"one":1,"a":"0"},{"two":"3"},"2"]
  65. // a[one,two] {one: 1, a} {two: '2'} '2'
  66. ```