1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | export 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 |
|
19 | const args = Array.prototype.slice.call(arguments, 1);
|
20 | let val, src;
|
21 | args.forEach(function (obj) {
|
22 |
|
23 | if (typeof obj !== 'object' || Array.isArray(obj)) {
|
24 | return;
|
25 | }
|
26 | Object.keys(obj).forEach(function (key) {
|
27 | src = target[key];
|
28 | val = obj[key];
|
29 |
|
30 | if (val === target) {
|
31 | return;
|
32 | |
33 |
|
34 |
|
35 |
|
36 | }
|
37 | else if (typeof val !== 'object' || val === null) {
|
38 | target[key] = val;
|
39 | return;
|
40 |
|
41 | }
|
42 | else if (Array.isArray(val)) {
|
43 | target[key] = deepCloneArray(val);
|
44 | return;
|
45 |
|
46 | }
|
47 | else if (isSpecificValue(val)) {
|
48 | target[key] = cloneSpecificValue(val);
|
49 | return;
|
50 |
|
51 | }
|
52 | else if (typeof src !== 'object' || src === null || Array.isArray(src)) {
|
53 | target[key] = deepExtend({}, val);
|
54 | return;
|
55 |
|
56 | }
|
57 | else {
|
58 | target[key] = deepExtend(src, val);
|
59 | return;
|
60 | }
|
61 | });
|
62 | });
|
63 | return target;
|
64 | };
|
65 | function isSpecificValue(val) {
|
66 | return (val instanceof Date
|
67 | || val instanceof RegExp) ? true : false;
|
68 | }
|
69 | function 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 |
|
82 |
|
83 | function 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 |
|
104 | export function getDeepFromObject(object = {}, name, defaultValue) {
|
105 | const keys = name.split('.');
|
106 |
|
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 | }
|
118 | export 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 | }
|
138 | export 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 |
|
147 | let bc = 0, bs, buffer, idx = 0;
|
148 |
|
149 | buffer = str.charAt(idx++);
|
150 |
|
151 | ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
|
152 |
|
153 |
|
154 | bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0) {
|
155 |
|
156 | buffer = chars.indexOf(buffer);
|
157 | }
|
158 | return output;
|
159 | }
|
160 |
|
161 | export 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 |
|
\ | No newline at end of file |