@silvermine/toolbox 0.2.0 → 0.4.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 (91) hide show
  1. package/.github/workflows/ci.yml +49 -0
  2. package/.markdownlint.json +3 -0
  3. package/.nvmrc +1 -0
  4. package/CHANGELOG.md +36 -0
  5. package/README.md +7 -3
  6. package/dist/commonjs/index.js +7 -0
  7. package/dist/commonjs/index.js.map +1 -1
  8. package/dist/commonjs/types/StrictUnion.js +4 -0
  9. package/dist/commonjs/types/StrictUnion.js.map +1 -0
  10. package/dist/commonjs/utils/compact.js +16 -0
  11. package/dist/commonjs/utils/compact.js.map +1 -0
  12. package/dist/commonjs/utils/is-arguments.js.map +1 -1
  13. package/dist/commonjs/utils/is-array-of-strings.js.map +1 -1
  14. package/dist/commonjs/utils/is-array.js.map +1 -1
  15. package/dist/commonjs/utils/is-boolean.js +8 -0
  16. package/dist/commonjs/utils/is-boolean.js.map +1 -0
  17. package/dist/commonjs/utils/is-enum-value.js.map +1 -1
  18. package/dist/commonjs/utils/is-number.js.map +1 -1
  19. package/dist/commonjs/utils/is-object.js.map +1 -1
  20. package/dist/commonjs/utils/is-promise-like.js.map +1 -1
  21. package/dist/commonjs/utils/is-promise.js.map +1 -1
  22. package/dist/commonjs/utils/is-set.js +13 -0
  23. package/dist/commonjs/utils/is-set.js.map +1 -0
  24. package/dist/commonjs/utils/is-string.js.map +1 -1
  25. package/dist/commonjs/utils/pick.js +15 -0
  26. package/dist/commonjs/utils/pick.js.map +1 -0
  27. package/dist/commonjs/utils/range.js +25 -0
  28. package/dist/commonjs/utils/range.js.map +1 -0
  29. package/dist/commonjs/utils/uniq.js +49 -0
  30. package/dist/commonjs/utils/uniq.js.map +1 -0
  31. package/dist/esm/index.js +7 -0
  32. package/dist/esm/index.js.map +1 -1
  33. package/dist/esm/types/StrictUnion.js +2 -0
  34. package/dist/esm/types/StrictUnion.js.map +1 -0
  35. package/dist/esm/utils/compact.js +12 -0
  36. package/dist/esm/utils/compact.js.map +1 -0
  37. package/dist/esm/utils/is-arguments.js.map +1 -1
  38. package/dist/esm/utils/is-array-of-strings.js.map +1 -1
  39. package/dist/esm/utils/is-array.js.map +1 -1
  40. package/dist/esm/utils/is-boolean.js +4 -0
  41. package/dist/esm/utils/is-boolean.js.map +1 -0
  42. package/dist/esm/utils/is-enum-value.js.map +1 -1
  43. package/dist/esm/utils/is-number.js.map +1 -1
  44. package/dist/esm/utils/is-object.js.map +1 -1
  45. package/dist/esm/utils/is-promise-like.js.map +1 -1
  46. package/dist/esm/utils/is-promise.js.map +1 -1
  47. package/dist/esm/utils/is-set.js +9 -0
  48. package/dist/esm/utils/is-set.js.map +1 -0
  49. package/dist/esm/utils/is-string.js.map +1 -1
  50. package/dist/esm/utils/pick.js +11 -0
  51. package/dist/esm/utils/pick.js.map +1 -0
  52. package/dist/esm/utils/range.js +21 -0
  53. package/dist/esm/utils/range.js.map +1 -0
  54. package/dist/esm/utils/uniq.js +46 -0
  55. package/dist/esm/utils/uniq.js.map +1 -0
  56. package/dist/types/index.d.ts +7 -0
  57. package/dist/types/types/StrictUnion.d.ts +15 -0
  58. package/dist/types/utils/compact.d.ts +3 -0
  59. package/dist/types/utils/is-arguments.d.ts +1 -1
  60. package/dist/types/utils/is-array-of-strings.d.ts +1 -1
  61. package/dist/types/utils/is-array.d.ts +1 -1
  62. package/dist/types/utils/is-boolean.d.ts +1 -0
  63. package/dist/types/utils/is-enum-value.d.ts +1 -1
  64. package/dist/types/utils/is-number.d.ts +1 -1
  65. package/dist/types/utils/is-object.d.ts +1 -1
  66. package/dist/types/utils/is-promise-like.d.ts +1 -1
  67. package/dist/types/utils/is-promise.d.ts +1 -1
  68. package/dist/types/utils/is-set.d.ts +6 -0
  69. package/dist/types/utils/is-string.d.ts +1 -1
  70. package/dist/types/utils/pick.d.ts +2 -0
  71. package/dist/types/utils/range.d.ts +7 -0
  72. package/dist/types/utils/uniq.d.ts +8 -0
  73. package/package.json +29 -32
  74. package/renovate.json +4 -0
  75. package/src/index.ts +7 -0
  76. package/src/types/StrictUnion.ts +15 -0
  77. package/src/utils/compact.ts +14 -0
  78. package/src/utils/is-arguments.ts +1 -1
  79. package/src/utils/is-array-of-strings.ts +1 -1
  80. package/src/utils/is-array.ts +1 -1
  81. package/src/utils/is-boolean.ts +3 -0
  82. package/src/utils/is-enum-value.ts +1 -1
  83. package/src/utils/is-number.ts +1 -1
  84. package/src/utils/is-object.ts +1 -1
  85. package/src/utils/is-promise-like.ts +1 -1
  86. package/src/utils/is-promise.ts +1 -1
  87. package/src/utils/is-set.ts +8 -0
  88. package/src/utils/is-string.ts +1 -1
  89. package/src/utils/pick.ts +24 -0
  90. package/src/utils/range.ts +24 -0
  91. package/src/utils/uniq.ts +59 -0
