1
/*
2
 * Copyright 2017-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance with
5
 * the License. A copy of the License is located at
6
 *
7
 *     http://aws.amazon.com/apache2.0/
8
 *
9
 * or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
10
 * CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
11
 * and limitations under the License.
12
 */
13

14 1
const MIME_MAP = [
15
	{ type: 'text/plain', ext: 'txt' },
16
	{ type: 'text/html', ext: 'html' },
17
	{ type: 'text/javascript', ext: 'js' },
18
	{ type: 'text/css', ext: 'css' },
19
	{ type: 'text/csv', ext: 'csv' },
20
	{ type: 'text/yaml', ext: 'yml' },
21
	{ type: 'text/yaml', ext: 'yaml' },
22
	{ type: 'text/calendar', ext: 'ics' },
23
	{ type: 'text/calendar', ext: 'ical' },
24

25
	{ type: 'image/apng', ext: 'apng' },
26
	{ type: 'image/bmp', ext: 'bmp' },
27
	{ type: 'image/gif', ext: 'gif' },
28
	{ type: 'image/x-icon', ext: 'ico' },
29
	{ type: 'image/x-icon', ext: 'cur' },
30
	{ type: 'image/jpeg', ext: 'jpg' },
31
	{ type: 'image/jpeg', ext: 'jpeg' },
32
	{ type: 'image/jpeg', ext: 'jfif' },
33
	{ type: 'image/jpeg', ext: 'pjp' },
34
	{ type: 'image/jpeg', ext: 'pjpeg' },
35
	{ type: 'image/png', ext: 'png' },
36
	{ type: 'image/svg+xml', ext: 'svg' },
37
	{ type: 'image/tiff', ext: 'tif' },
38
	{ type: 'image/tiff', ext: 'tiff' },
39
	{ type: 'image/webp', ext: 'webp' },
40

41
	{ type: 'application/json', ext: 'json' },
42
	{ type: 'application/xml', ext: 'xml' },
43
	{ type: 'application/x-sh', ext: 'sh' },
44
	{ type: 'application/zip', ext: 'zip' },
45
	{ type: 'application/x-rar-compressed', ext: 'rar' },
46
	{ type: 'application/x-tar', ext: 'tar' },
47
	{ type: 'application/x-bzip', ext: 'bz' },
48
	{ type: 'application/x-bzip2', ext: 'bz2' },
49
	{ type: 'application/pdf', ext: 'pdf' },
50
	{ type: 'application/java-archive', ext: 'jar' },
51
	{ type: 'application/msword', ext: 'doc' },
52
	{ type: 'application/vnd.ms-excel', ext: 'xls' },
53
	{ type: 'application/vnd.ms-excel', ext: 'xlsx' },
54

55
	{ type: 'message/rfc822', ext: 'eml' },
56
];
57

58 1
export const isEmpty = (obj = {}) => Object.keys(obj).length === 0;
59

60 1
export const sortByField = (list, field, dir) => {
61 1
	if (!list || !list.sort) {
62 1
		return false;
63
	}
64

65 1
	const dirX = dir && dir === 'desc' ? -1 : 1;
66 1
	list.sort(function(a, b) {
67 1
		const a_val = a[field];
68 1
		const b_val = b[field];
69

70 1
		if (typeof b_val === 'undefined') {
71 1
			return typeof a_val === 'undefined' ? 0 : 1 * dirX;
72
		}
73

74 1
		if (typeof a_val === 'undefined') {
75 1
			return -1 * dirX;
76
		}
77

78 1
		if (a_val < b_val) {
79 1
			return -1 * dirX;
80
		}
81 1
		if (a_val > b_val) {
82 1
			return 1 * dirX;
83
		}
84

85 1
		return 0;
86
	});
87

88 1
	return true;
89
};
90

91 1
export const objectLessAttributes = (obj, less) => {
92 1
	const ret = Object.assign({}, obj);
93 1
	if (less) {
94 1
		if (typeof less === 'string') {
95 1
			delete ret[less];
96
		} else {
97 1
			less.forEach(attr => {
98 1
				delete ret[attr];
99
			});
100
		}
101
	}
102

103 1
	return ret;
104
};
105

106 1
export const filenameToContentType = (
107
	filename,
108 1
	defVal = 'application/octet-stream'
109
) => {
110 1
	const name = filename.toLowerCase();
111

112 1
	const filtered = MIME_MAP.filter(mime => name.endsWith('.' + mime.ext));
113 1
	return filtered.length > 0 ? filtered[0].type : defVal;
114
};
115

116 1
export const isTextFile = contentType => {
117 1
	const type = contentType.toLowerCase();
118 1
	if (type.startsWith('text/')) {
119 1
		return true;
120
	}
121 1
	return (
122 1
		'application/json' === type ||
123
		'application/xml' === type ||
124
		'application/sh' === type
125
	);
126
};
127

128 1
export const generateRandomString = () => {
129 0
	let result = '';
130
	const chars =
131 0
		'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
132 0
	for (let i = 32; i > 0; i -= 1) {
133 0
		result += chars[Math.floor(Math.random() * chars.length)];
134
	}
135 0
	return result;
136
};
137

