@battis/typescript-tricks 0.5.1 → 0.5.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @battis/typescript-tricks
2
2
 
3
+ ## 0.5.3
4
+
5
+ ### Patch Changes
6
+
7
+ - f97ced8: fiddling with dist
8
+
9
+ ## 0.5.2
10
+
11
+ ### Patch Changes
12
+
13
+ - 6730759: bump dependencies
14
+
3
15
  ## 0.5.1
4
16
 
5
17
  ### Patch Changes
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- @battis/typescript-tricks
1
+ # @battis/typescript-tricks
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/@battis%2Ftypescript-tricks.svg)](https://badge.fury.io/js/@battis%2Ftypescript-tricks)
4
4
  [![Module type: CJS](https://img.shields.io/badge/module%20type-cjs-brightgreen)](https://nodejs.org/api/modules.html#modules-commonjs-modules)
@@ -0,0 +1,21 @@
1
+ export type Constructor<T = object> = new (...args: any[]) => T;
2
+ export declare const isConstructor: (value: any) => value is Constructor<any>;
3
+ /**
4
+ * ```ts
5
+ * class A {}
6
+ * class B {}
7
+ * class C extends A {}
8
+ *
9
+ * const a = new A();
10
+ * const b = new B();
11
+ * const c = new C();
12
+ *
13
+ * const list = [a, b, c];
14
+ * const filteredList = instanceOf(list, A);
15
+ * // filteredList = [a, c]
16
+ * ```
17
+ * @ see https://stackoverflow.com/a/65152869/294171
18
+ */
19
+ export declare function filterByType<Elements, Filter extends Elements>(array: Elements[], filterType: Constructor<Filter>): Filter[];
20
+ /** @deprecated use {@link filterByType} */
21
+ export declare const instanceOf: typeof filterByType;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.instanceOf = exports.filterByType = exports.isConstructor = void 0;
4
+ const isConstructor = (value) => {
5
+ return !!value && !!value.prototype && !!value.prototype.constructor;
6
+ };
7
+ exports.isConstructor = isConstructor;
8
+ /**
9
+ * ```ts
10
+ * class A {}
11
+ * class B {}
12
+ * class C extends A {}
13
+ *
14
+ * const a = new A();
15
+ * const b = new B();
16
+ * const c = new C();
17
+ *
18
+ * const list = [a, b, c];
19
+ * const filteredList = instanceOf(list, A);
20
+ * // filteredList = [a, c]
21
+ * ```
22
+ * @ see https://stackoverflow.com/a/65152869/294171
23
+ */
24
+ function filterByType(array, filterType) {
25
+ return array.filter((e) => e instanceof filterType);
26
+ }
27
+ exports.filterByType = filterByType;
28
+ /** @deprecated use {@link filterByType} */
29
+ exports.instanceOf = filterByType;
@@ -0,0 +1,11 @@
1
+ type EnumObject = {
2
+ [key: string]: number | string;
3
+ };
4
+ type EnumObjectEnum<E extends EnumObject> = E extends {
5
+ [key: string]: infer ET | string;
6
+ } ? ET : never;
7
+ /**
8
+ * @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
9
+ */
10
+ export declare function getEnumValues<E extends EnumObject>(enumObject: E): EnumObjectEnum<E>[];
11
+ export {};
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getEnumValues = void 0;
4
+ /**
5
+ * @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
6
+ */
7
+ function getEnumValues(enumObject) {
8
+ return Object.keys(enumObject)
9
+ .filter((key) => Number.isNaN(Number(key)))
10
+ .map((key) => enumObject[key]);
11
+ }
12
+ exports.getEnumValues = getEnumValues;
@@ -0,0 +1,9 @@
1
+ export type JSONValue = string | number | boolean | null | JSONValue[] | {
2
+ [key: string]: JSONValue;
3
+ };
4
+ export interface JSONObject {
5
+ [k: string]: JSONValue;
6
+ }
7
+ export interface JSONArray extends Array<JSONValue> {
8
+ }
9
+ export default JSONValue;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @see https://stackoverflow.com/a/61940388
3
+ */
4
+ export type MethodNames<T extends {
5
+ [K in keyof T]: any;
6
+ }> = {
7
+ [K in keyof T]: T[K] extends Function ? K : never;
8
+ }[keyof T];
9
+ export type MethodNamesMatching<T extends {
10
+ [K in keyof T]: any;
11
+ }, P extends Function = Function> = {
12
+ [K in keyof T]: T[K] extends Function ? P extends T[K] ? K : never : never;
13
+ }[keyof T];
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,2 @@
1
+ /** @deprecated see {@link https://www.typescriptlang.org/docs/handbook/mixins.html#constrained-mixins TypeScript docs} */
2
+ export type Mixin<T extends (...args: any[]) => any> = InstanceType<ReturnType<T>>;
package/dist/Mixin.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,40 @@
1
+ /**
2
+ * @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring at least one property
3
+ */
4
+ export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
5
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
6
+ }[Keys];
7
+ /**
8
+ * @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring only one property
9
+ */
10
+ export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
11
+ [K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
12
+ }[Keys];
13
+ /** @see {@link https://stackoverflow.com/a/51365037 StackOverflow response} */
14
+ export type RecursivePartial<T> = {
15
+ [P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
16
+ };
17
+ /**
18
+ * ```ts
19
+ * type A = {
20
+ * d: number,
21
+ * e: string
22
+ * f: boolean
23
+ * }
24
+ *
25
+ * type B = ReplaceProperty<A, 'e', number[]>;
26
+ * // type B = {
27
+ * // d: number,
28
+ * // e: number[],
29
+ * // f: boolean
30
+ * // }
31
+ * ```
32
+ * @see {@link https://stackoverflow.com/a/51599774 StackOverflow response}
33
+ */
34
+ export type ReplaceProperty<T, K extends keyof T, TReplace> = Identity<Pick<T, Exclude<keyof T, K>> & {
35
+ [P in K]: TReplace;
36
+ }>;
37
+ type Identity<T> = {
38
+ [P in keyof T]: T[P];
39
+ };
40
+ export {};
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,11 @@
1
+ export type AsynchronousFunction = () => Promise<any>;
2
+ /** @deprecated `null` is part of type unless using utility type `Nonnullable` */
3
+ export type Nullable<T> = T | null;
4
+ /** @deprecated use `?` */
5
+ export type Optional<T> = T | undefined;
6
+ export type Subset<T, U extends T> = T;
7
+ /** @deprecated use `Record<string,T>` */
8
+ export type AssociativeArray<T> = {
9
+ [key: string]: T;
10
+ };
11
+ export type JSONPrimitiveTypes = string | number | boolean | null;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,23 @@
1
+ /**
2
+
3
+ @see {@link https://stackoverflow.com/a/69571314/294171 StackOverflow response} on static interfaces
4
+
5
+ ```ts
6
+ interface InstanceInterface {
7
+ instanceMethod();
8
+ }
9
+
10
+ interface StaticInterface {
11
+ new(...args: any[]): InstanceInterface;
12
+ staticMethod();
13
+ }
14
+
15
+ class MyClass implements StaticImplements<StaticInterface, typeof MyClass> {
16
+ static staticMethod() { }
17
+ static ownStaticMethod() { }
18
+ instanceMethod() { }
19
+ ownInstanceMethod() { }
20
+ }
21
+ ```
22
+ */
23
+ export type StaticImplements<I extends new (...args: any[]) => any, C extends I> = InstanceType<C>;
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,8 @@
1
+ export * from './Constructor';
2
+ export * from './EnumeratedTypes';
3
+ export * from './Mixin';
4
+ export * from './PropertyRequirements';
5
+ export * from './Shorthand';
6
+ export * from './StaticImplements';
7
+ export * from './JSONValue';
8
+ export * from './MethodNames';
package/dist/index.js ADDED
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./Constructor"), exports);
18
+ __exportStar(require("./EnumeratedTypes"), exports);
19
+ __exportStar(require("./Mixin"), exports);
20
+ __exportStar(require("./PropertyRequirements"), exports);
21
+ __exportStar(require("./Shorthand"), exports);
22
+ __exportStar(require("./StaticImplements"), exports);
23
+ __exportStar(require("./JSONValue"), exports);
24
+ __exportStar(require("./MethodNames"), exports);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@battis/typescript-tricks",
3
3
  "description": "A handful of useful types and operators",
4
- "version": "0.5.1",
4
+ "version": "0.5.3",
5
5
  "author": "Seth Battis <seth@battis.net>",
6
6
  "license": "GPL-3.0",
7
7
  "main": "./dist/index.js",
@@ -15,11 +15,8 @@
15
15
  "url": "https://github.com/battis/typescript-config.git",
16
16
  "directory": "packages/tricks"
17
17
  },
18
- "bugs": {
19
- "url": "https://github.com/battis/typescript-config/issues"
20
- },
21
18
  "devDependencies": {
22
- "@tsconfig/recommended": "^1.0.3",
19
+ "@tsconfig/recommended": "^1.0.7",
23
20
  "npm-run-all": "^4.1.5",
24
21
  "shx": "^0.3.4",
25
22
  "typescript": "^4.9.5"