UNPKG

19.2 kBJavaScriptView Raw
1/**
2 * Extending object that entered in first argument.
3 *
4 * Returns extended object or false if have no target object or incorrect type.
5 *
6 * If you wish to clone source object (without modify it), just use empty new
7 * object as first argument, like this:
8 * deepExtend({}, yourObj_1, [yourObj_N]);
9 */
10export const deepExtend = function (...objects) {
11 if (arguments.length < 1 || typeof arguments[0] !== 'object') {
12 return false;
13 }
14 if (arguments.length < 2) {
15 return arguments[0];
16 }
17 const target = arguments[0];
18 // convert arguments to array and cut off target object
19 const args = Array.prototype.slice.call(arguments, 1);
20 let val, src;
21 args.forEach(function (obj) {
22 // skip argument if it is array or isn't object
23 if (typeof obj !== 'object' || Array.isArray(obj)) {
24 return;
25 }
26 Object.keys(obj).forEach(function (key) {
27 src = target[key]; // source value
28 val = obj[key]; // new value
29 // recursion prevention
30 if (val === target) {
31 return;
32 /**
33 * if new value isn't object then just overwrite by new value
34 * instead of extending.
35 */
36 }
37 else if (typeof val !== 'object' || val === null) {
38 target[key] = val;
39 return;
40 // just clone arrays (and recursive clone objects inside)
41 }
42 else if (Array.isArray(val)) {
43 target[key] = deepCloneArray(val);
44 return;
45 // custom cloning and overwrite for specific objects
46 }
47 else if (isSpecificValue(val)) {
48 target[key] = cloneSpecificValue(val);
49 return;
50 // overwrite by new value if source isn't object or array
51 }
52 else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
53 target[key] = deepExtend({}, val);
54 return;
55 // source value and new value is objects both, extending...
56 }
57 else {
58 target[key] = deepExtend(src, val);
59 return;
60 }
61 });
62 });
63 return target;
64};
65function isSpecificValue(val) {
66 return (val instanceof Date
67 || val instanceof RegExp) ? true : false;
68}
69function cloneSpecificValue(val) {
70 if (val instanceof Date) {
71 return new Date(val.getTime());
72 }
73 else if (val instanceof RegExp) {
74 return new RegExp(val);
75 }
76 else {
77 throw new Error('cloneSpecificValue: Unexpected situation');
78 }
79}
80/**
81 * Recursive cloning array.
82 */
83function deepCloneArray(arr) {
84 const clone = [];
85 arr.forEach(function (item, index) {
86 if (typeof item === 'object' && item !== null) {
87 if (Array.isArray(item)) {
88 clone[index] = deepCloneArray(item);
89 }
90 else if (isSpecificValue(item)) {
91 clone[index] = cloneSpecificValue(item);
92 }
93 else {
94 clone[index] = deepExtend({}, item);
95 }
96 }
97 else {
98 clone[index] = item;
99 }
100 });
101 return clone;
102}
103// getDeepFromObject({result: {data: 1}}, 'result.data', 2); // returns 1
104export function getDeepFromObject(object = {}, name, defaultValue) {
105 const keys = name.split('.');
106 // clone the object
107 let level = deepExtend({}, object || {});
108 keys.forEach((k) => {
109 if (level && typeof level[k] !== 'undefined') {
110 level = level[k];
111 }
112 else {
113 level = undefined;
114 }
115 });
116 return typeof level === 'undefined' ? defaultValue : level;
117}
118export function urlBase64Decode(str) {
119 let output = str.replace(/-/g, '+').replace(/_/g, '/');
120 switch (output.length % 4) {
121 case 0: {
122 break;
123 }
124 case 2: {
125 output += '==';
126 break;
127 }
128 case 3: {
129 output += '=';
130 break;
131 }
132 default: {
133 throw new Error('Illegal base64url string!');
134 }
135 }
136 return b64DecodeUnicode(output);
137}
138export function b64decode(str) {
139 const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
140 let output = '';
141 str = String(str).replace(/=+$/, '');
142 if (str.length % 4 === 1) {
143 throw new Error(`'atob' failed: The string to be decoded is not correctly encoded.`);
144 }
145 for (
146 // initialize result and counters
147 let bc = 0, bs, buffer, idx = 0;
148 // get next character
149 buffer = str.charAt(idx++);
150 // character found in table? initialize bit storage and add its ascii value;
151 ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
152 // and if not first of each 4 characters,
153 // convert the first 8 bits to one ascii character
154 bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
155 // try to find character in table (0-63, not found => -1)
156 buffer = chars.indexOf(buffer);
157 }
158 return output;
159}
160// https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding#The_Unicode_Problem
161export function b64DecodeUnicode(str) {
162 return decodeURIComponent(Array.prototype.map.call(b64decode(str), (c) => {
163 return '%' + ('00' + c.charCodeAt(0).toString(16)).slice(-2);
164 }).join(''));
165}
166//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uLy4uLy4uL3NyYy9mcmFtZXdvcmsvYXV0aC9oZWxwZXJzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7OztHQVFHO0FBQ0gsTUFBTSxDQUFDLE1BQU0sVUFBVSxHQUFHLFVBQVUsR0FBRyxPQUFjO0lBQ25ELElBQUksU0FBUyxDQUFDLE1BQU0sR0FBRyxDQUFDLElBQUksT0FBTyxTQUFTLENBQUMsQ0FBQyxDQUFDLEtBQUssUUFBUSxFQUFFLENBQUM7UUFDN0QsT0FBTyxLQUFLLENBQUM7SUFDZixDQUFDO0lBRUQsSUFBSSxTQUFTLENBQUMsTUFBTSxHQUFHLENBQUMsRUFBRSxDQUFDO1FBQ3pCLE9BQU8sU0FBUyxDQUFDLENBQUMsQ0FBQyxDQUFDO0lBQ3RCLENBQUM7SUFFRCxNQUFNLE1BQU0sR0FBRyxTQUFTLENBQUMsQ0FBQyxDQUFDLENBQUM7SUFFNUIsdURBQXVEO0lBQ3ZELE1BQU0sSUFBSSxHQUFHLEtBQUssQ0FBQyxTQUFTLENBQUMsS0FBSyxDQUFDLElBQUksQ0FBQyxTQUFTLEVBQUUsQ0FBQyxDQUFDLENBQUM7SUFFdEQsSUFBSSxHQUFHLEVBQUUsR0FBRyxDQUFDO0lBRWIsSUFBSSxDQUFDLE9BQU8sQ0FBQyxVQUFVLEdBQVE7UUFDN0IsK0NBQStDO1FBQy9DLElBQUksT0FBTyxHQUFHLEtBQUssUUFBUSxJQUFJLEtBQUssQ0FBQyxPQUFPLENBQUMsR0FBRyxDQUFDLEVB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