@battis/typescript-tricks 0.7.8 → 0.8.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 (72) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/coverage/clover.xml +32 -0
  3. package/coverage/coverage-final.json +5 -0
  4. package/coverage/lcov-report/Array.ts.html +163 -0
  5. package/coverage/lcov-report/Helpers.ts.html +106 -0
  6. package/coverage/lcov-report/JSON.ts.html +151 -0
  7. package/coverage/lcov-report/Key.ts.html +157 -0
  8. package/coverage/lcov-report/Object.ts.html +205 -0
  9. package/coverage/lcov-report/Record.ts.html +226 -0
  10. package/coverage/lcov-report/TypeJuggling.ts.html +142 -0
  11. package/coverage/lcov-report/base.css +224 -0
  12. package/coverage/lcov-report/block-navigation.js +87 -0
  13. package/coverage/lcov-report/favicon.png +0 -0
  14. package/coverage/lcov-report/index.html +161 -0
  15. package/coverage/lcov-report/prettify.css +1 -0
  16. package/coverage/lcov-report/prettify.js +2 -0
  17. package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
  18. package/coverage/lcov-report/sorter.js +210 -0
  19. package/coverage/lcov.info +94 -0
  20. package/dist/Array.d.ts +2 -0
  21. package/dist/Array.js +12 -0
  22. package/dist/{JSONValue.d.ts → JSON.d.ts} +2 -0
  23. package/dist/JSON.js +9 -0
  24. package/dist/Key.d.ts +3 -0
  25. package/dist/Key.js +18 -0
  26. package/dist/{MethodNames.d.ts → Methods.d.ts} +3 -5
  27. package/dist/{MethodNames.js → Methods.js} +1 -0
  28. package/dist/Object.d.ts +22 -0
  29. package/dist/{PropertyRequirements.d.ts → Properties.d.ts} +8 -10
  30. package/dist/Record.d.ts +1 -1
  31. package/dist/Record.js +5 -5
  32. package/dist/{OneOf.d.ts → TypeJuggling.d.ts} +3 -0
  33. package/dist/TypeJuggling.js +10 -0
  34. package/dist/_Deprecated.d.ts +63 -0
  35. package/dist/_Deprecated.js +82 -0
  36. package/dist/index.d.ts +9 -13
  37. package/dist/index.js +9 -13
  38. package/jest.config.ts +2 -1
  39. package/package.json +1 -1
  40. package/tests/Array/isEntries.test.ts +25 -0
  41. package/tests/JSON/isJSONPrimitive.test.ts +16 -0
  42. package/tests/Key/isKey.test.ts +13 -0
  43. package/tests/Key/isKeyof.test.ts +13 -0
  44. package/tests/Record/isRecord.test.ts +9 -32
  45. package/tests/TypeJuggling/isString.test.ts +14 -0
  46. package/tests/TypeJuggling/isUnknown.test.ts +7 -0
  47. package/dist/ArrayElement.d.ts +0 -1
  48. package/dist/Coerce/Coerce.d.ts +0 -1
  49. package/dist/Coerce/Coerce.js +0 -12
  50. package/dist/Coerce/Error.d.ts +0 -2
  51. package/dist/Coerce/Error.js +0 -16
  52. package/dist/Coerce.d.ts +0 -2
  53. package/dist/Coerce.js +0 -18
  54. package/dist/Constructor.d.ts +0 -21
  55. package/dist/Constructor.js +0 -29
  56. package/dist/EnumeratedTypes.d.ts +0 -11
  57. package/dist/EnumeratedTypes.js +0 -11
  58. package/dist/Mixin.d.ts +0 -2
  59. package/dist/Mixin.js +0 -2
  60. package/dist/OneOf.js +0 -3
  61. package/dist/OptionalProperty.d.ts +0 -1
  62. package/dist/OptionalProperty.js +0 -2
  63. package/dist/PropertyRequirements.js +0 -2
  64. package/dist/Shorthand.d.ts +0 -11
  65. package/dist/Shorthand.js +0 -2
  66. package/dist/StaticImplements.d.ts +0 -23
  67. package/dist/StaticImplements.js +0 -2
  68. package/tests/JSONValue/JSONPrimitive.test.ts +0 -25
  69. /package/dist/{Forms.d.ts → DOM.d.ts} +0 -0
  70. /package/dist/{ArrayElement.js → DOM.js} +0 -0
  71. /package/dist/{Forms.js → Object.js} +0 -0
  72. /package/dist/{JSONValue.js → Properties.js} +0 -0
