@kwiz/common 1.0.78 → 1.0.80

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.
Files changed (118) hide show
  1. package/.github/workflows/npm-publish.yml +24 -0
  2. package/.madgerc +2 -2
  3. package/LICENSE +21 -21
  4. package/fix-folder-imports.js +26 -26
  5. package/lib/cjs/helpers/sharepoint.js +5 -1
  6. package/lib/cjs/helpers/sharepoint.js.map +1 -1
  7. package/lib/cjs/helpers/typecheckers.js +5 -1
  8. package/lib/cjs/helpers/typecheckers.js.map +1 -1
  9. package/lib/cjs/types/libs/msal.types.js +26 -26
  10. package/lib/cjs/utils/sharepoint.rest/list.js +1 -1
  11. package/lib/cjs/utils/sharepoint.rest/list.js.map +1 -1
  12. package/lib/cjs/utils/sharepoint.rest/user.js +11 -11
  13. package/lib/esm/helpers/sharepoint.js +3 -0
  14. package/lib/esm/helpers/sharepoint.js.map +1 -1
  15. package/lib/esm/helpers/typecheckers.js +3 -0
  16. package/lib/esm/helpers/typecheckers.js.map +1 -1
  17. package/lib/esm/types/libs/msal.types.js +26 -26
  18. package/lib/esm/utils/sharepoint.rest/list.js +2 -2
  19. package/lib/esm/utils/sharepoint.rest/list.js.map +1 -1
  20. package/lib/esm/utils/sharepoint.rest/user.js +11 -11
  21. package/lib/types/helpers/sharepoint.d.ts +1 -0
  22. package/lib/types/helpers/typecheckers.d.ts +1 -0
  23. package/package.json +77 -77
  24. package/readme.md +17 -17
  25. package/src/_dependencies.ts +12 -12
  26. package/src/config.ts +17 -17
  27. package/src/helpers/Guid.ts +181 -181
  28. package/src/helpers/base64.ts +173 -173
  29. package/src/helpers/browser.test.js +13 -13
  30. package/src/helpers/browser.ts +1348 -1348
  31. package/src/helpers/browserinfo.ts +292 -292
  32. package/src/helpers/collections.base.test.js +25 -25
  33. package/src/helpers/collections.base.ts +437 -437
  34. package/src/helpers/collections.ts +107 -107
  35. package/src/helpers/color.ts +54 -54
  36. package/src/helpers/cookies.ts +59 -59
  37. package/src/helpers/date.test.js +119 -119
  38. package/src/helpers/date.ts +188 -188
  39. package/src/helpers/debug.ts +186 -186
  40. package/src/helpers/emails.ts +6 -6
  41. package/src/helpers/eval.ts +5 -5
  42. package/src/helpers/file.test.js +50 -50
  43. package/src/helpers/file.ts +58 -58
  44. package/src/helpers/flatted.ts +149 -149
  45. package/src/helpers/functions.ts +16 -16
  46. package/src/helpers/graph/calendar.types.ts +10 -10
  47. package/src/helpers/http.ts +69 -69
  48. package/src/helpers/images.ts +22 -22
  49. package/src/helpers/json.ts +38 -38
  50. package/src/helpers/md5.ts +189 -189
  51. package/src/helpers/objects.test.js +33 -33
  52. package/src/helpers/objects.ts +270 -270
  53. package/src/helpers/promises.test.js +37 -37
  54. package/src/helpers/promises.ts +165 -165
  55. package/src/helpers/random.ts +27 -27
  56. package/src/helpers/scheduler/scheduler.test.js +103 -103
  57. package/src/helpers/scheduler/scheduler.ts +131 -131
  58. package/src/helpers/sharepoint.ts +776 -772
  59. package/src/helpers/strings.test.js +101 -101
  60. package/src/helpers/strings.ts +317 -317
  61. package/src/helpers/typecheckers.test.js +34 -34
  62. package/src/helpers/typecheckers.ts +266 -262
  63. package/src/helpers/url.test.js +43 -43
  64. package/src/helpers/url.ts +207 -207
  65. package/src/helpers/urlhelper.ts +111 -111
  66. package/src/index.ts +6 -6
  67. package/src/types/auth.ts +54 -54
  68. package/src/types/common.types.ts +15 -15
  69. package/src/types/flatted.types.ts +59 -59
  70. package/src/types/globals.types.ts +6 -6
  71. package/src/types/graph/calendar.types.ts +80 -80
  72. package/src/types/knownscript.types.ts +18 -18
  73. package/src/types/libs/datajs.types.ts +28 -28
  74. package/src/types/libs/ics.types.ts +30 -30
  75. package/src/types/libs/msal.types.ts +49 -49
  76. package/src/types/locales.ts +124 -124
  77. package/src/types/localstoragecache.types.ts +8 -8
  78. package/src/types/location.types.ts +27 -27
  79. package/src/types/moment.ts +11 -11
  80. package/src/types/regex.types.ts +16 -16
  81. package/src/types/rest.types.ts +95 -95
  82. package/src/types/sharepoint.types.ts +1465 -1465
  83. package/src/types/sharepoint.utils.types.ts +287 -287
  84. package/src/utils/auth/common.ts +74 -74
  85. package/src/utils/auth/discovery.test.js +12 -12
  86. package/src/utils/auth/discovery.ts +132 -132
  87. package/src/utils/base64.ts +27 -27
  88. package/src/utils/consolelogger.ts +320 -320
  89. package/src/utils/date.ts +35 -35
  90. package/src/utils/emails.ts +24 -24
  91. package/src/utils/knownscript.ts +286 -286
  92. package/src/utils/localstoragecache.ts +441 -441
  93. package/src/utils/rest.ts +501 -501
  94. package/src/utils/script.ts +170 -170
  95. package/src/utils/sharepoint.rest/common.ts +154 -154
  96. package/src/utils/sharepoint.rest/date.ts +62 -62
  97. package/src/utils/sharepoint.rest/file.folder.ts +598 -598
  98. package/src/utils/sharepoint.rest/item.ts +547 -547
  99. package/src/utils/sharepoint.rest/list.ts +1388 -1388
  100. package/src/utils/sharepoint.rest/listutils/GetListItemsByCaml.ts +774 -774
  101. package/src/utils/sharepoint.rest/listutils/GetListItemsById.ts +275 -275
  102. package/src/utils/sharepoint.rest/listutils/common.ts +206 -206
  103. package/src/utils/sharepoint.rest/location.ts +141 -141
  104. package/src/utils/sharepoint.rest/navigation-links.ts +86 -86
  105. package/src/utils/sharepoint.rest/user-search.ts +252 -252
  106. package/src/utils/sharepoint.rest/user.ts +491 -491
  107. package/src/utils/sharepoint.rest/web.ts +1384 -1384
  108. package/src/utils/sod.ts +194 -194
  109. package/lib/cjs/helpers/_dependencies.js +0 -21
  110. package/lib/cjs/helpers/_dependencies.js.map +0 -1
  111. package/lib/cjs/utils/_dependencies.js +0 -24
  112. package/lib/cjs/utils/_dependencies.js.map +0 -1
  113. package/lib/esm/helpers/_dependencies.js +0 -3
  114. package/lib/esm/helpers/_dependencies.js.map +0 -1
  115. package/lib/esm/utils/_dependencies.js +0 -4
  116. package/lib/esm/utils/_dependencies.js.map +0 -1
  117. package/lib/types/helpers/_dependencies.d.ts +0 -2
  118. package/lib/types/utils/_dependencies.d.ts +0 -3
