@selfcommunity/utils 0.2.0-alpha.0 → 0.2.0-alpha.10

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 (100) hide show
  1. package/lib/cjs/index.d.ts +6 -5
  2. package/lib/cjs/index.js +11 -30
  3. package/lib/cjs/utils/array.d.ts +7 -1
  4. package/lib/cjs/utils/array.js +23 -2
  5. package/lib/cjs/utils/browser.d.ts +1 -1
  6. package/lib/cjs/utils/browser.js +10 -5
  7. package/lib/cjs/utils/cache.d.ts +15 -5
  8. package/lib/cjs/utils/cache.js +10 -5
  9. package/lib/cjs/utils/image.d.ts +0 -1
  10. package/lib/cjs/utils/image.js +0 -1
  11. package/lib/cjs/utils/localStorage.d.ts +33 -0
  12. package/lib/cjs/utils/localStorage.js +101 -0
  13. package/lib/cjs/utils/logger.d.ts +0 -1
  14. package/lib/cjs/utils/logger.js +0 -1
  15. package/lib/cjs/utils/number.d.ts +5 -0
  16. package/lib/cjs/utils/number.js +11 -0
  17. package/lib/cjs/utils/object.d.ts +5 -1
  18. package/lib/cjs/utils/object.js +9 -2
  19. package/lib/cjs/utils/string.d.ts +0 -1
  20. package/lib/cjs/utils/string.js +0 -1
  21. package/lib/cjs/utils/url.d.ts +0 -1
  22. package/lib/cjs/utils/url.js +0 -1
  23. package/lib/cjs/utils/websocket.d.ts +0 -1
  24. package/lib/cjs/utils/websocket.js +0 -1
  25. package/lib/cjs/utils/window.d.ts +0 -1
  26. package/lib/cjs/utils/window.js +0 -1
  27. package/lib/esm/index.d.ts +6 -5
  28. package/lib/esm/index.js +6 -5
  29. package/lib/esm/utils/array.d.ts +7 -1
  30. package/lib/esm/utils/array.js +21 -1
  31. package/lib/esm/utils/browser.d.ts +1 -1
  32. package/lib/esm/utils/browser.js +8 -4
  33. package/lib/esm/utils/cache.d.ts +15 -5
  34. package/lib/esm/utils/cache.js +10 -5
  35. package/lib/esm/utils/image.d.ts +0 -1
  36. package/lib/esm/utils/image.js +0 -1
  37. package/lib/esm/utils/localStorage.d.ts +33 -0
  38. package/lib/esm/utils/localStorage.js +97 -0
  39. package/lib/esm/utils/logger.d.ts +0 -1
  40. package/lib/esm/utils/logger.js +0 -1
  41. package/lib/esm/utils/number.d.ts +5 -0
  42. package/lib/esm/utils/number.js +7 -0
  43. package/lib/esm/utils/object.d.ts +5 -1
  44. package/lib/esm/utils/object.js +7 -1
  45. package/lib/esm/utils/string.d.ts +0 -1
  46. package/lib/esm/utils/string.js +0 -1
  47. package/lib/esm/utils/url.d.ts +0 -1
  48. package/lib/esm/utils/url.js +0 -1
  49. package/lib/esm/utils/websocket.d.ts +0 -1
  50. package/lib/esm/utils/websocket.js +0 -1
  51. package/lib/esm/utils/window.d.ts +0 -1
  52. package/lib/esm/utils/window.js +0 -1
  53. package/lib/umd/utils.js +1 -2
  54. package/lib/umd/utils.js.LICENSE.txt +1 -1
  55. package/package.json +12 -15
  56. package/lib/cjs/index.d.ts.map +0 -1
  57. package/lib/cjs/index.js.map +0 -1
  58. package/lib/cjs/utils/array.d.ts.map +0 -1
  59. package/lib/cjs/utils/array.js.map +0 -1
  60. package/lib/cjs/utils/browser.d.ts.map +0 -1
  61. package/lib/cjs/utils/browser.js.map +0 -1
  62. package/lib/cjs/utils/cache.d.ts.map +0 -1
  63. package/lib/cjs/utils/cache.js.map +0 -1
  64. package/lib/cjs/utils/image.d.ts.map +0 -1
  65. package/lib/cjs/utils/image.js.map +0 -1
  66. package/lib/cjs/utils/logger.d.ts.map +0 -1
  67. package/lib/cjs/utils/logger.js.map +0 -1
  68. package/lib/cjs/utils/object.d.ts.map +0 -1
  69. package/lib/cjs/utils/object.js.map +0 -1
  70. package/lib/cjs/utils/string.d.ts.map +0 -1
  71. package/lib/cjs/utils/string.js.map +0 -1
  72. package/lib/cjs/utils/url.d.ts.map +0 -1
  73. package/lib/cjs/utils/url.js.map +0 -1
  74. package/lib/cjs/utils/websocket.d.ts.map +0 -1
  75. package/lib/cjs/utils/websocket.js.map +0 -1
  76. package/lib/cjs/utils/window.d.ts.map +0 -1
  77. package/lib/cjs/utils/window.js.map +0 -1
  78. package/lib/esm/index.d.ts.map +0 -1
  79. package/lib/esm/index.js.map +0 -1
  80. package/lib/esm/utils/array.d.ts.map +0 -1
  81. package/lib/esm/utils/array.js.map +0 -1
  82. package/lib/esm/utils/browser.d.ts.map +0 -1
  83. package/lib/esm/utils/browser.js.map +0 -1
  84. package/lib/esm/utils/cache.d.ts.map +0 -1
  85. package/lib/esm/utils/cache.js.map +0 -1
  86. package/lib/esm/utils/image.d.ts.map +0 -1
  87. package/lib/esm/utils/image.js.map +0 -1
  88. package/lib/esm/utils/logger.d.ts.map +0 -1
  89. package/lib/esm/utils/logger.js.map +0 -1
  90. package/lib/esm/utils/object.d.ts.map +0 -1
  91. package/lib/esm/utils/object.js.map +0 -1
  92. package/lib/esm/utils/string.d.ts.map +0 -1
  93. package/lib/esm/utils/string.js.map +0 -1
  94. package/lib/esm/utils/url.d.ts.map +0 -1
  95. package/lib/esm/utils/url.js.map +0 -1
  96. package/lib/esm/utils/websocket.d.ts.map +0 -1
  97. package/lib/esm/utils/websocket.js.map +0 -1
  98. package/lib/esm/utils/window.d.ts.map +0 -1
  99. package/lib/esm/utils/window.js.map +0 -1
  100. package/lib/umd/utils.js.map +0 -1
