node-path.js
3.6 KB
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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.createPath = createPath;
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function findParent(_ref, cb) {
var parentPath = _ref.parentPath;
if (parentPath == null) {
throw new Error("node is root");
}
var currentPath = parentPath;
while (cb(currentPath) !== false) {
// Hit the root node, stop
// $FlowIgnore
if (currentPath.parentPath == null) {
return null;
} // $FlowIgnore
currentPath = currentPath.parentPath;
}
return currentPath.node;
}
function insertBefore(context, newNode) {
return insert(context, newNode);
}
function insertAfter(context, newNode) {
return insert(context, newNode, 1);
}
function insert(_ref2, newNode) {
var node = _ref2.node,
inList = _ref2.inList,
parentPath = _ref2.parentPath,
parentKey = _ref2.parentKey;
var indexOffset = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
if (!inList) {
throw new Error('inList' + " error: " + ("insert can only be used for nodes that are within lists" || "unknown"));
}
if (!(parentPath != null)) {
throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
}
// $FlowIgnore
var parentList = parentPath.node[parentKey];
var indexInList = parentList.findIndex(function (n) {
return n === node;
});
parentList.splice(indexInList + indexOffset, 0, newNode);
}
function remove(_ref3) {
var node = _ref3.node,
parentKey = _ref3.parentKey,
parentPath = _ref3.parentPath;
if (!(parentPath != null)) {
throw new Error('parentPath != null' + " error: " + ("Can not remove root node" || "unknown"));
}
// $FlowIgnore
var parentNode = parentPath.node; // $FlowIgnore
var parentProperty = parentNode[parentKey];
if (Array.isArray(parentProperty)) {
// $FlowIgnore
parentNode[parentKey] = parentProperty.filter(function (n) {
return n !== node;
});
} else {
// $FlowIgnore
delete parentNode[parentKey];
}
node._deleted = true;
}
function stop(context) {
context.shouldStop = true;
}
function replaceWith(context, newNode) {
// $FlowIgnore
var parentNode = context.parentPath.node; // $FlowIgnore
var parentProperty = parentNode[context.parentKey];
if (Array.isArray(parentProperty)) {
var indexInList = parentProperty.findIndex(function (n) {
return n === context.node;
});
parentProperty.splice(indexInList, 1, newNode);
} else {
// $FlowIgnore
parentNode[context.parentKey] = newNode;
}
context.node._deleted = true;
context.node = newNode;
} // bind the context to the first argument of node operations
function bindNodeOperations(operations, context) {
var keys = Object.keys(operations);
var boundOperations = {};
keys.forEach(function (key) {
boundOperations[key] = operations[key].bind(null, context);
});
return boundOperations;
}
function createPathOperations(context) {
// $FlowIgnore
return bindNodeOperations({
findParent: findParent,
replaceWith: replaceWith,
remove: remove,
insertBefore: insertBefore,
insertAfter: insertAfter,
stop: stop
}, context);
}
function createPath(context) {
var path = _extends({}, context); // $FlowIgnore
Object.assign(path, createPathOperations(path)); // $FlowIgnore
return path;
}