@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.cjs.js CHANGED
@@ -259,17 +259,53 @@ const UtilsCore = {
259
259
 
260
260
  class UtilsGMCookie {
261
261
  /**
262
- * 获取Cookie
262
+ * 获取单个cookie
263
+ * @param cookieName cookie名
264
+ */
265
+ get(cookieName) {
266
+ if (typeof cookieName !== "string") {
267
+ throw new TypeError("Utils.GMCookie.get 参数cookieName 必须为字符串");
268
+ }
269
+ let cookies = document.cookie.split(";");
270
+ let findValue = void 0;
271
+ for (const cookieItem of cookies) {
272
+ let item = cookieItem.trim();
273
+ let itemSplit = item.split("=");
274
+ let itemName = itemSplit[0];
275
+ itemSplit.splice(0, 1);
276
+ let itemValue = itemSplit.join("");
277
+ if (itemName === cookieName) {
278
+ findValue = {
279
+ domain: globalThis.location.hostname,
280
+ expirationDate: null,
281
+ hostOnly: true,
282
+ httpOnly: false,
283
+ name: cookieName,
284
+ path: "/",
285
+ sameSite: "unspecified",
286
+ secure: true,
287
+ session: false,
288
+ value: itemValue,
289
+ };
290
+ break;
291
+ }
292
+ }
293
+ return findValue;
294
+ }
295
+ /**
296
+ * 获取多组Cookie
263
297
  * @param paramDetails
264
298
  * @param callback
265
299
  * + cookies object[]
266
300
  * + error string|undefined
267
301
  **/
268
- list(paramDetails = {}, callback = (resultData, error) => { }) {
302
+ list(paramDetails, callback) {
303
+ if (paramDetails == null) {
304
+ throw new Error("Utils.GMCookie.list 参数不能为空");
305
+ }
269
306
  let resultData = [];
270
307
  try {
271
308
  let details = {
272
- // @ts-ignore
273
309
  url: globalThis.location.href,
274
310
  domain: globalThis.location.hostname,
275
311
  name: "",
@@ -278,9 +314,11 @@ class UtilsGMCookie {
278
314
  details = utils.assign(details, paramDetails);
279
315
  let cookies = document.cookie.split(";");
280
316
  cookies.forEach((item) => {
281
- item = item.trimStart();
282
- let itemName = item.split("=")[0];
283
- let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
317
+ item = item.trim();
318
+ let itemSplit = item.split("=");
319
+ let itemName = itemSplit[0];
320
+ itemSplit.splice(0, 1);
321
+ let itemValue = itemSplit.join("");
284
322
  let nameRegexp = details.name instanceof RegExp
285
323
  ? details.name
286
324
  : new RegExp("^" + details.name, "g");
@@ -297,14 +335,60 @@ class UtilsGMCookie {
297
335
  session: false,
298
336
  value: itemValue,
299
337
  });
300
- return;
301
338
  }
302
339
  });
303
- callback(resultData);
340
+ if (typeof callback === "function") {
341
+ callback(resultData);
342
+ }
304
343
  }
305
344
  catch (error) {
306
- callback(resultData, error);
345
+ if (typeof callback === "function") {
346
+ callback(resultData, error);
347
+ }
348
+ }
349
+ }
350
+ /**
351
+ * 获取多组Cookie
352
+ * @param paramDetails
353
+ **/
354
+ getList(paramDetails) {
355
+ if (paramDetails == null) {
356
+ throw new Error("Utils.GMCookie.list 参数不能为空");
307
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.trim();
369
+ let itemSplit = item.split("=");
370
+ let itemName = itemSplit[0];
371
+ itemSplit.splice(0, 1);
372
+ let itemValue = itemSplit.join("");
373
+ let nameRegexp = details.name instanceof RegExp
374
+ ? details.name
375
+ : new RegExp("^" + details.name, "g");
376
+ if (itemName.match(nameRegexp)) {
377
+ resultData.push({
378
+ domain: globalThis.location.hostname,
379
+ expirationDate: null,
380
+ hostOnly: true,
381
+ httpOnly: false,
382
+ name: itemName,
383
+ path: "/",
384
+ sameSite: "unspecified",
385
+ secure: true,
386
+ session: false,
387
+ value: itemValue,
388
+ });
389
+ }
390
+ });
391
+ return resultData;
308
392
  }
309
393
  /**
310
394
  * 设置Cookie