@@ -1,9 +1,11 @@
1
1
  import { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify } from './utils/string';
2
2
  import { isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter } from './utils/url';
3
3
  import { getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering } from './utils/window';
4
- import { mergeDeep, isObject, objectWithoutProperties } from './utils/object';
5
- import { arraysEqual } from './utils/array';
6
- import { loadVersionBrowser } from './utils/browser';
4
+ import { mergeDeep, isObject, objectWithoutProperties, isFunc } from './utils/object';
5
+ import { isInteger } from './utils/number';
6
+ import { arraysEqual, groupBy } from './utils/array';
7
+ import { loadVersionBrowser, iOS } from './utils/browser';
8
+ import { LocalStorageDB } from './utils/localStorage';
7
9
  import LRUCache, { LruCache, LruCacheType, CacheStrategies } from './utils/cache';
8
10
  import { Logger } from './utils/logger';
9
11
  import WSClient, { WSClientType, WSClientPropTypes } from './utils/websocket';
@@ -11,5 +13,4 @@ import { resizeImage } from './utils/image';
11
13
  /**
12
14
  * Export all utilities
13
15
  */
14
- export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, arraysEqual, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser, LRUCache, LruCache, LruCacheType, CacheStrategies, resizeImage };
15
- //# sourceMappingURL=index.d.ts.map
16
+ export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, isFunc, isInteger, arraysEqual, groupBy, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser, iOS, LocalStorageDB, LRUCache, LruCache, LruCacheType, CacheStrategies, resizeImage };
package/lib/cjs/index.js CHANGED
@@ -1,32 +1,7 @@
1
1
  "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || function (mod) {
