Simple email application for Android. Original source code: https://framagit.org/dystopia-project/simple-email
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.

209 lines
4.1 KiB

  1. # traverse
  2. Traverse and transform objects by visiting every node on a recursive walk.
  3. [![browser support](http://ci.testling.com/substack/js-traverse.png)](http://ci.testling.com/substack/js-traverse)
  4. [![build status](https://secure.travis-ci.org/substack/js-traverse.png)](http://travis-ci.org/substack/js-traverse)
  5. # examples
  6. ## transform negative numbers in-place
  7. negative.js
  8. ````javascript
  9. var traverse = require('traverse');
  10. var obj = [ 5, 6, -3, [ 7, 8, -2, 1 ], { f : 10, g : -13 } ];
  11. traverse(obj).forEach(function (x) {
  12. if (x < 0) this.update(x + 128);
  13. });
  14. console.dir(obj);
  15. ````
  16. Output:
  17. [ 5, 6, 125, [ 7, 8, 126, 1 ], { f: 10, g: 115 } ]
  18. ## collect leaf nodes
  19. leaves.js
  20. ````javascript
  21. var traverse = require('traverse');
  22. var obj = {
  23. a : [1,2,3],
  24. b : 4,
  25. c : [5,6],
  26. d : { e : [7,8], f : 9 },
  27. };
  28. var leaves = traverse(obj).reduce(function (acc, x) {
  29. if (this.isLeaf) acc.push(x);
  30. return acc;
  31. }, []);
  32. console.dir(leaves);
  33. ````
  34. Output:
  35. [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
  36. ## scrub circular references
  37. scrub.js:
  38. ````javascript
  39. var traverse = require('traverse');
  40. var obj = { a : 1, b : 2, c : [ 3, 4 ] };
  41. obj.c.push(obj);
  42. var scrubbed = traverse(obj).map(function (x) {
  43. if (this.circular) this.remove()
  44. });
  45. console.dir(scrubbed);
  46. ````
  47. output:
  48. { a: 1, b: 2, c: [ 3, 4 ] }
  49. # methods
  50. Each method that takes an `fn` uses the context documented below in the context
  51. section.
  52. ## .map(fn)
  53. Execute `fn` for each node in the object and return a new object with the
  54. results of the walk. To update nodes in the result use `this.update(value)`.
  55. ## .forEach(fn)
  56. Execute `fn` for each node in the object but unlike `.map()`, when
  57. `this.update()` is called it updates the object in-place.
  58. ## .reduce(fn, acc)
  59. For each node in the object, perform a
  60. [left-fold](http://en.wikipedia.org/wiki/Fold_(higher-order_function))
  61. with the return value of `fn(acc, node)`.
  62. If `acc` isn't specified, `acc` is set to the root object for the first step
  63. and the root element is skipped.
  64. ## .paths()
  65. Return an `Array` of every possible non-cyclic path in the object.
  66. Paths are `Array`s of string keys.
  67. ## .nodes()
  68. Return an `Array` of every node in the object.
  69. ## .clone()
  70. Create a deep clone of the object.
  71. ## .get(path)
  72. Get the element at the array `path`.
  73. ## .set(path, value)
  74. Set the element at the array `path` to `value`.
  75. ## .has(path)
  76. Return whether the element at the array `path` exists.
  77. # context
  78. Each method that takes a callback has a context (its `this` object) with these
  79. attributes:
  80. ## this.node
  81. The present node on the recursive walk
  82. ## this.path
  83. An array of string keys from the root to the present node
  84. ## this.parent
  85. The context of the node's parent.
  86. This is `undefined` for the root node.
  87. ## this.key
  88. The name of the key of the present node in its parent.
  89. This is `undefined` for the root node.
  90. ## this.isRoot, this.notRoot
  91. Whether the present node is the root node
  92. ## this.isLeaf, this.notLeaf
  93. Whether or not the present node is a leaf node (has no children)
  94. ## this.level
  95. Depth of the node within the traversal
  96. ## this.circular
  97. If the node equals one of its parents, the `circular` attribute is set to the
  98. context of that parent and the traversal progresses no deeper.
  99. ## this.update(value, stopHere=false)
  100. Set a new value for the present node.
  101. All the elements in `value` will be recursively traversed unless `stopHere` is
  102. true.
  103. ## this.remove(stopHere=false)
  104. Remove the current element from the output. If the node is in an Array it will
  105. be spliced off. Otherwise it will be deleted from its parent.
  106. ## this.delete(stopHere=false)
  107. Delete the current element from its parent in the output. Calls `delete` even on
  108. Arrays.
  109. ## this.before(fn)
  110. Call this function before any of the children are traversed.
  111. You can assign into `this.keys` here to traverse in a custom order.
  112. ## this.after(fn)
  113. Call this function after any of the children are traversed.
  114. ## this.pre(fn)
  115. Call this function before each of the children are traversed.
  116. ## this.post(fn)
  117. Call this function after each of the children are traversed.
  118. # install
  119. Using [npm](http://npmjs.org) do:
  120. $ npm install traverse
  121. # license
  122. MIT