@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.
- package/.github/workflows/ci.yml +49 -0
- package/.markdownlint.json +3 -0
- package/.nvmrc +1 -0
- package/CHANGELOG.md +36 -0
- package/README.md +7 -3
- package/dist/commonjs/index.js +7 -0
- package/dist/commonjs/index.js.map +1 -1
- package/dist/commonjs/types/StrictUnion.js +4 -0
- package/dist/commonjs/types/StrictUnion.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/is-arguments.js.map +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-enum-value.js.map +1 -1
- package/dist/commonjs/utils/is-number.js.map +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/pick.js +15 -0
- package/dist/commonjs/utils/pick.js.map +1 -0
- package/dist/commonjs/utils/range.js +25 -0
- package/dist/commonjs/utils/range.js.map +1 -0
- package/dist/commonjs/utils/uniq.js +49 -0
- package/dist/commonjs/utils/uniq.js.map +1 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/StrictUnion.js +2 -0
- package/dist/esm/types/StrictUnion.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/is-arguments.js.map +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-enum-value.js.map +1 -1
- package/dist/esm/utils/is-number.js.map +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/pick.js +11 -0
- package/dist/esm/utils/pick.js.map +1 -0
- package/dist/esm/utils/range.js +21 -0
- package/dist/esm/utils/range.js.map +1 -0
- package/dist/esm/utils/uniq.js +46 -0
- package/dist/esm/utils/uniq.js.map +1 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/types/StrictUnion.d.ts +15 -0
- package/dist/types/utils/compact.d.ts +3 -0
- 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-enum-value.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/pick.d.ts +2 -0
- package/dist/types/utils/range.d.ts +7 -0
- package/dist/types/utils/uniq.d.ts +8 -0
- package/package.json +29 -32
- package/renovate.json +4 -0
- package/src/index.ts +7 -0
- package/src/types/StrictUnion.ts +15 -0
- package/src/utils/compact.ts +14 -0
- package/src/utils/is-arguments.ts +1 -1
- package/src/utils/is-array-of-strings.ts +1 -1
- package/src/utils/is-array.ts +1 -1
- package/src/utils/is-boolean.ts +3 -0
- package/src/utils/is-enum-value.ts +1 -1
- package/src/utils/is-number.ts +1 -1
- package/src/utils/is-object.ts +1 -1
- package/src/utils/is-promise-like.ts +1 -1
- 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/pick.ts +24 -0
- package/src/utils/range.ts +24 -0
- 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"}
|
package/dist/types/index.d.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';
|
|
@@ -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 {};
|
|
@@ -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:
|
|
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:
|
|
8
|
+
export declare function isArrayOfStrings(values: unknown): values is string[];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function isBoolean(o: unknown): o is boolean;
|
|
@@ -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;
|
|
@@ -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.
|
|
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
|
-
"
|
|
11
|
-
"test": "
|
|
12
|
-
"
|
|
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.
|
|
36
|
+
"tslib": "^1.9.0 || ^2"
|
|
31
37
|
},
|
|
32
38
|
"devDependencies": {
|
|
33
|
-
"@silvermine/chai-strictly-equal": "1.1.
|
|
34
|
-
"@silvermine/eslint-config": "
|
|
35
|
-
"@silvermine/standardization": "
|
|
36
|
-
"@silvermine/typescript-config": "git+https://github.com/silvermine/typescript-config
|
|
37
|
-
"@types/chai": "4.
|
|
38
|
-
"@types/mocha": "5.2.
|
|
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.
|
|
41
|
-
"chai": "4.
|
|
42
|
-
"check-node-version": "4.
|
|
43
|
-
"coveralls": "3.
|
|
44
|
-
"
|
|
45
|
-
"
|
|
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": "
|
|
49
|
-
"grunt-contrib-clean": "2.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.
|
|
58
|
+
"nyc": "13.3.0",
|
|
55
59
|
"sinon": "5.1.1",
|
|
56
|
-
"source-map-support": "0.5.
|
|
57
|
-
"standard-version": "5.0.2",
|
|
60
|
+
"source-map-support": "0.5.21",
|
|
58
61
|
"ts-node": "7.0.1",
|
|
59
|
-
"tslib": "
|
|
60
|
-
"typescript": "3.9.
|
|
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
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:
|
|
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:
|
|
11
|
+
export function isArrayOfStrings(values: unknown): values is string[] {
|
|
12
12
|
if (!isArray(values)) {
|
|
13
13
|
return false;
|
|
14
14
|
}
|
package/src/utils/is-array.ts
CHANGED
|
@@ -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];
|
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,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:
|
|
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:
|
|
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
|
}
|
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
|
}
|
|
@@ -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
|
+
};
|