package/dist/index.js CHANGED
@@ -14,17 +14,13 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- __exportStar(require("./ArrayElement"), exports);
18
- __exportStar(require("./Coerce"), exports);
19
- __exportStar(require("./Constructor"), exports);
20
- __exportStar(require("./EnumeratedTypes"), exports);
21
- __exportStar(require("./Forms"), exports);
22
- __exportStar(require("./JSONValue"), exports);
23
- __exportStar(require("./MethodNames"), exports);
24
- __exportStar(require("./Mixin"), exports);
25
- __exportStar(require("./OneOf"), exports);
26
- __exportStar(require("./OptionalProperty"), exports);
27
- __exportStar(require("./PropertyRequirements"), exports);
17
+ __exportStar(require("./Array"), exports);
18
+ __exportStar(require("./DOM"), exports);
19
+ __exportStar(require("./JSON"), exports);
20
+ __exportStar(require("./Key"), exports);
21
+ __exportStar(require("./Methods"), exports);
22
+ __exportStar(require("./Object"), exports);
23
+ __exportStar(require("./Properties"), exports);
28
24
  __exportStar(require("./Record"), exports);
29
- __exportStar(require("./Shorthand"), exports);
30
- __exportStar(require("./StaticImplements"), exports);
25
+ __exportStar(require("./TypeJuggling"), exports);
26
+ __exportStar(require("./_Deprecated"), exports);
package/jest.config.ts CHANGED
@@ -24,5 +24,6 @@ export default {
24
24
  }
25
25
  }
26
26
  ]
27
- }
27
+ },
28
+ collectCoverage: true
28
29
  } satisfies Config;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@battis/typescript-tricks",
3
- "version": "0.7.8",
3
+ "version": "0.8.0",
4
4
  "description": "A handful of useful types and operators",
5
5
  "homepage": "https://github.com/battis/typescript-config/tree/main/packages/typescript-tricks#readme",
