-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14.js
More file actions
422 lines (422 loc) · 11.8 KB
/
Copy path14.js
File metadata and controls
422 lines (422 loc) · 11.8 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var array_1 = require("./array");
var from_1 = require("./from");
var is_1 = require("./is");
var object_1 = require("./object");
var function_1 = require("./function");
var string_1 = require("./string");
function toArray(val, id, def) {
if (is_1.isArray(id)) {
def = id;
id = undefined;
}
if (!is_1.isValue(val))
return toDefault(null, def || []);
if (is_1.isArray(val))
return val;
var ARRAY_LIKE_EXP = /^(.+(,|\||\s).+){1,}$/;
// id = id || '$id';
if (is_1.isPlainObject(val) && id) {
var arr = [];
for (var p in val) {
if (val.hasOwnProperty(p)) {
var cur = val[p];
if (is_1.isPlainObject(cur)) {
var tmp = {};
tmp[id] = p;
var obj = Object.assign({}, cur, tmp);
arr = array_1.push(arr, obj).array;
}
else {
arr = array_1.push(arr, val).array;
}
}
}
return arr;
}
if (is_1.isString(val) && ARRAY_LIKE_EXP.test(val)) {
var arr = string_1.split(val);
return arr;
}
return [val];
}
exports.toArray = toArray;
/**
* To Boolean
* Converts value if not boolean to boolean.
* Will convert 'true', '1', 'yes' or '+' to true.
*
* @param val the value to inspect.
* @param def optional default value on null.
*/
function toBoolean(val, def) {
if (is_1.isBoolean(val))
return val;
if (!is_1.isValue(val))
return toDefault(null, def);
val = val.toString();
return (parseFloat(val) > 0 ||
is_1.isInfinite(val) ||
val === 'true' ||
val === 'yes' ||
val === '1' ||
val === '+');
}
exports.toBoolean = toBoolean;
/**
* To Date
* Converts value to date using Date.parse when string.
* Optionally you can pass a format object containing
* Intl.DateFormatOptions and locales. You may also pass
* the timezone ONLY as a string. In this case locale en-US
* is assumed.
*
* @param val the value to be converted to date.
* @param format date locale format options.
* @param def a default date when null.
*/
function toDate(val, format, def) {
if (is_1.isDate(format)) {
def = format;
format = undefined;
}
var opts = format;
// Date format options a simple timezine
// ex: 'America/Los_Angeles'.
if (is_1.isString(opts)) {
opts = {
timeZone: format
};
}
// This just checks loosely if string is
// date like string, below parse should
// catch majority of scenarios.
function canParse() {
return !/^[0-9]+$/.test(val) &&
(is_1.isString(val) && /[0-9]/g.test(val) &&
/(\.|\/|-|:)/g.test(val));
}
function parseDate() {
var epoch = Date.parse(val);
if (!isNaN(epoch)) {
var date = from_1.fromEpoch(epoch);
if (opts) {
opts.locales = opts.locales || 'en-US';
date = new Date(date.toLocaleString(opts.locales, opts));
}
return date;
}
return toDefault(null, def);
}
if (is_1.isDate(val))
return val;
if (!canParse())
return toDefault(null, def);
return function_1.tryWrap(parseDate)(def);
}
exports.toDate = toDate;
/**
* To Default
* Returns a default value when provided if
* primary value is null or undefined. If neither
* then null is returned.
*
* @param val the value to return if defined.
* @param def an optional default value to be returned.
*/
function toDefault(val, def) {
if (is_1.isValue(val) && !(is_1.isEmpty(val) && !is_1.isEmpty(def)))
return val;
return is_1.isValue(def) ? def : null;
}
exports.toDefault = toDefault;
/**
* To Epoch
* Converts a Date type to an epoch.
*
* @param val the date value to convert.
* @param def optional default value when null.
*/
function toEpoch(val, def) {
return toDefault((is_1.isDate(val) && val.getTime()), def);
}
exports.toEpoch = toEpoch;
/**
* To Float
* Converts value to a float.
*
* @param val the value to convert to float.
*/
function toFloat(val, def) {
if (is_1.isFloat(val))
return val;
if (!is_1.isValue(val))
return toDefault(null, def);
var parsed = function_1.tryWrap(parseFloat, val)(def);
if (is_1.isFloat(parsed) || is_1.isNumber(parsed))
return parsed;
if (toBoolean(val))
return 1;
return 0;
}
exports.toFloat = toFloat;
/**
* To JSON
* Simple wrapper to strinigy using JSON.
*
* @param obj the object to be stringified.
* @param pretty an integer or true for tabs in JSON.
* @param def optional default value on null.
*/
function toJSON(obj, pretty, def) {
if (is_1.isString(pretty)) {
def = pretty;
pretty = undefined;
}
var tabs = 0;
pretty = is_1.isBoolean(pretty) ? 2 : pretty;
tabs = pretty ? pretty : tabs;
if (!is_1.isObject(obj))
return toDefault(null, def);
return function_1.tryWrap(JSON.stringify, obj, null, tabs)(def);
}
exports.toJSON = toJSON;
/**
* To Integer
* Convert value to integer.
*
* @param val the value to convert to integer.
* @param def optional default value on null or error.
*/
function toInteger(val, def) {
if (!is_1.isValue(val))
return toDefault(null, def);
var parsed = function_1.tryWrap(parseInt, val)(def);
if (is_1.isInteger(parsed))
return parsed;
if (toBoolean(val))
return 1;
return 0;
}
exports.toInteger = toInteger;
/**
* To Map
* Converts arrays, strings, to an object literal.
*
* @example
* Array: ['one', 'two', 'three'] Maps To: { 0: 'one', 1: 'two', 2: 'three' }
* String: 'Star Wars' Maps To: { 0: 'Star Wars' }
* String: 'Star Wars, Star Trek' Maps To { 0: 'Star Wars', 1: 'Star Trek' }
* Array: [{ id: '123', name: 'Joe' }] Maps To: { 123: { name: 'Joe' }}
* Array: [{ name: 'Joe' }, { name: 'Amy' }]
* Maps To: { 0: { name: 'Joe' }, 2: { name: 'Amy' }}
*
* NOTE: mixed content arrays not supported.
*
* @param val the value to be mapped.
* @param id optional id key when iterating array of objects.
* @param def optional default value on null or error.
*/
function toMap(val, id, def) {
if (is_1.isValue(id) && !is_1.isString(id)) {
def = id;
id = undefined;
}
if (is_1.isPlainObject(val))
return val;
if (!is_1.isValue(val) || (!is_1.isString(val) && !is_1.isArray(val)))
return toDefault(null, def);
// Default id key.
id = id || '$id';
var exp = /(\/|\.|,|;|\|)/g;
var i = 0;
var obj = {};
if (is_1.isString(val)) {
// simple string.
if (!exp.test(val))
return { 0: val };
// split string into array, iterate.
val = string_1.split(val);
val.forEach(function (v, i) { return obj[i] = v; });
return obj;
}
while (i < val.length) {
if (is_1.isString(val[i])) {
obj[i] = val[i];
}
else if (is_1.isPlainObject(val[i])) {
var itm = Object.assign({}, val[i]);
var key = itm[id] ? itm[id] : i;
obj[key] = itm[id] ? object_1.del(itm, id) : itm;
}
i++;
}
return obj;
}
exports.toMap = toMap;
/**
* To Nested
* Takes an object that was flattened by toUnnested
* and re-nests it.
*
* @param val the flattened object to be nested.
*/
function toNested(val, def) {
function nest(src) {
var dest = {};
for (var p in src) {
if (src.hasOwnProperty(p))
if (/\./g.test(p))
object_1.set(dest, p, src[p]);
else
dest[p] = src[p];
}
return dest;
}
return function_1.tryWrap(nest, val)(def);
}
exports.toNested = toNested;
/**
* To Number
* Converts value to number.
*
* @param val the value to convert to number.
* @param def optional default value on null.
*/
function toNumber(val, def) {
return toFloat(val);
}
exports.toNumber = toNumber;
/**
* To Regular Expression
* Attempts to convert to a regular expression
* from a string.
*
* @param val the value to convert to RegExp.
* @param def optional express as default on null.
*/
function toRegExp(val, def) {
var exp = /^\/.+\/(g|i|m)?([m,i,u,y]{1,4})?/;
var optsExp = /(g|i|m)?([m,i,u,y]{1,4})?$/;
if (is_1.isRegExp(val))
return val;
if (!is_1.isValue(val) || !is_1.isString(val))
return toDefault(null, def);
function regExpFromStr() {
var opts;
if (exp.test(val)) {
opts = optsExp.exec(val)[0];
val = val.replace(/^\//, '').replace(optsExp, '').replace(/\/$/, '');
}
return new RegExp(val, opts);
}
return function_1.tryWrap(regExpFromStr)(def);
}
exports.toRegExp = toRegExp;
/**
* To String
* When not null or undefined calls to string method on object.
*
* @param val the value to convert to string.
* @param def optional default value on null.
*/
function toString(val, def) {
if (is_1.isString(val))
return val;
if (!is_1.isValue(val))
return toDefault(null, def);
function _toString() {
return val.toString();
}
return function_1.tryWrap(_toString)(def);
}
exports.toString = toString;
/**
* To Unnested
* Takes a nested object and flattens it
* to a single level safely. To disable key
* prefixing set prefix to false.
*
* @param val the object to be unnested.
* @param prefix when NOT false parent key is prefixed to children.
* @param def optional default value on null.
*/
function toUnnested(obj, prefix, def) {
if (is_1.isValue(prefix) && !is_1.isBoolean(prefix)) {
def = prefix;
prefix = undefined;
}
var dupes = 0;
function unnest(src, dest, pre) {
dest = dest || {};
for (var p in src) {
if (dupes > 0)
return;
if (src.hasOwnProperty(p)) {
if (is_1.isPlainObject(src[p])) {
var parent = prefix !== false &&
(pre && pre.length) ?
pre + '.' + p : p;
unnest(src[p], dest, parent);
}
else {
var name = prefix !== false &&
pre && pre.length ?
pre + '.' + p : p;
if (dest[name])
dupes += 1;
else
dest[name] = src[p];
}
}
}
if (dupes > 0)
return null;
return dest;
}
return function_1.tryWrap(unnest, object_1.clone(obj))(def);
}
exports.toUnnested = toUnnested;
/**
* To Window
* Adds key to window object if is browser.
*
* @param key the key or object to add to the window object.
* @param val the corresponding value to add to window object.
* @param exclude string or array of keys to exclude.
*/
function toWindow(key, val, exclude) {
/* istanbul ignore if */
if (!is_1.isBrowser())
return;
exclude = toArray(exclude);
var _keys, i;
// key/val was passed.
if (is_1.isString(key)) {
if (!is_1.isPlainObject(val)) {
window[key] = val;
}
else {
var obj = {};
_keys = array_1.keys(val);
i = _keys.length;
while (i--) {
if (!array_1.contains(exclude, _keys[i]))
obj[_keys[i]] = val[_keys[i]];
}
window[key] = obj;
}
}
// object passed to key.
else if (is_1.isPlainObject(key)) {
_keys = array_1.keys(key);
i = _keys.length;
while (i--) {
if (!array_1.contains(exclude, _keys[i]))
window[_keys[i]] = key[_keys[i]];
}
}
}
exports.toWindow = toWindow;
//# sourceMappingURL=to.js.map