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