complete-common 1.3.0 → 2.0.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/dist/functions/array.d.cts +7 -7
- package/dist/functions/array.d.mts +7 -7
- package/dist/functions/array.d.ts +7 -7
- package/dist/functions/array.d.ts.map +1 -1
- package/dist/functions/assert.d.cts +12 -0
- package/dist/functions/assert.d.mts +12 -0
- package/dist/functions/assert.d.ts +12 -0
- package/dist/functions/assert.d.ts.map +1 -1
- package/dist/functions/enums.d.cts +1 -2
- package/dist/functions/enums.d.mts +1 -2
- package/dist/functions/enums.d.ts +1 -2
- package/dist/functions/enums.d.ts.map +1 -1
- package/dist/functions/map.d.cts +6 -2
- package/dist/functions/map.d.mts +6 -2
- package/dist/functions/map.d.ts +6 -2
- package/dist/functions/map.d.ts.map +1 -1
- package/dist/functions/object.d.cts +3 -30
- package/dist/functions/object.d.mts +3 -30
- package/dist/functions/object.d.ts +3 -30
- package/dist/functions/object.d.ts.map +1 -1
- package/dist/functions/set.d.cts +6 -26
- package/dist/functions/set.d.mts +6 -26
- package/dist/functions/set.d.ts +6 -26
- package/dist/functions/set.d.ts.map +1 -1
- package/dist/functions/sort.d.cts +1 -1
- package/dist/functions/sort.d.mts +1 -1
- package/dist/functions/sort.d.ts +1 -1
- package/dist/functions/sort.d.ts.map +1 -1
- package/dist/index.cjs +34 -44
- package/dist/index.d.cts +1 -0
- package/dist/index.d.mts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +34 -41
- package/dist/types/TranspiledEnum.d.cts +3 -0
- package/dist/types/TranspiledEnum.d.mts +3 -0
- package/dist/types/TranspiledEnum.d.ts +3 -0
- package/dist/types/TranspiledEnum.d.ts.map +1 -0
- package/package.json +5 -5
- package/src/functions/array.ts +8 -13
- package/src/functions/assert.ts +23 -0
- package/src/functions/enums.ts +1 -1
- package/src/functions/map.ts +8 -5
- package/src/functions/object.ts +3 -44
- package/src/functions/set.ts +8 -41
- package/src/functions/sort.ts +3 -2
- package/src/index.ts +1 -0
- package/src/types/TranspiledEnum.ts +2 -0
package/src/functions/object.ts
CHANGED
|
@@ -12,11 +12,10 @@ import type { ReadonlyRecord } from "../types/ReadonlyRecord.js";
|
|
|
12
12
|
*
|
|
13
13
|
* This is efficient such that it avoids converting the object values into an array.
|
|
14
14
|
*/
|
|
15
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
16
15
|
export function objectFilter<K extends string | number | symbol, V>(
|
|
17
16
|
object: ReadonlyRecord<K, V>,
|
|
18
17
|
predicate: (value: V) => boolean,
|
|
19
|
-
): V[] {
|
|
18
|
+
): readonly V[] {
|
|
20
19
|
const array: V[] = [];
|
|
21
20
|
|
|
22
21
|
// eslint-disable-next-line complete/no-for-in
|
|
@@ -41,13 +40,10 @@ export function objectFilter<K extends string | number | symbol, V>(
|
|
|
41
40
|
*
|
|
42
41
|
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
43
42
|
* only having string keys under the hood.
|
|
44
|
-
*
|
|
45
|
-
* Also see the `objectToReadonlyMap` function.
|
|
46
43
|
*/
|
|
47
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
48
44
|
export function objectToMap<K extends string | number | symbol, V>(
|
|
49
45
|
object: Record<K, V>,
|
|
50
|
-
):
|
|
46
|
+
): ReadonlyMap<K, V> {
|
|
51
47
|
const map = new Map<K, V>();
|
|
52
48
|
|
|
53
49
|
for (const [key, value] of Object.entries(object)) {
|
|
@@ -57,38 +53,16 @@ export function objectToMap<K extends string | number | symbol, V>(
|
|
|
57
53
|
return map;
|
|
58
54
|
}
|
|
59
55
|
|
|
60
|
-
/**
|
|
61
|
-
* Helper function to convert an object to a read-only map.
|
|
62
|
-
*
|
|
63
|
-
* This is useful when you need to construct a type safe object with the `satisfies` operator, but
|
|
64
|
-
* then later on you need to query it in a way where you expect the return value to be T or
|
|
65
|
-
* undefined. In this situation, by converting the object to a map, you can avoid unsafe type
|
|
66
|
-
* assertions.
|
|
67
|
-
*
|
|
68
|
-
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
69
|
-
* only having string keys under the hood.
|
|
70
|
-
*
|
|
71
|
-
* Also see the `objectToMap` function.
|
|
72
|
-
*/
|
|
73
|
-
export function objectToReadonlyMap<K extends string | number | symbol, V>(
|
|
74
|
-
object: Record<K, V>,
|
|
75
|
-
): ReadonlyMap<K, V> {
|
|
76
|
-
return objectToMap(object);
|
|
77
|
-
}
|
|
78
|
-
|
|
79
56
|
/**
|
|
80
57
|
* Helper function to convert an object to a reverse map.
|
|
81
58
|
*
|
|
82
59
|
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
83
60
|
* only having string keys under the hood.
|
|
84
|
-
*
|
|
85
|
-
* Also see the `objectToReverseReadonlyMap` function.
|
|
86
61
|
*/
|
|
87
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
88
62
|
export function objectToReverseMap<
|
|
89
63
|
K extends string | number | symbol,
|
|
90
64
|
V extends string | number | symbol,
|
|
91
|
-
>(object: Record<K, V>):
|
|
65
|
+
>(object: Record<K, V>): ReadonlyMap<V, K> {
|
|
92
66
|
const map = new Map<V, K>();
|
|
93
67
|
|
|
94
68
|
for (const [key, value] of Object.entries(object)) {
|
|
@@ -97,18 +71,3 @@ export function objectToReverseMap<
|
|
|
97
71
|
|
|
98
72
|
return map;
|
|
99
73
|
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Helper function to convert an object to a reverse read-only map.
|
|
103
|
-
*
|
|
104
|
-
* Note that the converted map will only have string keys, due to the nature of JavaScript objects
|
|
105
|
-
* only having string keys under the hood.
|
|
106
|
-
*
|
|
107
|
-
* Also see the `objectToReverseMap` function.
|
|
108
|
-
*/
|
|
109
|
-
export function objectToReverseReadonlyMap<
|
|
110
|
-
K extends string | number | symbol,
|
|
111
|
-
V extends string | number | symbol,
|
|
112
|
-
>(object: Record<K, V>): ReadonlyMap<V, K> {
|
|
113
|
-
return objectToReverseMap(object);
|
|
114
|
-
}
|
package/src/functions/set.ts
CHANGED
|
@@ -28,8 +28,9 @@ export function addSetsToSet<T>(
|
|
|
28
28
|
*
|
|
29
29
|
* This function is variadic, meaning that you can specify N sets.
|
|
30
30
|
*/
|
|
31
|
-
|
|
32
|
-
|
|
31
|
+
export function combineSets<T>(
|
|
32
|
+
...sets: ReadonlyArray<ReadonlySet<T>>
|
|
33
|
+
): ReadonlySet<T> {
|
|
33
34
|
const newSet = new Set<T>();
|
|
34
35
|
for (const set of sets) {
|
|
35
36
|
for (const value of set) {
|
|
@@ -41,8 +42,7 @@ export function combineSets<T>(...sets: ReadonlyArray<ReadonlySet<T>>): Set<T> {
|
|
|
41
42
|
}
|
|
42
43
|
|
|
43
44
|
/** Helper function to copy a set. (You can also use a Set constructor to accomplish this task.) */
|
|
44
|
-
|
|
45
|
-
export function copySet<T>(oldSet: ReadonlySet<T>): Set<T> {
|
|
45
|
+
export function copySet<T>(oldSet: ReadonlySet<T>): ReadonlySet<T> {
|
|
46
46
|
const newSet = new Set<T>();
|
|
47
47
|
for (const value of oldSet) {
|
|
48
48
|
newSet.add(value);
|
|
@@ -51,26 +51,10 @@ export function copySet<T>(oldSet: ReadonlySet<T>): Set<T> {
|
|
|
51
51
|
return newSet;
|
|
52
52
|
}
|
|
53
53
|
|
|
54
|
-
/**
|
|
55
|
-
* Helper function to convert the keys of an object to a read-only set.
|
|
56
|
-
*
|
|
57
|
-
* Also see the `objectKeysToSet` function.
|
|
58
|
-
*/
|
|
59
|
-
export function objectKeysToReadonlySet<K extends string | number | symbol, V>(
|
|
60
|
-
object: Record<K, V>,
|
|
61
|
-
): ReadonlySet<K> {
|
|
62
|
-
return objectKeysToSet(object);
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* Helper function to convert the keys of an object to a set.
|
|
67
|
-
*
|
|
68
|
-
* Also see the `objectKeysToReadonlySet` function.
|
|
69
|
-
*/
|
|
70
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
54
|
+
/** Helper function to convert the keys of an object to a set. */
|
|
71
55
|
export function objectKeysToSet<K extends string | number | symbol, V>(
|
|
72
56
|
object: Record<K, V>,
|
|
73
|
-
):
|
|
57
|
+
): ReadonlySet<K> {
|
|
74
58
|
const set = new Set<K>();
|
|
75
59
|
|
|
76
60
|
for (const key of Object.keys(object)) {
|
|
@@ -80,27 +64,10 @@ export function objectKeysToSet<K extends string | number | symbol, V>(
|
|
|
80
64
|
return set;
|
|
81
65
|
}
|
|
82
66
|
|
|
83
|
-
/**
|
|
84
|
-
* Helper function to convert the values of an object to a read-only set.
|
|
85
|
-
*
|
|
86
|
-
* Also see the `objectValuesToSet` function.
|
|
87
|
-
*/
|
|
88
|
-
export function objectValuesToReadonlySet<
|
|
89
|
-
K extends string | number | symbol,
|
|
90
|
-
V,
|
|
91
|
-
>(object: Record<K, V>): ReadonlySet<V> {
|
|
92
|
-
return objectValuesToSet(object);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Helper function to convert the values of an object to a set.
|
|
97
|
-
*
|
|
98
|
-
* Also see the `objectValuesToReadonlySet` function.
|
|
99
|
-
*/
|
|
100
|
-
// eslint-disable-next-line complete/no-mutable-return
|
|
67
|
+
/** Helper function to convert the values of an object to a set. */
|
|
101
68
|
export function objectValuesToSet<K extends string | number | symbol, V>(
|
|
102
69
|
object: Record<K, V>,
|
|
103
|
-
):
|
|
70
|
+
): ReadonlySet<V> {
|
|
104
71
|
const set = new Set<V>();
|
|
105
72
|
|
|
106
73
|
for (const key of Object.values(object)) {
|
package/src/functions/sort.ts
CHANGED
|
@@ -11,8 +11,9 @@
|
|
|
11
11
|
* From:
|
|
12
12
|
* https://stackoverflow.com/questions/8996963/how-to-perform-case-insensitive-sorting-array-of-string-in-javascript
|
|
13
13
|
*/
|
|
14
|
-
|
|
15
|
-
|
|
14
|
+
export function sortCaseInsensitive(
|
|
15
|
+
array: readonly string[],
|
|
16
|
+
): readonly string[] {
|
|
16
17
|
const newArray = [...array];
|
|
17
18
|
newArray.sort((a, b) =>
|
|
18
19
|
a.localeCompare(b, undefined, {
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,7 @@ export type * from "./types/ObjectValues.js";
|
|
|
24
24
|
export * from "./types/ReadonlyMap.js";
|
|
25
25
|
export type * from "./types/ReadonlyRecord.js";
|
|
26
26
|
export * from "./types/ReadonlySet.js";
|
|
27
|
+
export type * from "./types/TranspiledEnum.js";
|
|
27
28
|
export type * from "./types/Tuple.js";
|
|
28
29
|
export type * from "./types/WidenLiteral.js";
|
|
29
30
|
export type * from "./types/Writeable.js";
|