@@ -0,0 +1 @@
1
+ {"version":3,"file":"uniq.js","sourceRoot":"","sources":["../../../src/utils/uniq.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,CAAO,GAAQ,EAAE,QAA8C,EAAO,EAAE;IAC1F,MAAM,MAAM,GAAQ,EAAE,EAChB,IAAI,GAAQ,EAAE,CAAC;IAErB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;QACnD,IAAI,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,EACd,QAAQ,GAAG,QAAQ,CAAC,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,CAAC;QAEvC,IAAI,IAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC,CAAC,EAAE;YAChC,IAAI,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;YACpB,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SACrB;KACH;IACD,OAAO,MAAM,CAAC;AACjB,CAAC,CAAC;AAGF,MAAM,UAAU,GAAG,CAAO,GAAQ,EAAO,EAAE;IACxC,MAAM,MAAM,GAAQ,EAAE,CAAC;IAEvB,IAAI,QAA2B,CAAC;IAEhC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,GAAG,MAAM,EAAE,CAAC,EAAE,EAAE;QACnD,IAAI,KAAK,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC;QAEnB,IAAI,CAAC,KAAK,CAAC,IAAI,QAAQ,KAAK,KAAK,EAAE;YAChC,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;SACrB;QACD,QAAQ,GAAG,KAAK,CAAC;KACnB;IACD,OAAO,MAAM,CAAC;AACjB,CAAC,CAAC;AAGF,MAAM,YAAY,GAAG,CAAI,GAAQ,EAAO,EAAE;IACvC,MAAM,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,KAAK,EAAE,IAAI,EAAE,EAAE;QAC9C,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,KAAK,CAAC;IACxC,CAAC,CAAC,CAAC;IAEH,OAAO,MAAM,CAAC;AACjB,CAAC,CAAC;AAGF;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,IAAI,GAAG,CAAO,GAAQ,EAAE,QAAkB,EAAE,QAA+C,EAAO,EAAE;IAC9G,IAAI,QAAQ,EAAE;QACX,OAAO,YAAY,CAAC,GAAG,EAAE,QAAQ,CAAC,CAAC;KACrC;IACD,IAAI,QAAQ,EAAE;QACX,OAAO,UAAU,CAAC,GAAG,CAAC,CAAC;KACzB;IACD,OAAO,YAAY,CAAC,GAAG,CAAC,CAAC;AAC5B,CAAC,CAAC"}
@@ -4,6 +4,7 @@ export * from './types/Optional';
4
4
  export * from './types/PropsWithType';
5
5
  export * from './types/RequireDefined';
6
6
  export * from './types/RequireOptional';
7
+ export * from './types/StrictUnion';
7
8
  export * from './types/StringArrayOfStringsMap';
8
9
  export * from './types/StringMap';
9
10
  export * from './types/StringUnknownMap';
@@ -12,6 +13,7 @@ export * from './utils/has-defined';
12
13
  export * from './utils/is-arguments';
13
14
  export * from './utils/is-array';
14
15
  export * from './utils/is-array-of-strings';
16
+ export * from './utils/is-boolean';
15
17
  export * from './utils/is-empty';
16
18
  export * from './utils/is-enum-value';
17
19
  export * from './utils/is-map-with-values-of-type';
@@ -19,14 +21,19 @@ export * from './utils/is-number';
19
21
  export * from './utils/is-object';
20
22
  export * from './utils/is-promise';
21
23
  export * from './utils/is-promise-like';
24
+ export * from './utils/is-set';
22
25
  export * from './utils/is-string';
23
26
  export * from './utils/is-undefined';
24
27
  export * from './utils/is-null';
25
28
  export * from './utils/is-not-null-or-undefined';
26
29
  export * from './utils/chunk';
30
+ export * from './utils/compact';
27
31
  export * from './utils/flatten';
32
+ export * from './utils/pick';
28
33
  export * from './utils/pluck';
29
34
  export * from './utils/delay';
30
35
  export * from './utils/get';
31
36
  export * from './utils/escape-html';
32
37
  export * from './utils/make-template';
38
+ export * from './utils/uniq';
39
+ export * from './utils/range';
@@ -0,0 +1,15 @@
1
+ declare type UnionKeys<T> = T extends unknown ? keyof T : never;
2
+ declare type InvalidKeys<K extends string | number | symbol> = {
3
+ [P in K]?: never;
4
+ };
5
+ declare type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<Exclude<UnionKeys<TAll>, keyof T>>) : never;
6
+ /**
7
+ * A basic TypeScript union (e.g. A | B) results in a type containing the available
8
+ * properties from the provided types. When StrictUnion is used (e.g. StrictUnion<A | B>),
9
+ * the resulting type can only contain the properties from one of the types (e.g. all the
10
+ * properties from A, but none of the properties from B).
11
+ *
12
+ * See: https://github.com/microsoft/TypeScript/issues/20863#issuecomment-520551758
13
+ */
14
+ export declare type StrictUnion<T> = StrictUnionHelper<T, T>;
15
+ export {};
@@ -0,0 +1,3 @@
1
+ declare type FalsyVals = '' | 0 | false | null | undefined;
2
+ export declare function compact<T>(arr: (T | FalsyVals)[]): T[];
3
+ export {};
@@ -4,4 +4,4 @@
4
4
  * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
