@silvermine/toolbox 0.3.0 → 0.5.0
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/.github/workflows/ci.yml +2 -2
- package/.vscode/settings.json +3 -0
- package/CHANGELOG.md +32 -0
- package/dist/commonjs/index.js +4 -0
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/types/KeyValueStringObject.js +4 -6
- package/dist/commonjs/types/KeyValueStringObject.js.map +1 -1
- package/dist/commonjs/types/StrictUnion.js +0 -1
- package/dist/commonjs/types/StrictUnion.js.map +1 -1
- package/dist/commonjs/types/StringArrayOfStringsMap.js +3 -5
- package/dist/commonjs/types/StringArrayOfStringsMap.js.map +1 -1
- package/dist/commonjs/types/StringMap.js +3 -5
- package/dist/commonjs/types/StringMap.js.map +1 -1
- package/dist/commonjs/types/StringUnknownMap.js.map +1 -1
- package/dist/commonjs/types/UnionKeys.js +3 -0
- package/dist/commonjs/types/UnionKeys.js.map +1 -0
- package/dist/commonjs/utils/compact.js +16 -0
- package/dist/commonjs/utils/compact.js.map +1 -0
- package/dist/commonjs/utils/get-tag-string.js.map +1 -1
- package/dist/commonjs/utils/get.js +3 -0
- package/dist/commonjs/utils/get.js.map +1 -1
- package/dist/commonjs/utils/has-defined.js.map +1 -1
- package/dist/commonjs/utils/is-arguments.js.map +1 -1
- package/dist/commonjs/utils/is-array-of-strings.js +1 -1
- package/dist/commonjs/utils/is-array-of-strings.js.map +1 -1
- package/dist/commonjs/utils/is-array.js.map +1 -1
- package/dist/commonjs/utils/is-boolean.js +8 -0
- package/dist/commonjs/utils/is-boolean.js.map +1 -0
- package/dist/commonjs/utils/is-empty.js +10 -11
- package/dist/commonjs/utils/is-empty.js.map +1 -1
- package/dist/commonjs/utils/is-enum-value.js.map +1 -1
- package/dist/commonjs/utils/is-map-with-values-of-type.js +1 -1
- package/dist/commonjs/utils/is-map-with-values-of-type.js.map +1 -1
- package/dist/commonjs/utils/is-number.js.map +1 -1
- package/dist/commonjs/utils/is-object.js +1 -1
- package/dist/commonjs/utils/is-object.js.map +1 -1
- package/dist/commonjs/utils/is-promise-like.js.map +1 -1
- package/dist/commonjs/utils/is-promise.js.map +1 -1
- package/dist/commonjs/utils/is-set.js +13 -0
- package/dist/commonjs/utils/is-set.js.map +1 -0
- package/dist/commonjs/utils/is-string.js.map +1 -1
- package/dist/commonjs/utils/make-template.js +3 -6
- package/dist/commonjs/utils/make-template.js.map +1 -1
- package/dist/commonjs/utils/range.js +25 -0
- package/dist/commonjs/utils/range.js.map +1 -0
- package/dist/commonjs/utils/uniq.js +2 -2
- package/dist/commonjs/utils/uniq.js.map +1 -1
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/KeyValueStringObject.js +4 -6
- package/dist/esm/types/KeyValueStringObject.js.map +1 -1
- package/dist/esm/types/StrictUnion.js +0 -1
- package/dist/esm/types/StrictUnion.js.map +1 -1
- package/dist/esm/types/StringArrayOfStringsMap.js +3 -5
- package/dist/esm/types/StringArrayOfStringsMap.js.map +1 -1
- package/dist/esm/types/StringMap.js +3 -5
- package/dist/esm/types/StringMap.js.map +1 -1
- package/dist/esm/types/StringUnknownMap.js.map +1 -1
- package/dist/esm/types/UnionKeys.js +1 -0
- package/dist/esm/types/UnionKeys.js.map +1 -0
- package/dist/esm/utils/compact.js +12 -0
- package/dist/esm/utils/compact.js.map +1 -0
- package/dist/esm/utils/get-tag-string.js.map +1 -1
- package/dist/esm/utils/get.js +3 -0
- package/dist/esm/utils/get.js.map +1 -1
- package/dist/esm/utils/has-defined.js.map +1 -1
- package/dist/esm/utils/is-arguments.js.map +1 -1
- package/dist/esm/utils/is-array-of-strings.js +1 -1
- package/dist/esm/utils/is-array-of-strings.js.map +1 -1
- package/dist/esm/utils/is-array.js.map +1 -1
- package/dist/esm/utils/is-boolean.js +4 -0
- package/dist/esm/utils/is-boolean.js.map +1 -0
- package/dist/esm/utils/is-empty.js +10 -11
- package/dist/esm/utils/is-empty.js.map +1 -1
- package/dist/esm/utils/is-enum-value.js.map +1 -1
- package/dist/esm/utils/is-map-with-values-of-type.js +1 -1
- package/dist/esm/utils/is-map-with-values-of-type.js.map +1 -1
- package/dist/esm/utils/is-number.js.map +1 -1
- package/dist/esm/utils/is-object.js +1 -1
- package/dist/esm/utils/is-object.js.map +1 -1
- package/dist/esm/utils/is-promise-like.js.map +1 -1
- package/dist/esm/utils/is-promise.js.map +1 -1
- package/dist/esm/utils/is-set.js +9 -0
- package/dist/esm/utils/is-set.js.map +1 -0
- package/dist/esm/utils/is-string.js.map +1 -1
- package/dist/esm/utils/make-template.js +3 -6
- package/dist/esm/utils/make-template.js.map +1 -1
- package/dist/esm/utils/range.js +21 -0
- package/dist/esm/utils/range.js.map +1 -0
- package/dist/esm/utils/uniq.js +2 -2
- package/dist/esm/utils/uniq.js.map +1 -1
- package/dist/types/index.d.ts +4 -0
- package/dist/types/types/KeyValueStringObject.d.ts +1 -1
- package/dist/types/types/StrictUnion.d.ts +1 -1
- package/dist/types/types/StringArrayOfStringsMap.d.ts +1 -1
- package/dist/types/types/StringMap.d.ts +1 -1
- package/dist/types/types/StringUnknownMap.d.ts +1 -1
- package/dist/types/types/UnionKeys.d.ts +9 -0
- package/dist/types/utils/compact.d.ts +3 -0
- package/dist/types/utils/get-tag-string.d.ts +1 -1
- package/dist/types/utils/has-defined.d.ts +4 -1
- package/dist/types/utils/is-arguments.d.ts +1 -1
- package/dist/types/utils/is-array-of-strings.d.ts +1 -1
- package/dist/types/utils/is-array.d.ts +1 -1
- package/dist/types/utils/is-boolean.d.ts +1 -0
- package/dist/types/utils/is-empty.d.ts +11 -1
- package/dist/types/utils/is-enum-value.d.ts +1 -1
- package/dist/types/utils/is-map-with-values-of-type.d.ts +1 -1
- package/dist/types/utils/is-number.d.ts +1 -1
- package/dist/types/utils/is-object.d.ts +1 -1
- package/dist/types/utils/is-promise-like.d.ts +1 -1
- package/dist/types/utils/is-promise.d.ts +1 -1
- package/dist/types/utils/is-set.d.ts +6 -0
- package/dist/types/utils/is-string.d.ts +1 -1
- package/dist/types/utils/make-template.d.ts +1 -1
- package/dist/types/utils/range.d.ts +7 -0
- package/eslint.config.js +15 -0
- package/package.json +18 -18
- package/renovate.json +4 -0
- package/src/index.ts +4 -0
- package/src/types/KeyValueStringObject.ts +5 -7
- package/src/types/StrictUnion.ts +1 -2
- package/src/types/StringArrayOfStringsMap.ts +5 -7
- package/src/types/StringMap.ts +5 -7
- package/src/types/StringUnknownMap.ts +1 -1
- package/src/types/UnionKeys.ts +9 -0
- package/src/utils/compact.ts +14 -0
- package/src/utils/get-tag-string.ts +1 -1
- package/src/utils/get.ts +3 -0
- package/src/utils/has-defined.ts +2 -1
- package/src/utils/is-arguments.ts +1 -1
- package/src/utils/is-array-of-strings.ts +2 -2
- package/src/utils/is-array.ts +1 -1
- package/src/utils/is-boolean.ts +3 -0
- package/src/utils/is-empty.ts +35 -3
- package/src/utils/is-enum-value.ts +1 -1
- package/src/utils/is-map-with-values-of-type.ts +3 -3
- package/src/utils/is-number.ts +1 -1
- package/src/utils/is-object.ts +2 -2
- package/src/utils/is-promise-like.ts +2 -2
- package/src/utils/is-promise.ts +1 -1
- package/src/utils/is-set.ts +8 -0
- package/src/utils/is-string.ts +1 -1
- package/src/utils/make-template.ts +8 -11
- package/src/utils/range.ts +24 -0
- package/src/utils/uniq.ts +3 -3
|
@@ -9,4 +9,4 @@
|
|
|
9
9
|
* @see https://github.com/jashkenas/underscore/blob/d5fe0fd4060f13b40608cb9d92eda6d857e8752c/underscore.js#L1322
|
|
10
10
|
* @returns `true` if `o` is an `object`
|
|
11
11
|
*/
|
|
12
|
-
export declare function isObject(o:
|
|
12
|
+
export declare function isObject(o: unknown): o is object;
|
|
@@ -7,7 +7,7 @@ export interface ToolboxTemplateSettings {
|
|
|
7
7
|
};
|
|
8
8
|
}
|
|
9
9
|
export interface ToolboxTemplateFunction {
|
|
10
|
-
(data: Record<string,
|
|
10
|
+
(data: Record<string, unknown>): string;
|
|
11
11
|
}
|
|
12
12
|
/**
|
|
13
13
|
* A simple replacement for _.template from either Underscore or Lodash that removes the
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of numbers (positive and/or negative) progressing from
|
|
3
|
+
* start up to, but not including, end. A step of -1 is used if a negative start
|
|
4
|
+
* is specified without an end or step. If end is not specified, it's set to
|
|
5
|
+
* start with start then set to 0.
|
|
6
|
+
*/
|
|
7
|
+
export declare function range(start: number, end?: number, step?: number): number[];
|
package/eslint.config.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
const config = require('@silvermine/eslint-config'),
|
|
2
|
+
node = require('@silvermine/eslint-config/partials/node'),
|
|
3
|
+
nodeTests = require('@silvermine/eslint-config/partials/node-tests');
|
|
4
|
+
|
|
5
|
+
module.exports = [
|
|
6
|
+
...config,
|
|
7
|
+
{
|
|
8
|
+
files: [ 'tests/**.ts' ],
|
|
9
|
+
...nodeTests,
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
files: [ '**/*.ts', '**/*.js' ],
|
|
13
|
+
...node,
|
|
14
|
+
},
|
|
15
|
+
];
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@silvermine/toolbox",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.5.0",
|
|
4
4
|
"description": "A library of common TypeScript types, custom type guards, and utility functions.",
|
|
5
5
|
"main": "./dist/commonjs/index",
|
|
6
6
|
"types": "./dist/types/index.d.ts",
|
|
@@ -36,30 +36,30 @@
|
|
|
36
36
|
"tslib": "^1.9.0 || ^2"
|
|
37
37
|
},
|
|
38
38
|
"devDependencies": {
|
|
39
|
-
"@silvermine/chai-strictly-equal": "1.1.
|
|
40
|
-
"@silvermine/eslint-config": "github:silvermine/eslint-config-silvermine#
|
|
39
|
+
"@silvermine/chai-strictly-equal": "1.1.1",
|
|
40
|
+
"@silvermine/eslint-config": "github:silvermine/eslint-config-silvermine#634db8f6cfb2314070ba59339faf86f089c8e65d",
|
|
41
41
|
"@silvermine/standardization": "2.0.0",
|
|
42
42
|
"@silvermine/typescript-config": "git+https://github.com/silvermine/typescript-config#23213e33077089e723629dead5342abe6f3b3c8c",
|
|
43
|
-
"@types/chai": "4.
|
|
44
|
-
"@types/mocha": "5.2.
|
|
43
|
+
"@types/chai": "4.3.20",
|
|
44
|
+
"@types/mocha": "5.2.7",
|
|
45
45
|
"@types/node": "12.20.45",
|
|
46
|
-
"@types/sinon": "5.0.
|
|
47
|
-
"chai": "4.
|
|
48
|
-
"check-node-version": "4.
|
|
49
|
-
"coveralls": "3.
|
|
50
|
-
"eslint": "8.
|
|
51
|
-
"grunt": "1.
|
|
52
|
-
"grunt-cli": "1.
|
|
53
|
-
"grunt-concurrent": "
|
|
54
|
-
"grunt-contrib-clean": "2.0.
|
|
46
|
+
"@types/sinon": "5.0.7",
|
|
47
|
+
"chai": "4.5.0",
|
|
48
|
+
"check-node-version": "4.2.1",
|
|
49
|
+
"coveralls": "3.1.1",
|
|
50
|
+
"eslint": "8.57.1",
|
|
51
|
+
"grunt": "1.6.1",
|
|
52
|
+
"grunt-cli": "1.5.0",
|
|
53
|
+
"grunt-concurrent": "3.0.0",
|
|
54
|
+
"grunt-contrib-clean": "2.0.1",
|
|
55
55
|
"grunt-contrib-watch": "1.1.0",
|
|
56
56
|
"grunt-exec": "3.0.0",
|
|
57
57
|
"mocha": "5.2.0",
|
|
58
|
-
"nyc": "13.
|
|
58
|
+
"nyc": "13.3.0",
|
|
59
59
|
"sinon": "5.1.1",
|
|
60
|
-
"source-map-support": "0.5.
|
|
60
|
+
"source-map-support": "0.5.21",
|
|
61
61
|
"ts-node": "7.0.1",
|
|
62
|
-
"tslib": "2.6.
|
|
63
|
-
"typescript": "3.9.
|
|
62
|
+
"tslib": "2.6.3",
|
|
63
|
+
"typescript": "3.9.10"
|
|
64
64
|
}
|
|
65
65
|
}
|
package/renovate.json
ADDED
package/src/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ export * from './utils/has-defined';
|
|
|
14
14
|
export * from './utils/is-arguments';
|
|
15
15
|
export * from './utils/is-array';
|
|
16
16
|
export * from './utils/is-array-of-strings';
|
|
17
|
+
export * from './utils/is-boolean';
|
|
17
18
|
export * from './utils/is-empty';
|
|
18
19
|
export * from './utils/is-enum-value';
|
|
19
20
|
export * from './utils/is-map-with-values-of-type';
|
|
@@ -21,12 +22,14 @@ export * from './utils/is-number';
|
|
|
21
22
|
export * from './utils/is-object';
|
|
22
23
|
export * from './utils/is-promise';
|
|
23
24
|
export * from './utils/is-promise-like';
|
|
25
|
+
export * from './utils/is-set';
|
|
24
26
|
export * from './utils/is-string';
|
|
25
27
|
export * from './utils/is-undefined';
|
|
26
28
|
export * from './utils/is-null';
|
|
27
29
|
export * from './utils/is-not-null-or-undefined';
|
|
28
30
|
|
|
29
31
|
export * from './utils/chunk';
|
|
32
|
+
export * from './utils/compact';
|
|
30
33
|
export * from './utils/flatten';
|
|
31
34
|
export * from './utils/pick';
|
|
32
35
|
export * from './utils/pluck';
|
|
@@ -35,3 +38,4 @@ export * from './utils/get';
|
|
|
35
38
|
export * from './utils/escape-html';
|
|
36
39
|
export * from './utils/make-template';
|
|
37
40
|
export * from './utils/uniq';
|
|
41
|
+
export * from './utils/range';
|
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import { isObject } from '../utils/is-object';
|
|
2
|
-
import { isArray } from '../utils/is-array';
|
|
3
|
-
import { isArguments } from '../utils/is-arguments';
|
|
4
1
|
import { isArrayOfStrings } from '../utils/is-array-of-strings';
|
|
5
2
|
import { isString } from '../utils/is-string';
|
|
3
|
+
import { isStringUnknownMap } from './StringUnknownMap';
|
|
6
4
|
|
|
7
5
|
/**
|
|
8
6
|
* `KeyValueStringObject`s have `string`s as keys and one of:
|
|
@@ -32,15 +30,15 @@ export interface KeyValueStringObject { [k: string]: (string | string[] | KeyVal
|
|
|
32
30
|
*
|
|
33
31
|
* @returns `true` if `o` is a `KeyValueStringObject`
|
|
34
32
|
*/
|
|
35
|
-
export function isKeyValueStringObject(o:
|
|
33
|
+
export function isKeyValueStringObject(o: unknown): o is KeyValueStringObject {
|
|
36
34
|
// Arrays and the array-like `arguments` variable are objects, so they would not be
|
|
37
35
|
// caught by an `isObject` check
|
|
38
|
-
if (!
|
|
36
|
+
if (!isStringUnknownMap(o)) {
|
|
39
37
|
return false;
|
|
40
38
|
}
|
|
41
39
|
|
|
42
|
-
for (
|
|
43
|
-
|
|
40
|
+
for (const k of Object.keys(o)) {
|
|
41
|
+
const v: unknown = o[k];
|
|
44
42
|
|
|
45
43
|
if (!isString(v) && !isArrayOfStrings(v) && !isKeyValueStringObject(v)) {
|
|
46
44
|
return false;
|
package/src/types/StrictUnion.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import { UnionKeys } from './UnionKeys';
|
|
2
2
|
|
|
3
|
-
type UnionKeys<T> = T extends unknown ? keyof T : never;
|
|
4
3
|
type InvalidKeys<K extends string | number | symbol> = { [P in K]?: never };
|
|
5
4
|
type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<Exclude<UnionKeys<TAll>, keyof T>>) : never;
|
|
6
5
|
|
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { isArguments } from '../utils/is-arguments';
|
|
2
1
|
import { isArrayOfStrings } from '../utils/is-array-of-strings';
|
|
3
|
-
import {
|
|
4
|
-
import { isArray } from '../utils/is-array';
|
|
2
|
+
import { isStringUnknownMap } from './StringUnknownMap';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* `StringArrayOfStringsMap`s have `string`s as keys and `string[]` as values. For
|
|
@@ -21,15 +19,15 @@ export interface StringArrayOfStringsMap { [s: string]: string[] }
|
|
|
21
19
|
*
|
|
22
20
|
* @returns `true` if `o` is a `StringArrayOfStringsMap`
|
|
23
21
|
*/
|
|
24
|
-
export function isStringArrayOfStringsMap(o:
|
|
22
|
+
export function isStringArrayOfStringsMap(o: unknown): o is StringArrayOfStringsMap {
|
|
25
23
|
// Arrays and the array-like `arguments` variable are objects, so they would not be
|
|
26
24
|
// caught by an `isObject` check
|
|
27
|
-
if (!
|
|
25
|
+
if (!isStringUnknownMap(o)) {
|
|
28
26
|
return false;
|
|
29
27
|
}
|
|
30
28
|
|
|
31
|
-
for (
|
|
32
|
-
if (!isArrayOfStrings(
|
|
29
|
+
for (const k of Object.keys(o)) {
|
|
30
|
+
if (!isArrayOfStrings(o[k])) {
|
|
33
31
|
return false;
|
|
34
32
|
}
|
|
35
33
|
}
|
package/src/types/StringMap.ts
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import { isObject } from '../utils/is-object';
|
|
2
|
-
import { isArray } from '../utils/is-array';
|
|
3
|
-
import { isArguments } from '../utils/is-arguments';
|
|
4
1
|
import { isString } from '../utils/is-string';
|
|
2
|
+
import { isStringUnknownMap } from './StringUnknownMap';
|
|
5
3
|
|
|
6
4
|
/**
|
|
7
5
|
* `StringMap`s have `string`s as keys and `string`s as values. For example:
|
|
@@ -19,15 +17,15 @@ export interface StringMap { [s: string]: string }
|
|
|
19
17
|
*
|
|
20
18
|
* @returns `true` if `o` is a `StringMap`
|
|
21
19
|
*/
|
|
22
|
-
export function isStringMap(o:
|
|
20
|
+
export function isStringMap(o: unknown): o is StringMap {
|
|
23
21
|
// Arrays and the array-like `arguments` variable are objects, so they would not be
|
|
24
22
|
// caught by an `isObject` check
|
|
25
|
-
if (!
|
|
23
|
+
if (!isStringUnknownMap(o)) {
|
|
26
24
|
return false;
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
for (
|
|
30
|
-
if (!isString(
|
|
27
|
+
for (const k of Object.keys(o)) {
|
|
28
|
+
if (!isString(o[k])) {
|
|
31
29
|
return false;
|
|
32
30
|
}
|
|
33
31
|
}
|
|
@@ -57,6 +57,6 @@ export interface StringUnknownMap { [s: string]: unknown }
|
|
|
57
57
|
*
|
|
58
58
|
* @returns `true` if `o` is a `StringUnknownMap`
|
|
59
59
|
*/
|
|
60
|
-
export function isStringUnknownMap(o:
|
|
60
|
+
export function isStringUnknownMap(o: unknown): o is StringUnknownMap {
|
|
61
61
|
return isObject(o) && !isArray(o) && !isArguments(o);
|
|
62
62
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
type FalsyVals = '' | 0 | false | null | undefined;
|
|
2
|
+
|
|
3
|
+
function notFalsy<T>(value: T | FalsyVals): value is T {
|
|
4
|
+
return value !== ''
|
|
5
|
+
&& value !== 0
|
|
6
|
+
&& value !== false
|
|
7
|
+
&& value !== null
|
|
8
|
+
&& value !== undefined
|
|
9
|
+
&& !(typeof value === 'number' && isNaN(value));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function compact<T>(arr: (T | FalsyVals)[]): T[] {
|
|
13
|
+
return arr.filter(notFalsy);
|
|
14
|
+
}
|
|
@@ -34,6 +34,6 @@
|
|
|
34
34
|
* @see https://github.com/jashkenas/underscore/blob/d5fe0fd4060f13b40608cb9d92eda6d857e8752c/underscore.js#L1326
|
|
35
35
|
* @see https://github.com/lodash/lodash/blob/750067f42d3aa5f927604ece2c6df0ff2b2e9d72/isNumber.js#L31
|
|
36
36
|
*/
|
|
37
|
-
export function getTagString(o:
|
|
37
|
+
export function getTagString(o: unknown): string {
|
|
38
38
|
return Object.prototype.toString.call(o);
|
|
39
39
|
}
|
package/src/utils/get.ts
CHANGED
|
@@ -32,6 +32,7 @@ function toKey(value: unknown): string | symbol {
|
|
|
32
32
|
const IS_DEEP_PROP_REGEX = /\.|\[(?:[^[\]]*|(["'])(?:(?!\1)[^\\]|\\.)*?\1)\]/,
|
|
33
33
|
IS_PLAIN_PROP_REGEX = /^\w*$/;
|
|
34
34
|
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
35
36
|
function isKey(value: any, object: any): boolean {
|
|
36
37
|
if (Array.isArray(value)) {
|
|
37
38
|
return false;
|
|
@@ -93,6 +94,7 @@ function stringToPath(str: string): string[] {
|
|
|
93
94
|
return result;
|
|
94
95
|
}
|
|
95
96
|
|
|
97
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
96
98
|
function createPathArray(value: any, object: any): string[] {
|
|
97
99
|
if (Array.isArray(value)) {
|
|
98
100
|
return value;
|
|
@@ -139,6 +141,7 @@ function get<TResult = unknown>(
|
|
|
139
141
|
resultObj: unknown = obj;
|
|
140
142
|
|
|
141
143
|
while (resultObj !== null && resultObj !== undefined && index < length) {
|
|
144
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
142
145
|
resultObj = (resultObj as any)[toKey(pathArray[index])];
|
|
143
146
|
|
|
144
147
|
index += 1;
|
package/src/utils/has-defined.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { isUndefined } from './is-undefined';
|
|
2
2
|
import { RequireDefined } from '../types/RequireDefined';
|
|
3
|
+
import { UnionKeys } from '../types/UnionKeys';
|
|
3
4
|
|
|
4
5
|
/**
|
|
5
6
|
* Tests that an optional or possibly-undefined property on type `T` is defined (that is,
|
|
@@ -18,6 +19,6 @@ import { RequireDefined } from '../types/RequireDefined';
|
|
|
18
19
|
* @param propName the name of the optional or possibly-undefined property that you want
|
|
19
20
|
* to be required
|
|
20
21
|
*/
|
|
21
|
-
export function hasDefined<T, U extends keyof T
|
|
22
|
+
export function hasDefined<T, U extends keyof T | UnionKeys<T>>(o: T, propName: U): o is RequireDefined<Extract<T, { [key in U]?: unknown }>, U> {
|
|
22
23
|
return Object.prototype.hasOwnProperty.call(o, propName) && !isUndefined(o[propName]);
|
|
23
24
|
}
|
|
@@ -6,6 +6,6 @@ import { getTagString } from './get-tag-string';
|
|
|
6
6
|
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
|
|
7
7
|
* @returns `true` if `o` is a function's array-like `arguments` variable
|
|
8
8
|
*/
|
|
9
|
-
export function isArguments(o:
|
|
9
|
+
export function isArguments(o: unknown): o is IArguments {
|
|
10
10
|
return getTagString(o) === '[object Arguments]';
|
|
11
11
|
}
|
|
@@ -8,12 +8,12 @@ import { isString } from './is-string';
|
|
|
8
8
|
* not counted as `string` values, even if the TypeScript compiler's `strictNullChecks`
|
|
9
9
|
* flag is set to `false` in your project.
|
|
10
10
|
*/
|
|
11
|
-
export function isArrayOfStrings(values:
|
|
11
|
+
export function isArrayOfStrings(values: unknown): values is string[] {
|
|
12
12
|
if (!isArray(values)) {
|
|
13
13
|
return false;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
for (
|
|
16
|
+
for (const v of values) {
|
|
17
17
|
if (!isString(v)) {
|
|
18
18
|
return false;
|
|
19
19
|
}
|
package/src/utils/is-array.ts
CHANGED
package/src/utils/is-empty.ts
CHANGED
|
@@ -2,6 +2,32 @@ import { isArray } from './is-array';
|
|
|
2
2
|
import { isString } from './is-string';
|
|
3
3
|
import { isArguments } from './is-arguments';
|
|
4
4
|
import { isUndefined } from './is-undefined';
|
|
5
|
+
import { isNull } from './is-null';
|
|
6
|
+
import { isBoolean } from './is-boolean';
|
|
7
|
+
import { isNumber } from './is-number';
|
|
8
|
+
import { isSet } from './is-set';
|
|
9
|
+
import { isObject } from './is-object';
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
interface IEmptyArguments extends IArguments {
|
|
13
|
+
length: 0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface IEmptyObj {
|
|
17
|
+
[s: string]: never;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
type IEmptyTypes = (
|
|
21
|
+
null |
|
|
22
|
+
undefined |
|
|
23
|
+
boolean |
|
|
24
|
+
number |
|
|
25
|
+
never[] |
|
|
26
|
+
'' |
|
|
27
|
+
IEmptyArguments |
|
|
28
|
+
Set<never> |
|
|
29
|
+
IEmptyObj
|
|
30
|
+
);
|
|
5
31
|
|
|
6
32
|
/**
|
|
7
33
|
* Checks if `o` is an empty object. An object is "empty" if it:
|
|
@@ -12,12 +38,18 @@ import { isUndefined } from './is-undefined';
|
|
|
12
38
|
*
|
|
13
39
|
* @returns `true` if `o` is empty
|
|
14
40
|
*/
|
|
15
|
-
export function isEmpty(o:
|
|
16
|
-
|
|
41
|
+
export function isEmpty(o: unknown[]): boolean;
|
|
42
|
+
export function isEmpty(o: object): boolean;
|
|
43
|
+
export function isEmpty(o: unknown): o is IEmptyTypes;
|
|
44
|
+
export function isEmpty(o: unknown): boolean {
|
|
45
|
+
if (isNull(o) || isUndefined(o) || isBoolean(o) || isNumber(o)) {
|
|
17
46
|
return true;
|
|
18
47
|
}
|
|
19
48
|
if (isArray(o) || isString(o) || isArguments(o)) {
|
|
20
49
|
return o.length === 0;
|
|
21
50
|
}
|
|
22
|
-
|
|
51
|
+
if (isSet(o)) {
|
|
52
|
+
return o.size === 0;
|
|
53
|
+
}
|
|
54
|
+
return isObject(o) && Object.keys(o).length === 0;
|
|
23
55
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Type guard to check to see if the given value is a valid value for the enum.
|
|
3
3
|
*/
|
|
4
|
-
export function isEnumValue<T>(enumType: T, value:
|
|
4
|
+
export function isEnumValue<T>(enumType: T, value: unknown): value is T[keyof T] {
|
|
5
5
|
return (Object.keys(enumType) as Array<keyof T>)
|
|
6
6
|
.map((key) => {
|
|
7
7
|
return enumType[key];
|
|
@@ -6,13 +6,13 @@ import { isStringUnknownMap } from '../types/StringUnknownMap';
|
|
|
6
6
|
* @returns `true` if `o` is a map with `string` keys and values that pass the provided
|
|
7
7
|
* type guard.
|
|
8
8
|
*/
|
|
9
|
-
export function isMapWithValuesOfType<T>(guard: (x:
|
|
9
|
+
export function isMapWithValuesOfType<T>(guard: (x: unknown) => x is T, o: unknown): o is Record<string, T> {
|
|
10
10
|
if (!isStringUnknownMap(o)) {
|
|
11
11
|
return false;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
for (
|
|
15
|
-
if (!guard(
|
|
14
|
+
for (const k of Object.keys(o)) {
|
|
15
|
+
if (!guard(o[k])) {
|
|
16
16
|
return false;
|
|
17
17
|
}
|
|
18
18
|
}
|
package/src/utils/is-number.ts
CHANGED
|
@@ -8,6 +8,6 @@ import { isObject } from './is-object';
|
|
|
8
8
|
* considered `number`s. If you need to check for one of those values, you can use the
|
|
9
9
|
* built-in `Number.isNaN` or `Number.isFinite` functions.
|
|
10
10
|
*/
|
|
11
|
-
export function isNumber(o:
|
|
11
|
+
export function isNumber(o: unknown): o is number {
|
|
12
12
|
return typeof o === 'number' || (isObject(o) && getTagString(o) === '[object Number]');
|
|
13
13
|
}
|
package/src/utils/is-object.ts
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
* @see https://github.com/jashkenas/underscore/blob/d5fe0fd4060f13b40608cb9d92eda6d857e8752c/underscore.js#L1322
|
|
10
10
|
* @returns `true` if `o` is an `object`
|
|
11
11
|
*/
|
|
12
|
-
export function isObject(o:
|
|
13
|
-
|
|
12
|
+
export function isObject(o: unknown): o is object {
|
|
13
|
+
const type = typeof o;
|
|
14
14
|
|
|
15
15
|
return o !== null && (type === 'object' || type === 'function');
|
|
16
16
|
}
|
|
@@ -8,6 +8,6 @@ import { isObject } from './is-object';
|
|
|
8
8
|
*
|
|
9
9
|
* @returns `true` if `o` is `Promise`-like (i.e. has a `then` function)
|
|
10
10
|
*/
|
|
11
|
-
export function isPromiseLike(o:
|
|
12
|
-
return isPromise(o) || (isObject(o) && typeof (o as
|
|
11
|
+
export function isPromiseLike(o: unknown): o is PromiseLike<unknown> {
|
|
12
|
+
return isPromise(o) || (isObject(o) && typeof (o as PromiseLike<unknown>).then === 'function');
|
|
13
13
|
}
|
package/src/utils/is-promise.ts
CHANGED
|
@@ -6,6 +6,6 @@ import { getTagString } from './get-tag-string';
|
|
|
6
6
|
*
|
|
7
7
|
* @returns `true` if `o` is a `Promise`
|
|
8
8
|
*/
|
|
9
|
-
export function isPromise(o:
|
|
9
|
+
export function isPromise(o: unknown): o is Promise<unknown> {
|
|
10
10
|
return isObject(o) && getTagString(o) === '[object Promise]';
|
|
11
11
|
}
|
package/src/utils/is-string.ts
CHANGED
|
@@ -8,6 +8,6 @@ import { getTagString } from './get-tag-string';
|
|
|
8
8
|
* `string` values, even if the TypeScript compiler's `strictNullChecks` flag is set to
|
|
9
9
|
* `false` in your project.
|
|
10
10
|
*/
|
|
11
|
-
export function isString(o:
|
|
11
|
+
export function isString(o: unknown): o is string {
|
|
12
12
|
return typeof o === 'string' || (isObject(o) && getTagString(o) === '[object String]');
|
|
13
13
|
}
|
|
@@ -6,6 +6,7 @@ const DEFAULT_SETTINGS: ToolboxTemplateSettings = {
|
|
|
6
6
|
interpolate: /<%=([\s\S]+?)%>/g,
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
9
10
|
function getValue(path: string, data: any): unknown {
|
|
10
11
|
return get(data, (path || '').trim(), '');
|
|
11
12
|
}
|
|
@@ -16,7 +17,7 @@ export interface ToolboxTemplateSettings {
|
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export interface ToolboxTemplateFunction {
|
|
19
|
-
(data: Record<string,
|
|
20
|
+
(data: Record<string, unknown>): string;
|
|
20
21
|
}
|
|
21
22
|
|
|
22
23
|
/**
|
|
@@ -51,23 +52,19 @@ export interface ToolboxTemplateFunction {
|
|
|
51
52
|
export function makeTemplate(text: string, userSettings?: ToolboxTemplateSettings): (data: unknown) => string {
|
|
52
53
|
type TemplateFunction = (data: unknown) => unknown;
|
|
53
54
|
|
|
54
|
-
let
|
|
55
|
-
index = 0,
|
|
56
|
-
settings = Object.assign({}, DEFAULT_SETTINGS, userSettings || {}),
|
|
57
|
-
regExpPattern, matcher;
|
|
55
|
+
let index = 0;
|
|
58
56
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
matcher = new RegExp(regExpPattern.join('|') + '|$', 'g');
|
|
57
|
+
const parts: (TemplateFunction | string)[] = [],
|
|
58
|
+
settings = Object.assign({}, DEFAULT_SETTINGS, userSettings || {}),
|
|
59
|
+
regExpPattern = [ settings.escape.source, settings.interpolate.source ],
|
|
60
|
+
matcher = new RegExp(regExpPattern.join('|') + '|$', 'g');
|
|
64
61
|
|
|
65
62
|
text.replace(matcher, (match, escape, interpolate, offset) => {
|
|
66
63
|
parts.push(text.slice(index, offset));
|
|
67
64
|
index = offset + match.length;
|
|
68
65
|
|
|
69
66
|
if (escape) {
|
|
70
|
-
parts.push((data:
|
|
67
|
+
parts.push((data: unknown) => {
|
|
71
68
|
return escapeHTML(getValue(escape, data));
|
|
72
69
|
});
|
|
73
70
|
} else if (interpolate) {
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array of numbers (positive and/or negative) progressing from
|
|
3
|
+
* start up to, but not including, end. A step of -1 is used if a negative start
|
|
4
|
+
* is specified without an end or step. If end is not specified, it's set to
|
|
5
|
+
* start with start then set to 0.
|
|
6
|
+
*/
|
|
7
|
+
export function range(start: number, end?: number, step?: number): number[] {
|
|
8
|
+
if (end === undefined) {
|
|
9
|
+
end = start;
|
|
10
|
+
start = 0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
if (!step) {
|
|
14
|
+
step = start < end ? 1 : -1;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const length = Math.max(Math.ceil((end - start) / step), 0),
|
|
18
|
+
result = Array(length);
|
|
19
|
+
|
|
20
|
+
for (let i = 0; i < length; i++) {
|
|
21
|
+
result[i] = start + (i * step);
|
|
22
|
+
}
|
|
23
|
+
return result;
|
|
24
|
+
}
|
package/src/utils/uniq.ts
CHANGED
|
@@ -3,8 +3,8 @@ const iterateeUniq = <T, U>(arr: T[], iteratee: (value: T, i: number, arr: T[])
|
|
|
3
3
|
seen: U[] = [];
|
|
4
4
|
|
|
5
5
|
for (let i = 0, length = arr.length; i < length; i++) {
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
const value = arr[i],
|
|
7
|
+
computed = iteratee(value, i, arr);
|
|
8
8
|
|
|
9
9
|
if (seen.indexOf(computed) === -1) {
|
|
10
10
|
seen.push(computed);
|
|
@@ -21,7 +21,7 @@ const sortedUniq = <T, U>(arr: T[]): T[] => {
|
|
|
21
21
|
let lastSeen: T | U | undefined;
|
|
22
22
|
|
|
23
23
|
for (let i = 0, length = arr.length; i < length; i++) {
|
|
24
|
-
|
|
24
|
+
const value = arr[i];
|
|
25
25
|
|
|
26
26
|
if (i === 0 || lastSeen !== value) {
|
|
27
27
|
result.push(value);
|