6
6
  "repository": {
@@ -0,0 +1,25 @@
1
+ import { isEntries } from '../../src/Array';
2
+ import { isString } from '../../src/TypeJuggling';
3
+
4
+ test('truthy', () => {
5
+ expect(isEntries([])).toBeTruthy();
6
+ expect(isEntries([[1, 2]])).toBeTruthy();
7
+ expect(
8
+ isEntries([
9
+ ['a', 1],
10
+ [2, 'b']
11
+ ])
12
+ ).toBeTruthy();
13
+ });
14
+
15
+ test('falsy', () => {
16
+ expect(isEntries({ a: 123 })).toBeFalsy();
17
+ expect(isEntries([1, 2, 3])).toBeFalsy();
18
+ expect(isEntries('foo')).toBeFalsy();
19
+ expect(isEntries(['a', ['b', 1]])).toBeFalsy();
20
+ expect(isEntries([['a', 1], ['b', 2], 'c'])).toBeFalsy();
21
+ });
22
+
23
+ test('typed', () => {
24
+ expect(isEntries<string>([], isString)).toBeTruthy();
25
+ });
@@ -0,0 +1,16 @@
1
+ import { isJSONPrimitive } from '../../src/JSON';
2
+
3
+ test('truthy', () => {
4
+ expect(isJSONPrimitive('foo')).toBeTruthy();
5
+ expect(isJSONPrimitive(1)).toBeTruthy();
6
+ expect(isJSONPrimitive(3.14159)).toBeTruthy();
7
+ expect(isJSONPrimitive(true)).toBeTruthy();
8
+ expect(isJSONPrimitive(false)).toBeTruthy();
9
+ expect(isJSONPrimitive(null)).toBeTruthy();
10
+ });
11
+
12
+ test('falsy', () => {
13
+ expect(isJSONPrimitive([])).toBeFalsy();
14
+ expect(isJSONPrimitive({ a: 1 })).toBeFalsy();
15
+ expect(isJSONPrimitive(undefined)).toBeFalsy();
16
+ });
@@ -0,0 +1,13 @@
1
+ import { isKey } from '../../src/Key';
2
+
3
+ test('truthy', () => {
4
+ expect(isKey('a')).toBeTruthy();
5
+ expect(isKey(0)).toBeTruthy();
6
+ expect(isKey(Symbol('foo'))).toBeTruthy();
7
+ });
8
+
9
+ test('falsy', () => {
10
+ expect(isKey(true)).toBeFalsy();
11
+ expect(isKey({ foo: 'bar' })).toBeFalsy();
12
+ expect(isKey(3.14159)).toBeFalsy();
13
+ });
@@ -0,0 +1,13 @@
1
+ import { isKeyof } from '../../src/Key';
2
+
3
+ test('truthy', () => {
4
+ expect(isKeyof('a', { a: 1 })).toBeTruthy();
5
+ expect(isKeyof(1, { 1: 'foo' })).toBeTruthy();
6
+ const s = Symbol('foo');
7
+ expect(isKeyof(s, { [s]: 'bar' })).toBeTruthy();
8
+ });
9
+
10
+ test('falsy', () => {
11
+ expect(isKeyof('a', { b: 2 })).toBeFalsy();
12
+ expect(isKeyof('a', new Date())).toBeFalsy();
13
+ });
@@ -1,22 +1,7 @@
1
- import { JSONPrimitive } from '../../src/JSONValue.js';
1
+ import { isJSONPrimitive, JSONPrimitive } from '../../src/JSON.js';
2
+ import { isKey, Key } from '../../src/Key.js';
2
3
  import { isRecord } from '../../src/Record.js';
3
- import { isJSONPrimitive } from '../JSONValue/JSONPrimitive.test.js';
4
-
5
- function isKey(obj: unknown): obj is keyof object {
6
- return (
7
- typeof obj === 'string' ||
8
- typeof obj === 'number' ||
9
- typeof obj === 'symbol'
10
- );
11
- }
12
-
13
- function isUnknown(obj: unknown): obj is unknown {
14
- return true;
15
- }
16
-
17
- function isString(obj: unknown): obj is string {
18
- return typeof obj === 'string';
19
- }
4
+ import { isString, isUnknown } from '../../src/TypeJuggling.js';
20
5
 
21
6
  test('Record<string,string>', () => {
22
7
  expect(
@@ -36,21 +21,13 @@ test('Record<string,JSONPrimitive>', () => {
36
21
 
37
22
  test('built-in objects', () => {
38
23
  expect(
39
- isRecord<keyof object, unknown>(new String('foo bar'), isKey, isUnknown)
40
- ).toBeFalsy();
41
- expect(
42
- isRecord<keyof object, unknown>(new Number(123), isKey, isUnknown)
43
- ).toBeFalsy();
44
- expect(
45
- isRecord<keyof object, unknown>(Math.PI, isKey, isUnknown)
46
- ).toBeFalsy();
47
- expect(
48
- isRecord<keyof object, unknown>(new Uint8Array(0), isKey, isUnknown)
49
- ).toBeFalsy();
50
- expect(
51
- isRecord<keyof object, unknown>(new Map(), isKey, isUnknown)
24
+ isRecord<Key, unknown>(new String('foo bar'), isKey, isUnknown)
52
25
  ).toBeFalsy();
26
+ expect(isRecord<Key, unknown>(new Number(123), isKey, isUnknown)).toBeFalsy();
27
+ expect(isRecord<Key, unknown>(Math.PI, isKey, isUnknown)).toBeFalsy();
53
28
  expect(
54
- isRecord<keyof object, unknown>(new Date(), isKey, isUnknown)
29
+ isRecord<Key, unknown>(new Uint8Array(0), isKey, isUnknown)
55
30
  ).toBeFalsy();
31
+ expect(isRecord<Key, unknown>(new Map(), isKey, isUnknown)).toBeFalsy();
32
+ expect(isRecord<Key, unknown>(new Date(), isKey, isUnknown)).toBeFalsy();
56
33
  });
@@ -0,0 +1,14 @@
1
+ import { isString } from '../../src/TypeJuggling';
2
+
3
+ test('truthy', () => {
4
+ expect(isString('foo')).toBeTruthy();
5
+ expect(isString('')).toBeTruthy();
6
+ expect(isString(`bar`)).toBeTruthy();
7
+ });
8
+
9
+ test('falsy', () => {
10
+ expect(isString(1)).toBeFalsy();
11
+ expect(isString(undefined)).toBeFalsy();
12
+ expect(isString(null)).toBeFalsy();
13
+ expect(isString({ a: 123 })).toBeFalsy();
14
+ });
@@ -0,0 +1,7 @@
1
+ import { isUnknown } from '../../src/TypeJuggling';
2
+
3
+ test('truthy', () => {
4
+ expect(isUnknown(true)).toBeTruthy();
5
+ expect(isUnknown(null)).toBeTruthy();
6
+ expect(isUnknown({ a: 123 })).toBeTruthy();
7
+ });
@@ -1 +0,0 @@
1
- export type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
@@ -1 +0,0 @@
1
- export declare function Coerce<T>(u: unknown, isT: (u: unknown) => u is T, toT?: (u: unknown) => T): T;
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Coerce = Coerce;
4
- function FailedCoercion() {
5
- throw new TypeError('Attempted coercion to a non-matching type');
6
- }
7
- function Coerce(u, isT, toT = (FailedCoercion)) {
8
- if (isT(u)) {
9
- return u;
10
- }
11
- return toT(u);
12
- }
@@ -1,2 +0,0 @@
1
- export declare function isError(error: unknown): error is Error;
2
- export declare function CoerceError(error: unknown): Error;
@@ -1,16 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isError = isError;
4
- exports.CoerceError = CoerceError;
5
- const Coerce_js_1 = require("./Coerce.js");
6
- function isError(error) {
7
- return (typeof error === 'object' &&
8
- error !== null &&
9
- 'message' in error &&
10
- 'name' in error &&
11
- typeof error.name === 'string' &&
12
- typeof error.message === 'string');
13
- }
14
- function CoerceError(error) {
15
- return (0, Coerce_js_1.Coerce)(error, isError, (e) => e);
16
- }
package/dist/Coerce.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export * from './Coerce/Coerce';
2
- export * from './Coerce/Error';
package/dist/Coerce.js DELETED
@@ -1,18 +0,0 @@
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("./Coerce/Coerce"), exports);
18
- __exportStar(require("./Coerce/Error"), exports);
@@ -1,21 +0,0 @@
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;
@@ -1,29 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.instanceOf = exports.isConstructor = void 0;
4
- exports.filterByType = filterByType;
5
- const isConstructor = (value) => {
6
- return !!value && !!value.prototype && !!value.prototype.constructor;
7
- };
8
- exports.isConstructor = isConstructor;
9
- /**
10
- * ```ts
11
- * class A {}
12
- * class B {}
13
- * class C extends A {}
14
- *
15
- * const a = new A();
16
- * const b = new B();
17
- * const c = new C();
18
- *
19
- * const list = [a, b, c];
20
- * const filteredList = instanceOf(list, A);
21
- * // filteredList = [a, c]
22
- * ```
23
- * @ see https://stackoverflow.com/a/65152869/294171
24
- */
25
- function filterByType(array, filterType) {
26
- return array.filter((e) => e instanceof filterType);
27
- }
28
- /** @deprecated use {@link filterByType} */
29
- exports.instanceOf = filterByType;
@@ -1,11 +0,0 @@
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 {};
@@ -1,11 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getEnumValues = getEnumValues;
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
- }
package/dist/Mixin.d.ts DELETED
@@ -1,2 +0,0 @@
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 DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
package/dist/OneOf.js DELETED
@@ -1,3 +0,0 @@
1
- "use strict";
2
- /** @see https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-types#comment123255834_53229567 */
3
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1 +0,0 @@
1
- export type OptionalProperty<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,11 +0,0 @@
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;
package/dist/Shorthand.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,23 +0,0 @@
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>;
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
@@ -1,25 +0,0 @@
1
- import { JSONPrimitive } from '../../src/JSONValue.js';
2
-
3
- export function isJSONPrimitive(obj: unknown): obj is JSONPrimitive {
4
- return (
5
- obj === null ||
6
- typeof obj === 'number' ||
7
- typeof obj === 'string' ||
8
- typeof obj === 'string'
9
- );
10
- }
11
-
12
- test('truthy', () => {
13
- expect(isJSONPrimitive(null)).toBeTruthy();
14
- expect(isJSONPrimitive(123)).toBeTruthy();
15
- expect(isJSONPrimitive(3.14159)).toBeTruthy();
16
- expect(isJSONPrimitive('foo bar')).toBeTruthy();
17
- expect(isJSONPrimitive(`interpolate ${new Date()}`)).toBeTruthy();
18
- });
19
-
20
- test('falsy', () => {
21
- expect(isJSONPrimitive(undefined)).toBeFalsy();
22
- expect(isJSONPrimitive([1, 2, 3])).toBeFalsy();
23
- expect(isJSONPrimitive({ foo: 'bar' })).toBeFalsy();
24
- expect(isJSONPrimitive(new Date())).toBeFalsy();
25
- });
File without changes
File without changes
File without changes
File without changes