1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | var 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 | ];
|
53 | export var isEmpty = function (obj) {
|
54 | if (obj === void 0) { obj = {}; }
|
55 | return Object.keys(obj).length === 0;
|
56 | };
|
57 | export 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 | };
|
81 | export 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 | };
|
95 | export 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 | };
|
101 | export 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 | };
|
110 | export 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 | };
|
118 | export 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 | };
|
138 | export 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 | };
|
146 | export 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 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 | export 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 |
|
182 |
|
183 |
|
184 |
|
185 |
|
186 | export 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 |
|
206 |
|
207 |
|
208 |
|
209 | export 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 |
|
219 |
|
220 | var JS = (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 | }());
|
237 | export { JS };
|
238 |
|
239 |
|
240 |
|
241 | export default JS;
|
242 |
|
\ | No newline at end of file |