5
5
  * @returns `true` if `o` is a function's array-like `arguments` variable
6
6
  */
7
- export declare function isArguments(o: any): boolean;
7
+ export declare function isArguments(o: unknown): o is IArguments;
@@ -5,4 +5,4 @@
5
5
  * not counted as `string` values, even if the TypeScript compiler's `strictNullChecks`
6
6
  * flag is set to `false` in your project.
7
7
  */
8
- export declare function isArrayOfStrings(values: any): values is string[];
8
+ export declare function isArrayOfStrings(values: unknown): values is string[];
@@ -3,4 +3,4 @@
3
3
  *
4
4
  * @returns `true` if `o` is an `Array`, regardless of the types that it contains
5
5
  */
6
- export declare function isArray(o: any): o is unknown[];
6
+ export declare function isArray(o: unknown): o is unknown[];
@@ -0,0 +1 @@
1
+ export declare function isBoolean(o: unknown): o is boolean;
@@ -1,4 +1,4 @@
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 declare function isEnumValue<T>(enumType: T, value: any): value is T[keyof T];
4
+ export declare function isEnumValue<T>(enumType: T, value: unknown): value is T[keyof T];
@@ -5,4 +5,4 @@
5
5
  * considered `number`s. If you need to check for one of those values, you can use the
