@jamesrock/rockjs 1.22.0 → 1.24.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 +208 -8
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -9,15 +9,17 @@ export const pluckLast = (a) => a.splice(a.length-1, 1)[0];
9
9
  export const getRandom = (a) => a[randomIndex(a)];
10
10
  export const getFirst = (a) => a[0];
11
11
  export const getLast = (a) => a[a.length-1];
12
- export const isLandscape = () => window.matchMedia('(orientation: landscape)').matches;
13
- export const isTiny = () => !window.matchMedia('(min-width: 450px)').matches;
14
- export const isDarkMode = () => window.matchMedia('(prefers-color-scheme: dark)').matches;
15
- export const makeEven = (value) => value % 2 === 1 ? value - 1 : value;
16
- export const limit = (value, max) => value > max ? max : value;
12
+ export const isOrientation = (orientation) => matchMedia(`(orientation: ${orientation})`).matches;
13
+ export const maxWidth = (width) => matchMedia(`(max-width: ${width}px)`).matches;
14
+ export const minWidth = (width) => matchMedia(`(min-width: ${width}px)`).matches;
15
+ export const isColorScheme = (colorScheme) => matchMedia(`(prefers-color-scheme: ${colorScheme})`).matches;
16
+ export const isLandscape = () => isOrientation('landscape');
17
+ export const isPortrait = () => isOrientation('portrait');
18
+ export const isTiny = () => !minWidth(450);
19
+ export const isDarkMode = () => isColorScheme('dark');
20
+ export const makeEven = (a) => (a % 2 === 1 ? a - 1 : a);
21
+ export const limit = (value, max) => (value > max ? max : value);
17
22
  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
23
  export const formatNumber = (n) => numberFormatter.format(n);
22
24
  export const formatCurrency = (n) => currencyFormatter.format(n);
23
25
  export const formatDate = (date, type = 'short') => date.toLocaleDateString('en-GB', { dateStyle: type });
@@ -31,6 +33,10 @@ export const roundTo = (number, to = 1) => (Math.round(number*to)/to);
31
33
  export const ceilTo = (number, to = 1) => (Math.ceil(number*to)/to);
32
34
  export const getXPercentOfY = (x, y) => (y*(x/100));
33
35
  export const getXAsPercentOfY = (x, y) => ((x/y)*100);
36
+ // to be removed
37
+ export const formatMinutes = (ms) => toDouble(Math.floor((ms % (1000 * 60 * 60)) / (1000 * 60)));
38
+ export const formatSeconds = (ms) => toDouble(Math.floor((ms % (1000 * 60)) / 1000));
39
+ export const formatTime = (ms) => `${formatMinutes(ms)}:${formatSeconds(ms)}`;
34
40
 
