@kizmann/pico-js 1.0.13 → 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/README.md +27 -7
- package/dist/pico-js.browser.js +2 -0
- package/dist/pico-js.browser.js.map +1 -0
- package/dist/pico-js.esm.js +2 -0
- package/dist/pico-js.esm.js.map +1 -0
- package/package.json +20 -7
- package/src/dom/DomAttribute.js +374 -0
- package/src/dom/DomBuilder.js +152 -0
- package/src/dom/DomEvent.js +253 -0
- package/src/dom/DomFinder.js +669 -0
- package/src/dom/DomForm.js +57 -0
- package/src/dom/DomGlobal.js +193 -0
- package/src/dom/DomInview.js +332 -0
- package/src/dom/DomMeta.js +66 -0
- package/src/dom/DomObserver.js +57 -0
- package/src/dom/DomRectangle.js +657 -0
- package/src/format/FormatFile.js +54 -0
- package/src/format/FormatOption.js +108 -0
- package/src/format/FormatParam.js +107 -0
- package/src/format/FormatParser.js +156 -0
- package/src/format/FormatUrl.js +75 -0
- package/src/index.browser.js +10 -0
- package/src/index.esm.js +138 -0
- package/src/now/NowDefault.js +533 -0
- package/src/now/NowFormat.js +196 -0
- package/src/now/NowGrid.js +251 -0
- package/src/now/NowHuman.js +118 -0
- package/src/now/NowMatch.js +175 -0
- package/src/now/NowRange.js +70 -0
- package/src/now/NowWalker.js +544 -0
- package/src/tool/scope.js +103 -0
- package/src/utils/Array.js +986 -0
- package/src/utils/Cookie.js +184 -0
- package/src/utils/Data.js +200 -0
- package/src/utils/Dom.js +208 -0
- package/src/utils/Event.js +140 -0
- package/src/utils/Format.js +62 -0
- package/src/utils/Hash.js +164 -0
- package/src/utils/Locale.js +229 -0
- package/src/utils/Mixed.js +887 -0
- package/src/utils/Now.js +234 -0
- package/src/utils/Number.js +238 -0
- package/src/utils/Object.js +655 -0
- package/src/utils/Route.js +67 -0
- package/src/utils/Runner.js +327 -0
- package/src/utils/String.js +618 -0
- package/src/{library/element.js → wip/Element.js} +90 -16
- package/src/{library/map.js → wip/Map.js} +256 -40
- package/types/dom/DomAttribute.d.ts +137 -0
- package/types/dom/DomBuilder.d.ts +67 -0
- package/types/dom/DomEvent.d.ts +103 -0
- package/types/dom/DomFinder.d.ts +321 -0
- package/types/dom/DomForm.d.ts +21 -0
- package/types/dom/DomGlobal.d.ts +79 -0
- package/types/dom/DomInview.d.ts +114 -0
- package/types/dom/DomMeta.d.ts +29 -0
- package/types/dom/DomObserver.d.ts +21 -0
- package/types/dom/DomRectangle.d.ts +270 -0
- package/types/format/FormatFile.d.ts +18 -0
- package/types/format/FormatOption.d.ts +40 -0
- package/types/format/FormatParam.d.ts +39 -0
- package/types/format/FormatParser.d.ts +46 -0
- package/types/format/FormatUrl.d.ts +17 -0
- package/types/index.browser.d.ts +1 -0
- package/types/index.esm.d.ts +52 -0
- package/types/now/NowDefault.d.ts +183 -0
- package/types/now/NowFormat.d.ts +70 -0
- package/types/now/NowGrid.d.ts +107 -0
- package/types/now/NowHuman.d.ts +37 -0
- package/types/now/NowMatch.d.ts +108 -0
- package/types/now/NowRange.d.ts +21 -0
- package/types/now/NowWalker.d.ts +301 -0
- package/types/tool/scope.d.ts +24 -0
- package/types/utils/Array.d.ts +480 -0
- package/types/utils/Cookie.d.ts +60 -0
- package/types/utils/Data.d.ts +91 -0
- package/types/utils/Dom.d.ts +138 -0
- package/types/utils/Event.d.ts +58 -0
- package/types/utils/Format.d.ts +37 -0
- package/types/utils/Hash.d.ts +81 -0
- package/types/utils/Locale.d.ts +115 -0
- package/types/utils/Mixed.d.ts +469 -0
- package/types/utils/Now.d.ts +125 -0
- package/types/utils/Number.d.ts +127 -0
- package/types/utils/Object.d.ts +255 -0
- package/types/utils/Route.d.ts +37 -0
- package/types/utils/Runner.d.ts +139 -0
- package/types/utils/String.d.ts +330 -0
- package/types/wip/Element.d.ts +119 -0
- package/types/wip/Map.d.ts +254 -0
- package/dist/.ignore.js +0 -0
- package/dist/pico-js.js +0 -2
- package/dist/pico-js.js.map +0 -1
- package/src/element/default.js +0 -46
- package/src/element/example.js +0 -58
- package/src/index.js +0 -90
- package/src/library/cookie.js +0 -123
- package/src/library/data.js +0 -111
- package/src/library/event.js +0 -91
- package/src/library/locale.js +0 -84
- package/src/library/queue.js +0 -64
- package/src/library/route.js +0 -28
- package/src/utility/any.js +0 -369
- package/src/utility/array.js +0 -410
- package/src/utility/dom.js +0 -1425
- package/src/utility/now.js +0 -544
- package/src/utility/number.js +0 -128
- package/src/utility/object.js +0 -429
- package/src/utility/string.js +0 -328
- package/types/index.d.ts +0 -77
- package/types/library/cookie.d.ts +0 -10
- package/types/library/data.d.ts +0 -15
- package/types/library/element.d.ts +0 -22
- package/types/library/event.d.ts +0 -13
- package/types/library/locale.d.ts +0 -14
- package/types/library/map.d.ts +0 -43
- package/types/library/queue.d.ts +0 -18
- package/types/library/route.d.ts +0 -11
- package/types/utility/any.d.ts +0 -35
- package/types/utility/array.d.ts +0 -46
- package/types/utility/dom.d.ts +0 -101
- package/types/utility/now.d.ts +0 -79
- package/types/utility/number.d.ts +0 -17
- package/types/utility/object.d.ts +0 -29
- package/types/utility/string.d.ts +0 -26
package/src/utils/Now.js
ADDED
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { Arr, Dom, Mix, Now, Num } from "#src/index.esm.js";
|
|
2
|
+
import { PicoNowDefaultPlugin } from "#src/now/NowDefault.js";
|
|
3
|
+
import { PicoNowFormatPlugin } from "#src/now/NowFormat.js";
|
|
4
|
+
import { PicoNowMatchPlugin } from "#src/now/NowMatch.js";
|
|
5
|
+
import { PicoNowGridPlugin } from "#src/now/NowGrid.js";
|
|
6
|
+
import { PicoNowWalkerPlugin } from "#src/now/NowWalker.js";
|
|
7
|
+
import { PicoNowRangePlugin } from "#src/now/NowRange.js";
|
|
8
|
+
import { PicoNowHumanPlugin } from "#src/now/NowHuman.js";
|
|
9
|
+
|
|
10
|
+
export const PicoNowPlugins = [
|
|
11
|
+
PicoNowDefaultPlugin,
|
|
12
|
+
PicoNowFormatPlugin,
|
|
13
|
+
PicoNowMatchPlugin,
|
|
14
|
+
PicoNowGridPlugin,
|
|
15
|
+
PicoNowWalkerPlugin,
|
|
16
|
+
PicoNowRangePlugin,
|
|
17
|
+
PicoNowHumanPlugin,
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* @class PicoNow
|
|
22
|
+
*
|
|
23
|
+
* @typedef {import('#src/now/NowMatch.js').PicoNowMatchInstance} PicoNowMatchInstance
|
|
24
|
+
* @typedef {import('#src/now/NowFormat.js').PicoNowFormatInstance} PicoNowFormatInstance
|
|
25
|
+
* @typedef {import('#src/now/NowMatch.js').PicoNowMatchInstance} PicoNowMatchInstance
|
|
26
|
+
* @typedef {import('#src/now/NowGrid.js').PicoNowGridInstance} PicoNowGridInstance
|
|
27
|
+
* @typedef {import('#src/now/NowWalker.js').PicoNowWalkerInstance} PicoNowWalkerInstance
|
|
28
|
+
* @typedef {import('#src/now/NowRange.js').PicoNowRangeInstance} PicoNowRangeInstance
|
|
29
|
+
* @typedef {import('#src/now/NowHuman.js').PicoNowHumanInstance} PicoNowHumanInstance
|
|
30
|
+
*
|
|
31
|
+
* @extends PicoNowDefaultInstance
|
|
32
|
+
* @extends PicoNowFormatInstance
|
|
33
|
+
* @extends PicoNowMatchInstance
|
|
34
|
+
* @extends PicoNowGridInstance
|
|
35
|
+
* @extends PicoNowWalkerInstance
|
|
36
|
+
* @extends PicoNowRangeInstance
|
|
37
|
+
* @extends PicoNowHumanInstance
|
|
38
|
+
*/
|
|
39
|
+
export class PicoNow
|
|
40
|
+
{
|
|
41
|
+
/**
|
|
42
|
+
* Init hooks for instance
|
|
43
|
+
*
|
|
44
|
+
* @type {Array<function>}
|
|
45
|
+
*/
|
|
46
|
+
static init = [];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Original input value
|
|
50
|
+
*
|
|
51
|
+
* @type {any}
|
|
52
|
+
*/
|
|
53
|
+
input = null;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Current Date instance
|
|
57
|
+
*
|
|
58
|
+
* @type {Date}
|
|
59
|
+
*/
|
|
60
|
+
value = null;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Create Now instance from input
|
|
64
|
+
*
|
|
65
|
+
* @example new Now().toUTC() // => string
|
|
66
|
+
*
|
|
67
|
+
* @param {any} [value] Date input
|
|
68
|
+
* @param {string} [format] Input format
|
|
69
|
+
*/
|
|
70
|
+
constructor(value = null, format = 'YYYY-MM-DD HH:mm:ss')
|
|
71
|
+
{
|
|
72
|
+
if ( value === 'now' ) {
|
|
73
|
+
value = null;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
let input = value;
|
|
77
|
+
|
|
78
|
+
if ( value && value.toDate ) {
|
|
79
|
+
value = value.toDate();
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if ( ! (value instanceof Date) ) {
|
|
83
|
+
value = new Date();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
this.input = value;
|
|
87
|
+
|
|
88
|
+
if ( Mix.isNix(input) ) {
|
|
89
|
+
this.input = input;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
this.value = value;
|
|
93
|
+
|
|
94
|
+
if ( Mix.isStr(input) ) {
|
|
95
|
+
this.parse(input, format);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
Arr.each(Now.init, (fn) => {
|
|
99
|
+
el = fn.call(this, el, format);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
return this;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Extend Now with a plugin
|
|
107
|
+
*
|
|
108
|
+
* @example Now.extend(fn)
|
|
109
|
+
*
|
|
110
|
+
* @param {function} plugin Plugin function
|
|
111
|
+
* @returns {void} No return value
|
|
112
|
+
*/
|
|
113
|
+
static extend(plugin)
|
|
114
|
+
{
|
|
115
|
+
plugin.call({}, this);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Create Now instance helper
|
|
120
|
+
*
|
|
121
|
+
* @example Now.make("now") // => Now
|
|
122
|
+
*
|
|
123
|
+
* @param {any} [value] Date input
|
|
124
|
+
* @param {string} [format] Input format
|
|
125
|
+
* @returns {PicoNow} Now instance
|
|
126
|
+
*/
|
|
127
|
+
static make(value = null, format = 'YYYY-MM-DD HH:mm:ss')
|
|
128
|
+
{
|
|
129
|
+
return new Now(value, format);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Reset date parts to start
|
|
134
|
+
*
|
|
135
|
+
* @example Now.make().reset() // => Now
|
|
136
|
+
*
|
|
137
|
+
* @param {any} [config] Reset flags
|
|
138
|
+
* @returns {PicoNow} Now instance
|
|
139
|
+
*/
|
|
140
|
+
reset(config = null)
|
|
141
|
+
{
|
|
142
|
+
if ( config == null ) {
|
|
143
|
+
config = { month: true, day: true, time: true };
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
if ( config.month ) {
|
|
147
|
+
this.value.setMonth(0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if ( config.day ) {
|
|
151
|
+
this.value.setDate(1);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if ( config.time ) {
|
|
155
|
+
this.value.setHours(0, 0, 0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return this;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Clone Now instance
|
|
163
|
+
*
|
|
164
|
+
* @example Now.make().clone() // => Now
|
|
165
|
+
*
|
|
166
|
+
* @param {boolean} [reset] Reset clone
|
|
167
|
+
* @returns {PicoNow} New instance
|
|
168
|
+
*/
|
|
169
|
+
clone(reset = false)
|
|
170
|
+
{
|
|
171
|
+
return new Now(new Date(this.value), null, reset);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Check if input is valid date
|
|
176
|
+
*
|
|
177
|
+
* @example Now.make("x").valid() // => false
|
|
178
|
+
*
|
|
179
|
+
* @returns {boolean} True if valid
|
|
180
|
+
*/
|
|
181
|
+
valid()
|
|
182
|
+
{
|
|
183
|
+
return ! isNaN(this.input);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Get Date instance
|
|
188
|
+
*
|
|
189
|
+
* @example Now.make().toDate() // => Date
|
|
190
|
+
*
|
|
191
|
+
* @returns {Date} Date instance
|
|
192
|
+
*/
|
|
193
|
+
toDate()
|
|
194
|
+
{
|
|
195
|
+
return this.value;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Get UTC string
|
|
200
|
+
*
|
|
201
|
+
* @example Now.make().toUTC() // => string
|
|
202
|
+
*
|
|
203
|
+
* @returns {string} UTC string
|
|
204
|
+
*/
|
|
205
|
+
toUTC()
|
|
206
|
+
{
|
|
207
|
+
return this.value.toUTCString();
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* @see PicoNow.reset
|
|
214
|
+
*/
|
|
215
|
+
PicoNow.prototype.resetTime = function () {
|
|
216
|
+
console.warn('Now.resetTime() is deprecated, use Now.reset({ time: true }) instead.');
|
|
217
|
+
return this.reset({ time: true });
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* @returns {typeof PicoNow}
|
|
222
|
+
*/
|
|
223
|
+
export function PicoNowBuilder() {
|
|
224
|
+
|
|
225
|
+
let cls = PicoNow;
|
|
226
|
+
|
|
227
|
+
for ( const plugin of PicoNowPlugins ) {
|
|
228
|
+
cls = plugin.call(cls, cls);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
return cls;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
export default PicoNowBuilder;
|
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
import { Mix, Num, Arr, Hash, Str, Obj } from "#src/index.esm.js";
|
|
2
|
+
|
|
3
|
+
export class PicoNumber
|
|
4
|
+
{
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Format number with decimals
|
|
8
|
+
*
|
|
9
|
+
* @example Num.fixed(1.234, 2) // => "1.23"
|
|
10
|
+
* @example Num.fixed("2.4", 0) // => "2"
|
|
11
|
+
*
|
|
12
|
+
* @param {any} value Number value
|
|
13
|
+
* @param {number} decimals Decimals count
|
|
14
|
+
* @returns {string} Fixed string
|
|
15
|
+
*/
|
|
16
|
+
static fixed(value, decimals = 2)
|
|
17
|
+
{
|
|
18
|
+
if ( ! Mix.isNum(value) ) {
|
|
19
|
+
value = Mix.num(value);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return value.toFixed(decimals);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Parse value as float
|
|
27
|
+
*
|
|
28
|
+
* @example Num.float("2.2") // => 2.2
|
|
29
|
+
* @example Num.float(3) // => 3
|
|
30
|
+
*
|
|
31
|
+
* @param {any} value Numeric value
|
|
32
|
+
* @returns {number} Parsed float
|
|
33
|
+
*/
|
|
34
|
+
static float(value)
|
|
35
|
+
{
|
|
36
|
+
if ( ! Mix.isNum(value) ) {
|
|
37
|
+
value = Mix.num(value);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return parseFloat(value);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Parse value as int
|
|
45
|
+
*
|
|
46
|
+
* @example Num.int("12") // => 12
|
|
47
|
+
* @example Num.int(12.9) // => 12
|
|
48
|
+
*
|
|
49
|
+
* @param {any} value Numeric value
|
|
50
|
+
* @returns {number} Parsed int
|
|
51
|
+
*/
|
|
52
|
+
static int(value)
|
|
53
|
+
{
|
|
54
|
+
if ( ! Mix.isNum(value) ) {
|
|
55
|
+
value = Mix.num(value);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return parseInt(value);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Round up to integer
|
|
63
|
+
*
|
|
64
|
+
* @example Num.ceil(1.1) // => 2
|
|
65
|
+
* @example Num.ceil("2.0") // => 2
|
|
66
|
+
*
|
|
67
|
+
* @param {any} value Numeric value
|
|
68
|
+
* @returns {number} Ceil value
|
|
69
|
+
*/
|
|
70
|
+
static ceil(value)
|
|
71
|
+
{
|
|
72
|
+
if ( ! Mix.isNum(value) ) {
|
|
73
|
+
value = Mix.num(value);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
return Math.ceil(value);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Round down to integer
|
|
81
|
+
*
|
|
82
|
+
* @example Num.floor(1.9) // => 1
|
|
83
|
+
* @example Num.floor("2.0") // => 2
|
|
84
|
+
*
|
|
85
|
+
* @param {any} value Numeric value
|
|
86
|
+
* @returns {number} Floor value
|
|
87
|
+
*/
|
|
88
|
+
static floor(value)
|
|
89
|
+
{
|
|
90
|
+
if ( ! Mix.isNum(value) ) {
|
|
91
|
+
value = Mix.num(value);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
return Math.floor(value);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Round to nearest integer
|
|
99
|
+
*
|
|
100
|
+
* @example Num.round(1.5) // => 2
|
|
101
|
+
* @example Num.round("2.2") // => 2
|
|
102
|
+
*
|
|
103
|
+
* @param {any} value Numeric value
|
|
104
|
+
* @returns {number} Rounded value
|
|
105
|
+
*/
|
|
106
|
+
static round(value)
|
|
107
|
+
{
|
|
108
|
+
if ( ! Mix.isNum(value) ) {
|
|
109
|
+
value = Mix.num(value);
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Math.round(value);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Sum numbers in list
|
|
117
|
+
*
|
|
118
|
+
* @example Num.combine([1, 2]) // => 3
|
|
119
|
+
* @example Num.combine([5, -2]) // => 3
|
|
120
|
+
*
|
|
121
|
+
* @param {Array<number>} value Number list
|
|
122
|
+
* @returns {number} Sum value
|
|
123
|
+
*/
|
|
124
|
+
static combine(value)
|
|
125
|
+
{
|
|
126
|
+
return Arr.reduce(value, (acc, val) => acc + val, 0);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Subtract numbers in list
|
|
131
|
+
*
|
|
132
|
+
* @example Num.subtract([5, 2]) // => 3
|
|
133
|
+
* @example Num.subtract([10, 3]) // => 7
|
|
134
|
+
*
|
|
135
|
+
* @param {Array<number>} value Number list
|
|
136
|
+
* @returns {number} Result value
|
|
137
|
+
*/
|
|
138
|
+
static subtract(value)
|
|
139
|
+
{
|
|
140
|
+
return Arr.reduce(value, (acc, val) => acc - val, 0);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Get decade base (10s)
|
|
145
|
+
*
|
|
146
|
+
* @example Num.decade(27) // => 20
|
|
147
|
+
* @example Num.decade("99") // => 90
|
|
148
|
+
*
|
|
149
|
+
* @param {any} value Numeric value
|
|
150
|
+
* @returns {number} Decade value
|
|
151
|
+
*/
|
|
152
|
+
static decade(value)
|
|
153
|
+
{
|
|
154
|
+
if ( ! Mix.isNum(value) ) {
|
|
155
|
+
value = Mix.num(value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return Math.floor(value / 10) * 10;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Convert number to power-of-2 list
|
|
163
|
+
*
|
|
164
|
+
* @example Num.matrix(5) // => [4, 1]
|
|
165
|
+
* @example Num.matrix(8) // => [8]
|
|
166
|
+
*
|
|
167
|
+
* @param {number} num Input number
|
|
168
|
+
* @param {number} [limit] Unused (legacy)
|
|
169
|
+
* @param {Array<number>} [base] Result base
|
|
170
|
+
* @returns {Array<number>} Power list
|
|
171
|
+
*/
|
|
172
|
+
static matrix(num, limit = 10, base = [])
|
|
173
|
+
{
|
|
174
|
+
let value = 0;
|
|
175
|
+
|
|
176
|
+
for ( let i = 20; i >= 0; i -- ) {
|
|
177
|
+
if ( num >= (value = Math.pow(2, i)) ) {
|
|
178
|
+
(base.push(value), num -= value);
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
return base;
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Calculate distance between coords
|
|
187
|
+
*
|
|
188
|
+
* @example Num.distance({lat:0,lng:0},{lat:0,lng:1}) // => number
|
|
189
|
+
*
|
|
190
|
+
* @param {any} cord1 First coord
|
|
191
|
+
* @param {any} cord2 Second coord
|
|
192
|
+
* @param {boolean} [miles] Return miles if true
|
|
193
|
+
* @returns {number} Distance value
|
|
194
|
+
*/
|
|
195
|
+
static distance(cord1, cord2, miles = false)
|
|
196
|
+
{
|
|
197
|
+
let cord = { lat: 0, lng: 0 };
|
|
198
|
+
|
|
199
|
+
[cord1, cord2] = [
|
|
200
|
+
{ ...cord, ...cord1 }, { ...cord, ...cord2 }
|
|
201
|
+
];
|
|
202
|
+
|
|
203
|
+
let radlat1 = (Math.PI * this.float(cord1.lat)) / 180;
|
|
204
|
+
let radlat2 = (Math.PI * this.float(cord2.lat)) / 180;
|
|
205
|
+
|
|
206
|
+
let theta = this.float(cord1.lng) - this.float(cord2.lng);
|
|
207
|
+
let radtheta = (theta) / 180;
|
|
208
|
+
|
|
209
|
+
let dist = this.combine([
|
|
210
|
+
Math.sin(radlat1) * Math.sin(radlat2),
|
|
211
|
+
Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta)
|
|
212
|
+
]);
|
|
213
|
+
|
|
214
|
+
dist = (Math.acos(dist > 1 ? 1 : dist) * 180) /
|
|
215
|
+
Math.PI * 60 * 1.1515;
|
|
216
|
+
|
|
217
|
+
return miles ? dist * 1.609344 : dist;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* @see PicoStr.number
|
|
224
|
+
*/
|
|
225
|
+
PicoNumber.format = (value, ...args) => {
|
|
226
|
+
console.warn('Num.format() is deprecated, use Str.number() instead.');
|
|
227
|
+
return Str.number(value, args[2] || null);
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* @see PicoHash.number
|
|
232
|
+
*/
|
|
233
|
+
PicoNumber.random = (...args) => {
|
|
234
|
+
console.warn('Num.random() is deprecated, use Hash.number() instead.');
|
|
235
|
+
return Hash.number(...args);
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export default PicoNumber;
|