UNPKG

8.71 kBJavaScriptView Raw
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 */
13var MIME_MAP = [
14 { type: 'text/plain', ext: 'txt' },
15 { type: 'text/html', ext: 'html' },
16 { type: 'text/javascript', ext: 'js' },
17 { type: 'text/css', ext: 'css' },
18 { type: 'text/csv', ext: 'csv' },
19 { type: 'text/yaml', ext: 'yml' },
20 { type: 'text/yaml', ext: 'yaml' },
21 { type: 'text/calendar', ext: 'ics' },
22 { type: 'text/calendar', ext: 'ical' },
23 { type: 'image/apng', ext: 'apng' },
24 { type: 'image/bmp', ext: 'bmp' },
25 { type: 'image/gif', ext: 'gif' },
26 { type: 'image/x-icon', ext: 'ico' },
27 { type: 'image/x-icon', ext: 'cur' },
28 { type: 'image/jpeg', ext: 'jpg' },
29 { type: 'image/jpeg', ext: 'jpeg' },
30 { type: 'image/jpeg', ext: 'jfif' },
31 { type: 'image/jpeg', ext: 'pjp' },
32 { type: 'image/jpeg', ext: 'pjpeg' },
33 { type: 'image/png', ext: 'png' },
34 { type: 'image/svg+xml', ext: 'svg' },
35 { type: 'image/tiff', ext: 'tif' },
36 { type: 'image/tiff', ext: 'tiff' },
37 { type: 'image/webp', ext: 'webp' },
38 { type: 'application/json', ext: 'json' },
39 { type: 'application/xml', ext: 'xml' },
40 { type: 'application/x-sh', ext: 'sh' },
41 { type: 'application/zip', ext: 'zip' },
42 { type: 'application/x-rar-compressed', ext: 'rar' },
43 { type: 'application/x-tar', ext: 'tar' },
44 { type: 'application/x-bzip', ext: 'bz' },
45 { type: 'application/x-bzip2', ext: 'bz2' },
46 { type: 'application/pdf', ext: 'pdf' },
47 { type: 'application/java-archive', ext: 'jar' },
48 { type: 'application/msword', ext: 'doc' },
49 { type: 'application/vnd.ms-excel', ext: 'xls' },
50 { type: 'application/vnd.ms-excel', ext: 'xlsx' },
51 { type: 'message/rfc822', ext: 'eml' },
52];
53export var isEmpty = function (obj) {
54 if (obj === void 0) { obj = {}; }
55 return Object.keys(obj).length === 0;
56};
57export var sortByField = function (list, field, dir) {
58 if (!list || !list.sort) {
59 return false;
60 }
61 var dirX = dir && dir === 'desc' ? -1 : 1;
62 list.sort(function (a, b) {
63 var a_val = a[field];
64 var b_val = b[field];
65 if (typeof b_val === 'undefined') {
66 return typeof a_val === 'undefined' ? 0 : 1 * dirX;
67 }
68 if (typeof a_val === 'undefined') {
69 return -1 * dirX;
70 }
71 if (a_val < b_val) {
72 return -1 * dirX;
73 }
74 if (a_val > b_val) {
75 return 1 * dirX;
76 }
77 return 0;
78 });
79 return true;
80};
81export var objectLessAttributes = function (obj, less) {
82 var ret = Object.assign({}, obj);
83 if (less) {
84 if (typeof less === 'string') {
85 delete ret[less];
86 }
87 else {
88 less.forEach(function (attr) {
89 delete ret[attr];
90 });
91 }
92 }
93 return ret;
94};
95export var filenameToContentType = function (filename, defVal) {
96 if (defVal === void 0) { defVal = 'application/octet-stream'; }
97 var name = filename.toLowerCase();
98 var filtered = MIME_MAP.filter(function (mime) { return name.endsWith('.' + mime.ext); });
99 return filtered.length > 0 ? filtered[0].type : defVal;
100};
101export var isTextFile = function (contentType) {
102 var type = contentType.toLowerCase();
103 if (type.startsWith('text/')) {
104 return true;
105 }
106 return ('application/json' === type ||
107 'application/xml' === type ||
108 'application/sh' === type);
109};
110export var generateRandomString = function () {
111 var result = '';
112 var chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
113 for (var i = 32; i > 0; i -= 1) {
114 result += chars[Math.floor(Math.random() * chars.length)];
115 }
116 return result;
117};
118export var makeQuerablePromise = function (promise) {
119 if (promise.isResolved)
120 return promise;
121 var isPending = true;
122 var isRejected = false;
123 var isFullfilled = false;
124 var result = promise.then(function (data) {
125 isFullfilled = true;
126 isPending = false;
127 return data;
128 }, function (e) {
129 isRejected = true;
130 isPending = false;
131 throw e;
132 });
133 result.isFullfilled = function () { return isFullfilled; };
134 result.isPending = function () { return isPending; };
135 result.isRejected = function () { return isRejected; };
136 return result;
137};
138export var isWebWorker = function () {
139 if (typeof self === 'undefined') {
140 return false;
141 }
142 var selfContext = self;
143 return typeof selfContext.WorkerGlobalScope !== 'undefined' &&
144 self instanceof selfContext.WorkerGlobalScope;
145};
146export var browserOrNode = function () {
147 var isBrowser = typeof window !== 'undefined' && typeof window.document !== 'undefined';
148 var isNode = typeof process !== 'undefined' &&
149 process.versions != null &&
150 process.versions.node != null;
151 return {
152 isBrowser: isBrowser,
153 isNode: isNode,
154 };
155};
156/**
157 * transfer the first letter of the keys to lowercase
158 * @param {Object} obj - the object need to be transferred
159 * @param {Array} whiteListForItself - whitelist itself from being transferred
160 * @param {Array} whiteListForChildren - whitelist its children keys from being transferred
161 */
162export var transferKeyToLowerCase = function (obj, whiteListForItself, whiteListForChildren) {
163 if (whiteListForItself === void 0) { whiteListForItself = []; }
164 if (whiteListForChildren === void 0) { whiteListForChildren = []; }
165 if (!isStrictObject(obj))
166 return obj;
167 var ret = {};
168 for (var key in obj) {
169 if (obj.hasOwnProperty(key)) {
170 var transferedKey = whiteListForItself.includes(key)
171 ? key
172 : key[0].toLowerCase() + key.slice(1);
173 ret[transferedKey] = whiteListForChildren.includes(key)
174 ? obj[key]
175 : transferKeyToLowerCase(obj[key], whiteListForItself, whiteListForChildren);
176 }
177 }
178 return ret;
179};
180/**
181 * transfer the first letter of the keys to lowercase
182 * @param {Object} obj - the object need to be transferred
183 * @param {Array} whiteListForItself - whitelist itself from being transferred
184 * @param {Array} whiteListForChildren - whitelist its children keys from being transferred
185 */
186export var transferKeyToUpperCase = function (obj, whiteListForItself, whiteListForChildren) {
187 if (whiteListForItself === void 0) { whiteListForItself = []; }
188 if (whiteListForChildren === void 0) { whiteListForChildren = []; }
189 if (!isStrictObject(obj))
190 return obj;
191 var ret = {};
192 for (var key in obj) {
193 if (obj.hasOwnProperty(key)) {
194 var transferredKey = whiteListForItself.includes(key)
195 ? key
196 : key[0].toUpperCase() + key.slice(1);
197 ret[transferredKey] = whiteListForChildren.includes(key)
198 ? obj[key]
199 : transferKeyToUpperCase(obj[key], whiteListForItself, whiteListForChildren);
200 }
201 }
202 return ret;
203};
204/**
205 * Return true if the object is a strict object
206 * which means it's not Array, Function, Number, String, Boolean or Null
207 * @param obj the Object
208 */
209export var isStrictObject = function (obj) {
210 return (obj instanceof Object &&
211 !(obj instanceof Array) &&
212 !(obj instanceof Function) &&
213 !(obj instanceof Number) &&
214 !(obj instanceof String) &&
215 !(obj instanceof Boolean));
216};
217/**
218 * @deprecated use per-function imports
219 */
220var JS = /** @class */ (function () {
221 function JS() {
222 }
223 JS.isEmpty = isEmpty;
224 JS.sortByField = sortByField;
225 JS.objectLessAttributes = objectLessAttributes;
226 JS.filenameToContentType = filenameToContentType;
227 JS.isTextFile = isTextFile;
228 JS.generateRandomString = generateRandomString;
229 JS.makeQuerablePromise = makeQuerablePromise;
230 JS.isWebWorker = isWebWorker;
231 JS.browserOrNode = browserOrNode;
232 JS.transferKeyToLowerCase = transferKeyToLowerCase;
233 JS.transferKeyToUpperCase = transferKeyToUpperCase;
234 JS.isStrictObject = isStrictObject;
235 return JS;
236}());
237export { JS };
238/**
239 * @deprecated use per-function imports
240 */
241export default JS;
242//# sourceMappingURL=JS.js.map
\No newline at end of file