@whitesev/utils 2.5.3 → 2.5.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
@@ -270,24 +270,24 @@ class UtilsGMCookie {
270
270
  }
271
271
  /**
272
272
  * 获取多组Cookie
273
- * @param paramDetails 配置
273
+ * @param option 配置
274
274
  * @param callback 获取操作后的回调
275
275
  * + cookies object[]
276
276
  * + error string|undefined
277
277
  **/
278
- list(paramDetails, callback) {
279
- if (paramDetails == null) {
278
+ list(option, callback) {
279
+ if (option == null) {
280
280
  throw new Error("Utils.GMCookie.list 参数不能为空");
281
281
  }
282
282
  let resultData = [];
283
283
  try {
284
- let details = {
284
+ let defaultOption = {
285
285
  url: this.windowApi.window.location.href,
286
286
  domain: this.windowApi.window.location.hostname,
287
287
  name: "",
288
288
  path: "/",
289
289
  };
290
- details = utils.assign(details, paramDetails);
290
+ defaultOption = utils.assign(defaultOption, option);
291
291
  let cookies = this.windowApi.document.cookie.split(";");
292
292
  cookies.forEach((item) => {
293
293
  item = item.trim();
@@ -295,9 +295,9 @@ class UtilsGMCookie {
295
295
  let itemName = itemSplit[0];
296
296
  itemSplit.splice(0, 1);
297
297
  let itemValue = decodeURIComponent(itemSplit.join(""));
298
- let nameRegexp = details.name instanceof RegExp
299
- ? details.name
300
- : new RegExp("^" + details.name, "g");
298
+ let nameRegexp = defaultOption.name instanceof RegExp
299
+ ? defaultOption.name
300
+ : new RegExp("^" + defaultOption.name, "g");
301
301
  if (itemName.match(nameRegexp)) {
302
302
  resultData.push({
303
303
  domain: this.windowApi.window.location.hostname,
@@ -325,20 +325,20 @@ class UtilsGMCookie {
325
325
  }
326
326
  /**
327
327
  * 获取多组Cookie
328
- * @param paramDetails 配置
328
+ * @param option 配置
329
329
  **/
330
- getList(paramDetails) {
331
- if (paramDetails == null) {
330
+ getList(option) {
331
+ if (option == null) {
332
332
  throw new Error("Utils.GMCookie.list 参数不能为空");
333
333
  }
334
334
  let resultData = [];
335
- let details = {
335
+ let defaultOption = {
336
336
  url: this.windowApi.window.location.href,
337
337
  domain: this.windowApi.window.location.hostname,
338
338
  name: "",
339
339
  path: "/",
340
340
  };
341
- details = utils.assign(details, paramDetails);
341
+ defaultOption = utils.assign(defaultOption, option);
342
342
  let cookies = this.windowApi.document.cookie.split(";");
343
343
  cookies.forEach((item) => {
344
344
  item = item.trim();
@@ -346,9 +346,9 @@ class UtilsGMCookie {
346
346
  let itemName = itemSplit[0];
347
347
  itemSplit.splice(0, 1);
348
348
  let itemValue = decodeURIComponent(itemSplit.join(""));
349
- let nameRegexp = details.name instanceof RegExp
350
- ? details.name
351
- : new RegExp("^" + details.name, "g");
349
+ let nameRegexp = defaultOption.name instanceof RegExp
350
+ ? defaultOption.name
351
+ : new RegExp("^" + defaultOption.name, "g");
352
352
  if (itemName.match(nameRegexp)) {
353
353
  resultData.push({
354
354
  domain: this.windowApi.window.location.hostname,
@@ -368,12 +368,13 @@ class UtilsGMCookie {
368
368
  }
369
369
  /**
370
370
  * 设置Cookie
371
- * @param paramDetails 配置
371
+ * @param option 配置
372
372
  * @param callback 设置操作后的回调(成功/失败)
373
373
  */
374
- set(paramDetails, callback = (error) => { }) {
374
+ set(option, callback) {
375
+ let errorInfo;
375
376
  try {
376
- let details = {
377
+ let defaultOption = {
377
378
  url: this.windowApi.window.location.href,
378
379
  name: "",
379
380
  value: "",
@@ -386,43 +387,52 @@ class UtilsGMCookie {
386
387
  */
387
388
  expirationDate: Math.floor(Date.now()) + 60 * 60 * 24 * 30,
388
389
  };
389
- details = utils.assign(details, paramDetails);
390
- let life = details.expirationDate
391
- ? details.expirationDate
390
+ defaultOption = utils.assign(defaultOption, option);
391
+ let life = defaultOption.expirationDate
392
+ ? defaultOption.expirationDate
392
393
  : Math.floor(Date.now()) + 60 * 60 * 24 * 30;
393
- let cookieStr = details.name +
394
+ let cookieStr = defaultOption.name +
394
395
  "=" +
395
- decodeURIComponent(details.value) +
396
+ decodeURIComponent(defaultOption.value) +
396
397
  ";expires=" +
397
398
  new Date(life).toGMTString() +
398
399
  "; path=/";
399
400
  this.windowApi.document.cookie = cookieStr;
400
- callback();
401
401
  }
402
402
  catch (error) {
403
- callback(error);
403
+ errorInfo = error;
404
+ }
405
+ finally {
406
+ if (typeof callback === "function") {
407
+ callback(errorInfo);
408
+ }
404
409
  }
405
410
  }
406
411
  /**
407
412
  * 删除Cookie
408
- * @param paramDetails 配置
413
+ * @param option 配置
409
414
  * @param callback 删除操作后的回调(成功/失败)
410
415
  */
411
- delete(paramDetails, callback = (error) => { }) {
416
+ delete(option, callback) {
417
+ let errorInfo;
412
418
  try {
413
- let details = {
419
+ let defaultOption = {
414
420
  url: this.windowApi.window.location.href,
415
421
  name: "",
416
- // @ts-ignore
417
- firstPartyDomain: "",
422
+ path: "/",
423
+ firstPartyDomain: this.windowApi.window.location.hostname,
418
424
  };
419
- details = utils.assign(details, paramDetails);
420
- let cookieStr = details.name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;";
425
+ defaultOption = utils.assign(defaultOption, option);
426
+ let cookieStr = `${defaultOption.name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=${defaultOption.path}; domain=${defaultOption.firstPartyDomain};`;
421
427
  this.windowApi.document.cookie = cookieStr;
422
- callback();
423
428
  }
424
429
  catch (error) {
425
- callback(error);
430
+ errorInfo = error;
431
+ }
432
+ finally {
433
+ if (typeof callback === "function") {
434
+ callback(errorInfo);
435
+ }
426
436
  }
427
437
  }
428
438
  }
@@ -4108,7 +4118,7 @@ class Utils {
4108
4118
  this.windowApi = new WindowApi(option);
4109
4119
  }
4110
4120
  /** 版本号 */
4111
- version = "2024.11.16";
4121
+ version = "2024.12.3";
4112
4122
  addStyle(cssText) {
4113
4123
  if (typeof cssText !== "string") {
4114
4124
  throw new Error("Utils.addStyle 参数cssText 必须为String类型");