1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | 3968× 3968× 2086× 3968× 168× 168× 36× 168× 168× 168× 9365× 9365× 11426× 215× 215× 215× 215× 215× 215× 215× 2900× 2900× 215× 215× 671× 671× 671× 671× 47× 624× 167× 167× 622× 622× 167× 1568× 1568× 33× 1568× 1× 1× 37× 32× 37× 37× 37× 79× 37× 37× 42× 39× 79× 37× 37× 1× 1× 1× 1× 1× 1× 2× 1× 1× 1× | import { get, setWith } from 'lodash-es' import isString from './isString' import isArray from './isArray' import deleteFromArray from './deleteFromArray' class TreeNode {} /* * A tree-structure for indexes. * * @class TreeIndex * @param {object} [obj] An object to operate on * @memberof module:Basics * @example * * var index = new TreeIndex({a: "aVal", b: {b1: 'b1Val', b2: 'b2Val'}}); */ class TreeIndex { /** * Get value at path. * * @return {object} The value stored for a given path * * @example * * obj.get(['b', 'b1']); * => b1Val */ get(path) { Iif (arguments.length > 1) { path = Array.prototype.slice(arguments, 0); } if (isString(path)) { path = [path]; } return get(this, path); } getAll(path) { Iif (arguments.length > 1) { path = Array.prototype.slice(arguments, 0); } if (isString(path)) { path = [path]; } Iif (!isArray(path)) { throw new Error('Illegal argument for TreeIndex.get()'); } var node = get(this, path); return this._collectValues(node); } set(path, value) { Iif (isString(path)) { path = [path]; } setWith(this, path, value, function(val) { if (!val) return new TreeNode(); }); } delete(path) { Iif (isString(path)) { delete this[path]; } else Iif(path.length === 1) { delete this[path[0]]; } else { var key = path[path.length-1]; path = path.slice(0, -1); var parent = get(this, path); Eif (parent) { delete parent[key]; } } } clear() { var root = this; for (var key in root) { if (root.hasOwnProperty(key)) { delete root[key]; } } } traverse(fn) { this._traverse(this, [], fn); } forEach(...args) { this.traverse(...args) } _traverse(root, path, fn) { var id; for (id in root) { Iif (!root.hasOwnProperty(id)) continue; var child = root[id]; var childPath = path.concat([id]); if (child instanceof TreeNode) { this._traverse(child, childPath, fn); } else { fn(child, childPath); } } } _collectValues(root) { // TODO: don't know if this is the best solution // We use this only for indexes, e.g., to get all annotation on one node var vals = {}; this._traverse(root, [], function(val, path) { var key = path[path.length-1]; vals[key] = val; }); return vals; } } class TreeIndexArrays extends TreeIndex { contains(path) { let val = super.get(path) return Boolean(val) } get(path) { let val = super.get(path) if (val instanceof TreeNode) { val = val.__values__ || []; } return val; } set(path, arr) { let val = super.get(path) val.__values__ = arr } add(path, value) { if (isString(path)) { path = [path]; } Iif (!isArray(path)) { throw new Error('Illegal arguments.'); } var arr; // We are using setWith, as it allows us to create nodes on the way down // setWith can be controlled via a hook called for each key in the path // If val is not defined, a new node must be created and returned. // If val is defined, then we must return undefined to keep the original tree node // __dummy__ is necessary as setWith is used to set a value, but we want // to append to the array setWith(this, path.concat(['__values__','__dummy__']), undefined, function(val, key) { if (key === '__values__') { if (!val) val = []; arr = val; } else if (!val) { val = new TreeNode(); } return val; }) delete arr.__dummy__ arr.push(value) } remove(path, value) { var arr = get(this, path); Eif (arr instanceof TreeNode) { Iif (arguments.length === 1) { delete arr.__values__ } else { deleteFromArray(arr.__values__, value); } } } _collectValues(root) { var vals = [] this._traverse(root, [], function(val) { vals.push(val); }) vals = Array.prototype.concat.apply([], vals) return vals } } TreeIndex.Arrays = TreeIndexArrays export default TreeIndex; |