@whitesev/utils 1.1.3 → 1.1.5

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,53 @@
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 findValue = void 0;
275
+ for (const cookieItem of cookies) {
276
+ let item = cookieItem.trim();
277
+ let itemSplit = item.split("=");
278
+ let itemName = itemSplit[0];
279
+ itemSplit.splice(0, 1);
280
+ let itemValue = itemSplit.join("");
281
+ if (itemName === cookieName) {
282
+ findValue = {
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: itemValue,
293
+ };
294
+ break;
295
+ }
296
+ }
297
+ return findValue;
298
+ }
299
+ /**
300
+ * 获取多组Cookie
267
301
  * @param paramDetails
268
302
  * @param callback
269
303
  * + cookies object[]
270
304
  * + error string|undefined
271
305
  **/
272
- list(paramDetails = {}, callback = (resultData, error) => { }) {
306
+ list(paramDetails, callback) {
307
+ if (paramDetails == null) {
308
+ throw new Error("Utils.GMCookie.list 参数不能为空");
309
+ }
273
310
  let resultData = [];
274
311
  try {
275
312
  let details = {
276
- // @ts-ignore
277
313
  url: globalThis.location.href,
278
314
  domain: globalThis.location.hostname,
279
315
  name: "",
@@ -282,9 +318,11 @@
282
318
  details = utils.assign(details, paramDetails);
283
319
  let cookies = document.cookie.split(";");
284
320
  cookies.forEach((item) => {
285
- item = item.trimStart();
286
- let itemName = item.split("=")[0];
287
- let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
321
+ item = item.trim();
322
+ let itemSplit = item.split("=");
323
+ let itemName = itemSplit[0];
324
+ itemSplit.splice(0, 1);
325
+ let itemValue = itemSplit.join("");
288
326
  let nameRegexp = details.name instanceof RegExp
289
327
  ? details.name
290
328
  : new RegExp("^" + details.name, "g");
@@ -301,14 +339,60 @@
301
339
  session: false,
302
340
  value: itemValue,
303
341
  });
304
- return;
305
342
  }
306
343
  });
307
- callback(resultData);
344
+ if (typeof callback === "function") {
345
+ callback(resultData);
346
+ }
308
347
  }
309
348
  catch (error) {
310
- callback(resultData, error);
349
+ if (typeof callback === "function") {
350
+ callback(resultData, error);
351
+ }
352
+ }
353
+ }
354
+ /**
355
+ * 获取多组Cookie
356
+ * @param paramDetails
357
+ **/
358
+ getList(paramDetails) {
359
+ if (paramDetails == null) {
360
+ throw new Error("Utils.GMCookie.list 参数不能为空");
311
361
  }
362
+ let resultData = [];
363
+ let details = {
364
+ url: globalThis.location.href,
365
+ domain: globalThis.location.hostname,
366
+ name: "",
367
+ path: "/",
368
+ };
369
+ details = utils.assign(details, paramDetails);
370
+ let cookies = document.cookie.split(";");
371
+ cookies.forEach((item) => {
372
+ item = item.trim();
373
+ let itemSplit = item.split("=");
374
+ let itemName = itemSplit[0];
375
+ itemSplit.splice(0, 1);
376
+ let itemValue = itemSplit.join("");
377
+ let nameRegexp = details.name instanceof RegExp
378
+ ? details.name
379
+ : new RegExp("^" + details.name, "g");
380
+ if (itemName.match(nameRegexp)) {
381
+ resultData.push({
382
+ domain: globalThis.location.hostname,
383
+ expirationDate: null,
384
+ hostOnly: true,
385
+ httpOnly: false,
386
+ name: itemName,
387
+ path: "/",
388
+ sameSite: "unspecified",
389
+ secure: true,
390
+ session: false,
391
+ value: itemValue,
392
+ });
393
+ }
394
+ });
395
+ return resultData;
312
396
  }
313
397
  /**
314
398
  * 设置Cookie