@jamesrock/rockjs 1.14.0 → 1.16.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 +128 -0
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -25,6 +25,44 @@ export const getTimeString = (type = 'short') => new Date().toLocaleTimeString('
25
25
  export const getDateTimeString = (type = 'short') => `${getDateString(type)} ${getTimeString(type)}`;
26
26
  export const isValidKey = (key, options) => options.includes(key);
27
27
  export const makeArray = (length, mapper = (a, i) => i) => Array.from({length}, mapper);
28
+ export const floorTo = (number, to) => (Math.floor(number*to)/to);
29
+ export const roundTo = (number, to) => (Math.round(number*to)/to);
30
+ export const ceilTo = (number, to) => (Math.ceil(number*to)/to);
31
+ export const getXPercentOfY = (x, y) => (y*(x/100));
32
+ export const getXAsPercentOfY = (x, y) => ((x/y)*100);
33
+
34
+ const sortingMethods = {
35
+ '0-9': (prop) => (a, b) => prop(a)-prop(b),
36
+ '9-0': (prop) => (a, b) => prop(b)-prop(a),
37
+ 'A-Z': (prop) => (a, b) => {
38
+ a = prop(a);
39
+ b = prop(b);
40
+ if(a<b) {
41
+ return -1;
42
+ }
43
+ else if(a>b) {
44
+ return 1;
45
+ }
46
+ else {
47
+ return 0;
48
+ };
49
+ },
50
+ 'Z-A': (prop) => (a, b) => {
51
+ a = prop(a);
52
+ b = prop(b);
53
+ if(b<a) {
54
+ return -1;
55
+ }
56
+ else if(b>a) {
57
+ return 1;
58
+ }
59
+ else {
60
+ return 0;
61
+ };
62
+ }
63
+ };
64
+
65
+ export const sort = (target, prop, method) => target.sort(sortingMethods[method](prop));
28
66
 
29
67
  export const setDocumentHeight = () => {
30
68
  document.documentElement.style.height = window.navigator.standalone ? '100vh' : '100svh';
@@ -393,3 +431,93 @@ export class BrickMaker extends DisplayObject {
393
431
  };
394
432
  value = null;
395
433
  };
434
+
435
+ export class Collection extends Array {
436
+ constructor() {
437
+
438
+ super();
439
+
440
+ };
441
+ getItemByKeyValue(key, value) {
442
+
443
+ return this.filter((item) => item[key]===value)[0];
444
+
445
+ };
446
+ getItemsByKeyValue(key, value) {
447
+
448
+ return this.filter((item) => item[key]===value);
449
+
450
+ };
451
+ append(item) {
452
+
453
+ this.push(item);
454
+ return item;
455
+
456
+ };
457
+ prepend(item) {
458
+
459
+ this.unshift(item);
460
+ return item;
461
+
462
+ };
463
+ exists(value) {
464
+
465
+ return (this.indexOf(value)>-1);
466
+
467
+ };
468
+ random() {
469
+
470
+ return getRandom(this);
471
+
472
+ };
473
+ remove(item) {
474
+
475
+ return this.removeAt(this.getIndexOf(item));
476
+
477
+ };
478
+ removeAt(index) {
479
+
480
+ this.splice(index, 1);
481
+ return this;
482
+
483
+ };
484
+ addAt(item, index) {
485
+
486
+ this.splice(index, 0, item);
487
+ return item;
488
+
489
+ };
490
+ first() {
491
+
492
+ return this[0];
493
+
494
+ };
495
+ last() {
496
+
497
+ return this[this.length-1];
498
+
499
+ };
500
+ swap(aIndex, bIndex) {
501
+
502
+ var
503
+ aProp = this[aIndex],
504
+ bProp = this[bIndex];
505
+
506
+ this[aIndex] = bProp;
507
+ this[bIndex] = aProp;
508
+
509
+ return this;
510
+
511
+ };
512
+ pushift() {
513
+
514
+ this.push(this.shift());
515
+ return this;
516
+
517
+ };
518
+ shuffle() {
519
+
520
+ return shuffle(this);
521
+
522
+ };
523
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.14.0",
3
+ "version": "1.16.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",