@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.
Files changed (146) hide show
  1. package/.github/workflows/ci.yml +2 -2
  2. package/.vscode/settings.json +3 -0
  3. package/CHANGELOG.md +32 -0
  4. package/dist/commonjs/index.js +4 -0
  5. package/dist/commonjs/index.js.map +1 -1
  6. package/dist/commonjs/types/KeyValueStringObject.js +4 -6
  7. package/dist/commonjs/types/KeyValueStringObject.js.map +1 -1
  8. package/dist/commonjs/types/StrictUnion.js +0 -1
  9. package/dist/commonjs/types/StrictUnion.js.map +1 -1
  10. package/dist/commonjs/types/StringArrayOfStringsMap.js +3 -5
  11. package/dist/commonjs/types/StringArrayOfStringsMap.js.map +1 -1
  12. package/dist/commonjs/types/StringMap.js +3 -5
  13. package/dist/commonjs/types/StringMap.js.map +1 -1
  14. package/dist/commonjs/types/StringUnknownMap.js.map +1 -1
  15. package/dist/commonjs/types/UnionKeys.js +3 -0
  16. package/dist/commonjs/types/UnionKeys.js.map +1 -0
  17. package/dist/commonjs/utils/compact.js +16 -0
  18. package/dist/commonjs/utils/compact.js.map +1 -0
  19. package/dist/commonjs/utils/get-tag-string.js.map +1 -1
  20. package/dist/commonjs/utils/get.js +3 -0
  21. package/dist/commonjs/utils/get.js.map +1 -1
  22. package/dist/commonjs/utils/has-defined.js.map +1 -1
  23. package/dist/commonjs/utils/is-arguments.js.map +1 -1
  24. package/dist/commonjs/utils/is-array-of-strings.js +1 -1
  25. package/dist/commonjs/utils/is-array-of-strings.js.map +1 -1
  26. package/dist/commonjs/utils/is-array.js.map +1 -1
  27. package/dist/commonjs/utils/is-boolean.js +8 -0
  28. package/dist/commonjs/utils/is-boolean.js.map +1 -0
  29. package/dist/commonjs/utils/is-empty.js +10 -11
  30. package/dist/commonjs/utils/is-empty.js.map +1 -1
  31. package/dist/commonjs/utils/is-enum-value.js.map +1 -1
  32. package/dist/commonjs/utils/is-map-with-values-of-type.js +1 -1
  33. package/dist/commonjs/utils/is-map-with-values-of-type.js.map +1 -1
  34. package/dist/commonjs/utils/is-number.js.map +1 -1
  35. package/dist/commonjs/utils/is-object.js +1 -1
  36. package/dist/commonjs/utils/is-object.js.map +1 -1
  37. package/dist/commonjs/utils/is-promise-like.js.map +1 -1
  38. package/dist/commonjs/utils/is-promise.js.map +1 -1
  39. package/dist/commonjs/utils/is-set.js +13 -0
  40. package/dist/commonjs/utils/is-set.js.map +1 -0
  41. package/dist/commonjs/utils/is-string.js.map +1 -1
  42. package/dist/commonjs/utils/make-template.js +3 -6
  43. package/dist/commonjs/utils/make-template.js.map +1 -1
  44. package/dist/commonjs/utils/range.js +25 -0
  45. package/dist/commonjs/utils/range.js.map +1 -0
  46. package/dist/commonjs/utils/uniq.js +2 -2
  47. package/dist/commonjs/utils/uniq.js.map +1 -1
  48. package/dist/esm/index.js +4 -0
  49. package/dist/esm/index.js.map +1 -1
  50. package/dist/esm/types/KeyValueStringObject.js +4 -6
  51. package/dist/esm/types/KeyValueStringObject.js.map +1 -1
  52. package/dist/esm/types/StrictUnion.js +0 -1
  53. package/dist/esm/types/StrictUnion.js.map +1 -1
  54. package/dist/esm/types/StringArrayOfStringsMap.js +3 -5
  55. package/dist/esm/types/StringArrayOfStringsMap.js.map +1 -1
  56. package/dist/esm/types/StringMap.js +3 -5
  57. package/dist/esm/types/StringMap.js.map +1 -1
  58. package/dist/esm/types/StringUnknownMap.js.map +1 -1
  59. package/dist/esm/types/UnionKeys.js +1 -0
  60. package/dist/esm/types/UnionKeys.js.map +1 -0
  61. package/dist/esm/utils/compact.js +12 -0
  62. package/dist/esm/utils/compact.js.map +1 -0
  63. package/dist/esm/utils/get-tag-string.js.map +1 -1
  64. package/dist/esm/utils/get.js +3 -0
  65. package/dist/esm/utils/get.js.map +1 -1
  66. package/dist/esm/utils/has-defined.js.map +1 -1
  67. package/dist/esm/utils/is-arguments.js.map +1 -1
  68. package/dist/esm/utils/is-array-of-strings.js +1 -1
  69. package/dist/esm/utils/is-array-of-strings.js.map +1 -1
  70. package/dist/esm/utils/is-array.js.map +1 -1
  71. package/dist/esm/utils/is-boolean.js +4 -0
  72. package/dist/esm/utils/is-boolean.js.map +1 -0
  73. package/dist/esm/utils/is-empty.js +10 -11
  74. package/dist/esm/utils/is-empty.js.map +1 -1
  75. package/dist/esm/utils/is-enum-value.js.map +1 -1
  76. package/dist/esm/utils/is-map-with-values-of-type.js +1 -1
  77. package/dist/esm/utils/is-map-with-values-of-type.js.map +1 -1
  78. package/dist/esm/utils/is-number.js.map +1 -1
  79. package/dist/esm/utils/is-object.js +1 -1
  80. package/dist/esm/utils/is-object.js.map +1 -1
  81. package/dist/esm/utils/is-promise-like.js.map +1 -1
  82. package/dist/esm/utils/is-promise.js.map +1 -1
  83. package/dist/esm/utils/is-set.js +9 -0
  84. package/dist/esm/utils/is-set.js.map +1 -0
  85. package/dist/esm/utils/is-string.js.map +1 -1
  86. package/dist/esm/utils/make-template.js +3 -6
  87. package/dist/esm/utils/make-template.js.map +1 -1
  88. package/dist/esm/utils/range.js +21 -0
  89. package/dist/esm/utils/range.js.map +1 -0
  90. package/dist/esm/utils/uniq.js +2 -2
  91. package/dist/esm/utils/uniq.js.map +1 -1
  92. package/dist/types/index.d.ts +4 -0
  93. package/dist/types/types/KeyValueStringObject.d.ts +1 -1
  94. package/dist/types/types/StrictUnion.d.ts +1 -1
  95. package/dist/types/types/StringArrayOfStringsMap.d.ts +1 -1
  96. package/dist/types/types/StringMap.d.ts +1 -1
  97. package/dist/types/types/StringUnknownMap.d.ts +1 -1
  98. package/dist/types/types/UnionKeys.d.ts +9 -0
  99. package/dist/types/utils/compact.d.ts +3 -0
  100. package/dist/types/utils/get-tag-string.d.ts +1 -1
  101. package/dist/types/utils/has-defined.d.ts +4 -1
  102. package/dist/types/utils/is-arguments.d.ts +1 -1
  103. package/dist/types/utils/is-array-of-strings.d.ts +1 -1
  104. package/dist/types/utils/is-array.d.ts +1 -1
  105. package/dist/types/utils/is-boolean.d.ts +1 -0
  106. package/dist/types/utils/is-empty.d.ts +11 -1
  107. package/dist/types/utils/is-enum-value.d.ts +1 -1
  108. package/dist/types/utils/is-map-with-values-of-type.d.ts +1 -1
  109. package/dist/types/utils/is-number.d.ts +1 -1
  110. package/dist/types/utils/is-object.d.ts +1 -1
  111. package/dist/types/utils/is-promise-like.d.ts +1 -1
  112. package/dist/types/utils/is-promise.d.ts +1 -1
  113. package/dist/types/utils/is-set.d.ts +6 -0
  114. package/dist/types/utils/is-string.d.ts +1 -1
  115. package/dist/types/utils/make-template.d.ts +1 -1
  116. package/dist/types/utils/range.d.ts +7 -0
  117. package/eslint.config.js +15 -0
  118. package/package.json +18 -18
  119. package/renovate.json +4 -0
  120. package/src/index.ts +4 -0
  121. package/src/types/KeyValueStringObject.ts +5 -7
  122. package/src/types/StrictUnion.ts +1 -2
  123. package/src/types/StringArrayOfStringsMap.ts +5 -7
  124. package/src/types/StringMap.ts +5 -7
  125. package/src/types/StringUnknownMap.ts +1 -1
  126. package/src/types/UnionKeys.ts +9 -0
  127. package/src/utils/compact.ts +14 -0
  128. package/src/utils/get-tag-string.ts +1 -1
  129. package/src/utils/get.ts +3 -0
  130. package/src/utils/has-defined.ts +2 -1
  131. package/src/utils/is-arguments.ts +1 -1
  132. package/src/utils/is-array-of-strings.ts +2 -2
  133. package/src/utils/is-array.ts +1 -1
  134. package/src/utils/is-boolean.ts +3 -0
  135. package/src/utils/is-empty.ts +35 -3
  136. package/src/utils/is-enum-value.ts +1 -1
  137. package/src/utils/is-map-with-values-of-type.ts +3 -3
  138. package/src/utils/is-number.ts +1 -1
  139. package/src/utils/is-object.ts +2 -2
  140. package/src/utils/is-promise-like.ts +2 -2
  141. package/src/utils/is-promise.ts +1 -1
  142. package/src/utils/is-set.ts +8 -0
  143. package/src/utils/is-string.ts +1 -1
  144. package/src/utils/make-template.ts +8 -11
  145. package/src/utils/range.ts +24 -0
  146. 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: any): o is object;