6
6
  * built-in `Number.isNaN` or `Number.isFinite` functions.
7
7
  */
8
- export declare function isNumber(o: any): o is number;
8
+ export declare function isNumber(o: unknown): o is number;
@@ -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;
@@ -0,0 +1,2 @@
1
+ export declare function pick<T extends object, K extends keyof T>(obj: T, props: K[]): Pick<T, K>;
2
+ export declare function pick<T extends object, K extends keyof T = never>(obj: T, ...props: K[]): Pick<T, K>;
@@ -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,8 @@
1
+ /**
2
+ * Produce a duplicate-free version of the array. If the array has already
3
+ * been sorted, you have the option of using a faster algorithm.
4
+ * The faster algorithm will not work with an iteratee if the iteratee
5
+ * is not a one-to-one function, so providing an iteratee will disable
6
+ * the faster algorithm.
7
+ */
8
+ export declare const uniq: <T, U>(arr: T[], isSorted?: boolean | undefined, iteratee?: ((value: T, i: number, arr: T[]) => U) | undefined) => T[];
package/package.json CHANGED
@@ -1,15 +1,21 @@
1
1
  {
2
2
  "name": "@silvermine/toolbox",
3
- "version": "0.2.0",
3
+ "version": "0.4.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",
7
7
  "module": "./dist/esm/index",
8
8
  "scripts": {
9
9
  "prepare": "grunt build",
10
- "test:node-version": "check-node-version --npm 6.14.12 --print",
11
- "test": "npm run test:node-version && TS_NODE_PROJECT='tests/tsconfig.json' TS_NODE_FILES=true nyc mocha --opts ./.mocha.opts",
12
- "commitlint": "commitlint --from 0b9d320"
10
+ "check-node-version": "check-node-version --npm 10.5.0 --print",
11
+ "test": "TS_NODE_PROJECT='tests/tsconfig.json' TS_NODE_FILES=true nyc mocha --opts ./.mocha.opts",
12
+ "eslint": "eslint '{,!(node_modules|dist)/**/}*.{js,ts}'",
13
+ "markdownlint": "markdownlint -c .markdownlint.json -i CHANGELOG.md '{,!(node_modules)/**/}*.md'",
14
+ "commitlint": "commitlint --from 0b9d320",
15
+ "standards": "npm run markdownlint && npm run eslint",
16
+ "release:preview": "node ./node_modules/@silvermine/standardization/scripts/release.js preview",
17
+ "release:prep-changelog": "node ./node_modules/@silvermine/standardization/scripts/release.js prep-changelog",
18
+ "release:finalize": "node ./node_modules/@silvermine/standardization/scripts/release.js finalize"
13
19
  },
14
20
  "author": "Matt Luedke",
15
21
  "license": "MIT",
@@ -27,42 +33,33 @@
27
33
  "type guards"
28
34
  ],
29
35
  "peerDependencies": {
30
- "tslib": "1.9.x"
36
+ "tslib": "^1.9.0 || ^2"
31
37
  },
