Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
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.2KB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. # flat [![Build Status](https://secure.travis-ci.org/hughsk/flat.png?branch=master)](http://travis-ci.org/hughsk/flat)
  2. Take a nested Javascript object and flatten it, or unflatten an object with
  3. delimited keys.
  4. ## Installation
  5. ``` bash
  6. $ npm install flat
  7. ```
  8. ## Methods
  9. ### flatten(original, options)
  10. Flattens the object - it'll return an object one level deep, regardless of how
  11. nested the original object was:
  12. ``` javascript
  13. var flatten = require('flat')
  14. flatten({
  15. key1: {
  16. keyA: 'valueI'
  17. },
  18. key2: {
  19. keyB: 'valueII'
  20. },
  21. key3: { a: { b: { c: 2 } } }
  22. })
  23. // {
  24. // 'key1.keyA': 'valueI',
  25. // 'key2.keyB': 'valueII',
  26. // 'key3.a.b.c': 2
  27. // }
  28. ```
  29. ### unflatten(original, options)
  30. Flattening is reversible too, you can call `flatten.unflatten()` on an object:
  31. ``` javascript
  32. var unflatten = require('flat').unflatten
  33. unflatten({
  34. 'three.levels.deep': 42,
  35. 'three.levels': {
  36. nested: true
  37. }
  38. })
  39. // {
  40. // three: {
  41. // levels: {
  42. // deep: 42,
  43. // nested: true
  44. // }
  45. // }
  46. // }
  47. ```
  48. ## Options
  49. ### delimiter
  50. Use a custom delimiter for (un)flattening your objects, instead of `.`.
  51. ### safe
  52. When enabled, both `flat` and `unflatten` will preserve arrays and their
  53. contents. This is disabled by default.
  54. ``` javascript
  55. var flatten = require('flat')
  56. flatten({
  57. this: [
  58. { contains: 'arrays' },
  59. { preserving: {
  60. them: 'for you'
  61. }}
  62. ]
  63. }, {
  64. safe: true
  65. })
  66. // {
  67. // 'this': [
  68. // { contains: 'arrays' },
  69. // { preserving: {
  70. // them: 'for you'
  71. // }}
  72. // ]
  73. // }
  74. ```
  75. ### object
  76. When enabled, arrays will not be created automatically when calling unflatten, like so:
  77. ``` javascript
  78. unflatten({
  79. 'hello.you.0': 'ipsum',
  80. 'hello.you.1': 'lorem',
  81. 'hello.other.world': 'foo'
  82. }, { object: true })
  83. // hello: {
  84. // you: {
  85. // 0: 'ipsum',
  86. // 1: 'lorem',
  87. // },
  88. // other: { world: 'foo' }
  89. // }
  90. ```
  91. ### overwrite
  92. When enabled, existing keys in the unflattened object may be overwritten if they cannot hold a newly encountered nested value:
  93. ```javascript
  94. unflatten({
  95. 'TRAVIS': 'true',
  96. 'TRAVIS_DIR': '/home/travis/build/kvz/environmental'
  97. }, { overwrite: true })
  98. // TRAVIS: {
  99. // DIR: '/home/travis/build/kvz/environmental'
  100. // }
  101. ```
  102. Without `overwrite` set to `true`, the `TRAVIS` key would already have been set to a string, thus could not accept the nested `DIR` element.
  103. This only makes sense on ordered arrays, and since we're overwriting data, should be used with care.
  104. ### maxDepth
  105. Maximum number of nested objects to flatten.
  106. ``` javascript
  107. var flatten = require('flat')
  108. flatten({
  109. key1: {
  110. keyA: 'valueI'
  111. },
  112. key2: {
  113. keyB: 'valueII'
  114. },
  115. key3: { a: { b: { c: 2 } } }
  116. }, { maxDepth: 2 })
  117. // {
  118. // 'key1.keyA': 'valueI',
  119. // 'key2.keyB': 'valueII',
  120. // 'key3.a': { b: { c: 2 } }
  121. // }
  122. ```
  123. ## Command Line Usage
  124. `flat` is also available as a command line tool. You can run it with
  125. [`npx`](https://ghub.io/npx):
  126. ```sh
  127. npx flat foo.json
  128. ```
  129. Or install the `flat` command globally:
  130. ```sh
  131. npm i -g flat && flat foo.json
  132. ```
  133. Accepts a filename as an argument:
  134. ```sh
  135. flat foo.json
  136. ```
  137. Also accepts JSON on stdin:
  138. ```sh
  139. cat foo.json | flat
  140. ```