138 1
export const makeQuerablePromise = promise => {
139 1
	if (promise.isResolved) return promise;
140

141 1
	let isPending = true;
142 1
	let isRejected = false;
143 1
	let isFullfilled = false;
144

145 1
	const result = promise.then(
146
		data => {
147 1
			isFullfilled = true;
148 1
			isPending = false;
149 1
			return data;
150
		},
151
		e => {
152 1
			isRejected = true;
153 1
			isPending = false;
154 1
			throw e;
155
		}
156
	);
157

158 1
	result.isFullfilled = () => isFullfilled;
159 1
	result.isPending = () => isPending;
160 1
	result.isRejected = () => isRejected;
161

162 1
	return result;
163
};
164

165 1
export const isWebWorker = () => {
166 1
	if (typeof self === 'undefined') {
167 0
		return false;
168
	}
169 0
	const selfContext = self as { WorkerGlobalScope? };
170 1
	return typeof selfContext.WorkerGlobalScope !== 'undefined' &&
171
		self instanceof selfContext.WorkerGlobalScope;
172
};
173

174 1
export const browserOrNode = () => {
175
	const isBrowser =
176 1
		typeof window !== 'undefined' && typeof window.document !== 'undefined';
177
	const isNode =
178 1
		typeof process !== 'undefined' &&
179
		process.versions != null &&
180
		process.versions.node != null;
181

182 1
	return {
183
		isBrowser,
184
		isNode,
185
	};
186
};
187

188
/**
189
 * transfer the first letter of the keys to lowercase
190
 * @param {Object} obj - the object need to be transferred
191
 * @param {Array} whiteListForItself - whitelist itself from being transferred
192
 * @param {Array} whiteListForChildren - whitelist its children keys from being transferred
193
 */
194 1
export const transferKeyToLowerCase = (
195
	obj,
196 1
	whiteListForItself = [],
197 1
	whiteListForChildren = []
198
) => {
199 1
	if (!isStrictObject(obj)) return obj;
200 1
	const ret = {};
201

202 1
	for (const key in obj) {
203 1
		if (obj.hasOwnProperty(key)) {
204 1
			const transferedKey = whiteListForItself.includes(key)
205 1
				? key
206
				: key[0].toLowerCase() + key.slice(1);
207

208 1
			ret[transferedKey] = whiteListForChildren.includes(key)
209 1
				? obj[key]
210
				: transferKeyToLowerCase(
211
						obj[key],
212
						whiteListForItself,
213
						whiteListForChildren
214
				  );
215
		}
216
	}
217

218 1
	return ret;
219
};
220

221
/**
222
 * transfer the first letter of the keys to lowercase
223
 * @param {Object} obj - the object need to be transferred
224
 * @param {Array} whiteListForItself - whitelist itself from being transferred
225
 * @param {Array} whiteListForChildren - whitelist its children keys from being transferred
226
 */
227 1
export const transferKeyToUpperCase = (
228
	obj,
229 1
	whiteListForItself = [],
230 1
	whiteListForChildren = []
231
) => {
232 1
	if (!isStrictObject(obj)) return obj;
233 1
	const ret = {};
234

235 1
	for (const key in obj) {
236 1
		if (obj.hasOwnProperty(key)) {
237 1
			const transferredKey = whiteListForItself.includes(key)
238 1
				? key
239
				: key[0].toUpperCase() + key.slice(1);
240

241 1
			ret[transferredKey] = whiteListForChildren.includes(key)
242 1
				? obj[key]
243
				: transferKeyToUpperCase(
244
						obj[key],
245
						whiteListForItself,
246
						whiteListForChildren
247
				  );
248
		}
249
	}
250 1
	return ret;
251
};
252

253
/**
254
 * Return true if the object is a strict object
255
 * which means it's not Array, Function, Number, String, Boolean or Null
256
 * @param obj the Object
257
 */
258 1
export const isStrictObject = obj => {
259 1
	return (
260 1
		obj instanceof Object &&
261
		!(obj instanceof Array) &&
262
		!(obj instanceof Function) &&
263
		!(obj instanceof Number) &&
264
		!(obj instanceof String) &&
265
		!(obj instanceof Boolean)
266
	);
267
};
268

269
/**
270
 * @deprecated use per-function imports
271
 */
272 1
export class JS {
273 1
	static isEmpty = isEmpty;
274 1
	static sortByField = sortByField;
275 1
	static objectLessAttributes = objectLessAttributes;
276 1
	static filenameToContentType = filenameToContentType;
277 1
	static isTextFile = isTextFile;
278 1
	static generateRandomString = generateRandomString;
279 1
	static makeQuerablePromise = makeQuerablePromise;
280 1
	static isWebWorker = isWebWorker;
281 1
	static browserOrNode = browserOrNode;
282 1
	static transferKeyToLowerCase = transferKeyToLowerCase;
283 1
	static transferKeyToUpperCase = transferKeyToUpperCase;
284 1
	static isStrictObject = isStrictObject;
285 1
}
286

287
/**
288
 * @deprecated use per-function imports
289
 */
290 1
export default JS;

Read our documentation on viewing source code .

Loading