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