@whitesev/utils 1.1.2 → 1.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.amd.js CHANGED
@@ -1,7 +1,6 @@
1
1
  define((function () { 'use strict';
2
2
 
3
3
  class ColorConversion {
4
- constructor() { }
5
4
  /**
6
5
  * 判断是否是16进制颜色
7
6
  * @param str
@@ -197,25 +196,23 @@ define((function () { 'use strict';
197
196
  }
198
197
  /**
199
198
  * 解码
200
- * @param {string} str
199
+ * @param str
201
200
  */
202
201
  decode(str) {
203
202
  var GBKMatcher = /%[0-9A-F]{2}%[0-9A-F]{2}/;
204
203
  var UTFMatcher = /%[0-9A-F]{2}/;
205
- var gbk = true, utf = true;
204
+ var utf = true;
205
+ let that = this;
206
206
  while (utf) {
207
- // @ts-ignore
208
- gbk = str.match(GBKMatcher);
209
- // @ts-ignore
210
- utf = str.match(UTFMatcher);
211
- // @ts-ignore
212
- if (gbk && gbk in G2Uhash) {
213
- // @ts-ignore
214
- str = str.replace(gbk, String.fromCharCode("0x" + G2Uhash[gbk]));
207
+ let gbkMatch = str.match(GBKMatcher);
208
+ let utfMatch = str.match(UTFMatcher);
209
+ utf = Boolean(utfMatch);
210
+ if (gbkMatch && gbkMatch in that.#G2Uhash) {
211
+ str = str.replace(gbkMatch, String.fromCharCode(("0x" + that.#G2Uhash[gbkMatch])));
215
212
  }
216
213
  else {
217
214
  // @ts-ignore
218
- str = str.replace(utf, decodeURIComponent(utf));
215
+ str = str.replace(utfMatch, decodeURIComponent(utfMatch));
219
216
  }
220
217
  }
221
218
  return str;
@@ -262,17 +259,51 @@ define((function () { 'use strict';
262
259
 
263
260
  class UtilsGMCookie {
264
261
  /**
265
- * 获取Cookie
262
+ * 获取单个cookie
263
+ * @param cookieName cookie名
264
+ */
265
+ get(cookieName) {
266
+ if (typeof cookieName !== "string") {
267
+ throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
268
+ }
269
+ let cookies = document.cookie.split(";");
270
+ let findCookie = cookies.find((item) => {
271
+ item = item.trimStart();
272
+ let itemName = item.split("=")[0];
273
+ let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
274
+ if (itemName === cookieName) {
275
+ return itemValue;
276
+ }
277
+ });
278
+ if (findCookie) {
279
+ return {
280
+ domain: globalThis.location.hostname,
281
+ expirationDate: null,
282
+ hostOnly: true,
283
+ httpOnly: false,
284
+ name: cookieName,
285
+ path: "/",
286
+ sameSite: "unspecified",
287
+ secure: true,
288
+ session: false,
289
+ value: findCookie,
290
+ };
291
+ }
292
+ }
293
+ /**
294
+ * 获取多组Cookie
266
295
  * @param paramDetails
267
296
  * @param callback
268
297
  * + cookies object[]
269
298
  * + error string|undefined
270
299
  **/
271
- list(paramDetails = {}, callback = (resultData, error) => { }) {
300
+ list(paramDetails, callback) {
301
+ if (paramDetails == null) {
302
+ throw new Error("Utils.GMCookie.list 参数不能为空");
303
+ }
272
304
  let resultData = [];
273
305
  try {
274
306
  let details = {
275
- // @ts-ignore
276
307
  url: globalThis.location.href,
277
308
  domain: globalThis.location.hostname,
278
309
  name: "",
@@ -300,14 +331,58 @@ define((function () { 'use strict';
300
331
  session: false,
301
332
  value: itemValue,
302
333
  });
303
- return;
304
334
  }
305
335
  });
306
- callback(resultData);
336
+ if (typeof callback === "function") {
337
+ callback(resultData);
338
+ }
307
339
  }
308
340
  catch (error) {
309
- callback(resultData, error);
341
+ if (typeof callback === "function") {
342
+ callback(resultData, error);
343
+ }
344
+ }
345
+ }
346
+ /**
347
+ * 获取多组Cookie
348
+ * @param paramDetails
349
+ **/
350
+ getList(paramDetails) {
351
+ if (paramDetails == null) {
352
+ throw new Error("Utils.GMCookie.list 参数不能为空");
310
353
  }
354
+ let resultData = [];
355
+ let details = {
356
+ url: globalThis.location.href,
357
+ domain: globalThis.location.hostname,
358
+ name: "",
359
+ path: "/",
360
+ };
361
+ details = utils.assign(details, paramDetails);
362
+ let cookies = document.cookie.split(";");
363
+ cookies.forEach((item) => {
364
+ item = item.trimStart();
365
+ let itemName = item.split("=")[0];
366
+ let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
367
+ let nameRegexp = details.name instanceof RegExp
368
+ ? details.name
369
+ : new RegExp("^" + details.name, "g");
370
+ if (itemName.match(nameRegexp)) {
371
+ resultData.push({
372
+ domain: globalThis.location.hostname,
373
+ expirationDate: null,
374
+ hostOnly: true,
375
+ httpOnly: false,
376
+ name: itemName,
377
+ path: "/",
378
+ sameSite: "unspecified",
379
+ secure: true,
380
+ session: false,
381
+ value: itemValue,
382
+ });
383
+ }
384
+ });
385
+ return resultData;
311
386
  }
312
387
  /**
313
388
  * 设置Cookie