@myparcel-dev/ts-utils 1.15.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/README.md +128 -0
- package/dist/index.cjs +118 -0
- package/dist/index.d.cts +135 -0
- package/dist/index.d.ts +135 -0
- package/dist/index.js +82 -0
- package/package.json +58 -0
package/README.md
ADDED
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
# TypeScript Utilities
|
|
2
|
+
|
|
3
|
+
This is a collection of TypeScript utilities that we reuse across all TS projects.
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@myparcel-dev/ts-utils/)
|
|
6
|
+
[](https://codecov.io/gh/myparcelnl/ts-utils)
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
**Using Yarn**
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
yarn add @myparcel-dev/ts-utils
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
**Using pnpm**
|
|
17
|
+
|
|
18
|
+
```bash
|
|
19
|
+
pnpm add @myparcel-dev/ts-utils
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
**Using npm**
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @myparcel-dev/ts-utils
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
> ⚠️ Note: You can install this package as a dev dependency if you only use the types.
|
|
29
|
+
|
|
30
|
+
## Contents
|
|
31
|
+
|
|
32
|
+
### [Type guards](src/type-guards)
|
|
33
|
+
|
|
34
|
+
#### [isEnumValue](src/type-guards/isEnumValue.ts)
|
|
35
|
+
|
|
36
|
+
Type guard for checking if a value is a key of the given enum.
|
|
37
|
+
|
|
38
|
+
```ts
|
|
39
|
+
import {isEnumValue} from '@myparcel-dev/ts-utils';
|
|
40
|
+
|
|
41
|
+
enum MyEnum {
|
|
42
|
+
A = 'A',
|
|
43
|
+
B = 'B',
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const value = 'A';
|
|
47
|
+
|
|
48
|
+
if (isEnumValue(MyEnum, value)) {
|
|
49
|
+
// value is of type MyEnum.A
|
|
50
|
+
}
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
#### [isInArray](src/type-guards/isInArray.ts)
|
|
54
|
+
|
|
55
|
+
Type guard which checks if given value is inside an array.
|
|
56
|
+
|
|
57
|
+
```ts
|
|
58
|
+
import {isInArray} from '@myparcel-dev/ts-utils';
|
|
59
|
+
|
|
60
|
+
const value = 'A'; // value is of type 'A'
|
|
61
|
+
|
|
62
|
+
isInArray(value, ['A', 'B']) // true
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
#### [isOfType](src/type-guards/isOfType.ts)
|
|
66
|
+
|
|
67
|
+
Type guard for checking if an object value is of a specific type by checking if a given K exists.
|
|
68
|
+
|
|
69
|
+
```ts
|
|
70
|
+
import {isOfType} from '@myparcel-dev/ts-utils';
|
|
71
|
+
|
|
72
|
+
interface BaseObject {
|
|
73
|
+
a: string;
|
|
74
|
+
b: number;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
interface ObjectWithC extends BaseObject {
|
|
78
|
+
c: string;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
const value = {
|
|
82
|
+
a: 'a',
|
|
83
|
+
b: 1,
|
|
84
|
+
c: 'c',
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
if (isOfType<ObjectWithC>(value, 'c')) {
|
|
88
|
+
// value is of type ObjectWithC
|
|
89
|
+
}
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
### [Types](src/types)
|
|
93
|
+
|
|
94
|
+
### [Utils](src/utils)
|
|
95
|
+
|
|
96
|
+
#### [asyncEvery](src/utils/asyncEvery.ts)
|
|
97
|
+
|
|
98
|
+
Returns true if every element in the array satisfies the provided predicate.
|
|
99
|
+
|
|
100
|
+
```ts
|
|
101
|
+
import {asyncEvery} from '@myparcel-dev/ts-utils';
|
|
102
|
+
|
|
103
|
+
await asyncEvery([1, 2, 3, 4, 5], async (value) => value > 0); // true
|
|
104
|
+
await asyncEvery([1, 2, 3, 4, 5], async (value) => value > 1); // false
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
#### [asyncSome](src/utils/asyncSome.ts)
|
|
108
|
+
|
|
109
|
+
Returns true if some element in the array satisfies the provided predicate.
|
|
110
|
+
|
|
111
|
+
```ts
|
|
112
|
+
import {asyncSome} from '@myparcel-dev/ts-utils';
|
|
113
|
+
|
|
114
|
+
await asyncSome([1, 2, 3, 4, 5], async (value) => value > 4); // true
|
|
115
|
+
await asyncSome([1, 2, 3, 4, 5], async (value) => value > 5); // false
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
#### [toArray](src/utils/toArray.ts)
|
|
119
|
+
|
|
120
|
+
Converts a value to an array. If the value is already an array, it will be returned as is.
|
|
121
|
+
|
|
122
|
+
```ts
|
|
123
|
+
import {toArray} from '@myparcel-dev/ts-utils';
|
|
124
|
+
|
|
125
|
+
toArray('plain value'); // ['plain value']
|
|
126
|
+
|
|
127
|
+
toArray(['already an array']); // ['already an array']
|
|
128
|
+
```
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
asyncEvery: () => asyncEvery,
|
|
24
|
+
asyncSome: () => asyncSome,
|
|
25
|
+
isEnumValue: () => isEnumValue,
|
|
26
|
+
isInArray: () => isInArray,
|
|
27
|
+
isOfType: () => isOfType,
|
|
28
|
+
isUndefined: () => isUndefined,
|
|
29
|
+
objectIsEqual: () => objectIsEqual,
|
|
30
|
+
partitionArray: () => partitionArray,
|
|
31
|
+
removePropertiesWith: () => removePropertiesWith,
|
|
32
|
+
toArray: () => toArray
|
|
33
|
+
});
|
|
34
|
+
module.exports = __toCommonJS(src_exports);
|
|
35
|
+
|
|
36
|
+
// src/type-guards/isEnumValue.ts
|
|
37
|
+
function isEnumValue(key, enumObject) {
|
|
38
|
+
return Object.values(enumObject).includes(key);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// src/type-guards/isInArray.ts
|
|
42
|
+
var isInArray = (value, array) => array.includes(value);
|
|
43
|
+
|
|
44
|
+
// src/type-guards/isOfType.ts
|
|
45
|
+
function isOfType(value, property) {
|
|
46
|
+
return value?.[property] !== void 0;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// src/utils/asyncEvery.ts
|
|
50
|
+
var asyncEvery = async (arr, predicate) => {
|
|
51
|
+
for (let i = 0; i < arr.length; i++) {
|
|
52
|
+
if (!await predicate(arr[i], i, arr)) {
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return true;
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
// src/utils/asyncSome.ts
|
|
60
|
+
var asyncSome = async (arr, predicate) => {
|
|
61
|
+
for (let i = 0; i < arr.length; i++) {
|
|
62
|
+
if (await predicate(arr[i], i, arr)) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return false;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
// src/utils/isUndefined.ts
|
|
70
|
+
var isUndefined = (value) => value === void 0;
|
|
71
|
+
|
|
72
|
+
// src/utils/objectIsEqual.ts
|
|
73
|
+
var objectIsEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
74
|
+
|
|
75
|
+
// src/utils/partitionArray.ts
|
|
76
|
+
var partitionArray = (array, predicate) => {
|
|
77
|
+
return (array ?? []).reduce(
|
|
78
|
+
(acc, value) => {
|
|
79
|
+
acc[predicate(value) ? 0 : 1].push(value);
|
|
80
|
+
return acc;
|
|
81
|
+
},
|
|
82
|
+
[[], []]
|
|
83
|
+
);
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// src/utils/removePropertiesWith.ts
|
|
87
|
+
var removePropertiesWith = (object, predicate) => {
|
|
88
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
|
89
|
+
if (!predicate(value)) {
|
|
90
|
+
acc[key] = value;
|
|
91
|
+
}
|
|
92
|
+
return acc;
|
|
93
|
+
}, {});
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/utils/toArray.ts
|
|
97
|
+
var toArray = (itemOrItems, separator) => {
|
|
98
|
+
if (itemOrItems === null || itemOrItems === void 0) {
|
|
99
|
+
return [];
|
|
100
|
+
}
|
|
101
|
+
if (separator && typeof itemOrItems === "string") {
|
|
102
|
+
return itemOrItems.split(separator);
|
|
103
|
+
}
|
|
104
|
+
return Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
|
|
105
|
+
};
|
|
106
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
107
|
+
0 && (module.exports = {
|
|
108
|
+
asyncEvery,
|
|
109
|
+
asyncSome,
|
|
110
|
+
isEnumValue,
|
|
111
|
+
isInArray,
|
|
112
|
+
isOfType,
|
|
113
|
+
isUndefined,
|
|
114
|
+
objectIsEqual,
|
|
115
|
+
partitionArray,
|
|
116
|
+
removePropertiesWith,
|
|
117
|
+
toArray
|
|
118
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard for checking if a value is a key of the given enum.
|
|
3
|
+
*/
|
|
4
|
+
declare function isEnumValue<T extends Record<string, unknown>>(key: unknown, enumObject: T): key is T[keyof T];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type guard which checks if given value is inside an array.
|
|
8
|
+
*/
|
|
9
|
+
declare const isInArray: <T extends readonly any[]>(value: any, array: T) => value is T[number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type guard for checking if an object value is of a specific type by checking if a given property exists.
|
|
13
|
+
*/
|
|
14
|
+
declare function isOfType<T>(value: any, property: keyof T): value is T;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Forcibly type an array to have at least one element.
|
|
18
|
+
*/
|
|
19
|
+
type ArrayWithOneOrMore<T> = {
|
|
20
|
+
0: T;
|
|
21
|
+
} & T[];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Return a new record only with the keys whose values are assignable to the given type. If no keys are assignable to the given type, the resulting record must be empty.
|
|
25
|
+
*/
|
|
26
|
+
type ExtractRecord<T, U> = {
|
|
27
|
+
[K in keyof T as T[K] extends U ? K : never]: T[K];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extracts the keys of a record whose values are assignable to the given type.
|
|
32
|
+
*/
|
|
33
|
+
type ExtractRecordKeys<T extends Record<any, any>, U> = keyof ExtractRecord<T, U>;
|
|
34
|
+
|
|
35
|
+
type MakeOptional<T, K extends string | keyof T> = K extends keyof T ? Omit<T, K> & Partial<Pick<T, K>> : T;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Makes a type nullable or be undefined.
|
|
39
|
+
*/
|
|
40
|
+
type Maybe<T> = T | null | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A type for an instantiable class.
|
|
44
|
+
*/
|
|
45
|
+
type Newable<T> = new (...args: any[]) => T;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Allow a single value or an array of values.
|
|
49
|
+
*/
|
|
50
|
+
type OneOrMore<T> = T | T[];
|
|
51
|
+
|
|
52
|
+
type PromiseOr<T> = T | Promise<T>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Allows
|
|
56
|
+
*/
|
|
57
|
+
type ReadonlyOr<T> = T | Readonly<T>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Recursively makes all properties of an object optional.
|
|
61
|
+
*/
|
|
62
|
+
type RecursivePartial<T> = {
|
|
63
|
+
[P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Recursively makes all properties of an object required.
|
|
68
|
+
*/
|
|
69
|
+
type RecursiveRequired<T> = {
|
|
70
|
+
[P in keyof T]-?: T[P] extends (infer U)[] ? RecursiveRequired<U>[] : T[P] extends object | undefined ? RecursiveRequired<T[P]> : T[P];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type Replace<T, K extends keyof T, TReplace> = Pick<T, Exclude<keyof T, K>> & {
|
|
74
|
+
[P in K]: TReplace;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Makes all properties of an object optional except the ones specified.
|
|
79
|
+
*/
|
|
80
|
+
type RequireOnly<K, T extends keyof K> = Required<Pick<K, T>> & Partial<Omit<K, T>>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolves the type of given promise.
|
|
84
|
+
*/
|
|
85
|
+
type ResolvePromise<T extends Promise<any>> = T extends Promise<infer U> ? U : never;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Inverts a map type so that the values become the keys and the keys become the values.
|
|
89
|
+
*/
|
|
90
|
+
type ReverseMap<T extends Record<keyof T, keyof any>> = {
|
|
91
|
+
[P in T[keyof T]]: {
|
|
92
|
+
[K in keyof T]: T[K] extends P ? K : never;
|
|
93
|
+
}[keyof T];
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get the values of a type
|
|
98
|
+
*/
|
|
99
|
+
type ValueOf<T extends Record<string, unknown>> = T[keyof T];
|
|
100
|
+
|
|
101
|
+
type MakeRequired<Type, Keys extends keyof Type> = Omit<Type, Keys> & Required<Pick<Type, Keys>>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns true if every element in the array satisfies the provided predicate.
|
|
105
|
+
*/
|
|
106
|
+
declare const asyncEvery: <A>(arr: A[], predicate: (value: A, index: number, array: A[]) => PromiseOr<boolean>) => Promise<boolean>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns true if some element in the array satisfies the provided predicate.
|
|
110
|
+
*/
|
|
111
|
+
declare const asyncSome: <A>(arr: A[], predicate: (value: A, index: number, array: A[]) => PromiseOr<boolean>) => Promise<boolean>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns true if given value is undefined.
|
|
115
|
+
*/
|
|
116
|
+
declare const isUndefined: (value: unknown) => value is undefined;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Compares two objects and returns true if they are equal
|
|
120
|
+
*/
|
|
121
|
+
declare const objectIsEqual: (a: unknown, b: unknown) => boolean;
|
|
122
|
+
|
|
123
|
+
declare const partitionArray: <I>(array: I[] | null | undefined, predicate: (item: I) => boolean) => I[][];
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Removes keys and values from an object if predicate(value) returns true
|
|
127
|
+
*/
|
|
128
|
+
declare const removePropertiesWith: <T extends Record<string, unknown>>(object: T, predicate: (value: unknown) => boolean | Promise<boolean>) => Omit<T, keyof T>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convert input to an array, if it's not already an array.
|
|
132
|
+
*/
|
|
133
|
+
declare const toArray: <T>(itemOrItems: OneOrMore<T>, separator?: string) => T[];
|
|
134
|
+
|
|
135
|
+
export { type ArrayWithOneOrMore, type ExtractRecord, type ExtractRecordKeys, type MakeOptional, type MakeRequired, type Maybe, type Newable, type OneOrMore, type PromiseOr, type ReadonlyOr, type RecursivePartial, type RecursiveRequired, type Replace, type RequireOnly, type ResolvePromise, type ReverseMap, type ValueOf, asyncEvery, asyncSome, isEnumValue, isInArray, isOfType, isUndefined, objectIsEqual, partitionArray, removePropertiesWith, toArray };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Type guard for checking if a value is a key of the given enum.
|
|
3
|
+
*/
|
|
4
|
+
declare function isEnumValue<T extends Record<string, unknown>>(key: unknown, enumObject: T): key is T[keyof T];
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Type guard which checks if given value is inside an array.
|
|
8
|
+
*/
|
|
9
|
+
declare const isInArray: <T extends readonly any[]>(value: any, array: T) => value is T[number];
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Type guard for checking if an object value is of a specific type by checking if a given property exists.
|
|
13
|
+
*/
|
|
14
|
+
declare function isOfType<T>(value: any, property: keyof T): value is T;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Forcibly type an array to have at least one element.
|
|
18
|
+
*/
|
|
19
|
+
type ArrayWithOneOrMore<T> = {
|
|
20
|
+
0: T;
|
|
21
|
+
} & T[];
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Return a new record only with the keys whose values are assignable to the given type. If no keys are assignable to the given type, the resulting record must be empty.
|
|
25
|
+
*/
|
|
26
|
+
type ExtractRecord<T, U> = {
|
|
27
|
+
[K in keyof T as T[K] extends U ? K : never]: T[K];
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Extracts the keys of a record whose values are assignable to the given type.
|
|
32
|
+
*/
|
|
33
|
+
type ExtractRecordKeys<T extends Record<any, any>, U> = keyof ExtractRecord<T, U>;
|
|
34
|
+
|
|
35
|
+
type MakeOptional<T, K extends string | keyof T> = K extends keyof T ? Omit<T, K> & Partial<Pick<T, K>> : T;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Makes a type nullable or be undefined.
|
|
39
|
+
*/
|
|
40
|
+
type Maybe<T> = T | null | undefined;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* A type for an instantiable class.
|
|
44
|
+
*/
|
|
45
|
+
type Newable<T> = new (...args: any[]) => T;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Allow a single value or an array of values.
|
|
49
|
+
*/
|
|
50
|
+
type OneOrMore<T> = T | T[];
|
|
51
|
+
|
|
52
|
+
type PromiseOr<T> = T | Promise<T>;
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Allows
|
|
56
|
+
*/
|
|
57
|
+
type ReadonlyOr<T> = T | Readonly<T>;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Recursively makes all properties of an object optional.
|
|
61
|
+
*/
|
|
62
|
+
type RecursivePartial<T> = {
|
|
63
|
+
[P in keyof T]?: T[P] extends (infer U)[] ? RecursivePartial<U>[] : T[P] extends object | undefined ? RecursivePartial<T[P]> : T[P];
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Recursively makes all properties of an object required.
|
|
68
|
+
*/
|
|
69
|
+
type RecursiveRequired<T> = {
|
|
70
|
+
[P in keyof T]-?: T[P] extends (infer U)[] ? RecursiveRequired<U>[] : T[P] extends object | undefined ? RecursiveRequired<T[P]> : T[P];
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
type Replace<T, K extends keyof T, TReplace> = Pick<T, Exclude<keyof T, K>> & {
|
|
74
|
+
[P in K]: TReplace;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Makes all properties of an object optional except the ones specified.
|
|
79
|
+
*/
|
|
80
|
+
type RequireOnly<K, T extends keyof K> = Required<Pick<K, T>> & Partial<Omit<K, T>>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolves the type of given promise.
|
|
84
|
+
*/
|
|
85
|
+
type ResolvePromise<T extends Promise<any>> = T extends Promise<infer U> ? U : never;
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Inverts a map type so that the values become the keys and the keys become the values.
|
|
89
|
+
*/
|
|
90
|
+
type ReverseMap<T extends Record<keyof T, keyof any>> = {
|
|
91
|
+
[P in T[keyof T]]: {
|
|
92
|
+
[K in keyof T]: T[K] extends P ? K : never;
|
|
93
|
+
}[keyof T];
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Get the values of a type
|
|
98
|
+
*/
|
|
99
|
+
type ValueOf<T extends Record<string, unknown>> = T[keyof T];
|
|
100
|
+
|
|
101
|
+
type MakeRequired<Type, Keys extends keyof Type> = Omit<Type, Keys> & Required<Pick<Type, Keys>>;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Returns true if every element in the array satisfies the provided predicate.
|
|
105
|
+
*/
|
|
106
|
+
declare const asyncEvery: <A>(arr: A[], predicate: (value: A, index: number, array: A[]) => PromiseOr<boolean>) => Promise<boolean>;
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Returns true if some element in the array satisfies the provided predicate.
|
|
110
|
+
*/
|
|
111
|
+
declare const asyncSome: <A>(arr: A[], predicate: (value: A, index: number, array: A[]) => PromiseOr<boolean>) => Promise<boolean>;
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Returns true if given value is undefined.
|
|
115
|
+
*/
|
|
116
|
+
declare const isUndefined: (value: unknown) => value is undefined;
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Compares two objects and returns true if they are equal
|
|
120
|
+
*/
|
|
121
|
+
declare const objectIsEqual: (a: unknown, b: unknown) => boolean;
|
|
122
|
+
|
|
123
|
+
declare const partitionArray: <I>(array: I[] | null | undefined, predicate: (item: I) => boolean) => I[][];
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Removes keys and values from an object if predicate(value) returns true
|
|
127
|
+
*/
|
|
128
|
+
declare const removePropertiesWith: <T extends Record<string, unknown>>(object: T, predicate: (value: unknown) => boolean | Promise<boolean>) => Omit<T, keyof T>;
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Convert input to an array, if it's not already an array.
|
|
132
|
+
*/
|
|
133
|
+
declare const toArray: <T>(itemOrItems: OneOrMore<T>, separator?: string) => T[];
|
|
134
|
+
|
|
135
|
+
export { type ArrayWithOneOrMore, type ExtractRecord, type ExtractRecordKeys, type MakeOptional, type MakeRequired, type Maybe, type Newable, type OneOrMore, type PromiseOr, type ReadonlyOr, type RecursivePartial, type RecursiveRequired, type Replace, type RequireOnly, type ResolvePromise, type ReverseMap, type ValueOf, asyncEvery, asyncSome, isEnumValue, isInArray, isOfType, isUndefined, objectIsEqual, partitionArray, removePropertiesWith, toArray };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
// src/type-guards/isEnumValue.ts
|
|
2
|
+
function isEnumValue(key, enumObject) {
|
|
3
|
+
return Object.values(enumObject).includes(key);
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
// src/type-guards/isInArray.ts
|
|
7
|
+
var isInArray = (value, array) => array.includes(value);
|
|
8
|
+
|
|
9
|
+
// src/type-guards/isOfType.ts
|
|
10
|
+
function isOfType(value, property) {
|
|
11
|
+
return value?.[property] !== void 0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// src/utils/asyncEvery.ts
|
|
15
|
+
var asyncEvery = async (arr, predicate) => {
|
|
16
|
+
for (let i = 0; i < arr.length; i++) {
|
|
17
|
+
if (!await predicate(arr[i], i, arr)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return true;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// src/utils/asyncSome.ts
|
|
25
|
+
var asyncSome = async (arr, predicate) => {
|
|
26
|
+
for (let i = 0; i < arr.length; i++) {
|
|
27
|
+
if (await predicate(arr[i], i, arr)) {
|
|
28
|
+
return true;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return false;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
// src/utils/isUndefined.ts
|
|
35
|
+
var isUndefined = (value) => value === void 0;
|
|
36
|
+
|
|
37
|
+
// src/utils/objectIsEqual.ts
|
|
38
|
+
var objectIsEqual = (a, b) => JSON.stringify(a) === JSON.stringify(b);
|
|
39
|
+
|
|
40
|
+
// src/utils/partitionArray.ts
|
|
41
|
+
var partitionArray = (array, predicate) => {
|
|
42
|
+
return (array ?? []).reduce(
|
|
43
|
+
(acc, value) => {
|
|
44
|
+
acc[predicate(value) ? 0 : 1].push(value);
|
|
45
|
+
return acc;
|
|
46
|
+
},
|
|
47
|
+
[[], []]
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/utils/removePropertiesWith.ts
|
|
52
|
+
var removePropertiesWith = (object, predicate) => {
|
|
53
|
+
return Object.entries(object).reduce((acc, [key, value]) => {
|
|
54
|
+
if (!predicate(value)) {
|
|
55
|
+
acc[key] = value;
|
|
56
|
+
}
|
|
57
|
+
return acc;
|
|
58
|
+
}, {});
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// src/utils/toArray.ts
|
|
62
|
+
var toArray = (itemOrItems, separator) => {
|
|
63
|
+
if (itemOrItems === null || itemOrItems === void 0) {
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
if (separator && typeof itemOrItems === "string") {
|
|
67
|
+
return itemOrItems.split(separator);
|
|
68
|
+
}
|
|
69
|
+
return Array.isArray(itemOrItems) ? itemOrItems : [itemOrItems];
|
|
70
|
+
};
|
|
71
|
+
export {
|
|
72
|
+
asyncEvery,
|
|
73
|
+
asyncSome,
|
|
74
|
+
isEnumValue,
|
|
75
|
+
isInArray,
|
|
76
|
+
isOfType,
|
|
77
|
+
isUndefined,
|
|
78
|
+
objectIsEqual,
|
|
79
|
+
partitionArray,
|
|
80
|
+
removePropertiesWith,
|
|
81
|
+
toArray
|
|
82
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@myparcel-dev/ts-utils",
|
|
3
|
+
"version": "1.15.0",
|
|
4
|
+
"description": "TypeScript utilities",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"typescript",
|
|
7
|
+
"utility",
|
|
8
|
+
"utils",
|
|
9
|
+
"type-guard"
|
|
10
|
+
],
|
|
11
|
+
"license": "MIT",
|
|
12
|
+
"author": "Edie Lemoine <edie@myparcel.nl>",
|
|
13
|
+
"type": "module",
|
|
14
|
+
"exports": {
|
|
15
|
+
"require": "./dist/index.cjs",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
},
|
|
18
|
+
"types": "dist/index.d.ts",
|
|
19
|
+
"files": [
|
|
20
|
+
"dist"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"build": "tsup src/index.ts --dts --format esm,cjs",
|
|
24
|
+
"prepare": "is-ci || husky install",
|
|
25
|
+
"test": "vitest",
|
|
26
|
+
"test:run": "vitest run",
|
|
27
|
+
"test:types": "tsc -p src/__tests__/types/tsconfig.test.json"
|
|
28
|
+
},
|
|
29
|
+
"lint-staged": {
|
|
30
|
+
"*.{js,ts}": "eslint --fix",
|
|
31
|
+
"*.{json,md,yml}": "prettier --write",
|
|
32
|
+
"package.json": "sort-package-json"
|
|
33
|
+
},
|
|
34
|
+
"prettier": "@myparcel-dev/prettier-config",
|
|
35
|
+
"release": {
|
|
36
|
+
"extends": "@myparcel-dev/semantic-release-config/github-npm"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@myparcel-dev/semantic-release-config": "^6.0.0",
|
|
40
|
+
"@myparcel-eslint/eslint-config-prettier-typescript": "^1.2.0",
|
|
41
|
+
"@types/node": "^20.0.0",
|
|
42
|
+
"@vitest/coverage-v8": "^1.0.0",
|
|
43
|
+
"eslint": "^8.33.0",
|
|
44
|
+
"husky": "^8.0.3",
|
|
45
|
+
"is-ci": "^3.0.1",
|
|
46
|
+
"lint-staged": "^15.0.0",
|
|
47
|
+
"prettier": "^2.8.4",
|
|
48
|
+
"sort-package-json": "^2.4.1",
|
|
49
|
+
"ts-node": "^10.9.1",
|
|
50
|
+
"tsup": "^8.0.0",
|
|
51
|
+
"typescript": "^5.0.0",
|
|
52
|
+
"vitest": "^1.0.0"
|
|
53
|
+
},
|
|
54
|
+
"packageManager": "yarn@4.0.2",
|
|
55
|
+
"publishConfig": {
|
|
56
|
+
"access": "public"
|
|
57
|
+
}
|
|
58
|
+
}
|