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