32
38
  "devDependencies": {
33
- "@silvermine/chai-strictly-equal": "1.1.0",
34
- "@silvermine/eslint-config": "3.1.0-beta.0",
35
- "@silvermine/standardization": "1.2.1",
36
- "@silvermine/typescript-config": "git+https://github.com/silvermine/typescript-config.git#23213e33077089e723629dead5342abe6f3b3c8c",
37
- "@types/chai": "4.1.7",
38
- "@types/mocha": "5.2.5",
39
+ "@silvermine/chai-strictly-equal": "1.1.1",
40
+ "@silvermine/eslint-config": "github:silvermine/eslint-config-silvermine#aa8ecacb12ab778078fd195d3b15f09ecac76c11",
41
+ "@silvermine/standardization": "2.0.0",
42
+ "@silvermine/typescript-config": "git+https://github.com/silvermine/typescript-config#23213e33077089e723629dead5342abe6f3b3c8c",
43
+ "@types/chai": "4.3.18",
44
+ "@types/mocha": "5.2.7",
39
45
  "@types/node": "12.20.45",
40
- "@types/sinon": "5.0.5",
41
- "chai": "4.2.0",
42
- "check-node-version": "4.0.2",
43
- "coveralls": "3.0.9",
44
- "cz-conventional-changelog": "3.1.0",
45
- "eslint": "6.8.0",
46
- "grunt": "1.0.4",
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.0",
51
+ "grunt": "1.6.1",
47
52
  "grunt-cli": "1.3.2",
48
- "grunt-concurrent": "2.3.1",
49
- "grunt-contrib-clean": "2.0.0",
53
+ "grunt-concurrent": "3.0.0",
54
+ "grunt-contrib-clean": "2.0.1",
50
55
  "grunt-contrib-watch": "1.1.0",
51
- "grunt-eslint": "24.0.0",
52
56
  "grunt-exec": "3.0.0",
53
57
  "mocha": "5.2.0",
54
- "nyc": "13.1.0",
58
+ "nyc": "13.3.0",
55
59
  "sinon": "5.1.1",
56
- "source-map-support": "0.5.16",
57
- "standard-version": "5.0.2",
60
+ "source-map-support": "0.5.21",
58
61
  "ts-node": "7.0.1",
59
- "tslib": "1.9.3",
60
- "typescript": "3.9.5"
61
- },
62
- "dependencies": {},
63
- "config": {
64
- "commitizen": {
65
- "path": "./node_modules/cz-conventional-changelog"
66
- }
62
+ "tslib": "2.6.3",
63
+ "typescript": "3.9.10"
67
64
  }
68
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
@@ -4,6 +4,7 @@ export * from './types/Optional';
4
4
  export * from './types/PropsWithType';
5
5
  export * from './types/RequireDefined';
6
6
  export * from './types/RequireOptional';
7
+ export * from './types/StrictUnion';
7
8
  export * from './types/StringArrayOfStringsMap';
8
9
  export * from './types/StringMap';
9
10
  export * from './types/StringUnknownMap';
@@ -13,6 +14,7 @@ export * from './utils/has-defined';
13
14
  export * from './utils/is-arguments';
14
15
  export * from './utils/is-array';
15
16
  export * from './utils/is-array-of-strings';
17
+ export * from './utils/is-boolean';
16
18
  export * from './utils/is-empty';
17
19
  export * from './utils/is-enum-value';
18
20
  export * from './utils/is-map-with-values-of-type';
@@ -20,15 +22,20 @@ export * from './utils/is-number';
20
22
  export * from './utils/is-object';
21
23
  export * from './utils/is-promise';
22
24
  export * from './utils/is-promise-like';
25
+ export * from './utils/is-set';
23
26
  export * from './utils/is-string';
24
27
  export * from './utils/is-undefined';
25
28
  export * from './utils/is-null';
26
29
  export * from './utils/is-not-null-or-undefined';
27
30
 
28
31
  export * from './utils/chunk';
32
+ export * from './utils/compact';
29
33
  export * from './utils/flatten';
34
+ export * from './utils/pick';
30
35
  export * from './utils/pluck';
31
36
  export * from './utils/delay';
32
37
  export * from './utils/get';
33
38
  export * from './utils/escape-html';
34
39
  export * from './utils/make-template';
40
+ export * from './utils/uniq';
41
+ export * from './utils/range';
@@ -0,0 +1,15 @@
1
+ /* eslint-disable @typescript-eslint/no-type-alias */
2
+
3
+ type UnionKeys<T> = T extends unknown ? keyof T : never;
4
+ type InvalidKeys<K extends string | number | symbol> = { [P in K]?: never };
5
+ type StrictUnionHelper<T, TAll> = T extends unknown ? (T & InvalidKeys<Exclude<UnionKeys<TAll>, keyof T>>) : never;
6
+
7
+ /**
8
+ * A basic TypeScript union (e.g. A | B) results in a type containing the available
9
+ * properties from the provided types. When StrictUnion is used (e.g. StrictUnion<A | B>),
10
+ * the resulting type can only contain the properties from one of the types (e.g. all the
11
+ * properties from A, but none of the properties from B).
12
+ *
13
+ * See: https://github.com/microsoft/TypeScript/issues/20863#issuecomment-520551758
14
+ */
15
+ export type StrictUnion<T> = StrictUnionHelper<T, T>;
@@ -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
+ }
@@ -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,7 +8,7 @@ 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
  }
