@whitesev/utils 1.1.3 → 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.
@@ -262,17 +262,51 @@ System.register('Utils', [], (function (exports) {
262
262
 
263
263
  class UtilsGMCookie {
264
264
  /**
265
- * 获取Cookie
265
+ * 获取单个cookie
266
+ * @param cookieName cookie名
267
+ */
268
+ get(cookieName) {
269
+ if (typeof cookieName !== "string") {
270
+ throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
271
+ }
272
+ let cookies = document.cookie.split(";");
273
+ let findCookie = cookies.find((item) => {
274
+ item = item.trimStart();
275
+ let itemName = item.split("=")[0];
276
+ let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
277
+ if (itemName === cookieName) {
278
+ return itemValue;
279
+ }
280
+ });
281
+ if (findCookie) {
282
+ return {
283
+ domain: globalThis.location.hostname,
284
+ expirationDate: null,
285
+ hostOnly: true,
286
+ httpOnly: false,
287
+ name: cookieName,
288
+ path: "/",
289
+ sameSite: "unspecified",
290
+ secure: true,
291
+ session: false,
292
+ value: findCookie,
293
+ };
294
+ }
295
+ }
296
+ /**
297
+ * 获取多组Cookie
266
298
  * @param paramDetails
267
299
  * @param callback
268
300
  * + cookies object[]
269
301
  * + error string|undefined
270
302
  **/
271
- list(paramDetails = {}, callback = (resultData, error) => { }) {
303
+ list(paramDetails, callback) {
304
+ if (paramDetails == null) {
305
+ throw new Error("Utils.GMCookie.list 参数不能为空");
306
+ }
272
307
  let resultData = [];
273
308
  try {
274
309
  let details = {
275
- // @ts-ignore
276
310
  url: globalThis.location.href,
277
311
  domain: globalThis.location.hostname,
278
312
  name: "",
@@ -300,15 +334,59 @@ System.register('Utils', [], (function (exports) {
300
334
  session: false,
301
335
  value: itemValue,
302
336
  });
303
- return;
304
337
  }
305
338
  });
306
- callback(resultData);
339
+ if (typeof callback === "function") {
340
+ callback(resultData);
341
+ }
307
342
  }
308
343
  catch (error) {
309
- callback(resultData, error);
344
+ if (typeof callback === "function") {
345
+ callback(resultData, error);
346
+ }
310
347
  }
311
348
  }
349
+ /**
350
+ * 获取多组Cookie
351
+ * @param paramDetails
352
+ **/
353
+ getList(paramDetails) {
354
+ if (paramDetails == null) {
355
+ throw new Error("Utils.GMCookie.list 参数不能为空");
356
+ }
357
+ let resultData = [];
358
+ let details = {
359
+ url: globalThis.location.href,
360
+ domain: globalThis.location.hostname,
361
+ name: "",
362
+ path: "/",
363
+ };
364
+ details = utils.assign(details, paramDetails);
365
+ let cookies = document.cookie.split(";");
366
+ cookies.forEach((item) => {
367
+ item = item.trimStart();
368
+ let itemName = item.split("=")[0];
369
+ let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
370
+ let nameRegexp = details.name instanceof RegExp
371
+ ? details.name
372
+ : new RegExp("^" + details.name, "g");
373
+ if (itemName.match(nameRegexp)) {
374
+ resultData.push({
375
+ domain: globalThis.location.hostname,
376
+ expirationDate: null,
377
+ hostOnly: true,
378
+ httpOnly: false,
379
+ name: itemName,
380
+ path: "/",
381
+ sameSite: "unspecified",
382
+ secure: true,
383
+ session: false,
384
+ value: itemValue,
385
+ });
386
+ }
387
+ });
388
+ return resultData;
389
+ }
312
390
  /**
313
391
  * 设置Cookie
314
392
  * @param paramDetails