@koine/utils 1.0.72 → 1.0.75

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.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Capitalize first letter of the given string.
3
+ *
4
+ * @category text
5
+ * @see https://stackoverflow.com/a/11409944/1938970
6
+ */
7
+ export declare function capitalize<T extends string>(text?: null | T): "" | Capitalize<T>;
8
+ export default capitalize;
package/capitalize.js ADDED
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Capitalize first letter of the given string.
3
+ *
4
+ * @category text
5
+ * @see https://stackoverflow.com/a/11409944/1938970
6
+ */
7
+ export function capitalize(text) {
8
+ return text
9
+ ? (text.charAt(0).toUpperCase() + text.slice(1))
10
+ : "";
11
+ }
12
+ export default capitalize;
@@ -1,6 +1,3 @@
1
- declare type NullableRecord<T> = {
2
- [K in keyof T]: T[K] | null;
3
- };
4
1
  export declare type CreateStorageConfig = Record<string, any>;
5
2
  /**
6
3
  * Utility to create a storage instance to interact with `localStorage` using
@@ -16,8 +13,10 @@ export declare const createStorage: <T extends CreateStorageConfig>(config: Part
16
13
  get<TKey extends keyof T>(key: TKey): T[TKey] | null;
17
14
  /**
18
15
  * Get all storage values (it uses `localStorage.get()`).
16
+ *
17
+ * `undefined` and `null` values are not returned.
19
18
  */
20
- getAll(): NullableRecord<T>;
19
+ getAll(): T;
21
20
  /**
22
21
  * Set a storage value (it uses `localStorage.set()`).
23
22
  *
package/createStorage.js CHANGED
@@ -2,7 +2,7 @@ import { __assign } from "tslib";
2
2
  import { decode } from "./decode";
3
3
  import { encode } from "./encode";
4
4
  import { isBrowser } from "./isBrowser";
5
- import { isUndefined } from "./isUndefined";
5
+ import { isNullOrUndefined } from "./isNullOrUndefined";
6
6
  import { isString } from "./isString";
7
7
  /**
8
8
  * Utility to create a storage instance to interact with `localStorage` using
@@ -48,11 +48,16 @@ export var createStorage = function (config) {
48
48
  },
49
49
  /**
50
50
  * Get all storage values (it uses `localStorage.get()`).
51
+ *
52
+ * `undefined` and `null` values are not returned.
51
53
  */
52
54
  getAll: function () {
53
55
  var all = {};
54
56
  for (var key in keys) {
55
- all[key] = this.get(key);
57
+ var value = this.get(key);
58
+ if (!isNullOrUndefined(value)) {
59
+ all[key] = value;
60
+ }
56
61
  }
57
62
  return all;
58
63
  },