12
+ export declare function isObject(o: unknown): o is object;
@@ -5,4 +5,4 @@
5
5
  *
6
6
  * @returns `true` if `o` is `Promise`-like (i.e. has a `then` function)
7
7
  */
8
- export declare function isPromiseLike(o: any): o is PromiseLike<unknown>;
8
+ export declare function isPromiseLike(o: unknown): o is PromiseLike<unknown>;
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @returns `true` if `o` is a `Promise`
5
5
  */
6
- export declare function isPromise(o: any): o is Promise<unknown>;
6
+ export declare function isPromise(o: unknown): o is Promise<unknown>;
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Type guard for `Set`s.
3
+ *
4
+ * @returns `true` if `o` is a `Set`, regardless of the types that it contains
5
+ */
6
+ export declare function isSet(o: unknown): o is Set<unknown>;
@@ -5,4 +5,4 @@
5
5
  * `string` values, even if the TypeScript compiler's `strictNullChecks` flag is set to
6
6
  * `false` in your project.
7
7
  */
8
- export declare function isString(o: any): o is string;
8
+ export declare function isString(o: unknown): o is string;
@@ -7,7 +7,7 @@ export interface ToolboxTemplateSettings {
7
7
  };
8
8
  }
9
9
  export interface ToolboxTemplateFunction {
10
- (data: Record<string, any>): 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[];
@@ -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.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.0",
40
- "@silvermine/eslint-config": "github:silvermine/eslint-config-silvermine#39387feb42f5d78bf4947fbb7cb0727b5ea2bebe",
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.1.7",
44
- "@types/mocha": "5.2.5",
43
+ "@types/chai": "4.3.20",
44
+ "@types/mocha": "5.2.7",
45
45
  "@types/node": "12.20.45",
46
- "@types/sinon": "5.0.5",
47
- "chai": "4.2.0",
48
- "check-node-version": "4.0.2",
49
- "coveralls": "3.0.9",
50
- "eslint": "8.0.0",
51
- "grunt": "1.0.4",
52
- "grunt-cli": "1.3.2",
53
- "grunt-concurrent": "2.3.1",
54
- "grunt-contrib-clean": "2.0.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.1.0",
58
+ "nyc": "13.3.0",
59
59
  "sinon": "5.1.1",
60
- "source-map-support": "0.5.16",
60
+ "source-map-support": "0.5.21",
61
61
  "ts-node": "7.0.1",
62
- "tslib": "2.6.2",
63
- "typescript": "3.9.5"
62
+ "tslib": "2.6.3",
63
+ "typescript": "3.9.10"
64
64
  }
