@jamesrock/rockjs 1.18.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.
Files changed (2) hide show
  1. package/index.js +75 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -26,9 +26,9 @@ export const getTimeString = (type = 'short') => new Date().toLocaleTimeString('
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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.18.0",
3
+ "version": "1.19.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",