@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.
- package/CHANGELOG.md +36 -0
- package/coverage/clover.xml +32 -0
- package/coverage/coverage-final.json +5 -0
- package/coverage/lcov-report/Array.ts.html +163 -0
- package/coverage/lcov-report/Helpers.ts.html +106 -0
- package/coverage/lcov-report/JSON.ts.html +151 -0
- package/coverage/lcov-report/Key.ts.html +157 -0
- package/coverage/lcov-report/Object.ts.html +205 -0
- package/coverage/lcov-report/Record.ts.html +226 -0
- package/coverage/lcov-report/TypeJuggling.ts.html +142 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +161 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +94 -0
- package/dist/Array.d.ts +2 -0
- package/dist/Array.js +12 -0
- package/dist/{JSONValue.d.ts → JSON.d.ts} +2 -0
- package/dist/JSON.js +9 -0
- package/dist/Key.d.ts +3 -0
- package/dist/Key.js +18 -0
- package/dist/{MethodNames.d.ts → Methods.d.ts} +3 -5
- package/dist/{MethodNames.js → Methods.js} +1 -0
- package/dist/Object.d.ts +22 -0
- package/dist/{PropertyRequirements.d.ts → Properties.d.ts} +8 -10
- package/dist/Record.d.ts +1 -1
- package/dist/Record.js +5 -5
- package/dist/{OneOf.d.ts → TypeJuggling.d.ts} +3 -0
- package/dist/TypeJuggling.js +10 -0
- package/dist/_Deprecated.d.ts +63 -0
- package/dist/_Deprecated.js +82 -0
- package/dist/index.d.ts +9 -13
- package/dist/index.js +9 -13
- package/jest.config.ts +2 -1
- package/package.json +1 -1
- package/tests/Array/isEntries.test.ts +25 -0
- package/tests/JSON/isJSONPrimitive.test.ts +16 -0
- package/tests/Key/isKey.test.ts +13 -0
- package/tests/Key/isKeyof.test.ts +13 -0
- package/tests/Record/isRecord.test.ts +9 -32
- package/tests/TypeJuggling/isString.test.ts +14 -0
- package/tests/TypeJuggling/isUnknown.test.ts +7 -0
- package/dist/ArrayElement.d.ts +0 -1
- package/dist/Coerce/Coerce.d.ts +0 -1
- package/dist/Coerce/Coerce.js +0 -12
- package/dist/Coerce/Error.d.ts +0 -2
- package/dist/Coerce/Error.js +0 -16
- package/dist/Coerce.d.ts +0 -2
- package/dist/Coerce.js +0 -18
- package/dist/Constructor.d.ts +0 -21
- package/dist/Constructor.js +0 -29
- package/dist/EnumeratedTypes.d.ts +0 -11
- package/dist/EnumeratedTypes.js +0 -11
- package/dist/Mixin.d.ts +0 -2
- package/dist/Mixin.js +0 -2
- package/dist/OneOf.js +0 -3
- package/dist/OptionalProperty.d.ts +0 -1
- package/dist/OptionalProperty.js +0 -2
- package/dist/PropertyRequirements.js +0 -2
- package/dist/Shorthand.d.ts +0 -11
- package/dist/Shorthand.js +0 -2
- package/dist/StaticImplements.d.ts +0 -23
- package/dist/StaticImplements.js +0 -2
- package/tests/JSONValue/JSONPrimitive.test.ts +0 -25
- /package/dist/{Forms.d.ts → DOM.d.ts} +0 -0
- /package/dist/{ArrayElement.js → DOM.js} +0 -0
- /package/dist/{Forms.js → Object.js} +0 -0
- /package/dist/{JSONValue.js → Properties.js} +0 -0
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
var addSorting = (function() {
|
|
3
|
+
'use strict';
|
|
4
|
+
var cols,
|
|
5
|
+
currentSort = {
|
|
6
|
+
index: 0,
|
|
7
|
+
desc: false
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
// returns the summary table element
|
|
11
|
+
function getTable() {
|
|
12
|
+
return document.querySelector('.coverage-summary');
|
|
13
|
+
}
|
|
14
|
+
// returns the thead element of the summary table
|
|
15
|
+
function getTableHeader() {
|
|
16
|
+
return getTable().querySelector('thead tr');
|
|
17
|
+
}
|
|
18
|
+
// returns the tbody element of the summary table
|
|
19
|
+
function getTableBody() {
|
|
20
|
+
return getTable().querySelector('tbody');
|
|
21
|
+
}
|
|
22
|
+
// returns the th element for nth column
|
|
23
|
+
function getNthColumn(n) {
|
|
24
|
+
return getTableHeader().querySelectorAll('th')[n];
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function onFilterInput() {
|
|
28
|
+
const searchValue = document.getElementById('fileSearch').value;
|
|
29
|
+
const rows = document.getElementsByTagName('tbody')[0].children;
|
|
30
|
+
|
|
31
|
+
// Try to create a RegExp from the searchValue. If it fails (invalid regex),
|
|
32
|
+
// it will be treated as a plain text search
|
|
33
|
+
let searchRegex;
|
|
34
|
+
try {
|
|
35
|
+
searchRegex = new RegExp(searchValue, 'i'); // 'i' for case-insensitive
|
|
36
|
+
} catch (error) {
|
|
37
|
+
searchRegex = null;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
for (let i = 0; i < rows.length; i++) {
|
|
41
|
+
const row = rows[i];
|
|
42
|
+
let isMatch = false;
|
|
43
|
+
|
|
44
|
+
if (searchRegex) {
|
|
45
|
+
// If a valid regex was created, use it for matching
|
|
46
|
+
isMatch = searchRegex.test(row.textContent);
|
|
47
|
+
} else {
|
|
48
|
+
// Otherwise, fall back to the original plain text search
|
|
49
|
+
isMatch = row.textContent
|
|
50
|
+
.toLowerCase()
|
|
51
|
+
.includes(searchValue.toLowerCase());
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
row.style.display = isMatch ? '' : 'none';
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// loads the search box
|
|
59
|
+
function addSearchBox() {
|
|
60
|
+
var template = document.getElementById('filterTemplate');
|
|
61
|
+
var templateClone = template.content.cloneNode(true);
|
|
62
|
+
templateClone.getElementById('fileSearch').oninput = onFilterInput;
|
|
63
|
+
template.parentElement.appendChild(templateClone);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// loads all columns
|
|
67
|
+
function loadColumns() {
|
|
68
|
+
var colNodes = getTableHeader().querySelectorAll('th'),
|
|
69
|
+
colNode,
|
|
70
|
+
cols = [],
|
|
71
|
+
col,
|
|
72
|
+
i;
|
|
73
|
+
|
|
74
|
+
for (i = 0; i < colNodes.length; i += 1) {
|
|
75
|
+
colNode = colNodes[i];
|
|
76
|
+
col = {
|
|
77
|
+
key: colNode.getAttribute('data-col'),
|
|
78
|
+
sortable: !colNode.getAttribute('data-nosort'),
|
|
79
|
+
type: colNode.getAttribute('data-type') || 'string'
|
|
80
|
+
};
|
|
81
|
+
cols.push(col);
|
|
82
|
+
if (col.sortable) {
|
|
83
|
+
col.defaultDescSort = col.type === 'number';
|
|
84
|
+
colNode.innerHTML =
|
|
85
|
+
colNode.innerHTML + '<span class="sorter"></span>';
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return cols;
|
|
89
|
+
}
|
|
90
|
+
// attaches a data attribute to every tr element with an object
|
|
91
|
+
// of data values keyed by column name
|
|
92
|
+
function loadRowData(tableRow) {
|
|
93
|
+
var tableCols = tableRow.querySelectorAll('td'),
|
|
94
|
+
colNode,
|
|
95
|
+
col,
|
|
96
|
+
data = {},
|
|
97
|
+
i,
|
|
98
|
+
val;
|
|
99
|
+
for (i = 0; i < tableCols.length; i += 1) {
|
|
100
|
+
colNode = tableCols[i];
|
|
101
|
+
col = cols[i];
|
|
102
|
+
val = colNode.getAttribute('data-value');
|
|
103
|
+
if (col.type === 'number') {
|
|
104
|
+
val = Number(val);
|
|
105
|
+
}
|
|
106
|
+
data[col.key] = val;
|
|
107
|
+
}
|
|
108
|
+
return data;
|
|
109
|
+
}
|
|
110
|
+
// loads all row data
|
|
111
|
+
function loadData() {
|
|
112
|
+
var rows = getTableBody().querySelectorAll('tr'),
|
|
113
|
+
i;
|
|
114
|
+
|
|
115
|
+
for (i = 0; i < rows.length; i += 1) {
|
|
116
|
+
rows[i].data = loadRowData(rows[i]);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
// sorts the table using the data for the ith column
|
|
120
|
+
function sortByIndex(index, desc) {
|
|
121
|
+
var key = cols[index].key,
|
|
122
|
+
sorter = function(a, b) {
|
|
123
|
+
a = a.data[key];
|
|
124
|
+
b = b.data[key];
|
|
125
|
+
return a < b ? -1 : a > b ? 1 : 0;
|
|
126
|
+
},
|
|
127
|
+
finalSorter = sorter,
|
|
128
|
+
tableBody = document.querySelector('.coverage-summary tbody'),
|
|
129
|
+
rowNodes = tableBody.querySelectorAll('tr'),
|
|
130
|
+
rows = [],
|
|
131
|
+
i;
|
|
132
|
+
|
|
133
|
+
if (desc) {
|
|
134
|
+
finalSorter = function(a, b) {
|
|
135
|
+
return -1 * sorter(a, b);
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
for (i = 0; i < rowNodes.length; i += 1) {
|
|
140
|
+
rows.push(rowNodes[i]);
|
|
141
|
+
tableBody.removeChild(rowNodes[i]);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
rows.sort(finalSorter);
|
|
145
|
+
|
|
146
|
+
for (i = 0; i < rows.length; i += 1) {
|
|
147
|
+
tableBody.appendChild(rows[i]);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
// removes sort indicators for current column being sorted
|
|
151
|
+
function removeSortIndicators() {
|
|
152
|
+
var col = getNthColumn(currentSort.index),
|
|
153
|
+
cls = col.className;
|
|
154
|
+
|
|
155
|
+
cls = cls.replace(/ sorted$/, '').replace(/ sorted-desc$/, '');
|
|
156
|
+
col.className = cls;
|
|
157
|
+
}
|
|
158
|
+
// adds sort indicators for current column being sorted
|
|
159
|
+
function addSortIndicators() {
|
|
160
|
+
getNthColumn(currentSort.index).className += currentSort.desc
|
|
161
|
+
? ' sorted-desc'
|
|
162
|
+
: ' sorted';
|
|
163
|
+
}
|
|
164
|
+
// adds event listeners for all sorter widgets
|
|
165
|
+
function enableUI() {
|
|
166
|
+
var i,
|
|
167
|
+
el,
|
|
168
|
+
ithSorter = function ithSorter(i) {
|
|
169
|
+
var col = cols[i];
|
|
170
|
+
|
|
171
|
+
return function() {
|
|
172
|
+
var desc = col.defaultDescSort;
|
|
173
|
+
|
|
174
|
+
if (currentSort.index === i) {
|
|
175
|
+
desc = !currentSort.desc;
|
|
176
|
+
}
|
|
177
|
+
sortByIndex(i, desc);
|
|
178
|
+
removeSortIndicators();
|
|
179
|
+
currentSort.index = i;
|
|
180
|
+
currentSort.desc = desc;
|
|
181
|
+
addSortIndicators();
|
|
182
|
+
};
|
|
183
|
+
};
|
|
184
|
+
for (i = 0; i < cols.length; i += 1) {
|
|
185
|
+
if (cols[i].sortable) {
|
|
186
|
+
// add the click event handler on the th so users
|
|
187
|
+
// dont have to click on those tiny arrows
|
|
188
|
+
el = getNthColumn(i).querySelector('.sorter').parentElement;
|
|
189
|
+
if (el.addEventListener) {
|
|
190
|
+
el.addEventListener('click', ithSorter(i));
|
|
191
|
+
} else {
|
|
192
|
+
el.attachEvent('onclick', ithSorter(i));
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
// adds sorting functionality to the UI
|
|
198
|
+
return function() {
|
|
199
|
+
if (!getTable()) {
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
202
|
+
cols = loadColumns();
|
|
203
|
+
loadData();
|
|
204
|
+
addSearchBox();
|
|
205
|
+
addSortIndicators();
|
|
206
|
+
enableUI();
|
|
207
|
+
};
|
|
208
|
+
})();
|
|
209
|
+
|
|
210
|
+
window.addEventListener('load', addSorting);
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
TN:
|
|
2
|
+
SF:src/JSON.ts
|
|
3
|
+
FN:15,isJSONPrimitive
|
|
4
|
+
FNF:1
|
|
5
|
+
FNH:1
|
|
6
|
+
FNDA:1,isJSONPrimitive
|
|
7
|
+
DA:15,1
|
|
8
|
+
DA:16,1
|
|
9
|
+
LF:2
|
|
10
|
+
LH:2
|
|
11
|
+
BRDA:17,0,0,1
|
|
12
|
+
BRDA:17,0,1,1
|
|
13
|
+
BRDA:17,0,2,0
|
|
14
|
+
BRDA:17,0,3,0
|
|
15
|
+
BRF:4
|
|
16
|
+
BRH:2
|
|
17
|
+
end_of_record
|
|
18
|
+
TN:
|
|
19
|
+
SF:src/Key.ts
|
|
20
|
+
FN:3,isKey
|
|
21
|
+
FN:12,isKeyof
|
|
22
|
+
FNF:2
|
|
23
|
+
FNH:2
|
|
24
|
+
FNDA:18,isKey
|
|
25
|
+
FNDA:5,isKeyof
|
|
26
|
+
DA:3,3
|
|
27
|
+
DA:4,18
|
|
28
|
+
DA:12,3
|
|
29
|
+
DA:16,5
|
|
30
|
+
LF:4
|
|
31
|
+
LH:4
|
|
32
|
+
BRDA:5,0,0,18
|
|
33
|
+
BRDA:5,0,1,18
|
|
34
|
+
BRDA:5,0,2,7
|
|
35
|
+
BRDA:5,0,3,5
|
|
36
|
+
BRDA:7,1,0,7
|
|
37
|
+
BRDA:7,1,1,3
|
|
38
|
+
BRDA:17,2,0,5
|
|
39
|
+
BRDA:17,2,1,5
|
|
40
|
+
BRDA:17,2,2,3
|
|
41
|
+
BRDA:17,2,3,4
|
|
42
|
+
BRDA:17,2,4,1
|
|
43
|
+
BRDA:17,2,5,3
|
|
44
|
+
BRDA:17,2,6,1
|
|
45
|
+
BRF:13
|
|
46
|
+
BRH:13
|
|
47
|
+
end_of_record
|
|
48
|
+
TN:
|
|
49
|
+
SF:src/Record.ts
|
|
50
|
+
FN:1,isRecord
|
|
51
|
+
FN:13,(anonymous_1)
|
|
52
|
+
FN:45,(anonymous_2)
|
|
53
|
+
FNF:3
|
|
54
|
+
FNH:3
|
|
55
|
+
FNDA:10,isRecord
|
|
56
|
+
FNDA:10,(anonymous_1)
|
|
57
|
+
FNDA:200,(anonymous_2)
|
|
58
|
+
DA:1,1
|
|
59
|
+
DA:9,10
|
|
60
|
+
DA:14,10
|
|
61
|
+
DA:45,200
|
|
62
|
+
LF:4
|
|
63
|
+
LH:4
|
|
64
|
+
BRDA:10,0,0,10
|
|
65
|
+
BRDA:10,0,1,9
|
|
66
|
+
BRDA:10,0,2,9
|
|
67
|
+
BRDA:10,0,3,8
|
|
68
|
+
BRDA:14,1,0,10
|
|
69
|
+
BRDA:14,1,1,10
|
|
70
|
+
BRDA:14,1,2,10
|
|
71
|
+
BRDA:14,1,3,10
|
|
72
|
+
BRDA:14,1,4,10
|
|
73
|
+
BRDA:45,2,0,200
|
|
74
|
+
BRDA:45,2,1,95
|
|
75
|
+
BRF:11
|
|
76
|
+
BRH:11
|
|
77
|
+
end_of_record
|
|
78
|
+
TN:
|
|
79
|
+
SF:src/TypeJuggling.ts
|
|
80
|
+
FN:13,isUnknown
|
|
81
|
+
FN:17,isString
|
|
82
|
+
FNF:2
|
|
83
|
+
FNH:2
|
|
84
|
+
FNDA:7,isUnknown
|
|
85
|
+
FNDA:5,isString
|
|
86
|
+
DA:13,1
|
|
87
|
+
DA:14,7
|
|
88
|
+
DA:17,1
|
|
89
|
+
DA:18,5
|
|
90
|
+
LF:4
|
|
91
|
+
LH:4
|
|
92
|
+
BRF:0
|
|
93
|
+
BRH:0
|
|
94
|
+
end_of_record
|
package/dist/Array.d.ts
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export type ArrayElement<ArrayType extends readonly unknown[]> = ArrayType extends readonly (infer ElementType)[] ? ElementType : never;
|
|
2
|
+
export declare function isEntries<K extends string | number | symbol = string | number | symbol, V = unknown>(obj: unknown, isK?: (k: unknown) => k is K, isV?: (v: unknown) => v is V): obj is [K, V][];
|
package/dist/Array.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEntries = isEntries;
|
|
4
|
+
function isEntries(obj, isK, isV) {
|
|
5
|
+
return (Array.isArray(obj) &&
|
|
6
|
+
(obj.length === 0 ||
|
|
7
|
+
obj.reduce((entries, elt) => entries &&
|
|
8
|
+
Array.isArray(elt) &&
|
|
9
|
+
elt.length == 2 &&
|
|
10
|
+
(!isK || isK(elt[0])) &&
|
|
11
|
+
(!isV || isV(elt[1])), true)));
|
|
12
|
+
}
|
|
@@ -4,4 +4,6 @@ export interface JSONObject {
|
|
|
4
4
|
[k: string]: JSONValue;
|
|
5
5
|
}
|
|
6
6
|
export type JSONValue = JSONPrimitive | JSONArray | JSONObject;
|
|
7
|
+
/** @deprecated Default exports are not best practice */
|
|
7
8
|
export default JSONValue;
|
|
9
|
+
export declare function isJSONPrimitive(value: unknown): value is JSONPrimitive;
|
package/dist/JSON.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isJSONPrimitive = isJSONPrimitive;
|
|
4
|
+
function isJSONPrimitive(value) {
|
|
5
|
+
return (value === null ||
|
|
6
|
+
typeof value === 'string' ||
|
|
7
|
+
typeof value === 'number' ||
|
|
8
|
+
typeof value === 'boolean');
|
|
9
|
+
}
|
package/dist/Key.d.ts
ADDED
package/dist/Key.js
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isKey = isKey;
|
|
4
|
+
exports.isKeyof = isKeyof;
|
|
5
|
+
function isKey(value) {
|
|
6
|
+
return (value !== undefined &&
|
|
7
|
+
(typeof value === 'string' ||
|
|
8
|
+
(typeof value === 'number' && parseInt(value.toString())) === value ||
|
|
9
|
+
typeof value === 'symbol'));
|
|
10
|
+
}
|
|
11
|
+
function isKeyof(value, obj) {
|
|
12
|
+
return (isKey(value) &&
|
|
13
|
+
((typeof value === 'string' && Object.keys(obj).includes(value)) ||
|
|
14
|
+
(typeof value === 'number' &&
|
|
15
|
+
Object.keys(obj).includes(value.toString())) ||
|
|
16
|
+
(typeof value === 'symbol' &&
|
|
17
|
+
Object.getOwnPropertySymbols(obj).includes(value))));
|
|
18
|
+
}
|
|
@@ -1,13 +1,11 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @see https://stackoverflow.com/a/61940388
|
|
3
|
-
*/
|
|
1
|
+
/** @see https://stackoverflow.com/a/61940388 */
|
|
4
2
|
export type MethodNames<T extends {
|
|
5
|
-
[K in keyof T]:
|
|
3
|
+
[K in keyof T]: unknown;
|
|
6
4
|
}> = {
|
|
7
5
|
[K in keyof T]: T[K] extends Function ? K : never;
|
|
8
6
|
}[keyof T];
|
|
9
7
|
export type MethodNamesMatching<T extends {
|
|
10
|
-
[K in keyof T]:
|
|
8
|
+
[K in keyof T]: unknown;
|
|
11
9
|
}, P extends Function = Function> = {
|
|
12
10
|
[K in keyof T]: T[K] extends Function ? P extends T[K] ? K : never : never;
|
|
13
11
|
}[keyof T];
|
package/dist/Object.d.ts
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @see {@link https://stackoverflow.com/a/69571314/294171 StackOverflow response} on static interfaces
|
|
3
|
+
*
|
|
4
|
+
* ```ts
|
|
5
|
+
* interface InstanceInterface {
|
|
6
|
+
* instanceMethod();
|
|
7
|
+
* }
|
|
8
|
+
*
|
|
9
|
+
* interface StaticInterface {
|
|
10
|
+
* new(...args: any[]): InstanceInterface;
|
|
11
|
+
* staticMethod();
|
|
12
|
+
* }
|
|
13
|
+
*
|
|
14
|
+
* class MyClass implements StaticImplements<StaticInterface, typeof MyClass> {
|
|
15
|
+
* static staticMethod() { }
|
|
16
|
+
* static ownStaticMethod() { }
|
|
17
|
+
* instanceMethod() { }
|
|
18
|
+
* ownInstanceMethod() { }
|
|
19
|
+
* }
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export type StaticImplements<I extends new (...args: unknown[]) => object, C extends I> = InstanceType<C>;
|
|
@@ -1,12 +1,8 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring at least one property
|
|
3
|
-
*/
|
|
1
|
+
/** @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring at least one property */
|
|
4
2
|
export type RequireAtLeastOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
|
|
5
3
|
[K in Keys]-?: Required<Pick<T, K>> & Partial<Pick<T, Exclude<Keys, K>>>;
|
|
6
4
|
}[Keys];
|
|
7
|
-
/**
|
|
8
|
-
* @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring only one property
|
|
9
|
-
*/
|
|
5
|
+
/** @see {@link https://stackoverflow.com/a/49725198/294171 StackOverflow response} on requiring only one property */
|
|
10
6
|
export type RequireOnlyOne<T, Keys extends keyof T = keyof T> = Pick<T, Exclude<keyof T, Keys>> & {
|
|
11
7
|
[K in Keys]-?: Required<Pick<T, K>> & Partial<Record<Exclude<Keys, K>, undefined>>;
|
|
12
8
|
}[Keys];
|
|
@@ -17,10 +13,10 @@ export type RecursivePartial<T> = {
|
|
|
17
13
|
/**
|
|
18
14
|
* ```ts
|
|
19
15
|
* type A = {
|
|
20
|
-
* d: number
|
|
21
|
-
* e: string
|
|
22
|
-
* f: boolean
|
|
23
|
-
* }
|
|
16
|
+
* d: number;
|
|
17
|
+
* e: string;
|
|
18
|
+
* f: boolean;
|
|
19
|
+
* };
|
|
24
20
|
*
|
|
25
21
|
* type B = ReplaceProperty<A, 'e', number[]>;
|
|
26
22
|
* // type B = {
|
|
@@ -29,6 +25,7 @@ export type RecursivePartial<T> = {
|
|
|
29
25
|
* // f: boolean
|
|
30
26
|
* // }
|
|
31
27
|
* ```
|
|
28
|
+
*
|
|
32
29
|
* @see {@link https://stackoverflow.com/a/51599774 StackOverflow response}
|
|
33
30
|
*/
|
|
34
31
|
export type ReplaceProperty<T, K extends keyof T, TReplace> = Identity<Pick<T, Exclude<keyof T, K>> & {
|
|
@@ -37,4 +34,5 @@ export type ReplaceProperty<T, K extends keyof T, TReplace> = Identity<Pick<T, E
|
|
|
37
34
|
type Identity<T> = {
|
|
38
35
|
[P in keyof T]: T[P];
|
|
39
36
|
};
|
|
37
|
+
export type OptionalProperty<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
|
40
38
|
export {};
|
package/dist/Record.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function isRecord<K extends string | number | symbol, V>(obj: unknown, isK
|
|
1
|
+
export declare function isRecord<K extends string | number | symbol = string | number | symbol, V = unknown>(obj: unknown, isK?: (k: unknown) => k is K, isV?: (v: unknown) => v is V): obj is Record<K, V>;
|
package/dist/Record.js
CHANGED
|
@@ -4,10 +4,9 @@ exports.isRecord = isRecord;
|
|
|
4
4
|
function isRecord(obj, isK, isV) {
|
|
5
5
|
return (typeof obj === 'object' &&
|
|
6
6
|
obj !== null &&
|
|
7
|
-
Object.keys(obj).reduce((result, key) => result &&
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
.reduce((result, value) => result && isV(value), true) &&
|
|
7
|
+
Object.keys(obj).reduce((result, key) => result &&
|
|
8
|
+
(!isK || isK(key)) &&
|
|
9
|
+
(!isV || isV(obj[key])), true) &&
|
|
11
10
|
[
|
|
12
11
|
Function,
|
|
13
12
|
Boolean,
|
|
@@ -32,6 +31,7 @@ function isRecord(obj, isK, isV) {
|
|
|
32
31
|
DataView,
|
|
33
32
|
Promise,
|
|
34
33
|
DisposableStack,
|
|
35
|
-
AsyncDisposableStack
|
|
34
|
+
AsyncDisposableStack,
|
|
35
|
+
FormData
|
|
36
36
|
].reduce((result, T) => result && !(obj instanceof T), true));
|
|
37
37
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
export type AsynchronousFunction = () => Promise<unknown>;
|
|
1
2
|
/** @see https://stackoverflow.com/questions/42123407/does-typescript-support-mutually-exclusive-types#comment123255834_53229567 */
|
|
2
3
|
type UnionKeys<T> = T extends T ? keyof T : never;
|
|
3
4
|
type Expand<T> = T extends T ? {
|
|
@@ -6,4 +7,6 @@ type Expand<T> = T extends T ? {
|
|
|
6
7
|
export type OneOf<T extends object[]> = {
|
|
7
8
|
[K in keyof T]: Expand<T[K] & Partial<Record<Exclude<UnionKeys<T[number]>, keyof T[K]>, never>>>;
|
|
8
9
|
}[number];
|
|
10
|
+
export declare function isUnknown(obj: unknown): obj is unknown;
|
|
11
|
+
export declare function isString(obj: unknown): obj is string;
|
|
9
12
|
export {};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { JSONPrimitive } from './JSON.js';
|
|
2
|
+
/** @deprecated Use Error.isError() */
|
|
3
|
+
export declare function isError(error: unknown): error is Error;
|
|
4
|
+
/** @deprecated Use Error.isError() */
|
|
5
|
+
export declare function CoerceError(error: unknown): Error;
|
|
6
|
+
/** @deprecated `null` is part of type unless using utility type `Nonnullable` */
|
|
7
|
+
export type Nullable<T> = T | null;
|
|
8
|
+
/** @deprecated Use `?` */
|
|
9
|
+
export type Optional<T> = T | undefined;
|
|
10
|
+
/** @deprecated Use `Record<string,T>` */
|
|
11
|
+
export type AssociativeArray<T> = {
|
|
12
|
+
[key: string]: T;
|
|
13
|
+
};
|
|
14
|
+
/** @deprecated Use {@link JSONPrimitive} */
|
|
15
|
+
export type JSONPrimitiveTypes = JSONPrimitive;
|
|
16
|
+
/**
|
|
17
|
+
* @deprecated See
|
|
18
|
+
* {@link https://www.typescriptlang.org/docs/handbook/mixins.html#constrained-mixins TypeScript docs}
|
|
19
|
+
*/
|
|
20
|
+
export type Mixin<T extends (...args: any[]) => any> = InstanceType<ReturnType<T>>;
|
|
21
|
+
/** @deprecated Use {@link filterByType} */
|
|
22
|
+
export declare const instanceOf: typeof filterByType;
|
|
23
|
+
/** @deprecated Use-case unclear */
|
|
24
|
+
export declare function Coerce<T>(u: unknown, isT: (u: unknown) => u is T, toT?: (u: unknown) => T): T;
|
|
25
|
+
/** @deprecated Use-case unclear */
|
|
26
|
+
export type Subset<T, U extends T> = T;
|
|
27
|
+
type EnumObject = {
|
|
28
|
+
[key: string]: number | string;
|
|
29
|
+
};
|
|
30
|
+
type EnumObjectEnum<E extends EnumObject> = E extends {
|
|
31
|
+
[key: string]: infer ET | string;
|
|
32
|
+
} ? ET : never;
|
|
33
|
+
/**
|
|
34
|
+
* @deprecated Use
|
|
35
|
+
* {@link https://www.typescriptlang.org/docs/handbook/enums.html enum}
|
|
36
|
+
* @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
|
|
37
|
+
*/
|
|
38
|
+
export declare function getEnumValues<E extends EnumObject>(enumObject: E): EnumObjectEnum<E>[];
|
|
39
|
+
/** @deprecated Use-case unclear */
|
|
40
|
+
export type Constructor<T = object> = new (...args: unknown[]) => T;
|
|
41
|
+
/** @deprecated Use-case unclear */
|
|
42
|
+
export declare function isConstructor<T = unknown>(value: unknown): value is Constructor<T>;
|
|
43
|
+
/**
|
|
44
|
+
* ```ts
|
|
45
|
+
* class A {}
|
|
46
|
+
* class B {}
|
|
47
|
+
* class C extends A {}
|
|
48
|
+
*
|
|
49
|
+
* const a = new A();
|
|
50
|
+
* const b = new B();
|
|
51
|
+
* const c = new C();
|
|
52
|
+
*
|
|
53
|
+
* const list = [a, b, c];
|
|
54
|
+
* const filteredList = filterByType(list, A);
|
|
55
|
+
* // filteredList = [a, c]
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @ see https://stackoverflow.com/a/65152869/294171
|
|
59
|
+
*
|
|
60
|
+
* @deprecated Use-case unclear
|
|
61
|
+
*/
|
|
62
|
+
export declare function filterByType<Elements, Filter extends Elements>(array: Elements[], filterType: Constructor<Filter>): Filter[];
|
|
63
|
+
export {};
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.instanceOf = void 0;
|
|
4
|
+
exports.isError = isError;
|
|
5
|
+
exports.CoerceError = CoerceError;
|
|
6
|
+
exports.Coerce = Coerce;
|
|
7
|
+
exports.getEnumValues = getEnumValues;
|
|
8
|
+
exports.isConstructor = isConstructor;
|
|
9
|
+
exports.filterByType = filterByType;
|
|
10
|
+
/** @deprecated Use Error.isError() */
|
|
11
|
+
function isError(error) {
|
|
12
|
+
return (typeof error === 'object' &&
|
|
13
|
+
error !== null &&
|
|
14
|
+
'message' in error &&
|
|
15
|
+
'name' in error &&
|
|
16
|
+
typeof error.name === 'string' &&
|
|
17
|
+
typeof error.message === 'string');
|
|
18
|
+
}
|
|
19
|
+
/** @deprecated Use Error.isError() */
|
|
20
|
+
function CoerceError(error) {
|
|
21
|
+
return Coerce(error, isError, (e) => e);
|
|
22
|
+
}
|
|
23
|
+
/** @deprecated Use {@link filterByType} */
|
|
24
|
+
exports.instanceOf = filterByType;
|
|
25
|
+
/** @deprecated Use-case unclear */
|
|
26
|
+
function Coerce(u, isT, toT) {
|
|
27
|
+
if (isT(u)) {
|
|
28
|
+
return u;
|
|
29
|
+
}
|
|
30
|
+
try {
|
|
31
|
+
if (toT) {
|
|
32
|
+
return toT(u);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
throw new Error('No toT() method provided');
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
catch (_a) {
|
|
39
|
+
throw new Error('Attempted coercion to a non-matching type');
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* @deprecated Use
|
|
44
|
+
* {@link https://www.typescriptlang.org/docs/handbook/enums.html enum}
|
|
45
|
+
* @see {@link https://blog.oyam.dev/typescript-enum-values/ How to get an array of enum values in TypeScript}
|
|
46
|
+
*/
|
|
47
|
+
function getEnumValues(enumObject) {
|
|
48
|
+
return Object.keys(enumObject)
|
|
49
|
+
.filter((key) => Number.isNaN(Number(key)))
|
|
50
|
+
.map((key) => enumObject[key]);
|
|
51
|
+
}
|
|
52
|
+
/** @deprecated Use-case unclear */
|
|
53
|
+
function isConstructor(value) {
|
|
54
|
+
return (!!value &&
|
|
55
|
+
typeof value === 'object' &&
|
|
56
|
+
'prototype' in value &&
|
|
57
|
+
typeof value.prototype === 'object' &&
|
|
58
|
+
!!value.prototype &&
|
|
59
|
+
'constructor' in value.prototype.constructor);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* ```ts
|
|
63
|
+
* class A {}
|
|
64
|
+
* class B {}
|
|
65
|
+
* class C extends A {}
|
|
66
|
+
*
|
|
67
|
+
* const a = new A();
|
|
68
|
+
* const b = new B();
|
|
69
|
+
* const c = new C();
|
|
70
|
+
*
|
|
71
|
+
* const list = [a, b, c];
|
|
72
|
+
* const filteredList = filterByType(list, A);
|
|
73
|
+
* // filteredList = [a, c]
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @ see https://stackoverflow.com/a/65152869/294171
|
|
77
|
+
*
|
|
78
|
+
* @deprecated Use-case unclear
|
|
79
|
+
*/
|
|
80
|
+
function filterByType(array, filterType) {
|
|
81
|
+
return array.filter((e) => e instanceof filterType);
|
|
82
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,14 +1,10 @@
|
|
|
1
|
-
export * from './
|
|
2
|
-
export * from './
|
|
3
|
-
export * from './
|
|
4
|
-
export * from './
|
|
5
|
-
export * from './
|
|
6
|
-
export * from './
|
|
7
|
-
export * from './
|
|
8
|
-
export * from './Mixin';
|
|
9
|
-
export * from './OneOf';
|
|
10
|
-
export * from './OptionalProperty';
|
|
11
|
-
export * from './PropertyRequirements';
|
|
1
|
+
export * from './Array';
|
|
2
|
+
export * from './DOM';
|
|
3
|
+
export * from './JSON';
|
|
4
|
+
export * from './Key';
|
|
5
|
+
export * from './Methods';
|
|
6
|
+
export * from './Object';
|
|
7
|
+
export * from './Properties';
|
|
12
8
|
export * from './Record';
|
|
13
|
-
export * from './
|
|
14
|
-
export * from './
|
|
9
|
+
export * from './TypeJuggling';
|
|
10
|
+
export * from './_Deprecated';
|