19
- if (mod && mod.__esModule) return mod;
20
- var result = {};
21
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
- __setModuleDefault(result, mod);
23
- return result;
24
- };
25
- var __importDefault = (this && this.__importDefault) || function (mod) {
26
- return (mod && mod.__esModule) ? mod : { "default": mod };
27
- };
28
2
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.resizeImage = exports.CacheStrategies = exports.LruCache = exports.LRUCache = exports.loadVersionBrowser = exports.WSClient = exports.arraysEqual = exports.objectWithoutProperties = exports.isObject = exports.mergeDeep = exports.Logger = exports.isClientSideRendering = exports.getWindowHeight = exports.getWindowWidth = exports.getHighestSafeWindowContext = exports.updateQueryStringParameter = exports.getQueryStringParameter = exports.urlB64ToUint8Array = exports.appendURLSearchParams = exports.getDomain = exports.urlReplacer = exports.isValidUrls = exports.isValidUrl = exports.slugify = exports.random = exports.fallbackCopyTextToClipboard = exports.copyTextToClipboard = exports.camelCase = exports.stripHtml = exports.isString = exports.capitalize = void 0;
3
+ exports.resizeImage = exports.CacheStrategies = exports.LruCache = exports.LRUCache = exports.LocalStorageDB = exports.iOS = exports.loadVersionBrowser = exports.WSClient = exports.groupBy = exports.arraysEqual = exports.isInteger = exports.isFunc = exports.objectWithoutProperties = exports.isObject = exports.mergeDeep = exports.Logger = exports.isClientSideRendering = exports.getWindowHeight = exports.getWindowWidth = exports.getHighestSafeWindowContext = exports.updateQueryStringParameter = exports.getQueryStringParameter = exports.urlB64ToUint8Array = exports.appendURLSearchParams = exports.getDomain = exports.urlReplacer = exports.isValidUrls = exports.isValidUrl = exports.slugify = exports.random = exports.fallbackCopyTextToClipboard = exports.copyTextToClipboard = exports.camelCase = exports.stripHtml = exports.isString = exports.capitalize = void 0;
4
+ const tslib_1 = require("tslib");
30
5
  const string_1 = require("./utils/string");
31
6
  Object.defineProperty(exports, "capitalize", { enumerable: true, get: function () { return string_1.capitalize; } });
32
7
  Object.defineProperty(exports, "isString", { enumerable: true, get: function () { return string_1.isString; } });
@@ -54,18 +29,24 @@ const object_1 = require("./utils/object");
54
29
  Object.defineProperty(exports, "mergeDeep", { enumerable: true, get: function () { return object_1.mergeDeep; } });
55
30
  Object.defineProperty(exports, "isObject", { enumerable: true, get: function () { return object_1.isObject; } });
56
31
  Object.defineProperty(exports, "objectWithoutProperties", { enumerable: true, get: function () { return object_1.objectWithoutProperties; } });
32
+ Object.defineProperty(exports, "isFunc", { enumerable: true, get: function () { return object_1.isFunc; } });
33
+ const number_1 = require("./utils/number");
34
+ Object.defineProperty(exports, "isInteger", { enumerable: true, get: function () { return number_1.isInteger; } });
57
35
  const array_1 = require("./utils/array");
58
36
  Object.defineProperty(exports, "arraysEqual", { enumerable: true, get: function () { return array_1.arraysEqual; } });
37
+ Object.defineProperty(exports, "groupBy", { enumerable: true, get: function () { return array_1.groupBy; } });
59
38
  const browser_1 = require("./utils/browser");
60
39
  Object.defineProperty(exports, "loadVersionBrowser", { enumerable: true, get: function () { return browser_1.loadVersionBrowser; } });
61
- const cache_1 = __importStar(require("./utils/cache"));
40
+ Object.defineProperty(exports, "iOS", { enumerable: true, get: function () { return browser_1.iOS; } });
41
+ const localStorage_1 = require("./utils/localStorage");
42
+ Object.defineProperty(exports, "LocalStorageDB", { enumerable: true, get: function () { return localStorage_1.LocalStorageDB; } });
43
+ const cache_1 = tslib_1.__importStar(require("./utils/cache"));
62
44
  exports.LRUCache = cache_1.default;
63
45
  Object.defineProperty(exports, "LruCache", { enumerable: true, get: function () { return cache_1.LruCache; } });
64
46
  Object.defineProperty(exports, "CacheStrategies", { enumerable: true, get: function () { return cache_1.CacheStrategies; } });
65
47
  const logger_1 = require("./utils/logger");
66
48
  Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_1.Logger; } });