35
41
  const sortingMethods = {
36
42
  '0-9': (prop) => (a, b) => prop(a)-prop(b),
@@ -291,6 +297,24 @@ export class DisplayObject {
291
297
  return this;
292
298
 
293
299
  };
300
+ destroy() {
301
+
302
+ this.node.parentNode.removeChild(this.node);
303
+ return this;
304
+
305
+ };
306
+ setStyle(key, value) {
307
+
308
+ this.node.style[key] = value;
309
+ return this;
310
+
311
+ };
312
+ setProp(key, value) {
313
+
314
+ this.node.dataset[key] = value;
315
+ return this;
316
+
317
+ };
294
318
  addEventListener(event, handler, passive = true) {
295
319
 
296
320
  this.node.addEventListener(event, handler, {passive});
@@ -604,3 +628,179 @@ export class Time {
604
628
  formatHours = (ms) => toDouble(this.getHours(ms));
605
629
  format = (ms) => `${this.formatMinutes(ms)}:${this.formatSeconds(ms)}`;
606
630
  };
631
+
632
+ export class PlayingCard extends DisplayObject {
633
+ constructor(deck, value, suit) {
634
+
635
+ super();
636
+
637
+ this.deck = deck;
638
+ this.value = value;
639
+ this.suit = suit;
640
+ this.rawValue = this.deck.values.indexOf(this.value);
641
+ this.id = `${this.value}${this.suit}`;
642
+ this.color = this.deck.getSuitColor(this.suit);
643
+ this.icon = this.deck.getSuitIcon(this.suit);
644
+ this.node = this.make();
645
+
646
+ };
647
+ make() {
648
+
649
+ const
650
+ node = createNode('div', 'card'),
651
+ svg = createSVGNode('svg'),
652
+ use = createSVGNode('use');
653
+
654
+ use.setAttribute('href', `${this.deck.sprite}#${this.suit}${this.value}`);
655
+
656
+ svg.append(use);
657
+ node.append(svg);
658
+
659
+ return node;
660
+
661
+ };
662
+ };
663
+
664
+ export class DeckOfPlayingCards {
665
+ constructor({
666
+ sprite = '/sprite.svg',
667
+ saved = [],
668
+ cardMaker = (deck, value, suit) => new PlayingCard(deck, value, suit)
669
+ } = {}) {
670
+
671
+ this.sprite = sprite;
672
+ this.cardMaker = cardMaker;
673
+ this.cards = saved.length ? this.makeFromSaved(saved) : this.make();
674
+ this.map = this.makeMap();
675
+ this.shuffledMap = this.makeShuffledMap();
676
+
677
+ };
678
+ make() {
679
+
680
+ return shuffle(shuffle(this.makeDeckValues().map(([value, suit]) => this.cardMaker(this, value, suit))));
681
+
682
+ };
683
+ makeMap() {
684
+
685
+ var out = {};
686
+ this.cards.forEach((card) => {
687
+ out[card.id] = card;
688
+ });
689
+ return out;
690
+
691
+ };
692
+ makeShuffledMap() {
693
+
694
+ return this.cards.map((card) => card.id);
695
+
696
+ };
697
+ makeSaveMap() {
698
+
699
+ return this.cards.map((card) => [card.value, card.suit]);
700
+
701
+ };
702
+ makeFromSaved(saved) {
703
+
704
+ return saved.map(([value, suit]) => this.cardMaker(this, value, suit));
705
+
706
+ };
707
+ appendTo(target) {
708
+
709
+ this.shuffledMap.forEach((id) => {
710
+ this.map[id].appendTo(target);
711
+ });
712
+
713
+ return this;
714
+
715
+ };
716
+ destroy() {
717
+
718
+ this.shuffledMap.forEach((id) => {
719
+ this.map[id].destroy();
720
+ });
721
+
722
+ return this;
723
+
724
+ };
725
+ makeSuitValues() {
726
+
727
+ return makeArray(this.suits.length).map((suit) => this.suits[suit]);
728
+
729
+ };
730
+ makePipValues() {
731
+
732
+ return makeArray(this.values.length).map((value) => this.values[value]);
733
+
734
+ };
735
+ makeDeckValues() {
736
+
737
+ var out = [];
738
+
739
+ this.makeSuitValues().forEach((suit) => {
740
+ this.makePipValues().forEach((value) => {
741
+ out.push([value, suit]);
742
+ });
743
+ });
744
+
745
+ return out;
746
+
747
+ };
748
+ makeCard(value, suit) {
749
+
750
+ return this.cardMaker(this, value, suit);
751
+
752
+ };
753
+ indexOf(id) {
754
+
755
+ return this.shuffledMap.indexOf(id);
756
+
757
+ };
758
+ getCard(index = 0) {
759
+
760
+ return this.cards[index];
761
+
762
+ };
763
+ getSuitIcon(suit) {
764
+
765
+ return this.suitIcons[suit];
766
+
767
+ };
768
+ getSuitColor(suit) {
769
+
770
+ return this.suitColors[suit];
771
+
772
+ };
773
+ suits = [
774
+ 'C',
775
+ 'D',
776
+ 'H',
777
+ 'S'
778
+ ];
779
+ values = [
780
+ 'A',
781
+ '2',
782
+ '3',
783
+ '4',
784
+ '5',
785
+ '6',
786
+ '7',
787
+ '8',
788
+ '9',
789
+ '10',
790
+ 'J',
791
+ 'Q',
792
+ 'K'
793
+ ];
794
+ suitIcons = {
795
+ 'C': '♣',
796
+ 'D': '♦',
797
+ 'H': '♥',
798
+ 'S': '♠'
799
+ };
800
+ suitColors = {
801
+ 'C': 'black',
802
+ 'D': 'red',
803
+ 'H': 'red',
804
+ 'S': 'black'
805
+ };
806
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.22.0",
3
+ "version": "1.24.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",