tree crawl

crawl tree

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greatest.deepsurf.us/scripts/30801/201743/tree%20crawl.js

  1. // ==UserScript==
  2. // @name tree crawl
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description crawl tree
  6. // @author Piotr S.
  7. // @require https://cdnjs.cloudflare.com/ajax/libs/babel-standalone/6.24.2/babel.min.js
  8. // @require https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.23.0/polyfill.min.js
  9. // @match https://dynalist.io/d/*
  10. // ==/UserScript==
  11.  
  12. (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  13. let crawl = require('tree-crawl')
  14. },{"tree-crawl":2}],2:[function(require,module,exports){
  15. (function (global, factory) {
  16. typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
  17. typeof define === 'function' && define.amd ? define(factory) :
  18. (global.crawl = factory());
  19. }(this, (function () { 'use strict';
  20.  
  21. function Context(flags, cursor) {
  22. this.flags = flags;
  23. this.cursor = cursor;
  24. }
  25. Context.prototype = {
  26. skip: function skip() {
  27. this.flags.skip = true;
  28. },
  29. break: function _break() {
  30. this.flags.break = true;
  31. },
  32. remove: function remove() {
  33. this.flags.remove = true;
  34. },
  35. replace: function replace(node) {
  36. this.flags.replace = node;
  37. },
  38. get parent() {
  39. return this.cursor.parent;
  40. },
  41. get depth() {
  42. return this.cursor.depth;
  43. },
  44. get level() {
  45. return this.cursor.depth + 1;
  46. },
  47. get index() {
  48. return this.cursor.index;
  49. }
  50. };
  51. function ContextFactory(flags, cursor) {
  52. return new Context(flags, cursor);
  53. }
  54.  
  55. function Stack(initial) {
  56. this.xs = [initial];
  57. this.top = 0;
  58. }
  59. Stack.prototype = {
  60. push: function push(x) {
  61. this.top++;
  62. if (this.top < this.xs.length) {
  63. this.xs[this.top] = x;
  64. } else {
  65. this.xs.push(x);
  66. }
  67. },
  68. pushArrayReverse: function pushArrayReverse(xs) {
  69. for (var i = xs.length - 1; i >= 0; i--) {
  70. this.push(xs[i]);
  71. }
  72. },
  73. pop: function pop() {
  74. var x = this.peek();
  75. this.top--;
  76. return x;
  77. },
  78. peek: function peek() {
  79. return this.xs[this.top];
  80. },
  81. isEmpty: function isEmpty() {
  82. return -1 === this.top;
  83. }
  84. };
  85. function QueueFactory(initial) {
  86. return new Stack(initial);
  87. }
  88.  
  89. function DfsCursor() {
  90. this.depth = 0;
  91. this.stack = QueueFactory({ node: null, index: -1 });
  92. }
  93. DfsCursor.prototype = {
  94. moveDown: function moveDown(node) {
  95. this.depth++;
  96. this.stack.push({ node: node, index: 0 });
  97. },
  98. moveUp: function moveUp() {
  99. this.depth--;
  100. this.stack.pop();
  101. },
  102. moveNext: function moveNext() {
  103. this.stack.peek().index++;
  104. },
  105. get parent() {
  106. return this.stack.peek().node;
  107. },
  108. get index() {
  109. return this.stack.peek().index;
  110. }
  111. };
  112. function CursorFactory() {
  113. return new DfsCursor();
  114. }
  115.  
  116. function Flags() {
  117. this.break = false;
  118. this.skip = false;
  119. this.remove = false;
  120. this.replace = null;
  121. }
  122. Flags.prototype = {
  123. reset: function reset() {
  124. this.break = false;
  125. this.skip = false;
  126. this.remove = false;
  127. this.replace = null;
  128. }
  129. };
  130. function FlagsFactory() {
  131. return new Flags();
  132. }
  133.  
  134. function isNotEmpty(xs) {
  135. return 0 !== xs.length;
  136. }
  137.  
  138. function dfsPre(root, iteratee, getChildren) {
  139. var flags = FlagsFactory();
  140. var cursor = CursorFactory();
  141. var context = ContextFactory(flags, cursor);
  142. var stack = QueueFactory(root);
  143. var dummy = Object.assign({}, root);
  144. while (!stack.isEmpty()) {
  145. var node = stack.pop();
  146. if (node === dummy) {
  147. cursor.moveUp();
  148. continue;
  149. }
  150. flags.reset();
  151. iteratee(node, context);
  152. if (flags.break) break;
  153. if (flags.remove) continue;
  154. cursor.moveNext();
  155. if (!flags.skip) {
  156. if (flags.replace) {
  157. node = flags.replace;
  158. }
  159. var children = getChildren(node);
  160. if (isNotEmpty(children)) {
  161. stack.push(dummy);
  162. stack.pushArrayReverse(children);
  163. cursor.moveDown(node);
  164. }
  165. }
  166. }
  167. }
  168.  
  169. function dfsPost(root, iteratee, getChildren) {
  170. var flags = FlagsFactory();
  171. var cursor = CursorFactory();
  172. var context = ContextFactory(flags, cursor);
  173. var stack = QueueFactory(root);
  174. var ancestors = QueueFactory(null);
  175. while (!stack.isEmpty()) {
  176. var node = stack.peek();
  177. var parent = ancestors.peek();
  178. var children = getChildren(node);
  179. flags.reset();
  180. if (node === parent || !isNotEmpty(children)) {
  181. if (node === parent) {
  182. ancestors.pop();
  183. cursor.moveUp();
  184. }
  185. stack.pop();
  186. iteratee(node, context);
  187. if (flags.break) break;
  188. if (flags.remove) continue;
  189. cursor.moveNext();
  190. } else {
  191. ancestors.push(node);
  192. cursor.moveDown(node);
  193. stack.pushArrayReverse(children);
  194. }
  195. }
  196. }
  197.  
  198. var THRESHOLD = 32768;
  199. function Queue(initial) {
  200. this.xs = [initial];
  201. this.top = 0;
  202. this.maxLength = 0;
  203. }
  204. Queue.prototype = {
  205. enqueue: function enqueue(x) {
  206. this.xs.push(x);
  207. },
  208. enqueueMultiple: function enqueueMultiple(xs) {
  209. for (var i = 0, len = xs.length; i < len; i++) {
  210. this.enqueue(xs[i]);
  211. }
  212. },
  213. dequeue: function dequeue() {
  214. var x = this.peek();
  215. this.top++;
  216. if (this.top === THRESHOLD) {
  217. this.xs = this.xs.slice(this.top);
  218. this.top = 0;
  219. }
  220. return x;
  221. },
  222. peek: function peek() {
  223. return this.xs[this.top];
  224. },
  225. isEmpty: function isEmpty() {
  226. return this.top === this.xs.length;
  227. }
  228. };
  229. function QueueFactory$1(initial) {
  230. return new Queue(initial);
  231. }
  232.  
  233. function BfsCursor() {
  234. this.depth = 0;
  235. this.index = -1;
  236. this.queue = QueueFactory$1({ node: null, arity: 1 });
  237. this.levelNodes = 1;
  238. this.nextLevelNodes = 0;
  239. }
  240. BfsCursor.prototype = {
  241. store: function store(node, arity) {
  242. this.queue.enqueue({ node: node, arity: arity });
  243. this.nextLevelNodes += arity;
  244. },
  245. moveNext: function moveNext() {
  246. this.index++;
  247. },
  248. moveForward: function moveForward() {
  249. this.queue.peek().arity--;
  250. this.levelNodes--;
  251. if (0 === this.queue.peek().arity) {
  252. this.index = 0;
  253. this.queue.dequeue();
  254. }
  255. if (0 === this.levelNodes) {
  256. this.depth++;
  257. this.levelNodes = this.nextLevelNodes;
  258. this.nextLevelNodes = 0;
  259. }
  260. },
  261. get parent() {
  262. return this.queue.peek().node;
  263. }
  264. };
  265. function CursorFactory$1() {
  266. return new BfsCursor();
  267. }
  268.  
  269. function bfs(root, iteratee, getChildren) {
  270. var flags = FlagsFactory();
  271. var cursor = CursorFactory$1();
  272. var context = ContextFactory(flags, cursor);
  273. var queue = QueueFactory$1(root);
  274. while (!queue.isEmpty()) {
  275. var node = queue.dequeue();
  276. flags.reset();
  277. iteratee(node, context);
  278. if (flags.break) break;
  279. if (!flags.remove) {
  280. cursor.moveNext();
  281. if (flags.replace) {
  282. node = flags.replace;
  283. }
  284. if (!flags.skip) {
  285. var children = getChildren(node);
  286. if (isNotEmpty(children)) {
  287. queue.enqueueMultiple(children);
  288. cursor.store(node, children.length);
  289. }
  290. }
  291. }
  292. cursor.moveForward();
  293. }
  294. }
  295.  
  296. var defaultGetChildren = function defaultGetChildren(node) {
  297. return node.children;
  298. };
  299. DYNALIST.crawl = function(root, iteratee, options) {
  300. if (null == root) return;
  301. var order = options.order || 'pre';
  302. var getChildren = options.getChildren || defaultGetChildren;
  303. if ('pre' === order) {
  304. dfsPre(root, iteratee, getChildren);
  305. } else if ('post' === order) {
  306. dfsPost(root, iteratee, getChildren);
  307. } else if ('bfs' === order) {
  308. bfs(root, iteratee, getChildren);
  309. }
  310. }
  311.  
  312. return DYNALIST.crawl;
  313.  
  314. })));
  315.  
  316. },{}]},{},[1]);