@@ -73,7 +78,7 @@ export var createStorage = function (config) {
73
78
  setMany: function (newValues) {
74
79
  for (var key in newValues) {
75
80
  var value = newValues[key];
76
- if (!isUndefined(value)) {
81
+ if (!isNullOrUndefined(value)) {
77
82
  this.set(key, value);
78
83
  }
79
84
  else {
@@ -0,0 +1,9 @@
1
+ /**
2
+ *
3
+ * Returns an array of undefined values of the desired length, useful to build
4
+ * skeleton UIs.
5
+ *
6
+ * @category array
7
+ */
8
+ export declare function getEmptyArray(length: number | string): undefined[];
9
+ export default getEmptyArray;
@@ -0,0 +1,15 @@
1
+ import isNumber from "./isNumber";
2
+ /**
3
+ *
4
+ * Returns an array of undefined values of the desired length, useful to build
5
+ * skeleton UIs.
6
+ *
7
+ * @category array
8
+ */
9
+ export function getEmptyArray /* <T extends undefined | null = undefined> */(length) {
10
+ if (!isNumber(length)) {
11
+ length = parseInt(length, 10);
12
+ }
13
+ return Array.from({ length: length });
14
+ }
15
+ export default getEmptyArray;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
3
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
4
+ * router and elsewhere)
5
+ *
6
+ * @category location
7
+ *
8
+ * @param {string} [raw] - The _raw_ query parameter
9
+ * @param {string[]} [allowedValues=[]] - The list of values (as strings) that the parameter can have, if not one of them `null` is returned
10
+ */
11
+ export declare function getParamAmong<T extends string[]>(raw?: string | string[], allowedValues?: T): T[number] | null;
12
+ export default getParamAmong;
@@ -0,0 +1,17 @@
1
+ import getParamAsString from "./getParamAsString";
2
+ /**
3
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
4
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
5
+ * router and elsewhere)
6
+ *
7
+ * @category location
8
+ *
9
+ * @param {string} [raw] - The _raw_ query parameter
10
+ * @param {string[]} [allowedValues=[]] - The list of values (as strings) that the parameter can have, if not one of them `null` is returned
11
+ */
12
+ export function getParamAmong(raw, allowedValues) {
13
+ if (allowedValues === void 0) { allowedValues = []; }
14
+ var string = getParamAsString(raw);
15
+ return allowedValues.includes(string) ? string : null;
16
+ }
17
+ export default getParamAmong;
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Get query parameter as `int`eger treating the `ParsedUrlQuery` result of
3
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
4
+ * router and elsewhere)
5
+ *
6
+ * @category location
7
+ *
8
+ * @param {string} [raw] - The _raw_ query parameter
9
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
10
+ */
11
+ export declare function getParamAsInt(raw?: string | string[], fallback?: number): number | null;
12
+ export default getParamAsInt;
@@ -0,0 +1,19 @@
1
+ import getParamAsString from "./getParamAsString";
2
+ import isUndefined from "./isUndefined";
3
+ /**
4
+ * Get query parameter as `int`eger treating the `ParsedUrlQuery` result of
5
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
6
+ * router and elsewhere)
7
+ *
8
+ * @category location
9
+ *
10
+ * @param {string} [raw] - The _raw_ query parameter
11
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
12
+ */
13
+ export function getParamAsInt(raw, fallback) {
14
+ var string = getParamAsString(raw);
15
+ if (string)
16
+ parseInt(string, 10);
17
+ return isUndefined(fallback) ? null : fallback;
18
+ }
19
+ export default getParamAsInt;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
3
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
4
+ * router and elsewhere)
5
+ *
6
+ * @category location
7
+ *
8
+ * @param {string} [raw] - The _raw_ query parameter
9
+ */
10
+ export declare function getParamAsString(raw?: string | string[]): string;
11
+ export default getParamAsString;
@@ -0,0 +1,14 @@
1
+ import isArray from "./isArray";
2
+ /**
3
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
4
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
5
+ * router and elsewhere)
6
+ *
7
+ * @category location
8
+ *
9
+ * @param {string} [raw] - The _raw_ query parameter
10
+ */
11
+ export function getParamAsString(raw) {
12
+ return (isArray(raw) ? raw[0] : raw) || "";
13
+ }
14
+ export default getParamAsString;
package/index.d.ts CHANGED
@@ -2,6 +2,7 @@ export * from "./accentSets";
2
2
  export * from "./addOrReplaceAtIdx";
3
3
  export * from "./arrayToLookup";
4
4
  export * from "./buildUrlQueryString";
5
+ export * from "./capitalize";
5
6
  export * from "./changeUrlPath";
6
7
  export * from "./chunkByChunks";
7
8
  export * from "./chunkBySize";
@@ -16,8 +17,12 @@ export * from "./Emitter";
16
17
  export * from "./encode";
17
18
  export * from "./ensureInt";
18
19
  export * from "./findDuplicatedIndexes";
20
+ export * from "./getEmptyArray";
19
21
  export * from "./getKeys";
20
22
  export * from "./getNonce";
23
+ export * from "./getParamAmong";
24
+ export * from "./getParamAsInt";
25
+ export * from "./getParamAsString";
21
26
  export * from "./getType";
22
27
  export * from "./getUrlHashParams";
23
28
  export * from "./getUrlHashPathname";
package/index.js CHANGED
@@ -2,6 +2,7 @@ export * from "./accentSets";
2
2
  export * from "./addOrReplaceAtIdx";
3
3
  export * from "./arrayToLookup";
4
4
  export * from "./buildUrlQueryString";
5
+ export * from "./capitalize";
5
6
  export * from "./changeUrlPath";
6
7
  export * from "./chunkByChunks";
7
8
  export * from "./chunkBySize";
@@ -17,8 +18,12 @@ export * from "./encode";
17
18
  export * from "./ensureInt";
18
19
  // export * from "./env"
19
20
  export * from "./findDuplicatedIndexes";
21
+ export * from "./getEmptyArray";
20
22
  export * from "./getKeys";
21
23
  export * from "./getNonce";
24
+ export * from "./getParamAmong";
25
+ export * from "./getParamAsInt";
26
+ export * from "./getParamAsString";
22
27
  export * from "./getType";
23
28
  export * from "./getUrlHashParams";
24
29
  export * from "./getUrlHashPathname";
@@ -92,6 +97,7 @@ export * from "./readCookie";
92
97
  export * from "./redirectTo";
93
98
  export * from "./removeAccents";
94
99
  export * from "./removeCookie";
100
+ // export * from "./removeDuplicates";
95
101
  export * from "./removeDuplicatesByKey";
96
102
  export * from "./removeDuplicatesComparing";
97
103
  export * from "./removeIndexesFromArray";
@@ -0,0 +1,16 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.capitalize = void 0;
4
+ /**
5
+ * Capitalize first letter of the given string.
6
+ *
7
+ * @category text
8
+ * @see https://stackoverflow.com/a/11409944/1938970
9
+ */
10
+ function capitalize(text) {
11
+ return text
12
+ ? (text.charAt(0).toUpperCase() + text.slice(1))
13
+ : "";
14
+ }
15
+ exports.capitalize = capitalize;
16
+ exports.default = capitalize;
@@ -5,7 +5,7 @@ var tslib_1 = require("tslib");
5
5
  var decode_1 = require("./decode");
6
6
  var encode_1 = require("./encode");
7
7
  var isBrowser_1 = require("./isBrowser");
8
- var isUndefined_1 = require("./isUndefined");
8
+ var isNullOrUndefined_1 = require("./isNullOrUndefined");
9
9
  var isString_1 = require("./isString");
10
10
  /**
11
11
  * Utility to create a storage instance to interact with `localStorage` using
@@ -51,11 +51,16 @@ var createStorage = function (config) {
51
51
  },
52
52
  /**
53
53
  * Get all storage values (it uses `localStorage.get()`).
54
+ *
55
+ * `undefined` and `null` values are not returned.
54
56
  */
55
57
  getAll: function () {
56
58
  var all = {};
57
59
  for (var key in keys) {
58
- all[key] = this.get(key);
60
+ var value = this.get(key);
61
+ if (!(0, isNullOrUndefined_1.isNullOrUndefined)(value)) {
62
+ all[key] = value;
63
+ }
59
64
  }
60
65
  return all;
61
66
  },
@@ -76,7 +81,7 @@ var createStorage = function (config) {
76
81
  setMany: function (newValues) {
77
82
  for (var key in newValues) {
78
83
  var value = newValues[key];
79
- if (!(0, isUndefined_1.isUndefined)(value)) {
84
+ if (!(0, isNullOrUndefined_1.isNullOrUndefined)(value)) {
80
85
  this.set(key, value);
81
86
  }
82
87
  else {
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEmptyArray = void 0;
4
+ var isNumber_1 = require("./isNumber");
5
+ /**
6
+ *
7
+ * Returns an array of undefined values of the desired length, useful to build
8
+ * skeleton UIs.
9
+ *
10
+ * @category array
11
+ */
12
+ function getEmptyArray /* <T extends undefined | null = undefined> */(length) {
13
+ if (!(0, isNumber_1.default)(length)) {
14
+ length = parseInt(length, 10);
15
+ }
16
+ return Array.from({ length: length });
17
+ }
18
+ exports.getEmptyArray = getEmptyArray;
19
+ exports.default = getEmptyArray;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParamAmong = void 0;
4
+ var getParamAsString_1 = require("./getParamAsString");
5
+ /**
6
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
7
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
8
+ * router and elsewhere)
9
+ *
10
+ * @category location
11
+ *
12
+ * @param {string} [raw] - The _raw_ query parameter
13
+ * @param {string[]} [allowedValues=[]] - The list of values (as strings) that the parameter can have, if not one of them `null` is returned
14
+ */
15
+ function getParamAmong(raw, allowedValues) {
16
+ if (allowedValues === void 0) { allowedValues = []; }
17
+ var string = (0, getParamAsString_1.default)(raw);
18
+ return allowedValues.includes(string) ? string : null;
19
+ }
20
+ exports.getParamAmong = getParamAmong;
21
+ exports.default = getParamAmong;
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParamAsInt = void 0;
4
+ var getParamAsString_1 = require("./getParamAsString");
5
+ var isUndefined_1 = require("./isUndefined");
6
+ /**
7
+ * Get query parameter as `int`eger treating the `ParsedUrlQuery` result of
8
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
9
+ * router and elsewhere)
10
+ *
11
+ * @category location
12
+ *
13
+ * @param {string} [raw] - The _raw_ query parameter
14
+ * @param {number} [fallback] - Fallback number, we return `null` if not provided
15
+ */
16
+ function getParamAsInt(raw, fallback) {
17
+ var string = (0, getParamAsString_1.default)(raw);
18
+ if (string)
19
+ parseInt(string, 10);
20
+ return (0, isUndefined_1.default)(fallback) ? null : fallback;
21
+ }
22
+ exports.getParamAsInt = getParamAsInt;
23
+ exports.default = getParamAsInt;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getParamAsString = void 0;
4
+ var isArray_1 = require("./isArray");
5
+ /**
6
+ * Get query parameter as `string` treating the `ParsedUrlQuery` result of
7
+ * [`querystring`](https://nodejs.org/api/querystring.html) (used in next.js
8
+ * router and elsewhere)
9
+ *
10
+ * @category location
11
+ *
12
+ * @param {string} [raw] - The _raw_ query parameter
13
+ */
14
+ function getParamAsString(raw) {
15
+ return ((0, isArray_1.default)(raw) ? raw[0] : raw) || "";
16
+ }
17
+ exports.getParamAsString = getParamAsString;
18
+ exports.default = getParamAsString;
package/node/index.js CHANGED
@@ -5,6 +5,7 @@ tslib_1.__exportStar(require("./accentSets"), exports);
5
5
  tslib_1.__exportStar(require("./addOrReplaceAtIdx"), exports);
6
6
  tslib_1.__exportStar(require("./arrayToLookup"), exports);
7
7
  tslib_1.__exportStar(require("./buildUrlQueryString"), exports);
8
+ tslib_1.__exportStar(require("./capitalize"), exports);
8
9
  tslib_1.__exportStar(require("./changeUrlPath"), exports);
9
10
  tslib_1.__exportStar(require("./chunkByChunks"), exports);
10
11
  tslib_1.__exportStar(require("./chunkBySize"), exports);
@@ -20,8 +21,12 @@ tslib_1.__exportStar(require("./encode"), exports);
20
21
  tslib_1.__exportStar(require("./ensureInt"), exports);
21
22
  // export * from "./env"
22
23
  tslib_1.__exportStar(require("./findDuplicatedIndexes"), exports);
24
+ tslib_1.__exportStar(require("./getEmptyArray"), exports);
23
25
  tslib_1.__exportStar(require("./getKeys"), exports);
24
26
  tslib_1.__exportStar(require("./getNonce"), exports);
27
+ tslib_1.__exportStar(require("./getParamAmong"), exports);
28
+ tslib_1.__exportStar(require("./getParamAsInt"), exports);
29
+ tslib_1.__exportStar(require("./getParamAsString"), exports);
25
30
  tslib_1.__exportStar(require("./getType"), exports);
26
31
  tslib_1.__exportStar(require("./getUrlHashParams"), exports);
27
32
  tslib_1.__exportStar(require("./getUrlHashPathname"), exports);
@@ -95,6 +100,7 @@ tslib_1.__exportStar(require("./readCookie"), exports);
95
100
  tslib_1.__exportStar(require("./redirectTo"), exports);
96
101
  tslib_1.__exportStar(require("./removeAccents"), exports);
97
102
  tslib_1.__exportStar(require("./removeCookie"), exports);
103
+ // export * from "./removeDuplicates";
98
104
  tslib_1.__exportStar(require("./removeDuplicatesByKey"), exports);
99
105
  tslib_1.__exportStar(require("./removeDuplicatesComparing"), exports);
100
106
  tslib_1.__exportStar(require("./removeIndexesFromArray"), exports);
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ // /**
3
+ // * FIXME: Type 'Set<any>' can only be iterated through when using the
4
+ // * '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
5
+ // * I am not sure I want to use those ts options here. Let's keep it commented
6
+ // * for now
7
+ // *
8
+ // * @category array
9
+ // */
10
+ // export function removeDuplicates<T extends any[]>(arr: T) {
11
+ // return [...new Set(arr)];
12
+ // }
13
+ // export default removeDuplicates;
package/package.json CHANGED
@@ -7,7 +7,7 @@
7
7
  "peerDependencies": {
8
8
  "tslib": "^2.4.0"
9
9
  },
10
- "version": "1.0.72",
10
+ "version": "1.0.75",
11
11
  "module": "./index.js",
12
12
  "types": "./index.d.ts"
13
13
  }
File without changes
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ // /**
3
+ // * FIXME: Type 'Set<any>' can only be iterated through when using the
4
+ // * '--downlevelIteration' flag or with a '--target' of 'es2015' or higher.
5
+ // * I am not sure I want to use those ts options here. Let's keep it commented
6
+ // * for now
7
+ // *
8
+ // * @category array
9
+ // */
10
+ // export function removeDuplicates<T extends any[]>(arr: T) {
11
+ // return [...new Set(arr)];
12
+ // }
13
+ // export default removeDuplicates;
package/typings.d.ts CHANGED
@@ -6,3 +6,7 @@
6
6
 
7
7
  // eslint-disable-next-line no-var,@typescript-eslint/no-explicit-any
8
8
  declare var gtag: (...args: any[]) => any;
9
+
10
+ // declare type NullableRecord<T> = {
11
+ // [K in keyof T]: T[K] | null;
12
+ // };