@@ -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
+ }
@@ -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];
@@ -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,7 +9,7 @@
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 {
12
+ export function isObject(o: unknown): o is object {
13
13
  let type = typeof o;
14
14
 
15
15
  return o !== null && (type === 'object' || type === 'function');
@@ -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> {
11
+ export function isPromiseLike(o: unknown): o is PromiseLike<unknown> {
12
12
  return isPromise(o) || (isObject(o) && typeof (o as any).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
  }
@@ -0,0 +1,24 @@
1
+ import { isArray } from './is-array';
2
+
3
+ // NOTE: If changing these types, ensure the following cases cause a type error:
4
+ //
5
+ // const noProps = pick({ a: 1 });
6
+ //
7
+ // noProps.a; // Property 'a' does not exist on type 'Pick<{ a: number; }, never>'.
8
+ //
9
+ // const emptyArr = pick({ a: 1 }, []);
10
+ //
11
+ // emptyArr.a; // Property 'a' does not exist on type 'Pick<{ a: number; }, never>'.
12
+
13
+ export function pick<T extends object, K extends keyof T>(obj: T, props: K[]): Pick<T, K>;
14
+ export function pick<T extends object, K extends keyof T = never>(obj: T, ...props: K[]): Pick<T, K>;
15
+ export function pick<T extends object, K extends keyof T>(obj: T, ...args: K[] | [ K[] ]): Pick<T, K> {
16
+ const props = ((args.length === 1 && isArray(args[0])) ? args[0] : args) as K[];
17
+
18
+ return props.reduce((memo, prop) => {
19
+ if (prop in obj) {
20
+ memo[prop] = obj[prop];
21
+ }
22
+ return memo;
23
+ }, {} as Pick<T, K>);
24
+ }
@@ -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
+ }
@@ -0,0 +1,59 @@
1
+ const iterateeUniq = <T, U>(arr: T[], iteratee: (value: T, i: number, arr: T[]) => U): T[] => {
2
+ const result: T[] = [],
3
+ seen: U[] = [];
4
+
5
+ for (let i = 0, length = arr.length; i < length; i++) {
6
+ let value = arr[i],
7
+ computed = iteratee(value, i, arr);
8
+
9
+ if (seen.indexOf(computed) === -1) {
10
+ seen.push(computed);
11
+ result.push(value);
12
+ }
13
+ }
14
+ return result;
15
+ };
16
+
17
+
18
+ const sortedUniq = <T, U>(arr: T[]): T[] => {
19
+ const result: T[] = [];
20
+
21
+ let lastSeen: T | U | undefined;
22
+
23
+ for (let i = 0, length = arr.length; i < length; i++) {
24
+ let value = arr[i];
25
+
26
+ if (i === 0 || lastSeen !== value) {
27
+ result.push(value);
28
+ }
29
+ lastSeen = value;
30
+ }
31
+ return result;
32
+ };
33
+
34
+
35
+ const standardUniq = <T>(arr: T[]): T[] => {
36
+ const result = arr.filter((value, index, _arr) => {
37
+ return _arr.indexOf(value) === index;
38
+ });
39
+
40
+ return result;
41
+ };
42
+
43
+
44
+ /**
45
+ * Produce a duplicate-free version of the array. If the array has already
46
+ * been sorted, you have the option of using a faster algorithm.
47
+ * The faster algorithm will not work with an iteratee if the iteratee
48
+ * is not a one-to-one function, so providing an iteratee will disable
49
+ * the faster algorithm.
50
+ */
51
+ export const uniq = <T, U>(arr: T[], isSorted?: boolean, iteratee?: (value: T, i: number, arr: T[]) => U): T[] => {
52
+ if (iteratee) {
53
+ return iterateeUniq(arr, iteratee);
54
+ }
55
+ if (isSorted) {
56
+ return sortedUniq(arr);
57
+ }
58
+ return standardUniq(arr);
59
+ };