@jamesrock/rockjs 1.18.0 → 1.20.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 +79 -5
  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'];
@@ -319,7 +337,8 @@ export class BrickMaker extends DisplayObject {
319
337
  type = 'digits',
320
338
  color = 'red',
321
339
  scale = 30,
322
- gap = 1
340
+ gap = 1,
341
+ prefix = '0x'
323
342
  } = {}) {
324
343
 
325
344
  super();
@@ -329,6 +348,7 @@ export class BrickMaker extends DisplayObject {
329
348
  this.scale = scale;
330
349
  this.type = type;
331
350
  this.gap = gap;
351
+ this.prefix = prefix;
332
352
  this.hexMap = size*size > 16 ? makeHexMap() : makeHexMap(false);
333
353
  this.typeValues = {
334
354
  'digits': makeArray(size*size, () => 1),
@@ -387,7 +407,7 @@ export class BrickMaker extends DisplayObject {
387
407
  });
388
408
  total[y] = this.hexMap[total[y]];
389
409
  });
390
- this.value = `0x${total.join('')}`;
410
+ this.value = `${this.prefix}${total.join('')}`;
391
411
  break;
392
412
  };
393
413
  this.dispatchEvent('result');
@@ -522,3 +542,57 @@ export class Collection extends Array {
522
542
 
523
543
  };
524
544
  };
545
+
546
+ export class Time {
547
+ second = 1000;
548
+ minute = (this.second * 60);
549
+ hour = (this.minute * 60);
550
+ day = (this.hour * 24);
551
+ year = (this.day * 365);
552
+ getSeconds(ms) {
553
+ return floorTo(ms / this.second);
554
+ };
555
+ getMinutes(ms) {
556
+ return floorTo(ms / this.minute);
557
+ };
558
+ getHours(ms) {
559
+ return floorTo(ms / this.hour);
560
+ };
561
+ getDays(ms) {
562
+ return floorTo(ms / this.day);
563
+ };
564
+ getYears(ms) {
565
+ return floorTo(ms / this.year);
566
+ };
567
+ getMillisecondsInHour(ms) {
568
+ return (ms % this.hour);
569
+ };
570
+ getMillisecondsInMinute(ms) {
571
+ return (ms % this.minute);
572
+ };
573
+ getMillisecondsInDay(ms) {
574
+ return (ms % this.day);
575
+ };
576
+ getSecondsInDay(ms) {
577
+ return (this.getSeconds(ms) % this.day);
578
+ };
579
+ getMinutesInDay(ms) {
580
+ return (this.getMinutes(ms) % this.day);
581
+ };
582
+ getHoursInDay(ms) {
583
+ return (this.getHours(ms) % this.day);
584
+ };
585
+ getDaysInYear(ms) {
586
+ return (this.getDays(ms) % this.year);
587
+ };
588
+ getMinutesInHour(ms) {
589
+ return floorTo(this.getMillisecondsInHour(ms) / this.minute);
590
+ };
591
+ getSecondsInMinute(ms) {
592
+ return floorTo(this.getMillisecondsInMinute(ms) / this.second);
593
+ };
594
+ formatSeconds = (ms) => toDouble(this.getSecondsInMinute(ms));
595
+ formatMinutes = (ms) => toDouble(this.getMinutesInHour(ms));
596
+ formatHours = (ms) => toDouble(this.getHours(ms));
597
+ format = (ms) => `${this.formatMinutes(ms)}:${this.formatSeconds(ms)}`;
598
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.18.0",
3
+ "version": "1.20.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",