UNPKG

5.53 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=helpers.js.map
\No newline at end of file