@jamesrock/rockjs 1.17.0 → 1.19.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/index.js +82 -10
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
const
|
|
2
|
-
const formatSeconds = (time) => Math.floor((time % (1000 * 60)) / 1000);
|
|
3
|
-
const pad = (time) => time.toString().padStart(2, '0');
|
|
4
|
-
const formatter = new Intl.NumberFormat('en-GB');
|
|
1
|
+
const numberFormatter = new Intl.NumberFormat('en-GB');
|
|
5
2
|
const currencyFormatter = new Intl.NumberFormat('en-GB', {style: 'currency', currency: 'GBP'});
|
|
6
3
|
|
|
7
4
|
export const random = (min, max) => (Math.floor(Math.random()*((max-min)+1))+min);
|
|
@@ -17,18 +14,21 @@ export const isTiny = () => !window.matchMedia('(min-width: 450px)').matches;
|
|
|
17
14
|
export const isDarkMode = () => window.matchMedia('(prefers-color-scheme: dark)').matches;
|
|
18
15
|
export const makeEven = (value) => value % 2 === 1 ? value - 1 : value;
|
|
19
16
|
export const limit = (value, max) => value > max ? max : value;
|
|
20
|
-
export const
|
|
21
|
-
export const
|
|
17
|
+
export const toDouble = (a) => a.toString().padStart(2, '0');
|
|
18
|
+
export const formatMinutes = (ms) => toDouble(Math.floor((ms % (1000 * 60 * 60)) / (1000 * 60)));
|
|
19
|
+
export const formatSeconds = (ms) => toDouble(Math.floor((ms % (1000 * 60)) / 1000));
|
|
20
|
+
export const formatTime = (ms) => `${formatMinutes(ms)}:${formatSeconds(ms)}`;
|
|
21
|
+
export const formatNumber = (n) => numberFormatter.format(n);
|
|
22
22
|
export const formatCurrency = (n) => currencyFormatter.format(n);
|
|
23
23
|
export const formatDate = (date, type = 'short') => date.toLocaleDateString('en-GB', { dateStyle: type });
|
|
24
|
-
export const getDateString = (type = 'short') => new Date()
|
|
24
|
+
export const getDateString = (type = 'short') => formatDate(new Date(), type);
|
|
25
25
|
export const getTimeString = (type = 'short') => new Date().toLocaleTimeString('en-GB', { timeStyle: type });
|
|
26
26
|
export const getDateTimeString = (type = 'short') => `${getDateString(type)} ${getTimeString(type)}`;
|
|
27
27
|
export const isValidKey = (key, options) => options.includes(key);
|
|
28
28
|
export const makeArray = (length, mapper = (a, i) => i) => Array.from({length}, mapper);
|
|
29
|
-
export const floorTo = (number, to) => (Math.floor(number*to)/to);
|
|
30
|
-
export const roundTo = (number, to) => (Math.round(number*to)/to);
|
|
31
|
-
export const ceilTo = (number, to) => (Math.ceil(number*to)/to);
|
|
29
|
+
export const floorTo = (number, to = 1) => (Math.floor(number*to)/to);
|
|
30
|
+
export const roundTo = (number, to = 1) => (Math.round(number*to)/to);
|
|
31
|
+
export const ceilTo = (number, to = 1) => (Math.ceil(number*to)/to);
|
|
32
32
|
export const getXPercentOfY = (x, y) => (y*(x/100));
|
|
33
33
|
export const getXAsPercentOfY = (x, y) => ((x/y)*100);
|
|
34
34
|
|
|
@@ -144,6 +144,24 @@ export const makeBitArray = (size) => {
|
|
|
144
144
|
});
|
|
145
145
|
};
|
|
146
146
|
|
|
147
|
+
const makeBitMapItem = (a, size) => {
|
|
148
|
+
const ref = makeBitArray(size);
|
|
149
|
+
let leftover = a;
|
|
150
|
+
return makeArray(size, (v, i) => {
|
|
151
|
+
if(leftover >= ref[i]) {
|
|
152
|
+
leftover -= ref[i];
|
|
153
|
+
return 1;
|
|
154
|
+
}
|
|
155
|
+
else {
|
|
156
|
+
return 0;
|
|
157
|
+
};
|
|
158
|
+
});
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
export const makeBitMap = (size) => {
|
|
162
|
+
return makeArray(2**size, (a, i) => makeBitMapItem(i, size));
|
|
163
|
+
};
|
|
164
|
+
|
|
147
165
|
export const makeHexMap = (full = true) => {
|
|
148
166
|
const out = [];
|
|
149
167
|
const hexMap = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'];
|
|
@@ -522,3 +540,57 @@ export class Collection extends Array {
|
|
|
522
540
|
|
|
523
541
|
};
|
|
524
542
|
};
|
|
543
|
+
|
|
544
|
+
export class Time {
|
|
545
|
+
second = 1000;
|
|
546
|
+
minute = (this.second * 60);
|
|
547
|
+
hour = (this.minute * 60);
|
|
548
|
+
day = (this.hour * 24);
|
|
549
|
+
year = (this.day * 365);
|
|
550
|
+
getSeconds(ms) {
|
|
551
|
+
return floorTo(ms / this.second);
|
|
552
|
+
};
|
|
553
|
+
getMinutes(ms) {
|
|
554
|
+
return floorTo(ms / this.minute);
|
|
555
|
+
};
|
|
556
|
+
getHours(ms) {
|
|
557
|
+
return floorTo(ms / this.hour);
|
|
558
|
+
};
|
|
559
|
+
getDays(ms) {
|
|
560
|
+
return floorTo(ms / this.day);
|
|
561
|
+
};
|
|
562
|
+
getYears(ms) {
|
|
563
|
+
return floorTo(ms / this.year);
|
|
564
|
+
};
|
|
565
|
+
getMillisecondsInHour(ms) {
|
|
566
|
+
return (ms % this.hour);
|
|
567
|
+
};
|
|
568
|
+
getMillisecondsInMinute(ms) {
|
|
569
|
+
return (ms % this.minute);
|
|
570
|
+
};
|
|
571
|
+
getMillisecondsInDay(ms) {
|
|
572
|
+
return (ms % this.day);
|
|
573
|
+
};
|
|
574
|
+
getSecondsInDay(ms) {
|
|
575
|
+
return (this.getSeconds(ms) % this.day);
|
|
576
|
+
};
|
|
577
|
+
getMinutesInDay(ms) {
|
|
578
|
+
return (this.getMinutes(ms) % this.day);
|
|
579
|
+
};
|
|
580
|
+
getHoursInDay(ms) {
|
|
581
|
+
return (this.getHours(ms) % this.day);
|
|
582
|
+
};
|
|
583
|
+
getDaysInYear(ms) {
|
|
584
|
+
return (this.getDays(ms) % this.year);
|
|
585
|
+
};
|
|
586
|
+
getMinutesInHour(ms) {
|
|
587
|
+
return floorTo(this.getMillisecondsInHour(ms) / this.minute);
|
|
588
|
+
};
|
|
589
|
+
getSecondsInMinute(ms) {
|
|
590
|
+
return floorTo(this.getMillisecondsInMinute(ms) / this.second);
|
|
591
|
+
};
|
|
592
|
+
formatSeconds = (ms) => toDouble(this.getSecondsInMinute(ms));
|
|
593
|
+
formatMinutes = (ms) => toDouble(this.getMinutesInHour(ms));
|
|
594
|
+
formatHours = (ms) => toDouble(this.getHours(ms));
|
|
595
|
+
format = (ms) => `${this.formatMinutes(ms)}:${this.formatSeconds(ms)}`;
|
|
596
|
+
};
|