@lowdefy/helpers 3.23.0 → 4.0.0-alpha.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/applyArrayIndices.js +15 -30
- package/dist/cachedPromises.js +27 -0
- package/dist/get.js +86 -121
- package/dist/index.js +27 -95
- package/dist/mergeObjects.js +11 -25
- package/dist/omit.js +7 -20
- package/dist/serializer.js +134 -162
- package/dist/set.js +80 -124
- package/dist/stableStringify.js +68 -102
- package/dist/swap.js +7 -21
- package/dist/type.js +171 -240
- package/dist/unset.js +67 -102
- package/dist/urlQuery.js +29 -44
- package/package.json +14 -13
package/dist/serializer.js
CHANGED
|
@@ -1,173 +1,145 @@
|
|
|
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
|
-
return
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
return item;
|
|
65
|
-
});
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
return newValue;
|
|
69
|
-
};
|
|
70
|
-
|
|
71
|
-
var makeReviver = customReviver => (key, value) => {
|
|
72
|
-
var newValue = value;
|
|
73
|
-
|
|
74
|
-
if (customReviver) {
|
|
75
|
-
newValue = customReviver(key, value);
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
if (_type.default.isObject(newValue) && !_type.default.isUndefined(newValue._error)) {
|
|
79
|
-
var error = new Error(newValue._error.message);
|
|
80
|
-
error.name = newValue._error.name;
|
|
81
|
-
return error;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
if (_type.default.isObject(newValue) && !_type.default.isUndefined(newValue._date)) {
|
|
85
|
-
if (_type.default.isInt(newValue._date)) {
|
|
86
|
-
return new Date(newValue._date);
|
|
1
|
+
/* eslint-disable no-param-reassign */ /*
|
|
2
|
+
Copyright 2020-2021 Lowdefy, Inc
|
|
3
|
+
|
|
4
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
5
|
+
you may not use this file except in compliance with the License.
|
|
6
|
+
You may obtain a copy of the License at
|
|
7
|
+
|
|
8
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
9
|
+
|
|
10
|
+
Unless required by applicable law or agreed to in writing, software
|
|
11
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
12
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13
|
+
See the License for the specific language governing permissions and
|
|
14
|
+
limitations under the License.
|
|
15
|
+
*/ import type from './type.js';
|
|
16
|
+
import stableStringify from './stableStringify.js';
|
|
17
|
+
const makeReplacer = (customReplacer, isoStringDates)=>(key, value)=>{
|
|
18
|
+
let dateReplacer = (date)=>({
|
|
19
|
+
_date: date.valueOf()
|
|
20
|
+
})
|
|
21
|
+
;
|
|
22
|
+
if (isoStringDates) {
|
|
23
|
+
dateReplacer = (date)=>({
|
|
24
|
+
_date: date.toISOString()
|
|
25
|
+
})
|
|
26
|
+
;
|
|
27
|
+
}
|
|
28
|
+
let newValue = value;
|
|
29
|
+
if (customReplacer) {
|
|
30
|
+
newValue = customReplacer(key, value);
|
|
31
|
+
}
|
|
32
|
+
if (type.isError(newValue)) {
|
|
33
|
+
return {
|
|
34
|
+
_error: {
|
|
35
|
+
name: newValue.name,
|
|
36
|
+
message: newValue.message,
|
|
37
|
+
value: newValue.toString()
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (type.isObject(newValue)) {
|
|
42
|
+
Object.keys(newValue).forEach((k)=>{
|
|
43
|
+
if (type.isDate(newValue[k])) {
|
|
44
|
+
// shallow copy original value before reassigning a value in order not to mutate original value
|
|
45
|
+
newValue = {
|
|
46
|
+
...newValue
|
|
47
|
+
};
|
|
48
|
+
newValue[k] = dateReplacer(newValue[k]);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
return newValue;
|
|
52
|
+
}
|
|
53
|
+
if (type.isArray(newValue)) {
|
|
54
|
+
return newValue.map((item)=>{
|
|
55
|
+
if (type.isDate(item)) {
|
|
56
|
+
return dateReplacer(item);
|
|
57
|
+
}
|
|
58
|
+
return item;
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
return newValue;
|
|
87
62
|
}
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
63
|
+
;
|
|
64
|
+
const makeReviver = (customReviver)=>(key, value)=>{
|
|
65
|
+
let newValue = value;
|
|
66
|
+
if (customReviver) {
|
|
67
|
+
newValue = customReviver(key, value);
|
|
68
|
+
}
|
|
69
|
+
if (type.isObject(newValue) && !type.isUndefined(newValue._error)) {
|
|
70
|
+
const error = new Error(newValue._error.message);
|
|
71
|
+
error.name = newValue._error.name;
|
|
72
|
+
return error;
|
|
73
|
+
}
|
|
74
|
+
if (type.isObject(newValue) && !type.isUndefined(newValue._date)) {
|
|
75
|
+
if (type.isInt(newValue._date)) {
|
|
76
|
+
return new Date(newValue._date);
|
|
77
|
+
}
|
|
78
|
+
if (newValue._date === 'now') {
|
|
79
|
+
return newValue;
|
|
80
|
+
}
|
|
81
|
+
const result = new Date(newValue._date);
|
|
82
|
+
if (!type.isDate(result)) {
|
|
83
|
+
return newValue;
|
|
84
|
+
}
|
|
85
|
+
return result;
|
|
86
|
+
}
|
|
87
|
+
return newValue;
|
|
91
88
|
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
if (
|
|
96
|
-
|
|
89
|
+
;
|
|
90
|
+
const serialize = (json, options = {
|
|
91
|
+
})=>{
|
|
92
|
+
if (type.isUndefined(json)) return json;
|
|
93
|
+
if (type.isDate(json)) {
|
|
94
|
+
if (options.isoStringDates) {
|
|
95
|
+
return {
|
|
96
|
+
_date: json.toISOString()
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
return {
|
|
100
|
+
_date: json.valueOf()
|
|
101
|
+
};
|
|
97
102
|
}
|
|
98
|
-
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return newValue;
|
|
103
|
+
return JSON.parse(JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates)));
|
|
103
104
|
};
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
_date: json.toISOString()
|
|
113
|
-
};
|
|
105
|
+
const serializeToString = (json, options = {
|
|
106
|
+
})=>{
|
|
107
|
+
if (type.isUndefined(json)) return json;
|
|
108
|
+
if (type.isDate(json)) {
|
|
109
|
+
if (options.isoStringDates) {
|
|
110
|
+
return `{ "_date": "${json.toISOString()}" }`;
|
|
111
|
+
}
|
|
112
|
+
return `{ "_date": ${json.valueOf()} }`;
|
|
114
113
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return JSON.parse(JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates)));
|
|
122
|
-
};
|
|
123
|
-
|
|
124
|
-
var serializeToString = function serializeToString(json) {
|
|
125
|
-
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
126
|
-
if (_type.default.isUndefined(json)) return json;
|
|
127
|
-
|
|
128
|
-
if (_type.default.isDate(json)) {
|
|
129
|
-
if (options.isoStringDates) {
|
|
130
|
-
return "{ \"_date\": \"".concat(json.toISOString(), "\" }");
|
|
114
|
+
if (options.stable) {
|
|
115
|
+
return stableStringify(json, {
|
|
116
|
+
replacer: makeReplacer(options.replacer),
|
|
117
|
+
space: options.space
|
|
118
|
+
});
|
|
131
119
|
}
|
|
132
|
-
|
|
133
|
-
return "{ \"_date\": ".concat(json.valueOf(), " }");
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
if (options.stable) {
|
|
137
|
-
return (0, _stableStringify.default)(json, {
|
|
138
|
-
replacer: makeReplacer(options.replacer),
|
|
139
|
-
space: options.space
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
return JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates), options.space);
|
|
120
|
+
return JSON.stringify(json, makeReplacer(options.replacer, options.isoStringDates), options.space);
|
|
144
121
|
};
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
return JSON.parse(JSON.stringify(json), makeReviver(options.reviver));
|
|
122
|
+
const deserialize = (json, options = {
|
|
123
|
+
})=>{
|
|
124
|
+
if (type.isUndefined(json)) return json;
|
|
125
|
+
return JSON.parse(JSON.stringify(json), makeReviver(options.reviver));
|
|
150
126
|
};
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
return JSON.parse(str, makeReviver(options.reviver));
|
|
127
|
+
const deserializeFromString = (str, options = {
|
|
128
|
+
})=>{
|
|
129
|
+
if (type.isUndefined(str)) return str;
|
|
130
|
+
return JSON.parse(str, makeReviver(options.reviver));
|
|
156
131
|
};
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
return JSON.parse(JSON.stringify(json, makeReplacer(options.replacer)), makeReviver(options.reviver));
|
|
132
|
+
const copy = (json, options = {
|
|
133
|
+
})=>{
|
|
134
|
+
if (type.isUndefined(json)) return undefined;
|
|
135
|
+
if (type.isDate(json)) return new Date(json.valueOf());
|
|
136
|
+
return JSON.parse(JSON.stringify(json, makeReplacer(options.replacer)), makeReviver(options.reviver));
|
|
163
137
|
};
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
deserializeFromString
|
|
138
|
+
const serializer = {
|
|
139
|
+
copy,
|
|
140
|
+
serialize,
|
|
141
|
+
serializeToString,
|
|
142
|
+
deserialize,
|
|
143
|
+
deserializeFromString
|
|
171
144
|
};
|
|
172
|
-
|
|
173
|
-
exports.default = _default;
|
|
145
|
+
export default serializer;
|
package/dist/set.js
CHANGED
|
@@ -1,22 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.split = split;
|
|
7
|
-
exports.default = void 0;
|
|
8
|
-
|
|
9
|
-
var _type = _interopRequireDefault(require("./type"));
|
|
10
|
-
|
|
11
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
-
|
|
13
|
-
/* eslint-disable no-plusplus */
|
|
14
|
-
|
|
15
|
-
/* eslint-disable no-use-before-define */
|
|
16
|
-
|
|
17
|
-
/* eslint-disable no-param-reassign */
|
|
18
|
-
|
|
19
|
-
/*
|
|
1
|
+
/* eslint-disable no-plusplus */ /* eslint-disable no-use-before-define */ /* eslint-disable no-param-reassign */ /*
|
|
20
2
|
Copyright 2020-2021 Lowdefy, Inc
|
|
21
3
|
|
|
22
4
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
@@ -30,8 +12,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
30
12
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
31
13
|
See the License for the specific language governing permissions and
|
|
32
14
|
limitations under the License.
|
|
33
|
-
*/
|
|
34
|
-
// Derived from source:
|
|
15
|
+
*/ // Derived from source:
|
|
35
16
|
// https://github.com/jonschlinkert/set-value/blob/master/index.js
|
|
36
17
|
// https://www.npmjs.com/package/set-value
|
|
37
18
|
// The MIT License (MIT)
|
|
@@ -51,119 +32,94 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
51
32
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
52
33
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
53
34
|
// THE SOFTWARE.
|
|
35
|
+
import type from './type.js';
|
|
54
36
|
function isValidKey(key) {
|
|
55
|
-
|
|
37
|
+
return key !== '__proto__' && key !== 'constructor' && key !== 'prototype';
|
|
56
38
|
}
|
|
57
|
-
|
|
58
39
|
function isObjectOrFunction(val) {
|
|
59
|
-
|
|
40
|
+
return val !== null && (typeof val === 'object' || typeof val === 'function');
|
|
60
41
|
}
|
|
61
|
-
|
|
62
42
|
function result(target, path, value, merge) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
set.memo = {
|
|
72
|
-
|
|
43
|
+
if (merge && isObjectOrFunction(target[path]) && isObjectOrFunction(value)) {
|
|
44
|
+
target[path] = merge({
|
|
45
|
+
}, target[path], value);
|
|
46
|
+
} else {
|
|
47
|
+
target[path] = value;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
// eslint-disable-next-line no-use-before-define
|
|
51
|
+
set.memo = {
|
|
52
|
+
};
|
|
73
53
|
function set(target, path, value, options) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
var opts = options || {};
|
|
79
|
-
|
|
80
|
-
if (!_type.default.isArray(path) && !_type.default.isString(path)) {
|
|
81
|
-
return target;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
var {
|
|
85
|
-
merge
|
|
86
|
-
} = opts;
|
|
87
|
-
|
|
88
|
-
if (merge && typeof merge !== 'function') {
|
|
89
|
-
merge = Object.assign;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
var keys = (_type.default.isArray(path) ? path : split(path, opts)).filter(isValidKey);
|
|
93
|
-
var len = keys.length;
|
|
94
|
-
var orig = target;
|
|
95
|
-
|
|
96
|
-
if (!options && keys.length === 1) {
|
|
97
|
-
result(target, keys[0], value, merge);
|
|
98
|
-
return target;
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
for (var i = 0; i < len; i++) {
|
|
102
|
-
var prop = keys[i];
|
|
103
|
-
var propUp = keys[i + 1]; // changed to set an array value where the array was undefined be assigning value
|
|
104
|
-
|
|
105
|
-
if (!isObjectOrFunction(target[prop]) && !_type.default.isInt(parseInt(propUp, 10))) {
|
|
106
|
-
// changed
|
|
107
|
-
target[prop] = {};
|
|
108
|
-
} else if (!isObjectOrFunction(target[prop]) && _type.default.isInt(parseInt(propUp, 10))) {
|
|
109
|
-
// added
|
|
110
|
-
target[prop] = []; // added
|
|
54
|
+
if (!type.isObject(target)) {
|
|
55
|
+
return target;
|
|
111
56
|
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
57
|
+
const opts = options || {
|
|
58
|
+
};
|
|
59
|
+
if (!type.isArray(path) && !type.isString(path)) {
|
|
60
|
+
return target;
|
|
116
61
|
}
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
62
|
+
let { merge } = opts;
|
|
63
|
+
if (merge && typeof merge !== 'function') {
|
|
64
|
+
merge = Object.assign;
|
|
65
|
+
}
|
|
66
|
+
const keys = (type.isArray(path) ? path : split(path, opts)).filter(isValidKey);
|
|
67
|
+
const len = keys.length;
|
|
68
|
+
const orig = target;
|
|
69
|
+
if (!options && keys.length === 1) {
|
|
70
|
+
result(target, keys[0], value, merge);
|
|
71
|
+
return target;
|
|
72
|
+
}
|
|
73
|
+
for(let i = 0; i < len; i++){
|
|
74
|
+
const prop = keys[i];
|
|
75
|
+
const propUp = keys[i + 1]; // changed to set an array value where the array was undefined be assigning value
|
|
76
|
+
if (!isObjectOrFunction(target[prop]) && !type.isInt(parseInt(propUp, 10))) {
|
|
77
|
+
// changed
|
|
78
|
+
target[prop] = {
|
|
79
|
+
};
|
|
80
|
+
} else if (!isObjectOrFunction(target[prop]) && type.isInt(parseInt(propUp, 10))) {
|
|
81
|
+
// added
|
|
82
|
+
target[prop] = []; // added
|
|
83
|
+
}
|
|
84
|
+
if (i === len - 1) {
|
|
85
|
+
result(target, prop, value, merge);
|
|
86
|
+
break;
|
|
87
|
+
}
|
|
88
|
+
target = target[prop];
|
|
89
|
+
}
|
|
90
|
+
return orig;
|
|
122
91
|
}
|
|
123
|
-
|
|
124
92
|
function createKey(pattern, options) {
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
id += ";".concat(key, "=").concat(String(options[key]));
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
return id;
|
|
93
|
+
let id = pattern;
|
|
94
|
+
if (typeof options === 'undefined') {
|
|
95
|
+
return `${id}`;
|
|
96
|
+
}
|
|
97
|
+
const keys = Object.keys(options);
|
|
98
|
+
for(let i = 0; i < keys.length; i++){
|
|
99
|
+
const key = keys[i];
|
|
100
|
+
id += `;${key}=${String(options[key])}`;
|
|
101
|
+
}
|
|
102
|
+
return id;
|
|
139
103
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
} else {
|
|
151
|
-
keys = path.split(char);
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
for (var i = 0; i < keys.length; i++) {
|
|
155
|
-
var prop = keys[i];
|
|
156
|
-
|
|
157
|
-
while (prop && prop.slice(-1) === '\\' && keys[i + 1]) {
|
|
158
|
-
prop = prop.slice(0, -1) + char + keys[++i];
|
|
104
|
+
export function split(path, options) {
|
|
105
|
+
const id = createKey(path, options);
|
|
106
|
+
if (set.memo[id]) return set.memo[id];
|
|
107
|
+
const char = options && options.separator ? options.separator : '.';
|
|
108
|
+
let keys = [];
|
|
109
|
+
const res = [];
|
|
110
|
+
if (options && typeof options.split === 'function') {
|
|
111
|
+
keys = options.split(path);
|
|
112
|
+
} else {
|
|
113
|
+
keys = path.split(char);
|
|
159
114
|
}
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
115
|
+
for(let i = 0; i < keys.length; i++){
|
|
116
|
+
let prop = keys[i];
|
|
117
|
+
while(prop && prop.slice(-1) === '\\' && keys[i + 1]){
|
|
118
|
+
prop = prop.slice(0, -1) + char + keys[++i];
|
|
119
|
+
}
|
|
120
|
+
res.push(prop);
|
|
121
|
+
}
|
|
122
|
+
set.memo[id] = res;
|
|
123
|
+
return res;
|
|
166
124
|
}
|
|
167
|
-
|
|
168
|
-
var _default = set;
|
|
169
|
-
exports.default = _default;
|
|
125
|
+
export default set;
|