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