UNPKG

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