@bitbeater/ecma-utils 2.7.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 +23 -0
- package/dist/bytes.d.ts +65 -0
- package/dist/bytes.d.ts.map +1 -0
- package/dist/bytes.js +68 -0
- package/dist/bytes.js.map +1 -0
- package/dist/collection/CircularArray.d.ts +98 -0
- package/dist/collection/CircularArray.d.ts.map +1 -0
- package/dist/collection/CircularArray.js +134 -0
- package/dist/collection/CircularArray.js.map +1 -0
- package/dist/collection/EvictingDequeue.d.ts +22 -0
- package/dist/collection/EvictingDequeue.d.ts.map +1 -0
- package/dist/collection/EvictingDequeue.js +57 -0
- package/dist/collection/EvictingDequeue.js.map +1 -0
- package/dist/collection/collection.d.ts +4 -0
- package/dist/collection/collection.d.ts.map +1 -0
- package/dist/collection/collection.js +23 -0
- package/dist/collection/collection.js.map +1 -0
- package/dist/collection/collectionOperations.d.ts +125 -0
- package/dist/collection/collectionOperations.d.ts.map +1 -0
- package/dist/collection/collectionOperations.js +193 -0
- package/dist/collection/collectionOperations.js.map +1 -0
- package/dist/cons.d.ts +34 -0
- package/dist/cons.d.ts.map +1 -0
- package/dist/cons.js +49 -0
- package/dist/cons.js.map +1 -0
- package/dist/consts.d.ts +2 -0
- package/dist/consts.d.ts.map +1 -0
- package/dist/consts.js +5 -0
- package/dist/consts.js.map +1 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/logger.d.ts +95 -0
- package/dist/logger.d.ts.map +1 -0
- package/dist/logger.js +172 -0
- package/dist/logger.js.map +1 -0
- package/dist/math.d.ts +183 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +263 -0
- package/dist/math.js.map +1 -0
- package/dist/net/http.d.ts +22 -0
- package/dist/net/http.d.ts.map +1 -0
- package/dist/net/http.js +74 -0
- package/dist/net/http.js.map +1 -0
- package/dist/object.d.ts +98 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +127 -0
- package/dist/object.js.map +1 -0
- package/dist/path/path.d.ts +2 -0
- package/dist/path/path.d.ts.map +1 -0
- package/dist/path/path.js +8 -0
- package/dist/path/path.js.map +1 -0
- package/dist/promises.d.ts +3 -0
- package/dist/promises.d.ts.map +1 -0
- package/dist/promises.js +13 -0
- package/dist/promises.js.map +1 -0
- package/dist/random.d.ts +46 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.js +76 -0
- package/dist/random.js.map +1 -0
- package/dist/revivers.d.ts +25 -0
- package/dist/revivers.d.ts.map +1 -0
- package/dist/revivers.js +34 -0
- package/dist/revivers.js.map +1 -0
- package/dist/strings.d.ts +16 -0
- package/dist/strings.d.ts.map +1 -0
- package/dist/strings.js +34 -0
- package/dist/strings.js.map +1 -0
- package/dist/time/time.d.ts +189 -0
- package/dist/time/time.d.ts.map +1 -0
- package/dist/time/time.js +315 -0
- package/dist/time/time.js.map +1 -0
- package/dist/time/timer.d.ts +93 -0
- package/dist/time/timer.d.ts.map +1 -0
- package/dist/time/timer.js +182 -0
- package/dist/time/timer.js.map +1 -0
- package/dist/time.d.ts +189 -0
- package/dist/time.d.ts.map +1 -0
- package/dist/time.js +308 -0
- package/dist/time.js.map +1 -0
- package/dist/utils/task_duration_tracker.d.ts +102 -0
- package/dist/utils/task_duration_tracker.d.ts.map +1 -0
- package/dist/utils/task_duration_tracker.js +144 -0
- package/dist/utils/task_duration_tracker.js.map +1 -0
- package/dist/utils/utils.d.ts +24 -0
- package/dist/utils/utils.d.ts.map +1 -0
- package/dist/utils/utils.js +47 -0
- package/dist/utils/utils.js.map +1 -0
- package/package.json +68 -0
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @param n number to be rounded round
|
|
4
|
+
* @param positions decimal positions depth to round
|
|
5
|
+
* @returns the rounded number
|
|
6
|
+
*/
|
|
7
|
+
export declare function round(n: number, positions?: number): number;
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param start start value
|
|
11
|
+
* @param end end value
|
|
12
|
+
* @returns the percent difference
|
|
13
|
+
*
|
|
14
|
+
* ## Example
|
|
15
|
+
* ```ts
|
|
16
|
+
* percDiff(100,90)
|
|
17
|
+
* // output: -10
|
|
18
|
+
*
|
|
19
|
+
* percDiff(90,100)
|
|
20
|
+
* // output: 10
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
export declare function percDiff(start: number, end: number): number;
|
|
24
|
+
/**
|
|
25
|
+
*
|
|
26
|
+
* @param value the value tu be multiplied by the given percent value
|
|
27
|
+
* @param percent percent value
|
|
28
|
+
* @returns the value multiplied by percent
|
|
29
|
+
*
|
|
30
|
+
* ## Example
|
|
31
|
+
* ```ts
|
|
32
|
+
* calculatePercent(100,10)
|
|
33
|
+
* // output: 110
|
|
34
|
+
*
|
|
35
|
+
* calculatePercent(100,-10)
|
|
36
|
+
* // output: 90
|
|
37
|
+
* ```
|
|
38
|
+
*/
|
|
39
|
+
export declare function calculatePercent(value: number, percent: number): number;
|
|
40
|
+
/**
|
|
41
|
+
* ```
|
|
42
|
+
* Weighted Mean = Σwx/Σw
|
|
43
|
+
* ```
|
|
44
|
+
*
|
|
45
|
+
* The weighted arithmetic mean is similar to an ordinary arithmetic mean,
|
|
46
|
+
* except that instead of each of the data points contributing equally to the final average,
|
|
47
|
+
* some data points contribute more than others.
|
|
48
|
+
* If all the weights are equal, then the weighted mean is the same as the arithmetic mean.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```js
|
|
52
|
+
* weightedArithmeticMean([[251,0.1], [360, 0.5], [210, 0.7]]);
|
|
53
|
+
* // => 270.8461538461539
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* @link https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
|
|
57
|
+
*/
|
|
58
|
+
export declare function weightedArithmeticMean(value: number[], weight: number[]): number;
|
|
59
|
+
/**
|
|
60
|
+
* gets the precision of a number (how many decimal digits)
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```js
|
|
64
|
+
* getPrecision(2.1) //1
|
|
65
|
+
* getPrecision(9.65) //2
|
|
66
|
+
* getPrecision(0.479) //3
|
|
67
|
+
* getPrecision(1) //0
|
|
68
|
+
* getPrecision(0) //0
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
export declare function getPrecision(n?: number): number;
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* returns the nearest multiple of a number
|
|
75
|
+
* @example
|
|
76
|
+
* ```js
|
|
77
|
+
* getNearestMultiple(17, 5) // 15
|
|
78
|
+
* getNearestMultiple(11, 2) // 12
|
|
79
|
+
* getNearestMultiple(8, 5) // 10
|
|
80
|
+
* getNearestMultiple(5, 2) // 6
|
|
81
|
+
* getNearestMultiple(5, 0) // 0
|
|
82
|
+
* getNearestMultiple(0.5, 0.2) // 0.4
|
|
83
|
+
* ```
|
|
84
|
+
*/
|
|
85
|
+
export declare function getNearestMultiple(n: number, multiple: number): number;
|
|
86
|
+
/**
|
|
87
|
+
*
|
|
88
|
+
* returns the nearest low multiple of a number, the result will always be less or equal to input number
|
|
89
|
+
* @example
|
|
90
|
+
* ```js
|
|
91
|
+
* getNearestLowMultiple(17, 5) // 15
|
|
92
|
+
* getNearestLowMultiple(11, 2) // 10
|
|
93
|
+
* getNearestLowMultiple(8, 5) // 5
|
|
94
|
+
* getNearestLowMultiple(5, 2) // 4
|
|
95
|
+
* getNearestLowMultiple(5, 0) // 0
|
|
96
|
+
* getNearestLowMultiple(0.5, 0.2) // 0.4
|
|
97
|
+
* getNearestLowMultiple(0.7777777777777, 0.00001) // 0.7777700000000001 cause j♿ can't deal with decimal module
|
|
98
|
+
* getNearestLowMultiple(0.7777777777777, 0.00001, 5) // 0.77777
|
|
99
|
+
*
|
|
100
|
+
* ```
|
|
101
|
+
*/
|
|
102
|
+
export declare function getNearestLowMultiple(n: number, multiple: number, precision?: number): number;
|
|
103
|
+
/**
|
|
104
|
+
* truncate decimal numbers to decimal places, without rounding
|
|
105
|
+
* @example
|
|
106
|
+
* ```js
|
|
107
|
+
* truncateDecimals(5.469, 2); // => 5.46
|
|
108
|
+
* truncateDecimals(5.461, 2); // => 5.46
|
|
109
|
+
* ```
|
|
110
|
+
*/
|
|
111
|
+
export declare function truncateDecimals(n: number, digits: number): number;
|
|
112
|
+
/**
|
|
113
|
+
*
|
|
114
|
+
* Create a sequence of numbers, from start value to end value with span
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
* ```js
|
|
118
|
+
* numberSequence({start:1, end: 5, span:1}) // [1,2,3,4,5]
|
|
119
|
+
* numberSequence({start:1, end: 10, span:2}) // [1,3,5,7,9]
|
|
120
|
+
* numberSequence({start:1, end: 5, span:2}) // [1,3,5]
|
|
121
|
+
* numberSequence({start:5, end: 1, span:1}) // [5,4,3,2,1]
|
|
122
|
+
* numberSequence({start:1, end: -5, span:1}) // [1,0,-1,-2,-3,-4,-5]
|
|
123
|
+
* numberSequence({start:5, end: -1, span:1}) // [5,4,3,2,1,0,-1]
|
|
124
|
+
* numberSequence({start:0, end: 1, span:0.2}) // [0,0.2,0.4,0.6000000000000001,0.8,1]
|
|
125
|
+
* numberSequence({start:0, end: -1, span:0.2}) // [0,-0.2,-0.4,-0.6000000000000001,-0.8,-1]
|
|
126
|
+
* numberSequence({start:1, end: -1, span:0.3}) // [1,0.7,0.4,0.10000000000000009,-0.2,-0.5,-0.7999999999999998]
|
|
127
|
+
* numberSequence({start:-1, end:-5, span:1}) // [-1,-2,-3,-4,-5]
|
|
128
|
+
* numberSequence({start:-5, end:-1, span:1}) // [-5,-4,-3,-2,-1]
|
|
129
|
+
* numberSequence({start:-1, end:5, span:1}) // [-1,0,1,2,3,4,5]
|
|
130
|
+
* ```
|
|
131
|
+
*/
|
|
132
|
+
export declare function numberSequenceRange(range: {
|
|
133
|
+
start: number;
|
|
134
|
+
end: number;
|
|
135
|
+
span: number;
|
|
136
|
+
}): number[];
|
|
137
|
+
/**
|
|
138
|
+
*
|
|
139
|
+
* Create a sequence of numbers, from start value to end value with span
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* ```js
|
|
143
|
+
* numberSequenceByLength({ start: 0, span: 1, length: 10 }) // (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
144
|
+
* numberSequenceByLength({ start: 0, span: 1, length: 10, direction: '-' }) // (10) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
|
|
145
|
+
* numberSequenceByLength({ start: 0, span: 0.3, length: 10 }) // (10) [0, 0.3, 0.6, 0.8999999999999999, 1.2, 1.5, 1.7999999999999998, 2.1, 2.4, 2.6999999999999997]
|
|
146
|
+
* numberSequenceByLength({ start: 1.5, span: 0.3, length: 10, direction: '-' }) // (10) [1.5, 1.2, 0.9, 0.6000000000000001, 0.30000000000000004, 0, -0.2999999999999998, -0.6000000000000001, -0.8999999999999999, -1.1999999999999997]
|
|
147
|
+
* numberSequenceByLength({ start: -1.5, span: 0.3, length: 10, direction: '+' }) // (10) [-1.5, -1.2, -0.9, -0.6000000000000001, -0.30000000000000004, 0, 0.2999999999999998, 0.6000000000000001, 0.8999999999999999, 1.1999999999999997]
|
|
148
|
+
* ```
|
|
149
|
+
*/
|
|
150
|
+
export declare function numberSequenceByLength(len: {
|
|
151
|
+
start: number;
|
|
152
|
+
span: number;
|
|
153
|
+
length: number;
|
|
154
|
+
direction?: '+' | '-';
|
|
155
|
+
}): number[];
|
|
156
|
+
/**
|
|
157
|
+
*
|
|
158
|
+
* Create a sequence of fibonaci numbers, from start value to end value
|
|
159
|
+
*
|
|
160
|
+
*/
|
|
161
|
+
export declare function fibonacis(start: number, end: number): number[];
|
|
162
|
+
/**
|
|
163
|
+
* @example
|
|
164
|
+
* ```
|
|
165
|
+
* null => 0
|
|
166
|
+
* undefined => 0
|
|
167
|
+
* NaN => 0
|
|
168
|
+
* Infinity => 0
|
|
169
|
+
* -Infinity => 0
|
|
170
|
+
* new Date() => 0
|
|
171
|
+
* '' => 0
|
|
172
|
+
* ' ' => 0
|
|
173
|
+
* '123' => 123
|
|
174
|
+
* '-123' => -123
|
|
175
|
+
* '123.456' => 123
|
|
176
|
+
* 'abc' => 0
|
|
177
|
+
*
|
|
178
|
+
*```
|
|
179
|
+
* @param val any value
|
|
180
|
+
* @returns {number}
|
|
181
|
+
*/
|
|
182
|
+
export declare function parseIntOrZero(val: unknown, radix?: number): number;
|
|
183
|
+
//# sourceMappingURL=math.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,CAAC,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,MAAM,CAGtD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,UAElD;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAEvE;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,sBAAsB,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,EAAE,GAAG,MAAM,CAkBhF;AACD;;;;;;;;;;;GAWG;AACH,wBAAgB,YAAY,CAAC,CAAC,GAAE,MAAU,GAAG,MAAM,CAElD;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAItE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,SAAS,SAAI,GAAG,MAAM,CAIxF;AAED;;;;;;;GAOG;AACH,wBAAgB,gBAAgB,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,UAGzD;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,EAAE,CASjG;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,sBAAsB,CAAC,GAAG,EAAE;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,SAAS,CAAC,EAAE,GAAG,GAAG,GAAG,CAAA;CAAE,GAAG,MAAM,EAAE,CAS5H;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,MAAM,EAAE,CAY9D;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,wBAAgB,cAAc,CAAC,GAAG,EAAE,OAAO,EAAE,KAAK,CAAC,EAAE,MAAM,GAAG,MAAM,CAOnE"}
|
package/dist/math.js
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.round = round;
|
|
4
|
+
exports.percDiff = percDiff;
|
|
5
|
+
exports.calculatePercent = calculatePercent;
|
|
6
|
+
exports.weightedArithmeticMean = weightedArithmeticMean;
|
|
7
|
+
exports.getPrecision = getPrecision;
|
|
8
|
+
exports.getNearestMultiple = getNearestMultiple;
|
|
9
|
+
exports.getNearestLowMultiple = getNearestLowMultiple;
|
|
10
|
+
exports.truncateDecimals = truncateDecimals;
|
|
11
|
+
exports.numberSequenceRange = numberSequenceRange;
|
|
12
|
+
exports.numberSequenceByLength = numberSequenceByLength;
|
|
13
|
+
exports.fibonacis = fibonacis;
|
|
14
|
+
exports.parseIntOrZero = parseIntOrZero;
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param n number to be rounded round
|
|
18
|
+
* @param positions decimal positions depth to round
|
|
19
|
+
* @returns the rounded number
|
|
20
|
+
*/
|
|
21
|
+
function round(n, positions = 0) {
|
|
22
|
+
const exp = Math.pow(10, positions);
|
|
23
|
+
return Math.round(n * exp) / exp;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
*
|
|
27
|
+
* @param start start value
|
|
28
|
+
* @param end end value
|
|
29
|
+
* @returns the percent difference
|
|
30
|
+
*
|
|
31
|
+
* ## Example
|
|
32
|
+
* ```ts
|
|
33
|
+
* percDiff(100,90)
|
|
34
|
+
* // output: -10
|
|
35
|
+
*
|
|
36
|
+
* percDiff(90,100)
|
|
37
|
+
* // output: 10
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
function percDiff(start, end) {
|
|
41
|
+
return (end - start) / (start / 100);
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
*
|
|
45
|
+
* @param value the value tu be multiplied by the given percent value
|
|
46
|
+
* @param percent percent value
|
|
47
|
+
* @returns the value multiplied by percent
|
|
48
|
+
*
|
|
49
|
+
* ## Example
|
|
50
|
+
* ```ts
|
|
51
|
+
* calculatePercent(100,10)
|
|
52
|
+
* // output: 110
|
|
53
|
+
*
|
|
54
|
+
* calculatePercent(100,-10)
|
|
55
|
+
* // output: 90
|
|
56
|
+
* ```
|
|
57
|
+
*/
|
|
58
|
+
function calculatePercent(value, percent) {
|
|
59
|
+
return value + value * (percent / 100);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* ```
|
|
63
|
+
* Weighted Mean = Σwx/Σw
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* The weighted arithmetic mean is similar to an ordinary arithmetic mean,
|
|
67
|
+
* except that instead of each of the data points contributing equally to the final average,
|
|
68
|
+
* some data points contribute more than others.
|
|
69
|
+
* If all the weights are equal, then the weighted mean is the same as the arithmetic mean.
|
|
70
|
+
*
|
|
71
|
+
* @example
|
|
72
|
+
* ```js
|
|
73
|
+
* weightedArithmeticMean([[251,0.1], [360, 0.5], [210, 0.7]]);
|
|
74
|
+
* // => 270.8461538461539
|
|
75
|
+
* ```
|
|
76
|
+
*
|
|
77
|
+
* @link https://en.wikipedia.org/wiki/Weighted_arithmetic_mean
|
|
78
|
+
*/
|
|
79
|
+
function weightedArithmeticMean(value, weight) {
|
|
80
|
+
/**
|
|
81
|
+
*
|
|
82
|
+
* I tried to optimize the performance to the maximum, to do this:
|
|
83
|
+
* - I used the var because it is slightly faster than the let
|
|
84
|
+
* - the for loop also in this case is slightly faster than the while, and much faster than the methods of the array reduce or the forEach
|
|
85
|
+
* - Looping the array backwards is slightly faster than looping it from the start: https://stackoverflow.com/questions/8689573/why-is-iterating-through-an-array-backwards-faster-than-forwards
|
|
86
|
+
*/
|
|
87
|
+
var totWeightedValue = 0;
|
|
88
|
+
var totWeight = 0;
|
|
89
|
+
for (var i = value.length - 1; i >= 0; i--) {
|
|
90
|
+
totWeightedValue += value[i] * weight[i];
|
|
91
|
+
totWeight += weight[i];
|
|
92
|
+
}
|
|
93
|
+
return totWeightedValue / totWeight;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* gets the precision of a number (how many decimal digits)
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* ```js
|
|
100
|
+
* getPrecision(2.1) //1
|
|
101
|
+
* getPrecision(9.65) //2
|
|
102
|
+
* getPrecision(0.479) //3
|
|
103
|
+
* getPrecision(1) //0
|
|
104
|
+
* getPrecision(0) //0
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
function getPrecision(n = 0) {
|
|
108
|
+
return n?.toString()?.split('.')?.[1]?.length || 0;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
*
|
|
112
|
+
* returns the nearest multiple of a number
|
|
113
|
+
* @example
|
|
114
|
+
* ```js
|
|
115
|
+
* getNearestMultiple(17, 5) // 15
|
|
116
|
+
* getNearestMultiple(11, 2) // 12
|
|
117
|
+
* getNearestMultiple(8, 5) // 10
|
|
118
|
+
* getNearestMultiple(5, 2) // 6
|
|
119
|
+
* getNearestMultiple(5, 0) // 0
|
|
120
|
+
* getNearestMultiple(0.5, 0.2) // 0.4
|
|
121
|
+
* ```
|
|
122
|
+
*/
|
|
123
|
+
function getNearestMultiple(n, multiple) {
|
|
124
|
+
const log = n % multiple || n;
|
|
125
|
+
if (log >= multiple / 2)
|
|
126
|
+
return n + multiple - log;
|
|
127
|
+
return n - log;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
* returns the nearest low multiple of a number, the result will always be less or equal to input number
|
|
132
|
+
* @example
|
|
133
|
+
* ```js
|
|
134
|
+
* getNearestLowMultiple(17, 5) // 15
|
|
135
|
+
* getNearestLowMultiple(11, 2) // 10
|
|
136
|
+
* getNearestLowMultiple(8, 5) // 5
|
|
137
|
+
* getNearestLowMultiple(5, 2) // 4
|
|
138
|
+
* getNearestLowMultiple(5, 0) // 0
|
|
139
|
+
* getNearestLowMultiple(0.5, 0.2) // 0.4
|
|
140
|
+
* getNearestLowMultiple(0.7777777777777, 0.00001) // 0.7777700000000001 cause j♿ can't deal with decimal module
|
|
141
|
+
* getNearestLowMultiple(0.7777777777777, 0.00001, 5) // 0.77777
|
|
142
|
+
*
|
|
143
|
+
* ```
|
|
144
|
+
*/
|
|
145
|
+
function getNearestLowMultiple(n, multiple, precision = 0) {
|
|
146
|
+
var multiplier = 10 ** precision;
|
|
147
|
+
var integer = n * multiplier;
|
|
148
|
+
return (integer - (integer % (multiple * multiplier))) / multiplier;
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* truncate decimal numbers to decimal places, without rounding
|
|
152
|
+
* @example
|
|
153
|
+
* ```js
|
|
154
|
+
* truncateDecimals(5.469, 2); // => 5.46
|
|
155
|
+
* truncateDecimals(5.461, 2); // => 5.46
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function truncateDecimals(n, digits) {
|
|
159
|
+
const multiplier = Math.pow(10, digits);
|
|
160
|
+
return Math.floor(n * multiplier) / multiplier;
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
*
|
|
164
|
+
* Create a sequence of numbers, from start value to end value with span
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```js
|
|
168
|
+
* numberSequence({start:1, end: 5, span:1}) // [1,2,3,4,5]
|
|
169
|
+
* numberSequence({start:1, end: 10, span:2}) // [1,3,5,7,9]
|
|
170
|
+
* numberSequence({start:1, end: 5, span:2}) // [1,3,5]
|
|
171
|
+
* numberSequence({start:5, end: 1, span:1}) // [5,4,3,2,1]
|
|
172
|
+
* numberSequence({start:1, end: -5, span:1}) // [1,0,-1,-2,-3,-4,-5]
|
|
173
|
+
* numberSequence({start:5, end: -1, span:1}) // [5,4,3,2,1,0,-1]
|
|
174
|
+
* numberSequence({start:0, end: 1, span:0.2}) // [0,0.2,0.4,0.6000000000000001,0.8,1]
|
|
175
|
+
* numberSequence({start:0, end: -1, span:0.2}) // [0,-0.2,-0.4,-0.6000000000000001,-0.8,-1]
|
|
176
|
+
* numberSequence({start:1, end: -1, span:0.3}) // [1,0.7,0.4,0.10000000000000009,-0.2,-0.5,-0.7999999999999998]
|
|
177
|
+
* numberSequence({start:-1, end:-5, span:1}) // [-1,-2,-3,-4,-5]
|
|
178
|
+
* numberSequence({start:-5, end:-1, span:1}) // [-5,-4,-3,-2,-1]
|
|
179
|
+
* numberSequence({start:-1, end:5, span:1}) // [-1,0,1,2,3,4,5]
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
function numberSequenceRange(range) {
|
|
183
|
+
const { start: start, end: end, span } = { ...range };
|
|
184
|
+
const length = Math.floor(Math.abs(start - end) / span);
|
|
185
|
+
const retVal = new Array(length);
|
|
186
|
+
if (start < end)
|
|
187
|
+
for (var i = length + 1; i;)
|
|
188
|
+
retVal[--i] = start + span * i;
|
|
189
|
+
else
|
|
190
|
+
for (var i = length + 1; i;)
|
|
191
|
+
retVal[--i] = start - span * i;
|
|
192
|
+
return retVal;
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
*
|
|
196
|
+
* Create a sequence of numbers, from start value to end value with span
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* ```js
|
|
200
|
+
* numberSequenceByLength({ start: 0, span: 1, length: 10 }) // (10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
201
|
+
* numberSequenceByLength({ start: 0, span: 1, length: 10, direction: '-' }) // (10) [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
|
|
202
|
+
* numberSequenceByLength({ start: 0, span: 0.3, length: 10 }) // (10) [0, 0.3, 0.6, 0.8999999999999999, 1.2, 1.5, 1.7999999999999998, 2.1, 2.4, 2.6999999999999997]
|
|
203
|
+
* numberSequenceByLength({ start: 1.5, span: 0.3, length: 10, direction: '-' }) // (10) [1.5, 1.2, 0.9, 0.6000000000000001, 0.30000000000000004, 0, -0.2999999999999998, -0.6000000000000001, -0.8999999999999999, -1.1999999999999997]
|
|
204
|
+
* numberSequenceByLength({ start: -1.5, span: 0.3, length: 10, direction: '+' }) // (10) [-1.5, -1.2, -0.9, -0.6000000000000001, -0.30000000000000004, 0, 0.2999999999999998, 0.6000000000000001, 0.8999999999999999, 1.1999999999999997]
|
|
205
|
+
* ```
|
|
206
|
+
*/
|
|
207
|
+
function numberSequenceByLength(len) {
|
|
208
|
+
const { start, span, length, direction } = { ...len };
|
|
209
|
+
const retVal = new Array(length);
|
|
210
|
+
if (direction === '-')
|
|
211
|
+
for (var i = 0; i < length; i++)
|
|
212
|
+
retVal[i] = start - span * i;
|
|
213
|
+
else
|
|
214
|
+
for (var i = 0; i < length; i++)
|
|
215
|
+
retVal[i] = start + span * i;
|
|
216
|
+
return retVal;
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
*
|
|
220
|
+
* Create a sequence of fibonaci numbers, from start value to end value
|
|
221
|
+
*
|
|
222
|
+
*/
|
|
223
|
+
function fibonacis(start, end) {
|
|
224
|
+
const retVal = [];
|
|
225
|
+
var lastFib = start || 1;
|
|
226
|
+
var currentFib = start * 2 || 1;
|
|
227
|
+
while (true) {
|
|
228
|
+
if (lastFib > end)
|
|
229
|
+
return retVal;
|
|
230
|
+
const tmpLast = lastFib;
|
|
231
|
+
lastFib = currentFib;
|
|
232
|
+
currentFib = currentFib + tmpLast;
|
|
233
|
+
retVal.push(tmpLast);
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
/**
|
|
237
|
+
* @example
|
|
238
|
+
* ```
|
|
239
|
+
* null => 0
|
|
240
|
+
* undefined => 0
|
|
241
|
+
* NaN => 0
|
|
242
|
+
* Infinity => 0
|
|
243
|
+
* -Infinity => 0
|
|
244
|
+
* new Date() => 0
|
|
245
|
+
* '' => 0
|
|
246
|
+
* ' ' => 0
|
|
247
|
+
* '123' => 123
|
|
248
|
+
* '-123' => -123
|
|
249
|
+
* '123.456' => 123
|
|
250
|
+
* 'abc' => 0
|
|
251
|
+
*
|
|
252
|
+
*```
|
|
253
|
+
* @param val any value
|
|
254
|
+
* @returns {number}
|
|
255
|
+
*/
|
|
256
|
+
function parseIntOrZero(val, radix) {
|
|
257
|
+
//@ts-ignore
|
|
258
|
+
const retVal = Number.parseInt(val, radix);
|
|
259
|
+
if (isNaN(retVal))
|
|
260
|
+
return 0;
|
|
261
|
+
return retVal;
|
|
262
|
+
}
|
|
263
|
+
//# sourceMappingURL=math.js.map
|
package/dist/math.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"math.js","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":";;AAMA,sBAGC;AAiBD,4BAEC;AAiBD,4CAEC;AAoBD,wDAkBC;AAaD,oCAEC;AAeD,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,GAAG;YAAE,OAAO,MAAM,CAAC;QACjC,MAAM,OAAO,GAAG,OAAO,CAAC;QACxB,OAAO,GAAG,UAAU,CAAC;QACrB,UAAU,GAAG,UAAU,GAAG,OAAO,CAAC;QAClC,MAAM,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACtB,CAAC;AACF,CAAC;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,SAAgB,cAAc,CAAC,GAAY,EAAE,KAAc;IAC1D,YAAY;IACZ,MAAM,MAAM,GAAG,MAAM,CAAC,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,CAAC;IAE3C,IAAI,KAAK,CAAC,MAAM,CAAC;QAAE,OAAO,CAAC,CAAC;IAE5B,OAAO,MAAM,CAAC;AACf,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export type RetryCallBack = (error: Error, request: Request | string | URL, response?: Response, retry?: HttpRetryOptions) => void;
|
|
2
|
+
export interface HttpRetryOptions {
|
|
3
|
+
retryCount?: number;
|
|
4
|
+
retryDelay?: number;
|
|
5
|
+
maxRetries?: number;
|
|
6
|
+
onRetry?: RetryCallBack;
|
|
7
|
+
}
|
|
8
|
+
export interface RequestOptions {
|
|
9
|
+
retry?: HttpRetryOptions;
|
|
10
|
+
timeout?: number;
|
|
11
|
+
}
|
|
12
|
+
export type HttpRequest = Request | string | URL;
|
|
13
|
+
export declare function cookieStringToObject(cookie: string): {
|
|
14
|
+
[key: string]: string;
|
|
15
|
+
};
|
|
16
|
+
export declare function cookieObjectToString(cookie: {
|
|
17
|
+
[key: string]: string;
|
|
18
|
+
}): string;
|
|
19
|
+
export declare function cookieArrayToString(cookies: [string, string]): string;
|
|
20
|
+
export declare function http(req: HttpRequest, init?: RequestInit, options?: RequestOptions): Promise<Response>;
|
|
21
|
+
export declare function toURL(httpRequest: HttpRequest): URL;
|
|
22
|
+
//# sourceMappingURL=http.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/net/http.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,aAAa,GAAG,CAAC,KAAK,EAAE,KAAK,EAAE,OAAO,EAAE,OAAO,GAAG,MAAM,GAAG,GAAG,EAAE,QAAQ,CAAC,EAAE,QAAQ,EAAE,KAAK,CAAC,EAAE,gBAAgB,KAAK,IAAI,CAAC;AAEnI,MAAM,WAAW,gBAAgB;IAChC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,EAAE,aAAa,CAAC;CACxB;AACD,MAAM,WAAW,cAAc;IAC9B,KAAK,CAAC,EAAE,gBAAgB,CAAC;IACzB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,MAAM,WAAW,GAAG,OAAO,GAAG,MAAM,GAAG,GAAG,CAAC;AAEjD,wBAAgB,oBAAoB,CAAC,MAAM,EAAE,MAAM,GAAG;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,CAS9E;AAED,wBAAgB,oBAAoB,CAAC,MAAM,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAAE,GAAG,MAAM,CAM9E;AAED,wBAAgB,mBAAmB,CAAC,OAAO,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAMrE;AAQD,wBAAgB,IAAI,CAAC,GAAG,EAAE,WAAW,EAAE,IAAI,GAAE,WAAgB,EAAE,OAAO,CAAC,EAAE,cAAc,GAAG,OAAO,CAAC,QAAQ,CAAC,CAoB1G;AAED,wBAAgB,KAAK,CAAC,WAAW,EAAE,WAAW,GAAG,GAAG,CAGnD"}
|
package/dist/net/http.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cookieStringToObject = cookieStringToObject;
|
|
4
|
+
exports.cookieObjectToString = cookieObjectToString;
|
|
5
|
+
exports.cookieArrayToString = cookieArrayToString;
|
|
6
|
+
exports.http = http;
|
|
7
|
+
exports.toURL = toURL;
|
|
8
|
+
const math_1 = require("../math");
|
|
9
|
+
const promises_1 = require("../promises");
|
|
10
|
+
function cookieStringToObject(cookie) {
|
|
11
|
+
const cookies = {};
|
|
12
|
+
const parts = cookie.split(';');
|
|
13
|
+
for (const part of parts) {
|
|
14
|
+
const equlIndex = part.indexOf('=');
|
|
15
|
+
const [key, value] = [part.slice(0, equlIndex), part.slice(equlIndex + 1)];
|
|
16
|
+
cookies[key.trim()] = value.trim();
|
|
17
|
+
}
|
|
18
|
+
return cookies;
|
|
19
|
+
}
|
|
20
|
+
function cookieObjectToString(cookie) {
|
|
21
|
+
const parts = [];
|
|
22
|
+
for (const key in cookie) {
|
|
23
|
+
parts.push(`${key}=${cookie[key]}`);
|
|
24
|
+
}
|
|
25
|
+
return parts.join('; ');
|
|
26
|
+
}
|
|
27
|
+
function cookieArrayToString(cookies) {
|
|
28
|
+
const parts = [];
|
|
29
|
+
for (const cookie of cookies) {
|
|
30
|
+
parts.push(`${cookie[0]}=${cookie[1]}`);
|
|
31
|
+
}
|
|
32
|
+
return parts.join('; ');
|
|
33
|
+
}
|
|
34
|
+
class HttpError extends Error {
|
|
35
|
+
constructor(response) {
|
|
36
|
+
super(response.statusText);
|
|
37
|
+
this.response = response;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
function http(req, init = {}, options) {
|
|
41
|
+
const { reqTimeOutTimer, controller } = requestTimeout(options);
|
|
42
|
+
if (init.signal)
|
|
43
|
+
controller?.signal?.addEventListener('abort', e => init.signal?.dispatchEvent(e));
|
|
44
|
+
else
|
|
45
|
+
init.signal = controller?.signal;
|
|
46
|
+
return fetch(req, init)
|
|
47
|
+
.finally(() => clearTimeout(reqTimeOutTimer))
|
|
48
|
+
.then((response) => {
|
|
49
|
+
if (response.ok)
|
|
50
|
+
return response;
|
|
51
|
+
throw new HttpError(response);
|
|
52
|
+
})
|
|
53
|
+
.catch((error) => {
|
|
54
|
+
error?.response?.body?.cancel();
|
|
55
|
+
if ((0, math_1.parseIntOrZero)(options?.retry?.retryCount) >= (0, math_1.parseIntOrZero)(options?.retry?.maxRetries) - 1)
|
|
56
|
+
throw error;
|
|
57
|
+
options.retry.retryCount = (0, math_1.parseIntOrZero)(options?.retry?.retryCount);
|
|
58
|
+
options.retry.retryCount++;
|
|
59
|
+
options?.retry?.onRetry?.(error, req, error?.response, options?.retry);
|
|
60
|
+
return (0, promises_1.delay)((0, math_1.parseIntOrZero)(options?.retry?.retryDelay)).then(() => http(req, init, options));
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function toURL(httpRequest) {
|
|
64
|
+
// @ts-ignore
|
|
65
|
+
return new URL(httpRequest?.url || httpRequest.toString());
|
|
66
|
+
}
|
|
67
|
+
function requestTimeout(options) {
|
|
68
|
+
if (!options?.timeout)
|
|
69
|
+
return {};
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
const reqTimeOutTimer = setTimeout(() => controller.abort(), options.timeout);
|
|
72
|
+
return { reqTimeOutTimer, controller };
|
|
73
|
+
}
|
|
74
|
+
//# sourceMappingURL=http.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.js","sourceRoot":"","sources":["../../src/net/http.ts"],"names":[],"mappings":";;AAkBA,oDASC;AAED,oDAMC;AAED,kDAMC;AAQD,oBAoBC;AAED,sBAGC;AA5ED,kCAAyC;AACzC,0CAAoC;AAiBpC,SAAgB,oBAAoB,CAAC,MAAc;IAClD,MAAM,OAAO,GAA8B,EAAE,CAAC;IAC9C,MAAM,KAAK,GAAG,MAAM,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAChC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,MAAM,SAAS,GAAG,IAAI,CAAC,OAAO,CAAC,G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,CAAC,CAAC,CAAC;AACL,CAAC;AAED,SAAgB,KAAK,CAAC,WAAwB;IAC7C,aAAa;IACb,OAAO,IAAI,GAAG,CAAC,WAAW,EAAE,GAAG,IAAI,WAAW,CAAC,QAAQ,EAAE,CAAC,CAAC;AAC5D,CAAC;AAED,SAAS,cAAc,CAAC,OAAwB;IAC/C,IAAI,CAAC,OAAO,EAAE,OAAO;QAAE,OAAO,EAAE,CAAC;IACjC,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAC;IACzC,MAAM,eAAe,GAAG,UAAU,CAAC,GAAG,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,EAAE,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9E,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,CAAC;AACxC,CAAC"}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
export interface PathKeysOptions {
|
|
2
|
+
ignoreFunctions?: boolean;
|
|
3
|
+
unfoldArrays?: boolean;
|
|
4
|
+
omitFirstLevel: boolean;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Returns an array of a given object's keys paths (own and nested key names) in a reccursive manner, iterated in the same order that a normal loop would.
|
|
8
|
+
*
|
|
9
|
+
* @example ```ts
|
|
10
|
+
* const object={
|
|
11
|
+
* str:"hello",
|
|
12
|
+
* nr:1,
|
|
13
|
+
* fn: (a:number,b:number)=> a+b,
|
|
14
|
+
* arr:[1,2,3],
|
|
15
|
+
* nestedObject:{
|
|
16
|
+
* str:"world",
|
|
17
|
+
* nr:2,
|
|
18
|
+
* fn: (a:number,b:number)=> a+b,
|
|
19
|
+
* arr:[4.5],
|
|
20
|
+
* }
|
|
21
|
+
* };
|
|
22
|
+
*
|
|
23
|
+
* getPathKeys(object)
|
|
24
|
+
*
|
|
25
|
+
* // RETURNS:
|
|
26
|
+
* // [ 'str',
|
|
27
|
+
* // 'nr',
|
|
28
|
+
* // 'fn',
|
|
29
|
+
* // 'arr',
|
|
30
|
+
* // 'nestedObject',
|
|
31
|
+
* // 'nestedObject.str',
|
|
32
|
+
* // 'nestedObject.nr',
|
|
33
|
+
* // 'nestedObject.fn',
|
|
34
|
+
* // 'nestedObject.arr' ]
|
|
35
|
+
* ```
|
|
36
|
+
*
|
|
37
|
+
* @example ```ts
|
|
38
|
+
* const object={
|
|
39
|
+
* str:"hello",
|
|
40
|
+
* nestedObject:{
|
|
41
|
+
* str:"world",
|
|
42
|
+
* nr:2,
|
|
43
|
+
* fn: (a:number,b:number)=> a+b,
|
|
44
|
+
* arr:[4.5],
|
|
45
|
+
* }
|
|
46
|
+
* };
|
|
47
|
+
*
|
|
48
|
+
* getPathKeys(object,{omitFirstLevel:true})
|
|
49
|
+
*
|
|
50
|
+
* // RETURNS:
|
|
51
|
+
* // [ 'str',
|
|
52
|
+
* // 'nestedObject.str',
|
|
53
|
+
* // 'nestedObject.nr',
|
|
54
|
+
* // 'nestedObject.fn',
|
|
55
|
+
* // 'nestedObject.arr' ]
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @param obj object from which to derive the paths
|
|
59
|
+
* @param options options object
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
export declare function getPathKeys(obj: any, options?: PathKeysOptions): string[];
|
|
63
|
+
/**
|
|
64
|
+
* Checks if a value is a plain object (not null, not array, not date, not promise, not function)
|
|
65
|
+
*
|
|
66
|
+
* @param v value to check
|
|
67
|
+
* @returns boolean indicating if the value is a plain object
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```ts
|
|
71
|
+
* isPlainObject({}) // => true
|
|
72
|
+
* isPlainObject({ a: 1, b: { c: 2 } }) // => true
|
|
73
|
+
* isPlainObject(() => {}) // => false
|
|
74
|
+
* isPlainObject(null) // => false
|
|
75
|
+
* isPlainObject(undefined) // => false
|
|
76
|
+
* isPlainObject([]) // => false
|
|
77
|
+
* isPlainObject(new Date()) // => false
|
|
78
|
+
* isPlainObject("hello") // => false
|
|
79
|
+
* isPlainObject(123) // => false
|
|
80
|
+
* isPlainObject(true) // => false
|
|
81
|
+
* isPlainObject(Symbol("sym")) // => false
|
|
82
|
+
* isPlainObject(BigInt(123)) // => false
|
|
83
|
+
* isPlainObject(Promise.resolve(123)) // => false
|
|
84
|
+
* ```
|
|
85
|
+
*/
|
|
86
|
+
export declare function isPlainObject(v: unknown): v is Object;
|
|
87
|
+
/**
|
|
88
|
+
* returns the keys of an object as an array, with proper typing.
|
|
89
|
+
* @example
|
|
90
|
+
* ```ts
|
|
91
|
+
* const obj = { a: 1, b: 2, c: 3 };
|
|
92
|
+
* const keys = keysOf(obj); // ['a', 'b', 'c']
|
|
93
|
+
* ```
|
|
94
|
+
* @param o
|
|
95
|
+
* @returns
|
|
96
|
+
*/
|
|
97
|
+
export declare function keysOf<T extends object>(o: T): (keyof T)[];
|
|
98
|
+
//# sourceMappingURL=object.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object.d.ts","sourceRoot":"","sources":["../src/object.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,eAAe;IAC/B,eAAe,CAAC,EAAE,OAAO,CAAC;IAC1B,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB,cAAc,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,WAAW,CAAC,GAAG,EAAE,GAAG,EAAE,OAAO,CAAC,EAAE,eAAe,GAAG,MAAM,EAAE,CA2BzE;AAED;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,OAAO,GAAG,CAAC,IAAI,MAAM,CAErD;AAED;;;;;;;;;GASG;AACH,wBAAgB,MAAM,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAE1D"}
|