67
- const websocket_1 = __importDefault(require("./utils/websocket"));
49
+ const websocket_1 = tslib_1.__importDefault(require("./utils/websocket"));
68
50
  exports.WSClient = websocket_1.default;
69
51
  const image_1 = require("./utils/image");
70
52
  Object.defineProperty(exports, "resizeImage", { enumerable: true, get: function () { return image_1.resizeImage; } });
71
- //# sourceMappingURL=index.js.map
@@ -5,4 +5,10 @@
5
5
  * @return boolean
6
6
  */
7
7
  export declare const arraysEqual: (a: any, b: any) => boolean;
8
- //# sourceMappingURL=array.d.ts.map
8
+ /**
9
+ * Group items from an array together by some criteria or value.
10
+ * @param {Array} arr The array to group items from
11
+ * @param {String|Function} criteria The criteria to group by
12
+ * @return {Object} The grouped object
13
+ */
14
+ export declare const groupBy: (arr: any, criteria: any) => any;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.arraysEqual = void 0;
3
+ exports.groupBy = exports.arraysEqual = void 0;
4
4
  /**
5
5
  * Check if two arrays are equal
6
6
  * @param a
@@ -25,4 +25,25 @@ const arraysEqual = (a, b) => {
25
25
  return true;
26
26
  };
27
27
  exports.arraysEqual = arraysEqual;
28
- //# sourceMappingURL=array.js.map
28
+ /**
29
+ * Group items from an array together by some criteria or value.
30
+ * @param {Array} arr The array to group items from
31
+ * @param {String|Function} criteria The criteria to group by
32
+ * @return {Object} The grouped object
33
+ */
34
+ const groupBy = (arr, criteria) => {
35
+ return arr.reduce(function (obj, item) {
36
+ // Check if the criteria is a function to run on the item or a property of it
37
+ let key = typeof criteria === 'function' ? criteria(item) : item[criteria];
38
+ // If the key doesn't exist yet, create it
39
+ // eslint-disable-next-line no-prototype-builtins
40
+ if (!obj.hasOwnProperty(key)) {
41
+ obj[key] = [];
42
+ }
43
+ // Push the value to the object
44
+ obj[key].push(item);
45
+ // Return the object to the next item in the loop
46
+ return obj;
47
+ }, {});
48
+ };
49
+ exports.groupBy = groupBy;
@@ -6,4 +6,4 @@ export declare const loadVersionBrowser: () => {
6
6
  name: any;
7
7
  version: any;
8
8
  };
9
- //# sourceMappingURL=browser.d.ts.map
9
+ export declare const iOS: () => boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.loadVersionBrowser = void 0;
3
+ exports.iOS = exports.loadVersionBrowser = void 0;
4
4
  /**
5
5
  * Get browser version to track on backend
6
6
  * @return {{name: string, version: null}|{name: string, version: string}|{name: *, version: *}|{name: string, version}}
@@ -31,7 +31,7 @@ const loadVersionBrowser = () => {
31
31
  browsername = brand.substr(brand.indexOf(' ') + 1);
32
32
  return {
33
33
  name: browsername,
34
- version: browserversion,
34
+ version: browserversion
35
35
  };
36
36
  }
37
37
  }
@@ -40,7 +40,7 @@ const loadVersionBrowser = () => {
40
40
  if (chromeVersion !== null) {
41
41
  return {
42
42
  name: 'chrome',
43
- version: chromeVersion,
43
+ version: chromeVersion
44
44
  };
45
45
  }
46
46
  }
@@ -67,8 +67,13 @@ const loadVersionBrowser = () => {
67
67
  }
68
68
  return {
69
69
  name: M[0],
70
- version: M[1],
70
+ version: M[1]
71
71
  };
72
72
  };
73
73
  exports.loadVersionBrowser = loadVersionBrowser;
74
- //# sourceMappingURL=browser.js.map
74
+ const iOS = () => {
75
+ return (['iPad Simulator', 'iPhone Simulator', 'iPod Simulator', 'iPad', 'iPhone', 'iPod'].includes(navigator.platform) ||
76
+ // iPad on iOS 13 detection
77
+ (navigator.userAgent.includes('Mac') && 'ontouchend' in document));
78
+ };
79
+ exports.iOS = iOS;
@@ -2,8 +2,12 @@
2
2
  * LruCache interface
3
3
  */
