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