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.

SPECS.md 3.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Flatted Specifications
  2. This document describes operations performed to produce, or parse, the flatted output.
  3. ## stringify(any) => flattedString
  4. The output is always an `Array` that contains at index `0` the given value.
  5. If the value is an `Array` or an `Object`, per each property value passed through the callback, return the value as is if it's not an `Array`, an `Object`, or a `string`.
  6. In case it's an `Array`, an `Object`, or a `string`, return the index as `string`, associated through a `Map`.
  7. Giving the following example:
  8. ```js
  9. flatted.stringify('a'); // ["a"]
  10. flatted.stringify(['a']); // [["1"],"a"]
  11. flatted.stringify(['a', 1, 'b']); // [["1",1,"2"],"a","b"]
  12. ```
  13. There is an `input` containing `[array, "a", "b"]`, where the `array` has indexes `"1"` and `"2"` as strings, indexes that point respectively at `"a"` and `"b"` within the input `[array, "a", "b"]`.
  14. The exact same happens for objects.
  15. ```js
  16. flatted.stringify('a'); // ["a"]
  17. flatted.stringify({a: 'a'}); // [{"a":"1"},"a"]
  18. flatted.stringify({a: 'a', n: 1, b: 'b'}); // [{"a":"1","n":1,"b":"2"},"a","b"]
  19. ```
  20. Every object, string, or array, encountered during serialization will be stored once as stringified index.
  21. ```js
  22. // per each property/value of the object/array
  23. if (any == null || !/object|string/.test(typeof any))
  24. return any;
  25. if (!map.has(any)) {
  26. const index = String(arr.length);
  27. arr.push(any);
  28. map.set(any, index);
  29. }
  30. return map.get(any);
  31. ```
  32. This, performed before going through all properties, grants unique indexes per reference.
  33. The stringified indexes ensure there won't be conflicts with regularly stored numbers.
  34. ## parse(flattedString) => any
  35. Everything that is a `string` is wrapped as `new String`, but strings in the array, from index `1` on, is kept as regular `string`.
  36. ```js
  37. const input = JSON.parse('[{"a":"1"},"b"]', Strings).map(strings);
  38. // convert strings primitives into String instances
  39. function Strings(key, value) {
  40. return typeof value === 'string' ? new String(value) : value;
  41. }
  42. // converts String instances into strings primitives
  43. function strings(value) {
  44. return value instanceof String ? String(value) : value;
  45. }
  46. ```
  47. The `input` array will have a regular `string` at index `1`, but its object at index `0` will have an `instanceof String` as `.a` property.
  48. That is the key to place back values from the rest of the array, so that per each property of the object at index `0`, if the value is an `instanceof` String, something not serializable via JSON, it means it can be used to retrieve the position of its value from the `input` array.
  49. If such `value` is an object and it hasn't been parsed yet, add it as parsed and go through all its properties/values.
  50. ```js
  51. // outside any loop ...
  52. const parsed = new Set;
  53. // ... per each property/value ...
  54. if (value instanceof Primitive) {
  55. const tmp = input[parseInt(value)];
  56. if (typeof tmp === 'object' && !parsed.has(tmp)) {
  57. parsed.add(tmp);
  58. output[key] = tmp;
  59. if (typeof tmp === 'object' && tmp != null) {
  60. // perform this same logic per
  61. // each nested property/value ...
  62. }
  63. } else {
  64. output[key] = tmp;
  65. }
  66. } else
  67. output[key] = tmp;
  68. ```
  69. As summary, the whole logic is based on polluting the de-serialization with a kind of variable that is unexpected, hence secure to use as directive to retrieve an index with a value.
  70. The usage of a `Map` and a `Set` to flag known references/strings as visited/stored makes **flatted** a rock solid, fast, and compact, solution.