Repository navigation
Expand file tree
/
Copy pathutils.js
More file actions
230 lines (230 loc) · 7.44 KB
/
Copy pathutils.js
File metadata and controls
230 lines (230 loc) · 7.44 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.encode = exports.has = void 0;
exports.merge = merge;
exports.assign_single_source = assign_single_source;
exports.decode = decode;
exports.compact = compact;
exports.is_regexp = is_regexp;
exports.is_buffer = is_buffer;
exports.combine = combine;
exports.maybe_map = maybe_map;
const formats_1 = require("./formats.js");
const values_1 = require("../utils/values.js");
let has = (obj, key) => ((exports.has = Object.hasOwn ?? Function.prototype.call.bind(Object.prototype.hasOwnProperty)),
(0, exports.has)(obj, key));
exports.has = has;
const hex_table = /* @__PURE__ */ (() => {
const array = [];
for (let i = 0; i < 256; ++i) {
array.push('%' + ((i < 16 ? '0' : '') + i.toString(16)).toUpperCase());
}
return array;
})();
function compact_queue(queue) {
while (queue.length > 1) {
const item = queue.pop();
if (!item)
continue;
const obj = item.obj[item.prop];
if ((0, values_1.isArray)(obj)) {
const compacted = [];
for (let j = 0; j < obj.length; ++j) {
if (typeof obj[j] !== 'undefined') {
compacted.push(obj[j]);
}
}
// @ts-ignore
item.obj[item.prop] = compacted;
}
}
}
function array_to_object(source, options) {
const obj = options && options.plainObjects ? Object.create(null) : {};
for (let i = 0; i < source.length; ++i) {
if (typeof source[i] !== 'undefined') {
obj[i] = source[i];
}
}
return obj;
}
function merge(target, source, options = {}) {
if (!source) {
return target;
}
if (typeof source !== 'object') {
if ((0, values_1.isArray)(target)) {
target.push(source);
}
else if (target && typeof target === 'object') {
if ((options && (options.plainObjects || options.allowPrototypes)) || !(0, exports.has)(Object.prototype, source)) {
target[source] = true;
}
}
else {
return [target, source];
}
return target;
}
if (!target || typeof target !== 'object') {
return [target].concat(source);
}
let mergeTarget = target;
if ((0, values_1.isArray)(target) && !(0, values_1.isArray)(source)) {
// @ts-ignore
mergeTarget = array_to_object(target, options);
}
if ((0, values_1.isArray)(target) && (0, values_1.isArray)(source)) {
source.forEach(function (item, i) {
if ((0, exports.has)(target, i)) {
const targetItem = target[i];
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') {
target[i] = merge(targetItem, item, options);
}
else {
target.push(item);
}
}
else {
target[i] = item;
}
});
return target;
}
return Object.keys(source).reduce(function (acc, key) {
const value = source[key];
if ((0, exports.has)(acc, key)) {
acc[key] = merge(acc[key], value, options);
}
else {
acc[key] = value;
}
return acc;
}, mergeTarget);
}
function assign_single_source(target, source) {
return Object.keys(source).reduce(function (acc, key) {
acc[key] = source[key];
return acc;
}, target);
}
function decode(str, _, charset) {
const strWithoutPlus = str.replace(/\+/g, ' ');
if (charset === 'iso-8859-1') {
// unescape never throws, no try...catch needed:
return strWithoutPlus.replace(/%[0-9a-f]{2}/gi, unescape);
}
// utf-8
try {
return decodeURIComponent(strWithoutPlus);
}
catch (e) {
return strWithoutPlus;
}
}
const limit = 1024;
const encode = (str, _defaultEncoder, charset, _kind, format) => {
// This code was originally written by Brian White for the io.js core querystring library.
// It has been adapted here for stricter adherence to RFC 3986
if (str.length === 0) {
return str;
}
let string = str;
if (typeof str === 'symbol') {
string = Symbol.prototype.toString.call(str);
}
else if (typeof str !== 'string') {
string = String(str);
}
if (charset === 'iso-8859-1') {
return escape(string).replace(/%u[0-9a-f]{4}/gi, function ($0) {
return '%26%23' + parseInt($0.slice(2), 16) + '%3B';
});
}
let out = '';
for (let j = 0; j < string.length; j += limit) {
const segment = string.length >= limit ? string.slice(j, j + limit) : string;
const arr = [];
for (let i = 0; i < segment.length; ++i) {
let c = segment.charCodeAt(i);
if (c === 0x2d || // -
c === 0x2e || // .
c === 0x5f || // _
c === 0x7e || // ~
(c >= 0x30 && c <= 0x39) || // 0-9
(c >= 0x41 && c <= 0x5a) || // a-z
(c >= 0x61 && c <= 0x7a) || // A-Z
(format === formats_1.RFC1738 && (c === 0x28 || c === 0x29)) // ( )
) {
arr[arr.length] = segment.charAt(i);
continue;
}
if (c < 0x80) {
arr[arr.length] = hex_table[c];
continue;
}
if (c < 0x800) {
arr[arr.length] = hex_table[0xc0 | (c >> 6)] + hex_table[0x80 | (c & 0x3f)];
continue;
}
if (c < 0xd800 || c >= 0xe000) {
arr[arr.length] =
hex_table[0xe0 | (c >> 12)] + hex_table[0x80 | ((c >> 6) & 0x3f)] + hex_table[0x80 | (c & 0x3f)];
continue;
}
i += 1;
c = 0x10000 + (((c & 0x3ff) << 10) | (segment.charCodeAt(i) & 0x3ff));
arr[arr.length] =
hex_table[0xf0 | (c >> 18)] +
hex_table[0x80 | ((c >> 12) & 0x3f)] +
hex_table[0x80 | ((c >> 6) & 0x3f)] +
hex_table[0x80 | (c & 0x3f)];
}
out += arr.join('');
}
return out;
};
exports.encode = encode;
function compact(value) {
const queue = [{ obj: { o: value }, prop: 'o' }];
const refs = [];
for (let i = 0; i < queue.length; ++i) {
const item = queue[i];
// @ts-ignore
const obj = item.obj[item.prop];
const keys = Object.keys(obj);
for (let j = 0; j < keys.length; ++j) {
const key = keys[j];
const val = obj[key];
if (typeof val === 'object' && val !== null && refs.indexOf(val) === -1) {
queue.push({ obj: obj, prop: key });
refs.push(val);
}
}
}
compact_queue(queue);
return value;
}
function is_regexp(obj) {
return Object.prototype.toString.call(obj) === '[object RegExp]';
}
function is_buffer(obj) {
if (!obj || typeof obj !== 'object') {
return false;
}
return !!(obj.constructor && obj.constructor.isBuffer && obj.constructor.isBuffer(obj));
}
function combine(a, b) {
return [].concat(a, b);
}
function maybe_map(val, fn) {
if ((0, values_1.isArray)(val)) {
const mapped = [];
for (let i = 0; i < val.length; i += 1) {
mapped.push(fn(val[i]));
}
return mapped;
}
return fn(val);
}
//# sourceMappingURL=utils.js.map