4
4
  export interface LruCacheType<T> {
5
- get: (key: string, value?: T) => T;
6
- set: (key: string, value: T) => void;
5
+ get: (key: string, value?: T, options?: {
6
+ noSsr: boolean;
7
+ }) => T;
8
+ set: (key: string, value: T, options?: {
9
+ noSsr: boolean;
10
+ }) => void;
7
11
  hasKey: (key: string) => boolean;
8
12
  delete: (key: string) => void;
9
13
  deleteKeys: (keys: string[]) => void;
@@ -17,6 +21,7 @@ export interface LruCacheType<T> {
17
21
  export declare class LruCache<T> {
18
22
  private values;
19
23
  private maxEntries;
24
+ private ssr;
20
25
  /**
21
26
  * Initialize Cache
22
27
  * @param maxEntries
@@ -26,14 +31,20 @@ export declare class LruCache<T> {
26
31
  * Get a key from the map store
27
32
  * @param key
28
33
  * @param value
34
+ * @param options
29
35
  */
30
- get(key: string, value?: T): T;
36
+ get(key: string, value?: T, options?: {
37
+ noSsr: boolean;
38
+ }): T;
31
39
  /**
32
40
  * Set a key in the store
33
41
  * @param key
34
42
  * @param value
43
+ * @param options
35
44
  */
36
- set(key: string, value: T): void;
45
+ set(key: string, value: T, options?: {
46
+ noSsr: boolean;
47
+ }): void;
37
48
  /**
38
49
  * Check if key is in cache
39
50
  * @param key
@@ -77,4 +88,3 @@ export declare enum CacheStrategies {
77
88
  */
78
89
  declare const cache: LruCacheType<any>;
79
90
  export default cache;
80
- //# sourceMappingURL=cache.d.ts.map
@@ -12,7 +12,8 @@ class LruCache {
12
12
  constructor(maxEntries = 10000) {
13
13
  this.values = new Map();
14
14
  this.maxEntries = maxEntries;
15
- if (typeof window !== 'undefined') {
15
+ this.ssr = typeof window === 'undefined';
16
+ if (!this.ssr) {
16
17
  window['__viewSCCache'] = this.values;
17
18
  }
18
19
  }
@@ -20,8 +21,9 @@ class LruCache {
20
21
  * Get a key from the map store
21
22
  * @param key
22
23
  * @param value
24
+ * @param options
23
25
  */
24
- get(key, value) {
26
+ get(key, value, options = { noSsr: true }) {
25
27
  const hasKey = this.values.has(key);
26
28
  let entry;
27
29
  if (hasKey) {
@@ -33,7 +35,7 @@ class LruCache {
33
35
  else if (value) {
34
36
  // insert value if passed
35
37
  entry = value;
36
- this.values.set(key, entry);
38
+ !(this.ssr && options.noSsr) && this.values.set(key, entry);
37
39
  }
38
40
  return entry;
39
41
  }
@@ -41,8 +43,12 @@ class LruCache {
41
43
  * Set a key in the store
42
44
  * @param key
43
45
  * @param value
46
+ * @param options
44
47
  */
45
- set(key, value) {
48
+ set(key, value, options = { noSsr: true }) {
49
+ if (this.ssr && options.noSsr) {
50
+ return;
51
+ }
46
52
  if (this.values.size >= this.maxEntries) {
47
53
  // least-recently used cache eviction strategy
48
54
  const keyToDelete = this.values.keys().next().value;
@@ -119,4 +125,3 @@ var CacheStrategies;
119
125
  */
120
126
  const cache = new LruCache();
121
127
  exports.default = cache;
122
- //# sourceMappingURL=cache.js.map
@@ -1,3 +1,2 @@
1
1
  declare const resizeImage: (file: any) => Promise<unknown>;
2
2
  export { resizeImage };
3
- //# sourceMappingURL=image.d.ts.map
@@ -47,4 +47,3 @@ const resizeImage = (file) => new Promise((resolve, reject) => {
47
47
  reader.readAsDataURL(file);
48
48
  });
49
49
  exports.resizeImage = resizeImage;
50
- //# sourceMappingURL=image.js.map
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Manage window.localStorage
3
+ */
4
+ export declare class LocalStorageDB {
5
+ /**
6
+ * Set a key in window.localStorage
7
+ * @param key
8
+ * @param value
9
+ */
10
+ static set(key: any, value: any): void;
11
+ /**
12
+ * Get a key from window.localStorage, else return null
13
+ * @param key
14
+ */
15
+ static get(key: any): any;
16
+ /**
17
+ * Get all keys
18
+ */
19
+ static getAll(): any[];
20
+ /**
21
+ * Remove a single item from window.localStorage
22
+ * @param key
23
+ */
24
+ static remove(key: any): void;
25
+ /**
26
+ * Clear all keys from window.localStorage
27
+ */
28
+ static clearAll(): void;
29
+ /**
30
+ * Check if localStorage is supported
31
+ */
32
+ static checkifSupport(): boolean;
33
+ }
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LocalStorageDB = void 0;
4
+ /**
5
+ * Manage window.localStorage
6
+ */
7
+ class LocalStorageDB {
8
+ /**
9
+ * Set a key in window.localStorage
10
+ * @param key
11
+ * @param value
12
+ */
13
+ static set(key, value) {
14
+ if (this.checkifSupport()) {
15
+ try {
16
+ window.localStorage.setItem(key, value);
17
+ }
18
+ catch (e) {
19
+ console.error(e);
20
+ }
21
+ }
22
+ else {
23
+ console.error('No support. Use a fallback such as browser cookies or store on the server.');
24
+ }
25
+ }
26
+ /**
27
+ * Get a key from window.localStorage, else return null
28
+ * @param key
29
+ */
30
+ static get(key) {
31
+ try {
32
+ let data = window.localStorage.getItem(key);
33
+ if (data && typeof data === 'object') {
34
+ return JSON.parse(data);
35
+ }
36
+ else {
37
+ return data;
38
+ }
39
+ }
40
+ catch (e) {
41
+ return null;
42
+ }
43
+ }
44
+ /**
45
+ * Get all keys
46
+ */
47
+ static getAll() {
48
+ let array = [];
49
+ for (let i = 0; i < window.localStorage.length; i++) {
50
+ let key = localStorage.key(i);
51
+ array.push(this.get(key));
52
+ }
53
+ return array;
54
+ }
55
+ /**
56
+ * Remove a single item from window.localStorage
57
+ * @param key
58
+ */
59
+ static remove(key) {
60
+ try {
61
+ window.localStorage.removeItem(key);
62
+ if (window.localStorage.length == 0) {
63
+ this.clearAll();
64
+ }
65
+ }
66
+ catch (e) {
67
+ console.error(e);
68
+ }
69
+ finally {
70
+ if (this.get(key)) {
71
+ delete window.localStorage[key];
72
+ if (window.localStorage.length == 0) {
73
+ this.clearAll();
74
+ }
75
+ }
76
+ }
77
+ }
78
+ /**
79
+ * Clear all keys from window.localStorage
80
+ */
81
+ static clearAll() {
82
+ try {
83
+ window.localStorage.clear();
84
+ }
85
+ catch (e) {
86
+ console.error(e);
87
+ }
88
+ }
89
+ /**
90
+ * Check if localStorage is supported
91
+ */
92
+ static checkifSupport() {
93
+ try {
94
+ return 'localStorage' in window && window['localStorage'] !== null;
95
+ }
96
+ catch (e) {
97
+ return false;
98
+ }
99
+ }
100
+ }
101
+ exports.LocalStorageDB = LocalStorageDB;
@@ -8,4 +8,3 @@ export declare class Logger {
8
8
  static log(scope: any, message: any): void;
9
9
  static debug(scope: any, message: any): void;
10
10
  }
11
- //# sourceMappingURL=logger.d.ts.map
@@ -22,4 +22,3 @@ class Logger {
22
22
  }
23
23
  }
24
24
  exports.Logger = Logger;
25
- //# sourceMappingURL=logger.js.map
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Check if v is an integer number
3
+ * @param v
4
+ */
5
+ export declare function isInteger(v: any): boolean;
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isInteger = void 0;
4
+ /**
5
+ * Check if v is an integer number
6
+ * @param v
7
+ */
8
+ function isInteger(v) {
9
+ return !isNaN(v) && parseInt(String(Number(v))) == v && !isNaN(parseInt(String(v), 10));
10
+ }
11
+ exports.isInteger = isInteger;
@@ -16,4 +16,8 @@ export declare function mergeDeep(target: object, source: object): object;
16
16
  * @param keys
17
17
  */
18
18
  export declare function objectWithoutProperties<T>(obj: T | null, keys: string[]): T;
19
- //# sourceMappingURL=object.d.ts.map
19
+ /**
20
+ * Check if v is a func
21
+ * @param v
22
+ */
23
+ export declare function isFunc(v: any): boolean;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.objectWithoutProperties = exports.mergeDeep = exports.isObject = void 0;
3
+ exports.isFunc = exports.objectWithoutProperties = exports.mergeDeep = exports.isObject = void 0;
4
4
  /**
5
5
  * Check if v is an object
6
6
  * @param v
@@ -50,4 +50,11 @@ function objectWithoutProperties(obj, keys) {
50
50
  return target;
51
51
  }
52
52
  exports.objectWithoutProperties = objectWithoutProperties;
53
- //# sourceMappingURL=object.js.map
53
+ /**
54
+ * Check if v is a func
55
+ * @param v
56
+ */
57
+ function isFunc(v) {
58
+ return typeof v === 'function';
59
+ }
60
+ exports.isFunc = isFunc;
@@ -41,4 +41,3 @@ export declare function fallbackCopyTextToClipboard(text: any): Promise<void>;
41
41
  */
42
42
  export declare function copyTextToClipboard(text: any): Promise<void>;
43
43
  export declare function slugify(str: any): any;
44
- //# sourceMappingURL=string.d.ts.map
@@ -111,4 +111,3 @@ function slugify(str) {
111
111
  .replace(/^-+|-+$/g, '');
112
112
  }
113
113
  exports.slugify = slugify;
114
- //# sourceMappingURL=string.js.map
@@ -38,4 +38,3 @@ export declare const getQueryStringParameter: (uri: any, key: any) => string;
38
38
  * Add or update a query string parameter
39
39
  */
40
40
  export declare const updateQueryStringParameter: (uri: any, key: any, value: any) => any;
41
- //# sourceMappingURL=url.d.ts.map
@@ -112,4 +112,3 @@ const updateQueryStringParameter = (uri, key, value) => {
112
112
  return uri;
113
113
  };
114
114
  exports.updateQueryStringParameter = updateQueryStringParameter;
115
- //# sourceMappingURL=url.js.map
@@ -174,4 +174,3 @@ export default class WSClient implements WSClientType {
174
174
  */
175
175
  close(): void;
176
176
  }
177
- //# sourceMappingURL=websocket.d.ts.map
@@ -241,4 +241,3 @@ class WSClient {
241
241
  }
242
242
  }
243
243
  exports.default = WSClient;
244
- //# sourceMappingURL=websocket.js.map
@@ -2,4 +2,3 @@ export declare function getWindowWidth(): number;
2
2
  export declare function getWindowHeight(): number;
3
3
  export declare function getHighestSafeWindowContext(self?: Window & typeof globalThis): any;
4
4
  export declare function isClientSideRendering(): boolean;
5
- //# sourceMappingURL=window.d.ts.map
@@ -40,4 +40,3 @@ function isClientSideRendering() {
40
40
  return typeof window !== 'undefined';
41
41
  }
42
42
  exports.isClientSideRendering = isClientSideRendering;
43
- //# sourceMappingURL=window.js.map
@@ -1,9 +1,11 @@
1
1
  import { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify } from './utils/string';
2
2
  import { isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter } from './utils/url';
3
3
  import { getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering } from './utils/window';
4
- import { mergeDeep, isObject, objectWithoutProperties } from './utils/object';
5
- import { arraysEqual } from './utils/array';
6
- import { loadVersionBrowser } from './utils/browser';
4
+ import { mergeDeep, isObject, objectWithoutProperties, isFunc } from './utils/object';
5
+ import { isInteger } from './utils/number';
6
+ import { arraysEqual, groupBy } from './utils/array';
7
+ import { loadVersionBrowser, iOS } from './utils/browser';
8
+ import { LocalStorageDB } from './utils/localStorage';
7
9
  import LRUCache, { LruCache, LruCacheType, CacheStrategies } from './utils/cache';
8
10
  import { Logger } from './utils/logger';
9
11
  import WSClient, { WSClientType, WSClientPropTypes } from './utils/websocket';
@@ -11,5 +13,4 @@ import { resizeImage } from './utils/image';
11
13
  /**
12
14
  * Export all utilities
13
15
  */
14
- export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, arraysEqual, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser, LRUCache, LruCache, LruCacheType, CacheStrategies, resizeImage };
15
- //# sourceMappingURL=index.d.ts.map
16
+ export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, isFunc, isInteger, arraysEqual, groupBy, WSClient, WSClientType, WSClientPropTypes, loadVersionBrowser, iOS, LocalStorageDB, LRUCache, LruCache, LruCacheType, CacheStrategies, resizeImage };
package/lib/esm/index.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify } from './utils/string';
2
2
  import { isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter } from './utils/url';
3
3
  import { getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering } from './utils/window';
4
- import { mergeDeep, isObject, objectWithoutProperties } from './utils/object';
5
- import { arraysEqual } from './utils/array';
6
- import { loadVersionBrowser } from './utils/browser';
4
+ import { mergeDeep, isObject, objectWithoutProperties, isFunc } from './utils/object';
5
+ import { isInteger } from './utils/number';
6
+ import { arraysEqual, groupBy } from './utils/array';
7
+ import { loadVersionBrowser, iOS } from './utils/browser';
8
+ import { LocalStorageDB } from './utils/localStorage';
7
9
  import LRUCache, { LruCache, CacheStrategies } from './utils/cache';
8
10
  import { Logger } from './utils/logger';
9
11
  import WSClient from './utils/websocket';
@@ -11,5 +13,4 @@ import { resizeImage } from './utils/image';
11
13
  /**
12
14
  * Export all utilities
13
15
  */
14
- export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, arraysEqual, WSClient, loadVersionBrowser, LRUCache, LruCache, CacheStrategies, resizeImage };
15
- //# sourceMappingURL=index.js.map
16
+ export { capitalize, isString, stripHtml, camelCase, copyTextToClipboard, fallbackCopyTextToClipboard, random, slugify, isValidUrl, isValidUrls, urlReplacer, getDomain, appendURLSearchParams, urlB64ToUint8Array, getQueryStringParameter, updateQueryStringParameter, getHighestSafeWindowContext, getWindowWidth, getWindowHeight, isClientSideRendering, Logger, mergeDeep, isObject, objectWithoutProperties, isFunc, isInteger, arraysEqual, groupBy, WSClient, loadVersionBrowser, iOS, LocalStorageDB, LRUCache, LruCache, CacheStrategies, resizeImage };
@@ -5,4 +5,10 @@
5
5
  * @return boolean
6
6
  */
7
7
  export declare const arraysEqual: (a: any, b: any) => boolean;
8
- //# sourceMappingURL=array.d.ts.map
8
+ /**
9
+ * Group items from an array together by some criteria or value.
10
+ * @param {Array} arr The array to group items from
11
+ * @param {String|Function} criteria The criteria to group by
12
+ * @return {Object} The grouped object
13
+ */
14
+ export declare const groupBy: (arr: any, criteria: any) => any;
@@ -21,4 +21,24 @@ export const arraysEqual = (a, b) => {
21
21
  }
22
22
  return true;
23
23
  };
24
- //# sourceMappingURL=array.js.map
24
+ /**
25
+ * Group items from an array together by some criteria or value.
26
+ * @param {Array} arr The array to group items from
27
+ * @param {String|Function} criteria The criteria to group by
28
+ * @return {Object} The grouped object
29
+ */
30
+ export const groupBy = (arr, criteria) => {
31
+ return arr.reduce(function (obj, item) {
32
+ // Check if the criteria is a function to run on the item or a property of it
33
+ let key = typeof criteria === 'function' ? criteria(item) : item[criteria];
34
+ // If the key doesn't exist yet, create it
35
+ // eslint-disable-next-line no-prototype-builtins
36
+ if (!obj.hasOwnProperty(key)) {
37
+ obj[key] = [];
38
+ }
39
+ // Push the value to the object
40
+ obj[key].push(item);
41
+ // Return the object to the next item in the loop
42
+ return obj;
43
+ }, {});
44
+ };
@@ -6,4 +6,4 @@ export declare const loadVersionBrowser: () => {
6
6
  name: any;
7
7
  version: any;
8
8
  };
9
- //# sourceMappingURL=browser.d.ts.map
9
+ export declare const iOS: () => boolean;