@fleet-sdk/common 0.2.2 → 0.3.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 +13 -0
- package/dist/index.cjs.js +47 -5
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.mts +373 -31
- package/dist/index.d.ts +373 -31
- package/dist/index.esm.js +45 -6
- package/dist/index.esm.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +0 -3
- package/src/models/collection.ts +0 -107
- package/src/models/index.ts +0 -1
- package/src/types/block.ts +0 -44
- package/src/types/boxes.ts +0 -31
- package/src/types/common.ts +0 -31
- package/src/types/contextExtension.ts +0 -3
- package/src/types/index.ts +0 -9
- package/src/types/inputs.ts +0 -42
- package/src/types/proverResult.ts +0 -7
- package/src/types/registers.ts +0 -10
- package/src/types/token.ts +0 -21
- package/src/types/transactions.ts +0 -32
- package/src/utils/array.ts +0 -196
- package/src/utils/assertions.ts +0 -90
- package/src/utils/bigInt.ts +0 -186
- package/src/utils/bytes.bench.ts +0 -43
- package/src/utils/bytes.ts +0 -34
- package/src/utils/index.ts +0 -6
- package/src/utils/object.ts +0 -28
- package/src/utils/utxo.ts +0 -183
package/src/utils/array.ts
DELETED
@@ -1,196 +0,0 @@
|
|
1
|
-
import { SortingDirection, SortingSelector } from "../types";
|
2
|
-
import { assert, isEmpty } from "./assertions";
|
3
|
-
|
4
|
-
type ObjectSelector<T> = (item: T) => T[keyof T];
|
5
|
-
|
6
|
-
export function first(array: undefined): undefined;
|
7
|
-
export function first<T>(array: ArrayLike<T>): T;
|
8
|
-
export function first<T>(array: ArrayLike<T> | undefined): T | number | undefined {
|
9
|
-
if (!array) return undefined;
|
10
|
-
assert(array.length > 0, "Empty array.");
|
11
|
-
|
12
|
-
return array[0];
|
13
|
-
}
|
14
|
-
|
15
|
-
export function last(array: undefined): undefined;
|
16
|
-
export function last<T>(array: ArrayLike<T>): T;
|
17
|
-
export function last<T>(array: ArrayLike<T> | undefined): T | undefined {
|
18
|
-
if (!array) return undefined;
|
19
|
-
assert(array.length > 0, "Empty array.");
|
20
|
-
|
21
|
-
return at(array, -1);
|
22
|
-
}
|
23
|
-
|
24
|
-
export function at(array: undefined, index: number): undefined;
|
25
|
-
export function at<T>(array: ArrayLike<T>, index: number): T;
|
26
|
-
export function at<T>(array: ArrayLike<T> | undefined, index: number): T | undefined {
|
27
|
-
const len = array?.length;
|
28
|
-
if (!len) return undefined;
|
29
|
-
|
30
|
-
if (index < 0) {
|
31
|
-
index += len;
|
32
|
-
}
|
33
|
-
|
34
|
-
return array[index];
|
35
|
-
}
|
36
|
-
|
37
|
-
/**
|
38
|
-
* Check for duplicate elements using the equality operator
|
39
|
-
*/
|
40
|
-
export function hasDuplicates<T>(array: T[]): boolean {
|
41
|
-
return array.some((item, index) => {
|
42
|
-
return array.indexOf(item) !== index;
|
43
|
-
});
|
44
|
-
}
|
45
|
-
|
46
|
-
/**
|
47
|
-
* Check for duplicate keys in complex elements
|
48
|
-
*/
|
49
|
-
export function hasDuplicatesBy<T>(array: T[], selector: ObjectSelector<T>): boolean {
|
50
|
-
return array.some((item, index) => {
|
51
|
-
return array.findIndex((x) => selector(x) === selector(item)) !== index;
|
52
|
-
});
|
53
|
-
}
|
54
|
-
|
55
|
-
export function chunk<T>(array: T[], size: number): T[][] {
|
56
|
-
if (array.length <= size) {
|
57
|
-
return [array];
|
58
|
-
}
|
59
|
-
|
60
|
-
const chunks: T[][] = [];
|
61
|
-
for (let i = 0; i < array.length; i += size) {
|
62
|
-
chunks.push(array.slice(i, i + size));
|
63
|
-
}
|
64
|
-
|
65
|
-
return chunks;
|
66
|
-
}
|
67
|
-
|
68
|
-
export function orderBy<T>(
|
69
|
-
array: T[],
|
70
|
-
iteratee: SortingSelector<T>,
|
71
|
-
order: SortingDirection = "asc"
|
72
|
-
): T[] {
|
73
|
-
return [...array].sort((a: T, b: T) => {
|
74
|
-
if (iteratee(a) > iteratee(b)) {
|
75
|
-
return order === "asc" ? 1 : -1;
|
76
|
-
} else if (iteratee(a) < iteratee(b)) {
|
77
|
-
return order === "asc" ? -1 : 1;
|
78
|
-
} else {
|
79
|
-
return 0;
|
80
|
-
}
|
81
|
-
});
|
82
|
-
}
|
83
|
-
|
84
|
-
export function areEqual<T>(array1: ArrayLike<T>, array2: ArrayLike<T>): boolean {
|
85
|
-
if (array1 === array2) {
|
86
|
-
return true;
|
87
|
-
}
|
88
|
-
|
89
|
-
if (array1.length != array2.length) {
|
90
|
-
return false;
|
91
|
-
}
|
92
|
-
|
93
|
-
for (let i = 0; i < array1.length; i++) {
|
94
|
-
if (array1[i] !== array2[i]) {
|
95
|
-
return false;
|
96
|
-
}
|
97
|
-
}
|
98
|
-
|
99
|
-
return true;
|
100
|
-
}
|
101
|
-
|
102
|
-
export function areEqualBy<T>(
|
103
|
-
array1: ArrayLike<T>,
|
104
|
-
array2: ArrayLike<T>,
|
105
|
-
selector: ObjectSelector<T>
|
106
|
-
): boolean {
|
107
|
-
if (array1 === array2) {
|
108
|
-
return true;
|
109
|
-
}
|
110
|
-
|
111
|
-
if (array1.length != array2.length) {
|
112
|
-
return false;
|
113
|
-
}
|
114
|
-
|
115
|
-
for (let i = 0; i < array1.length; i++) {
|
116
|
-
if (selector(array1[i]) !== selector(array2[i])) {
|
117
|
-
return false;
|
118
|
-
}
|
119
|
-
}
|
120
|
-
|
121
|
-
return true;
|
122
|
-
}
|
123
|
-
|
124
|
-
export function startsWith<T>(array: ArrayLike<T>, target: ArrayLike<T>): boolean {
|
125
|
-
if (array === target) {
|
126
|
-
return true;
|
127
|
-
}
|
128
|
-
|
129
|
-
if (target.length > array.length) {
|
130
|
-
return false;
|
131
|
-
}
|
132
|
-
|
133
|
-
for (let i = 0; i < target.length; i++) {
|
134
|
-
if (target[i] !== array[i]) {
|
135
|
-
return false;
|
136
|
-
}
|
137
|
-
}
|
138
|
-
|
139
|
-
return true;
|
140
|
-
}
|
141
|
-
|
142
|
-
export function endsWith<T>(array: ArrayLike<T>, target: ArrayLike<T>): boolean {
|
143
|
-
if (array === target) {
|
144
|
-
return true;
|
145
|
-
}
|
146
|
-
|
147
|
-
if (target.length > array.length) {
|
148
|
-
return false;
|
149
|
-
}
|
150
|
-
|
151
|
-
const offset = array.length - target.length;
|
152
|
-
|
153
|
-
for (let i = target.length - 1; i >= 0; i--) {
|
154
|
-
if (target[i] !== array[i + offset]) {
|
155
|
-
return false;
|
156
|
-
}
|
157
|
-
}
|
158
|
-
|
159
|
-
return true;
|
160
|
-
}
|
161
|
-
|
162
|
-
export function uniq<T>(array: Array<T>): Array<T> {
|
163
|
-
if (isEmpty(array)) {
|
164
|
-
return array;
|
165
|
-
}
|
166
|
-
|
167
|
-
return Array.from(new Set(array));
|
168
|
-
}
|
169
|
-
|
170
|
-
export function uniqBy<T>(
|
171
|
-
array: Array<T>,
|
172
|
-
selector: ObjectSelector<T>,
|
173
|
-
selection: "keep-first" | "keep-last" = "keep-first"
|
174
|
-
): Array<T> {
|
175
|
-
if (isEmpty(array)) {
|
176
|
-
return array;
|
177
|
-
}
|
178
|
-
|
179
|
-
return Array.from(
|
180
|
-
array
|
181
|
-
.reduce((map, e) => {
|
182
|
-
const key = selector(e);
|
183
|
-
|
184
|
-
if (selection === "keep-first" && map.has(key)) {
|
185
|
-
return map;
|
186
|
-
}
|
187
|
-
|
188
|
-
return map.set(key, e);
|
189
|
-
}, new Map())
|
190
|
-
.values()
|
191
|
-
);
|
192
|
-
}
|
193
|
-
|
194
|
-
export function depthOf(array: unknown | unknown[]): number {
|
195
|
-
return Array.isArray(array) ? 1 + Math.max(0, ...array.map(depthOf)) : 0;
|
196
|
-
}
|
package/src/utils/assertions.ts
DELETED
@@ -1,90 +0,0 @@
|
|
1
|
-
export type AssertErrorMessageInput = string | Error | (() => string);
|
2
|
-
|
3
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
4
|
-
type Constructable = Function;
|
5
|
-
|
6
|
-
type JSPrimitiveTypes =
|
7
|
-
| "string"
|
8
|
-
| "number"
|
9
|
-
| "bigint"
|
10
|
-
| "boolean"
|
11
|
-
| "symbol"
|
12
|
-
| "undefined"
|
13
|
-
| "object"
|
14
|
-
| "function";
|
15
|
-
|
16
|
-
export function assert(condition: boolean, error: AssertErrorMessageInput): asserts condition {
|
17
|
-
if (condition) return;
|
18
|
-
|
19
|
-
let err: Error | undefined = undefined;
|
20
|
-
switch (typeof error) {
|
21
|
-
case "string":
|
22
|
-
err = new Error(error);
|
23
|
-
break;
|
24
|
-
case "function":
|
25
|
-
err = new Error(error());
|
26
|
-
break;
|
27
|
-
default:
|
28
|
-
err = error;
|
29
|
-
}
|
30
|
-
|
31
|
-
throw err;
|
32
|
-
}
|
33
|
-
|
34
|
-
export function assertTypeOf<T>(obj: T, expected: JSPrimitiveTypes): asserts obj {
|
35
|
-
const type = typeof obj;
|
36
|
-
|
37
|
-
if (type !== expected) {
|
38
|
-
throw new Error(`Expected an object of type '${expected}', got '${type}'.`);
|
39
|
-
}
|
40
|
-
}
|
41
|
-
|
42
|
-
const toString = (value: unknown) => Object.prototype.toString.call(value);
|
43
|
-
function getTypeName(value: unknown): string {
|
44
|
-
if (value === null) return "null";
|
45
|
-
const type = typeof value;
|
46
|
-
|
47
|
-
return type === "object" || type === "function" ? toString(value).slice(8, -1) : type;
|
48
|
-
}
|
49
|
-
|
50
|
-
export function assertInstanceOf<T>(obj: T, expected: Constructable): asserts obj {
|
51
|
-
const condition = obj instanceof expected;
|
52
|
-
|
53
|
-
if (!condition) {
|
54
|
-
throw new Error(`Expected an instance of '${expected.name}', got '${getTypeName(obj)}'.`);
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
export function isEmpty<T extends object>(obj?: T): obj is undefined;
|
59
|
-
export function isEmpty<T>(array?: T[]): array is undefined;
|
60
|
-
export function isEmpty<T>(obj?: T[] | object): obj is undefined {
|
61
|
-
if (!obj) return true;
|
62
|
-
|
63
|
-
return Array.isArray(obj) ? obj.length === 0 : Object.keys(obj).length === 0;
|
64
|
-
}
|
65
|
-
|
66
|
-
export function some<T extends object>(obj?: T): obj is T;
|
67
|
-
export function some<T>(array?: T[]): array is T[];
|
68
|
-
export function some<T>(obj?: T[] | object): boolean {
|
69
|
-
return !isEmpty(obj);
|
70
|
-
}
|
71
|
-
|
72
|
-
export function isTruthy<T>(value?: T): value is NonNullable<T> {
|
73
|
-
return !!value;
|
74
|
-
}
|
75
|
-
|
76
|
-
export function isFalsy<T>(value?: T): value is undefined {
|
77
|
-
return !value;
|
78
|
-
}
|
79
|
-
|
80
|
-
export function isUndefined(v: unknown): v is undefined {
|
81
|
-
return v === undefined || v === null || Number.isNaN(v);
|
82
|
-
}
|
83
|
-
|
84
|
-
export function isDefined<T>(v: T | undefined): v is T {
|
85
|
-
return !isUndefined(v);
|
86
|
-
}
|
87
|
-
|
88
|
-
export function hasKey(o: unknown, key: PropertyKey): boolean {
|
89
|
-
return Object.prototype.hasOwnProperty.call(o, key);
|
90
|
-
}
|
package/src/utils/bigInt.ts
DELETED
@@ -1,186 +0,0 @@
|
|
1
|
-
import { Amount } from "../types";
|
2
|
-
import { first } from "./array";
|
3
|
-
import { isEmpty, isUndefined } from "./assertions";
|
4
|
-
|
5
|
-
type NumberLike = string | number | bigint | boolean;
|
6
|
-
|
7
|
-
export const _0n = BigInt(0);
|
8
|
-
export const _1n = BigInt(1);
|
9
|
-
export const _7n = BigInt(7);
|
10
|
-
export const _10n = BigInt(10);
|
11
|
-
export const _63n = BigInt(63);
|
12
|
-
export const _127n = BigInt(127);
|
13
|
-
export const _128n = BigInt(128);
|
14
|
-
|
15
|
-
export function ensureBigInt(number: NumberLike): bigint {
|
16
|
-
return typeof number === "bigint" ? number : BigInt(number);
|
17
|
-
}
|
18
|
-
|
19
|
-
type ParsingOptions = {
|
20
|
-
/**
|
21
|
-
* Number of decimals.
|
22
|
-
*/
|
23
|
-
decimals?: number;
|
24
|
-
|
25
|
-
/**
|
26
|
-
* Thousand mark char.
|
27
|
-
* Default: `.`
|
28
|
-
*/
|
29
|
-
decimalMark?: string;
|
30
|
-
};
|
31
|
-
|
32
|
-
export function undecimalize(decimalStr: string, options?: ParsingOptions | number): bigint {
|
33
|
-
if (!decimalStr) {
|
34
|
-
return _0n;
|
35
|
-
}
|
36
|
-
|
37
|
-
options = typeof options == "number" ? { decimals: options } : options;
|
38
|
-
if (isUndefined(options)) {
|
39
|
-
options = {};
|
40
|
-
}
|
41
|
-
|
42
|
-
options.decimals = options.decimals || 0;
|
43
|
-
options.decimalMark = options.decimalMark || ".";
|
44
|
-
|
45
|
-
const fragments = decimalStr.split(options.decimalMark);
|
46
|
-
if (fragments.length > 2) {
|
47
|
-
throw new Error("Invalid numeric string.");
|
48
|
-
}
|
49
|
-
|
50
|
-
let [integer, decimal] = fragments;
|
51
|
-
integer = _removeLeadingZeros(integer);
|
52
|
-
const negative = integer.startsWith("-") ? "-" : "";
|
53
|
-
|
54
|
-
if (!decimal) {
|
55
|
-
decimal = "0".repeat(options.decimals);
|
56
|
-
} else if (decimal.length < options.decimals) {
|
57
|
-
decimal = decimal.padEnd(options.decimals, "0");
|
58
|
-
}
|
59
|
-
|
60
|
-
return BigInt(negative + _stripNonDigits(integer + decimal));
|
61
|
-
}
|
62
|
-
|
63
|
-
function _stripNonDigits(value: string): string {
|
64
|
-
return value.replace(/\D/g, "");
|
65
|
-
}
|
66
|
-
|
67
|
-
type FormattingOptions = {
|
68
|
-
/**
|
69
|
-
* Number of decimals.
|
70
|
-
*/
|
71
|
-
decimals: number;
|
72
|
-
|
73
|
-
/**
|
74
|
-
* Thousand mark char.
|
75
|
-
*/
|
76
|
-
thousandMark?: string;
|
77
|
-
|
78
|
-
/**
|
79
|
-
* Decimal mark char.
|
80
|
-
* Default: `.`
|
81
|
-
*/
|
82
|
-
decimalMark?: string;
|
83
|
-
};
|
84
|
-
|
85
|
-
export function decimalize(value: Amount, options?: FormattingOptions | number): string {
|
86
|
-
value = ensureBigInt(value);
|
87
|
-
if (!options) {
|
88
|
-
return value.toString();
|
89
|
-
}
|
90
|
-
|
91
|
-
options = typeof options == "number" ? { decimals: options } : options;
|
92
|
-
options.decimals = options.decimals || 0;
|
93
|
-
options.decimalMark = options.decimalMark || ".";
|
94
|
-
|
95
|
-
const pow = _10n ** BigInt(options.decimals);
|
96
|
-
const integer = value / pow;
|
97
|
-
const decimal = value - integer * pow;
|
98
|
-
|
99
|
-
return _buildFormattedDecimal(integer.toString(10), decimal.toString(10), options);
|
100
|
-
}
|
101
|
-
|
102
|
-
export function percent(value: bigint, percentage: bigint, precision = 2n) {
|
103
|
-
return (value * percentage) / 10n ** precision;
|
104
|
-
}
|
105
|
-
|
106
|
-
function _buildFormattedDecimal(
|
107
|
-
integer: string,
|
108
|
-
decimal: string,
|
109
|
-
options: FormattingOptions
|
110
|
-
): string {
|
111
|
-
const integerPart = _addThousandMarks(integer, options.thousandMark);
|
112
|
-
const decimalPart = _stripTrailingZeros(decimal.padStart(options.decimals, "0"));
|
113
|
-
|
114
|
-
if (decimalPart) {
|
115
|
-
return `${integerPart}${options.decimalMark}${decimalPart}`;
|
116
|
-
} else {
|
117
|
-
return integerPart;
|
118
|
-
}
|
119
|
-
}
|
120
|
-
|
121
|
-
function _addThousandMarks(value: string, mark?: string): string {
|
122
|
-
if (!mark) {
|
123
|
-
return value;
|
124
|
-
}
|
125
|
-
|
126
|
-
return value.replace(/\B(?=(\d{3})+(?!\d))/g, mark);
|
127
|
-
}
|
128
|
-
|
129
|
-
function _stripTrailingZeros(value: string): string {
|
130
|
-
if (!value.endsWith("0")) {
|
131
|
-
return value;
|
132
|
-
}
|
133
|
-
|
134
|
-
return value.replace(/\.?0+$/, "");
|
135
|
-
}
|
136
|
-
|
137
|
-
function _removeLeadingZeros(value: string): string {
|
138
|
-
if (!value.startsWith("0")) {
|
139
|
-
return value;
|
140
|
-
}
|
141
|
-
|
142
|
-
return value.replace(/^0+\.?/, "");
|
143
|
-
}
|
144
|
-
|
145
|
-
export function sumBy<T>(
|
146
|
-
collection: readonly T[],
|
147
|
-
iteratee: (value: T) => bigint,
|
148
|
-
condition?: (value: T) => boolean
|
149
|
-
): bigint {
|
150
|
-
let acc = _0n;
|
151
|
-
if (isEmpty(collection)) {
|
152
|
-
return acc;
|
153
|
-
}
|
154
|
-
|
155
|
-
for (const item of collection) {
|
156
|
-
if (isUndefined(condition) || condition(item)) {
|
157
|
-
acc += iteratee(item);
|
158
|
-
}
|
159
|
-
}
|
160
|
-
|
161
|
-
return acc;
|
162
|
-
}
|
163
|
-
|
164
|
-
export function min<T extends bigint | number>(...numbers: T[]): T {
|
165
|
-
let min = first(numbers);
|
166
|
-
|
167
|
-
for (const num of numbers) {
|
168
|
-
if (num < min) {
|
169
|
-
min = num;
|
170
|
-
}
|
171
|
-
}
|
172
|
-
|
173
|
-
return min;
|
174
|
-
}
|
175
|
-
|
176
|
-
export function max<T extends bigint | number>(...numbers: T[]): T {
|
177
|
-
let max = first(numbers);
|
178
|
-
|
179
|
-
for (const num of numbers) {
|
180
|
-
if (num > max) {
|
181
|
-
max = num;
|
182
|
-
}
|
183
|
-
}
|
184
|
-
|
185
|
-
return max;
|
186
|
-
}
|
package/src/utils/bytes.bench.ts
DELETED
@@ -1,43 +0,0 @@
|
|
1
|
-
import { bench, describe } from "vitest";
|
2
|
-
import { isHex } from "./bytes";
|
3
|
-
|
4
|
-
const HEX_PATTERN = /^[0-9A-Fa-f]+$/s;
|
5
|
-
export function isHexRegex(value?: string) {
|
6
|
-
if (!value || value.length % 2) return false;
|
7
|
-
|
8
|
-
return HEX_PATTERN.test(value);
|
9
|
-
}
|
10
|
-
|
11
|
-
const HEX_CHARSET = new Set("0123456789abcdefABCDEF");
|
12
|
-
function isHexChar(value: string) {
|
13
|
-
if (!value || value.length % 2) return false;
|
14
|
-
|
15
|
-
const valueSet = new Set(Array.from(value));
|
16
|
-
for (const c of valueSet) {
|
17
|
-
if (!HEX_CHARSET.has(c)) {
|
18
|
-
return false;
|
19
|
-
}
|
20
|
-
}
|
21
|
-
|
22
|
-
return true;
|
23
|
-
}
|
24
|
-
|
25
|
-
describe("Hex string checking", () => {
|
26
|
-
const validHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7";
|
27
|
-
const invalidHex = "0008cd026dc059d64a50d0dbf07755c2c4a4e557e3df8afa7141868b3ab200643d437ee7tt";
|
28
|
-
|
29
|
-
bench("Using charset", () => {
|
30
|
-
isHexChar(validHex);
|
31
|
-
isHexChar(invalidHex);
|
32
|
-
});
|
33
|
-
|
34
|
-
bench("Using regex", () => {
|
35
|
-
isHexRegex(validHex);
|
36
|
-
isHexRegex(invalidHex);
|
37
|
-
});
|
38
|
-
|
39
|
-
bench("Using number constructor", () => {
|
40
|
-
isHex(validHex);
|
41
|
-
isHex(invalidHex);
|
42
|
-
});
|
43
|
-
});
|
package/src/utils/bytes.ts
DELETED
@@ -1,34 +0,0 @@
|
|
1
|
-
import { assertInstanceOf } from ".";
|
2
|
-
|
3
|
-
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
|
4
|
-
const r = new Uint8Array(arrays.reduce((sum, a) => sum + a.length, 0));
|
5
|
-
|
6
|
-
let pad = 0;
|
7
|
-
for (const bytes of arrays) {
|
8
|
-
assertInstanceOf(bytes, Uint8Array);
|
9
|
-
|
10
|
-
r.set(bytes, pad);
|
11
|
-
pad += bytes.length;
|
12
|
-
}
|
13
|
-
|
14
|
-
return r;
|
15
|
-
}
|
16
|
-
|
17
|
-
export function isHex(value?: string) {
|
18
|
-
if (!value || value.length % 2) return false;
|
19
|
-
|
20
|
-
if (!value.startsWith("0x")) {
|
21
|
-
value = "0x" + value;
|
22
|
-
}
|
23
|
-
|
24
|
-
return !isNaN(Number(value));
|
25
|
-
}
|
26
|
-
|
27
|
-
/**
|
28
|
-
* Get hex string size in bytes
|
29
|
-
* @param hex
|
30
|
-
* @returns the byte size if the hex string
|
31
|
-
*/
|
32
|
-
export function byteSizeOf(hex: string): number {
|
33
|
-
return hex.length / 2;
|
34
|
-
}
|
package/src/utils/index.ts
DELETED
package/src/utils/object.ts
DELETED
@@ -1,28 +0,0 @@
|
|
1
|
-
import { isEmpty, isUndefined } from "./assertions";
|
2
|
-
|
3
|
-
export function clearUndefined(value: Record<string, unknown>) {
|
4
|
-
const result: Record<string, unknown> = {};
|
5
|
-
for (const key in value) {
|
6
|
-
const val = value[key];
|
7
|
-
if (!isUndefined(val)) {
|
8
|
-
result[key] = val;
|
9
|
-
}
|
10
|
-
}
|
11
|
-
|
12
|
-
return result;
|
13
|
-
}
|
14
|
-
|
15
|
-
export function ensureDefaults<T extends Partial<R>, R extends object = Required<T>>(
|
16
|
-
options: T | undefined,
|
17
|
-
defaults: R
|
18
|
-
): R & T {
|
19
|
-
if (isEmpty(options)) return defaults as R & T;
|
20
|
-
|
21
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
22
|
-
const newObj: any = { ...defaults, ...options };
|
23
|
-
for (const key in newObj) {
|
24
|
-
newObj[key as keyof object] = options[key as keyof R] ?? defaults[key as keyof R];
|
25
|
-
}
|
26
|
-
|
27
|
-
return newObj;
|
28
|
-
}
|