@@ -1,108 +1,108 @@
1
- import { IDictionary } from "../types/common.types";
2
- import { IMultiLevelGroup, IMultiLevelGroupItem } from "./collections.base";
3
- import { hasOwnProperty, objectsEqual } from "./objects";
4
- import { isFunction, isNotEmptyArray, isNullOrEmptyArray, isNullOrEmptyString, isNullOrUndefined, isNumber, isString } from "./typecheckers";
5
-
6
- /** check that every element in the arrays are the same value */
7
- export function arraysEqual(arr1: any[], arr2: any[]): boolean {
8
- if (isNullOrEmptyArray(arr1) && isNullOrEmptyArray(arr2)) return true;
9
- return Array.isArray(arr1) && Array.isArray(arr2) && arr1.length === arr2.length && arr1.every((v1: any, i: number) => {
10
- var v2 = arr2[i];
11
- if (isString(v1) || isNumber(v1)) {
12
- return v1 === v2;
13
- } else if (Array.isArray(v1) && Array.isArray(v2)) {
14
- return arraysEqual(v1, v2);
15
- } else {
16
- return objectsEqual(v1, v2);
17
- }
18
- });
19
- }
20
-
21
- /** Takes an array and transforms it into a dictionary. this will assign all items of the same key as an array. */
22
- export function groupBy<T>(arr: T[], getKeys: (element: T) => string[], filter?: (element: T) => boolean): IDictionary<T[]> {
23
- let dic: IDictionary<T[]> = {};
24
-
25
- if (isNotEmptyArray(arr))
26
- arr.forEach(i => {
27
- if (!isFunction(filter) || filter(i)) {
28
- let keys = getKeys(i);
29
- keys.forEach(key => {
30
- if (isNullOrEmptyString(key)) key = "";
31
- if (!hasOwnProperty(dic, key)) dic[key] = [i];
32
- else dic[key].push(i);
33
- });
34
- }
35
- });
36
- return dic;
37
- }
38
-
39
- var groupByMultipleCacheKey = "$groupByMultipleCache";
40
- /** allows nested multi-level grouping */
41
- export function GroupByMultiple<ItemType>(arr: ItemType[], groupDefinitions: {
42
- /** return all groups this item belongs to */
43
- getGroupsForThisElement: ((element: ItemType) => string[]);
44
- /** Optional, add a prefix to the group. For example: "Priority > " to fullTitle will be "Priority > High" */
45
- groupPrefix?: string;
46
- }[], options?: {
47
- filter?: (element: ItemType) => boolean;
48
- /** if groups were calculated, they are returned from cache. send true to clear that cache. send true if you suspect getKeysCollection might change on your existing array. */
49
- clearCache?: boolean;
50
- parentGroup?: IMultiLevelGroup<ItemType>;
51
- }): IDictionary<IMultiLevelGroup<ItemType>> {
52
- options = options || {};
53
- if (options.clearCache || isNullOrUndefined(arr[groupByMultipleCacheKey])) {
54
- let dic: IDictionary<IMultiLevelGroup<ItemType>> = {};
55
-
56
- let groupDefinition = groupDefinitions[0];//get first
57
- let getKeys = groupDefinition.getGroupsForThisElement;
58
-
59
- if (isNotEmptyArray(arr)) {
60
- let groupIndex = 0;
61
- arr.forEach(i => {
62
- if (!isFunction(options.filter) || options.filter(i)) {
63
- let keys = getKeys(i);
64
- keys.forEach(key => {
65
- if (isNullOrEmptyString(key)) key = "";
66
- if (!hasOwnProperty(dic, key)) {
67
- let groupKey = groupIndex.toString(10);
68
- let groupKeyParent = options.parentGroup;
69
- while (groupKeyParent) {
70
- groupKey = groupKeyParent.index + "_" + groupKey;
71
- groupKeyParent = groupKeyParent.parentGroup;
72
- }
73
- dic[key] = {
74
- groupItems: [],
75
- subGroups: {},
76
- depth: options.parentGroup ? options.parentGroup.depth + 1 : 0,
77
- parentGroup: options.parentGroup,
78
- key: groupKey,
79
- index: groupIndex,
80
- title: key,
81
- groupPrefix: groupDefinition.groupPrefix,
82
- fullTitle: `${isNullOrEmptyString(groupDefinition.groupPrefix) ? "" : groupDefinition.groupPrefix}${key}`
83
- };
84
- groupIndex++;
85
- }
86
- let itemWithGroup = i as (ItemType & IMultiLevelGroupItem<ItemType>);
87
- itemWithGroup.parentGroup = dic[key];
88
- dic[key].groupItems.push(itemWithGroup);
89
- });
90
- }
91
- });
92
- }
93
-
94
- if (isNotEmptyArray(groupDefinitions) && groupDefinitions.length > 1) {
95
- //run for every group and call this again
96
- Object.keys(dic).forEach(groupName => {
97
- let currentGroup = dic[groupName];
98
- currentGroup.subGroups = GroupByMultiple(currentGroup.groupItems, groupDefinitions.slice(1), {
99
- ...options,
100
- parentGroup: currentGroup
101
- });
102
- });
103
- }
104
-
105
- arr[groupByMultipleCacheKey] = dic;
106
- }
107
- return arr[groupByMultipleCacheKey];
1
+ import { IDictionary } from "../types/common.types";
2
+ import { IMultiLevelGroup, IMultiLevelGroupItem } from "./collections.base";
3
+ import { hasOwnProperty, objectsEqual } from "./objects";
4
+ import { isFunction, isNotEmptyArray, isNullOrEmptyArray, isNullOrEmptyString, isNullOrUndefined, isNumber, isString } from "./typecheckers";
5
+
6
+ /** check that every element in the arrays are the same value */
7
+ export function arraysEqual(arr1: any[], arr2: any[]): boolean {
8
+ if (isNullOrEmptyArray(arr1) && isNullOrEmptyArray(arr2)) return true;
9
+ return Array.isArray(arr1) && Array.isArray(arr2) && arr1.length === arr2.length && arr1.every((v1: any, i: number) => {
10
+ var v2 = arr2[i];
11
+ if (isString(v1) || isNumber(v1)) {
12
+ return v1 === v2;
13
+ } else if (Array.isArray(v1) && Array.isArray(v2)) {
14
+ return arraysEqual(v1, v2);
15
+ } else {
16
+ return objectsEqual(v1, v2);
17
+ }
18
+ });
19
+ }
20
+
21
+ /** Takes an array and transforms it into a dictionary. this will assign all items of the same key as an array. */
22
+ export function groupBy<T>(arr: T[], getKeys: (element: T) => string[], filter?: (element: T) => boolean): IDictionary<T[]> {
23
+ let dic: IDictionary<T[]> = {};
24
+
25
+ if (isNotEmptyArray(arr))
26
+ arr.forEach(i => {
27
+ if (!isFunction(filter) || filter(i)) {
28
+ let keys = getKeys(i);
29
+ keys.forEach(key => {
30
+ if (isNullOrEmptyString(key)) key = "";
31
+ if (!hasOwnProperty(dic, key)) dic[key] = [i];
32
+ else dic[key].push(i);
33
+ });
34
+ }
35
+ });
36
+ return dic;
37
+ }
38
+
39
+ var groupByMultipleCacheKey = "$groupByMultipleCache";
40
+ /** allows nested multi-level grouping */
41
+ export function GroupByMultiple<ItemType>(arr: ItemType[], groupDefinitions: {
42
+ /** return all groups this item belongs to */
43
+ getGroupsForThisElement: ((element: ItemType) => string[]);
44
+ /** Optional, add a prefix to the group. For example: "Priority > " to fullTitle will be "Priority > High" */
45
+ groupPrefix?: string;
46
+ }[], options?: {
47
+ filter?: (element: ItemType) => boolean;
48
+ /** if groups were calculated, they are returned from cache. send true to clear that cache. send true if you suspect getKeysCollection might change on your existing array. */
49
+ clearCache?: boolean;
50
+ parentGroup?: IMultiLevelGroup<ItemType>;
51
+ }): IDictionary<IMultiLevelGroup<ItemType>> {
52
+ options = options || {};
53
+ if (options.clearCache || isNullOrUndefined(arr[groupByMultipleCacheKey])) {
54
+ let dic: IDictionary<IMultiLevelGroup<ItemType>> = {};
55
+
56
+ let groupDefinition = groupDefinitions[0];//get first
57
+ let getKeys = groupDefinition.getGroupsForThisElement;
58
+
59
+ if (isNotEmptyArray(arr)) {
60
+ let groupIndex = 0;
61
+ arr.forEach(i => {
62
+ if (!isFunction(options.filter) || options.filter(i)) {
63
+ let keys = getKeys(i);
64
+ keys.forEach(key => {
65
+ if (isNullOrEmptyString(key)) key = "";
66
+ if (!hasOwnProperty(dic, key)) {
67
+ let groupKey = groupIndex.toString(10);
68
+ let groupKeyParent = options.parentGroup;
69
+ while (groupKeyParent) {
70
+ groupKey = groupKeyParent.index + "_" + groupKey;
71
+ groupKeyParent = groupKeyParent.parentGroup;
72
+ }
73
+ dic[key] = {
74
+ groupItems: [],
75
+ subGroups: {},
76
+ depth: options.parentGroup ? options.parentGroup.depth + 1 : 0,
77
+ parentGroup: options.parentGroup,
78
+ key: groupKey,
79
+ index: groupIndex,
80
+ title: key,
81
+ groupPrefix: groupDefinition.groupPrefix,
82
+ fullTitle: `${isNullOrEmptyString(groupDefinition.groupPrefix) ? "" : groupDefinition.groupPrefix}${key}`
83
+ };
84
+ groupIndex++;
85
+ }
86
+ let itemWithGroup = i as (ItemType & IMultiLevelGroupItem<ItemType>);
87
+ itemWithGroup.parentGroup = dic[key];
88
+ dic[key].groupItems.push(itemWithGroup);
89
+ });
90
+ }
91
+ });
92
+ }
93
+
94
+ if (isNotEmptyArray(groupDefinitions) && groupDefinitions.length > 1) {
95
+ //run for every group and call this again
96
+ Object.keys(dic).forEach(groupName => {
97
+ let currentGroup = dic[groupName];
98
+ currentGroup.subGroups = GroupByMultiple(currentGroup.groupItems, groupDefinitions.slice(1), {
99
+ ...options,
100
+ parentGroup: currentGroup
101
+ });
102
+ });
103
+ }
104
+
105
+ arr[groupByMultipleCacheKey] = dic;
106
+ }
107
+ return arr[groupByMultipleCacheKey];
108
108
  }
@@ -1,55 +1,55 @@
1
- import { isNullOrEmptyString } from "./typecheckers";
2
-
3
- /** get the oposite color of a color, or the best contrasting black or white. This is useful to know which color text to show on a dynamic background color */
4
- export function invertColor(color: string, blackOrWhite?: boolean, defaultIfEmpty?: string) {
5
- if (isNullOrEmptyString(color) && !isNullOrEmptyString(defaultIfEmpty)) return defaultIfEmpty;
6
- let rgba = colorToRGBA(color);
7
-
8
- if (blackOrWhite) {
9
- // http://stackoverflow.com/a/3943023/112731
10
- return (rgba.r * 0.299 + rgba.g * 0.587 + rgba.b * 0.114) > 186
11
- ? '#000000'
12
- : '#FFFFFF';
13
- }
14
- // invert color components
15
- let _r = (255 - rgba.r);
16
- let _g = (255 - rgba.g);
17
- let _b = (255 - rgba.b);
18
- // pad each with zeros and return
19
- return "#" + byteToHex(_r) + byteToHex(_g) + byteToHex(_b);
20
- }
21
-
22
-
23
- /** Returns the color as an array of [r, g, b, a] -- all range from 0 - 255 */
24
- export function colorToRGBA(color: string) {
25
- // Returns the color as an array of [r, g, b, a] -- all range from 0 - 255
26
- // color must be a valid canvas fillStyle. This will cover most anything
27
- // you'd want to use.
28
- // Examples:
29
- // colorToRGBA('red') # [255, 0, 0, 255]
30
- // colorToRGBA('#f00') # [255, 0, 0, 255]
31
- let cvs = document.createElement('canvas');
32
- cvs.height = 1;
33
- cvs.width = 1;
34
- let ctx = cvs.getContext('2d');
35
- ctx.fillStyle = color;
36
- ctx.fillRect(0, 0, 1, 1);
37
- let data = ctx.getImageData(0, 0, 1, 1).data;
38
- return { r: data[0], g: data[1], b: data[2], a: data[3] };
39
- }
40
-
41
- function byteToHex(num: number) {
42
- // Turns a number (0-255) into a 2-character hex number (00-ff)
43
- return ('0' + num.toString(16)).slice(-2);
44
- }
45
-
46
- /** Convert any CSS color to a hex representation, returns #000000 */
47
- export function colorToHex(color: string) {
48
- //
49
- // Examples:
50
- // colorToHex('red') # '#ff0000'
51
- // colorToHex('rgb(255, 0, 0)') # '#ff0000'
52
- let rgba = colorToRGBA(color);
53
- let hex = byteToHex(rgba.r) + byteToHex(rgba.g) + byteToHex(rgba.b);
54
- return "#" + hex;
1
+ import { isNullOrEmptyString } from "./typecheckers";
2
+
3
+ /** get the oposite color of a color, or the best contrasting black or white. This is useful to know which color text to show on a dynamic background color */
4
+ export function invertColor(color: string, blackOrWhite?: boolean, defaultIfEmpty?: string) {
5
+ if (isNullOrEmptyString(color) && !isNullOrEmptyString(defaultIfEmpty)) return defaultIfEmpty;
6
+ let rgba = colorToRGBA(color);
7
+
8
+ if (blackOrWhite) {
9
+ // http://stackoverflow.com/a/3943023/112731
10
+ return (rgba.r * 0.299 + rgba.g * 0.587 + rgba.b * 0.114) > 186
11
+ ? '#000000'
12
+ : '#FFFFFF';
13
+ }
14
+ // invert color components
15
+ let _r = (255 - rgba.r);
16
+ let _g = (255 - rgba.g);
17
+ let _b = (255 - rgba.b);
18
+ // pad each with zeros and return
19
+ return "#" + byteToHex(_r) + byteToHex(_g) + byteToHex(_b);
20
+ }
21
+
22
+
23
+ /** Returns the color as an array of [r, g, b, a] -- all range from 0 - 255 */
24
+ export function colorToRGBA(color: string) {
25
+ // Returns the color as an array of [r, g, b, a] -- all range from 0 - 255
26
+ // color must be a valid canvas fillStyle. This will cover most anything
27
+ // you'd want to use.
28
+ // Examples:
29
+ // colorToRGBA('red') # [255, 0, 0, 255]
30
+ // colorToRGBA('#f00') # [255, 0, 0, 255]
31
+ let cvs = document.createElement('canvas');
32
+ cvs.height = 1;
33
+ cvs.width = 1;
34
+ let ctx = cvs.getContext('2d');
35
+ ctx.fillStyle = color;
36
+ ctx.fillRect(0, 0, 1, 1);
37
+ let data = ctx.getImageData(0, 0, 1, 1).data;
38
+ return { r: data[0], g: data[1], b: data[2], a: data[3] };
39
+ }
40
+
41
+ function byteToHex(num: number) {
42
+ // Turns a number (0-255) into a 2-character hex number (00-ff)
43
+ return ('0' + num.toString(16)).slice(-2);
44
+ }
45
+
46
+ /** Convert any CSS color to a hex representation, returns #000000 */
47
+ export function colorToHex(color: string) {
48
+ //
49
+ // Examples:
50
+ // colorToHex('red') # '#ff0000'
51
+ // colorToHex('rgb(255, 0, 0)') # '#ff0000'
52
+ let rgba = colorToRGBA(color);
53
+ let hex = byteToHex(rgba.r) + byteToHex(rgba.g) + byteToHex(rgba.b);
54
+ return "#" + hex;
55
55
  }
@@ -1,60 +1,60 @@
1
- import { trim } from "./strings";
2
- import { isNullOrEmptyString, isNumeric, isString } from "./typecheckers";
3
-
4
- var _zeroDay = new Date(0);
5
- var _today = new Date();
6
-
7
- export function deleteCookie(cookieName: string, path?: string) {
8
- var days = (_zeroDay.getTime() - _today.getTime()) / (24 * 60 * 60 * 1000);
9
- setCookie(cookieName, "", Math.round(days), path);
10
- }
11
-
12
- export function getAllCookies(prefix?: string): string[] {
13
- let cookies = document.cookie.split(';');
14
- let names: string[] = [];
15
- for (var k = 0; k < cookies.length; k++) {
16
- let cookieSplit = cookies[k].split('=');
17
- let cookieName = trim(cookieSplit[0]);
18
- if (isNullOrEmptyString(prefix) || cookieName.indexOf(prefix) === 0)
19
- names.push(cookieName);
20
- }
21
- return names;
22
- }
23
-
24
- /** get a cookie's value by that name, or null */
25
- export function getCookie(cookieName: string) {
26
- try {
27
- let cookies = document.cookie.split(';');
28
- for (var k = 0; k < cookies.length; k++) {
29
- let cookieSplit = cookies[k].split('=');
30
- if (trim(cookieSplit[0]) === cookieName) {
31
- return decodeURIComponent(trim(cookieSplit[1]));
32
- }
33
- }
34
- } catch (e) { }
35
- return null;
36
- }
37
- /** set a cookie by that name and value. if you do not send expireDays, it will be a session cookie (in memory) */
38
- export function setCookie(name: string, value: string, expireDays?: number, path?: string) {
39
- var cookie: string[] = [];
40
-
41
- var cookieValue = `${name}=${isString(value) ? value : ""}`;
42
- cookie.push(cookieValue);
43
-
44
- if (isNumeric(expireDays)) {
45
- var d = new Date();
46
- d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
47
- var cookieExpires = "expires=" + d.toUTCString();
48
- cookie.push(cookieExpires);
49
- }
50
-
51
- var cookiePath = `${isString(path) ? `path=` + path : "path=/"}`;
52
- cookie.push(cookiePath);
53
-
54
- document.cookie = cookie.join(";");
55
- }
56
-
57
- export function cookieEnabled() {
58
- let enabled = "navigator" in globalThis && navigator.cookieEnabled === true;
59
- return enabled;
1
+ import { trim } from "./strings";
2
+ import { isNullOrEmptyString, isNumeric, isString } from "./typecheckers";
3
+
4
+ var _zeroDay = new Date(0);
5
+ var _today = new Date();
6
+
7
+ export function deleteCookie(cookieName: string, path?: string) {
8
+ var days = (_zeroDay.getTime() - _today.getTime()) / (24 * 60 * 60 * 1000);
9
+ setCookie(cookieName, "", Math.round(days), path);
10
+ }
11
+
12
+ export function getAllCookies(prefix?: string): string[] {
13
+ let cookies = document.cookie.split(';');
14
+ let names: string[] = [];
15
+ for (var k = 0; k < cookies.length; k++) {
16
+ let cookieSplit = cookies[k].split('=');
17
+ let cookieName = trim(cookieSplit[0]);
18
+ if (isNullOrEmptyString(prefix) || cookieName.indexOf(prefix) === 0)
19
+ names.push(cookieName);
20
+ }
21
+ return names;
22
+ }
23
+
24
+ /** get a cookie's value by that name, or null */
25
+ export function getCookie(cookieName: string) {
26
+ try {
27
+ let cookies = document.cookie.split(';');
28
+ for (var k = 0; k < cookies.length; k++) {
29
+ let cookieSplit = cookies[k].split('=');
30
+ if (trim(cookieSplit[0]) === cookieName) {
31
+ return decodeURIComponent(trim(cookieSplit[1]));
32
+ }
33
+ }
34
+ } catch (e) { }
35
+ return null;
36
+ }
37
+ /** set a cookie by that name and value. if you do not send expireDays, it will be a session cookie (in memory) */
38
+ export function setCookie(name: string, value: string, expireDays?: number, path?: string) {
39
+ var cookie: string[] = [];
40
+
41
+ var cookieValue = `${name}=${isString(value) ? value : ""}`;
42
+ cookie.push(cookieValue);
43
+
44
+ if (isNumeric(expireDays)) {
45
+ var d = new Date();
46
+ d.setTime(d.getTime() + (expireDays * 24 * 60 * 60 * 1000));
47
+ var cookieExpires = "expires=" + d.toUTCString();
48
+ cookie.push(cookieExpires);
49
+ }
50
+
51
+ var cookiePath = `${isString(path) ? `path=` + path : "path=/"}`;
52
+ cookie.push(cookiePath);
53
+
54
+ document.cookie = cookie.join(";");
55
+ }
56
+
57
+ export function cookieEnabled() {
58
+ let enabled = "navigator" in globalThis && navigator.cookieEnabled === true;
59
+ return enabled;
60
60
  }