65
65
  }
package/renovate.json ADDED
@@ -0,0 +1,4 @@
1
+ {
2
+ "$schema": "https://docs.renovatebot.com/renovate-schema.json",
3
+ "extends": [ "github>silvermine/renovate-config" ]
4
+ }
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: any): o is KeyValueStringObject {
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 (!isObject(o) || isArray(o) || isArguments(o)) {
36
+ if (!isStringUnknownMap(o)) {
39
37
  return false;
40
38
  }
41
39
 
42
- for (let k of Object.keys(o)) {
43
- let v: any = (o as any)[k];
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;
@@ -1,6 +1,5 @@
1
- /* eslint-disable @typescript-eslint/no-type-alias */
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 { isObject } from '../utils/is-object';
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: any): o is StringArrayOfStringsMap {
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 (!isObject(o) || isArray(o) || isArguments(o)) {
25
+ if (!isStringUnknownMap(o)) {
28
26
  return false;
29
27
  }
30
28
 
31
- for (let k of Object.keys(o)) {
32
- if (!isArrayOfStrings((o as any)[k])) {
29
+ for (const k of Object.keys(o)) {
30
+ if (!isArrayOfStrings(o[k])) {
33
31
  return false;
34
32
  }
35
33
  }
@@ -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: any): o is StringMap {
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 (!isObject(o) || isArray(o) || isArguments(o)) {
23
+ if (!isStringUnknownMap(o)) {
26
24
  return false;
27
25
  }
28
26
 
29
- for (let k of Object.keys(o)) {
30
- if (!isString((o as any)[k])) {
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: any): o is StringUnknownMap {
60
+ export function isStringUnknownMap(o: unknown): o is StringUnknownMap {
61
61
  return isObject(o) && !isArray(o) && !isArguments(o);
62
62
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Extracts all the keys of a union type. For example:
3
+ *
4
+ * ```
5
+ * type Union = { a: number } | { b: string };
6
+ * type Keys = UnionKeys<Union>; // 'a' | 'b'
7
+ * ```
8
+ */
9
+ export type UnionKeys<T> = T extends T ? keyof T : never;
@@ -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: any): string {
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;
@@ -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>(o: T, propName: U): o is RequireDefined<T, U> {
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: any): boolean {
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: any): values is string[] {
11
+ export function isArrayOfStrings(values: unknown): values is string[] {
12
12
  if (!isArray(values)) {
13
13
  return false;
14
14
  }
15
15
 
16
- for (let v of values) {
16
+ for (const v of values) {
17
17
  if (!isString(v)) {
18
18
  return false;
19
19
  }
@@ -3,6 +3,6 @@
3
3
  *
4
4
  * @returns `true` if `o` is an `Array`, regardless of the types that it contains
5
5
  */
6
- export function isArray(o: any): o is unknown[] {
6
+ export function isArray(o: unknown): o is unknown[] {
7
7
  return Array.isArray(o);
8
8
  }
@@ -0,0 +1,3 @@
1
+ export function isBoolean(o: unknown): o is boolean {
2
+ return o === true || o === false;
3
+ }
@@ -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: any): boolean {
16
- if (o === null || isUndefined(o)) {
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
- return Object.keys(o).length === 0;
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: any): value is T[keyof T] {
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: any) => x is T, o: any): o is Record<string, T> {
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 (let k of Object.keys(o)) {
15
- if (!guard((o as any)[k])) {
14
+ for (const k of Object.keys(o)) {
15
+ if (!guard(o[k])) {
16
16
  return false;
17
17
  }
18
18
  }
@@ -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: any): o is number {
11
+ export function isNumber(o: unknown): o is number {
12
12
  return typeof o === 'number' || (isObject(o) && getTagString(o) === '[object Number]');
13
13
  }
@@ -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: any): o is object {
13
- let type = typeof o;
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: any): o is PromiseLike<unknown> {
12
- return isPromise(o) || (isObject(o) && typeof (o as any).then === 'function');
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
  }
@@ -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: any): o is Promise<unknown> {
9
+ export function isPromise(o: unknown): o is Promise<unknown> {
10
10
  return isObject(o) && getTagString(o) === '[object Promise]';
11
11
  }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Type guard for `Set`s.
3
+ *
4
+ * @returns `true` if `o` is a `Set`, regardless of the types that it contains
5
+ */
6
+ export function isSet(o: unknown): o is Set<unknown> {
7
+ return o instanceof Set;
8
+ }
@@ -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: any): o is string {
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, any>): 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 parts: (TemplateFunction | string)[] = [],
55
- index = 0,
56
- settings = Object.assign({}, DEFAULT_SETTINGS, userSettings || {}),
57
- regExpPattern, matcher;
55
+ let index = 0;
58
56
 
59
- regExpPattern = [
60
- settings.escape.source,
61
- settings.interpolate.source,
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: any) => {
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
- let value = arr[i],
7
- computed = iteratee(value, i, arr);
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
- let value = arr[i];
24
+ const value = arr[i];
25
25
 
26
26
  if (i === 0 || lastSeen !== value) {
27
27
  result.push(value);