@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.esm.js CHANGED
@@ -257,17 +257,53 @@ const UtilsCore = {
257
257
 
258
258
  class UtilsGMCookie {
259
259
  /**
260
- * 获取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 findValue = void 0;
269
+ for (const cookieItem of cookies) {
270
+ let item = cookieItem.trim();
271
+ let itemSplit = item.split("=");
272
+ let itemName = itemSplit[0];
273
+ itemSplit.splice(0, 1);
274
+ let itemValue = itemSplit.join("");
275
+ if (itemName === cookieName) {
276
+ findValue = {
277
+ domain: globalThis.location.hostname,
278
+ expirationDate: null,
279
+ hostOnly: true,
280
+ httpOnly: false,
281
+ name: cookieName,
282
+ path: "/",
283
+ sameSite: "unspecified",
284
+ secure: true,
285
+ session: false,
286
+ value: itemValue,
287
+ };
288
+ break;
289
+ }
290
+ }
291
+ return findValue;
292
+ }
293
+ /**
294
+ * 获取多组Cookie
261
295
  * @param paramDetails
262
296
  * @param callback
263
297
  * + cookies object[]
264
298
  * + error string|undefined
265
299
  **/
266
- list(paramDetails = {}, callback = (resultData, error) => { }) {
300
+ list(paramDetails, callback) {
301
+ if (paramDetails == null) {
302
+ throw new Error("Utils.GMCookie.list 参数不能为空");
303
+ }
267
304
  let resultData = [];
268
305
  try {
269
306
  let details = {
270
- // @ts-ignore
271
307
  url: globalThis.location.href,
272
308
  domain: globalThis.location.hostname,
273
309
  name: "",
@@ -276,9 +312,11 @@ class UtilsGMCookie {
276
312
  details = utils.assign(details, paramDetails);
277
313
  let cookies = document.cookie.split(";");
278
314
  cookies.forEach((item) => {
279
- item = item.trimStart();
280
- let itemName = item.split("=")[0];
281
- let itemValue = item.replace(new RegExp("^" + itemName + "="), "");
315
+ item = item.trim();
316
+ let itemSplit = item.split("=");
317
+ let itemName = itemSplit[0];
318
+ itemSplit.splice(0, 1);
319
+ let itemValue = itemSplit.join("");
282
320
  let nameRegexp = details.name instanceof RegExp
283
321
  ? details.name
284
322
  : new RegExp("^" + details.name, "g");
@@ -295,14 +333,60 @@ class UtilsGMCookie {
295
333
  session: false,
296
334
  value: itemValue,
297
335
  });
298
- return;
299
336
  }
300
337
  });
301
- callback(resultData);
338
+ if (typeof callback === "function") {
339
+ callback(resultData);
340
+ }
302
341
  }
303
342
  catch (error) {
304
- callback(resultData, error);
343
+ if (typeof callback === "function") {
344
+ callback(resultData, error);
345
+ }
346
+ }
347
+ }
348
+ /**
349
+ * 获取多组Cookie
350
+ * @param paramDetails
351
+ **/
352
+ getList(paramDetails) {
353
+ if (paramDetails == null) {
354
+ throw new Error("Utils.GMCookie.list 参数不能为空");
305
355
  }
356
+ let resultData = [];
357
+ let details = {
358
+ url: globalThis.location.href,
359
+ domain: globalThis.location.hostname,
360
+ name: "",
361
+ path: "/",
362
+ };
363
+ details = utils.assign(details, paramDetails);
364
+ let cookies = document.cookie.split(";");
365
+ cookies.forEach((item) => {
366
+ item = item.trim();
367
+ let itemSplit = item.split("=");
368
+ let itemName = itemSplit[0];
369
+ itemSplit.splice(0, 1);
370
+ let itemValue = itemSplit.join("");
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;
306
390
  }
307
391
  /**
308
392
  * 设置Cookie