@jamesrock/rockjs 1.29.0 → 1.30.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 +60 -49
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,8 +10,8 @@ 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
12
  export const isOrientation = (orientation) => matchMedia(`(orientation: ${orientation})`).matches;
13
- export const maxWidth = (width) => matchMedia(`(max-width: ${width}px)`).matches;
14
13
  export const minWidth = (width) => matchMedia(`(min-width: ${width}px)`).matches;
14
+ export const minHeight = (height) => matchMedia(`(min-height: ${height}px)`).matches;
15
15
  export const isColorScheme = (colorScheme) => matchMedia(`(prefers-color-scheme: ${colorScheme})`).matches;
16
16
  export const isLandscape = () => isOrientation('landscape');
17
17
  export const isPortrait = () => isOrientation('portrait');
@@ -89,90 +89,88 @@ export const shuffle = (a) => {
89
89
  return a;
90
90
  };
91
91
 
92
- export const createNode = (type, className) => {
93
- const node = document.createElement(type);
94
- if(className) {
95
- className.split('.').forEach((a) => {
96
- node.classList.add(a);
97
- });
98
- };
92
+ const applyClassName = (node, className) => {
93
+ className?.split('.').forEach((a) => {
94
+ node.classList.add(a);
95
+ });
99
96
  return node;
100
97
  };
101
98
 
102
- export const createSVGNode = (type = 'svg', className) => {
103
- const node = document.createElementNS('http://www.w3.org/2000/svg', type);
104
- if(className) {
105
- className.split('.').forEach((a) => {
106
- node.classList.add(a);
107
- });
108
- };
109
- return node;
110
- };
99
+ export const makeNode = (type, className) => applyClassName(document.createElement(type), className);
111
100
 
112
- export const createButton = (label = '{label}', className) => {
113
- const btn = createNode('button', className);
101
+ export const makeSVGNode = (type = 'svg', className) => applyClassName(document.createElementNS('http://www.w3.org/2000/svg', type), className);
102
+
103
+ export const makeButton = (label = '{label}', className) => {
104
+ const btn = makeNode('button', className);
114
105
  btn.innerText = label;
115
106
  return btn;
116
107
  };
117
108
 
118
- export const createInput = (value = 0, type = 'number') => {
119
- const input = createNode('input');
109
+ export const makeInput = (value = 0, type = 'number') => {
110
+ const input = makeNode('input');
120
111
  input.type = type;
121
112
  input.value = value;
122
113
  return input;
123
114
  };
124
115
 
125
- export const createOutput = (rows = 7) => {
126
- const textarea = createNode('textarea');
127
- textarea.rows = rows;
128
- return textarea;
116
+ export const makeOutput = (rows = 7) => {
117
+ const node = makeNode('textarea');
118
+ node.rows = rows;
119
+ return node;
129
120
  };
130
121
 
131
- export const createContainer = (className) => createNode('div', className);
122
+ export const makeContainer = (className) => makeNode('div', className);
132
123
 
133
- export const createSelect = (options) => {
134
- const node = createNode('select');
124
+ export const makeSelect = (options) => {
125
+ const node = makeNode('select');
135
126
  options.forEach(([label, value]) => {
136
- const option = createOption(label, value);
127
+ const option = makeOption(label, value);
137
128
  node.appendChild(option);
138
129
  })
139
130
  return node;
140
131
  };
141
132
 
142
- export const createOption = (label = '{label}', value = 0) => {
143
- const node = createNode('option');
133
+ export const makeOption = (label = '{label}', value = 0) => {
134
+ const node = makeNode('option');
144
135
  node.innerText = label;
145
136
  node.value = value;
146
137
  return node;
147
138
  };
148
139
 
149
- export const createHeading = (level = 1, label = '{label}') => {
150
- const node = createNode(`h${level}`);
140
+ export const makeHeading = (level = 1, label = '{label}') => {
141
+ const node = makeNode(`h${level}`);
151
142
  node.innerText = label;
152
143
  return node;
153
144
  };
154
145
 
155
- export const createRadio = (value = 0, name = '{name}', id = '{id}', checked = false) => {
156
- const node = createInput(value, 'radio');
146
+ export const makeLink = (href = '{href}', label = '{label}', className) => {
147
+ const node = makeNode('a', className);
148
+ node.href = href;
149
+ node.innerText = label;
150
+ return node;
151
+ };
152
+
153
+ export const makeRadio = (value = 0, name = '{name}', id = '{id}', checked = false) => {
154
+ const node = makeInput(value, 'radio');
157
155
  node.name = name;
158
156
  node.id = id;
159
157
  node.checked = checked;
160
158
  return node;
161
159
  };
162
160
 
163
- export const createLabel = (label = '{label}', id = '{id}', className) => {
164
- const node = createNode('label', className);
161
+ export const makeLabel = (label = '{label}', id = '{id}', className) => {
162
+ const node = makeNode('label', className);
165
163
  node.innerHTML = label;
166
164
  node.setAttribute('for', id);
167
165
  return node;
168
166
  };
169
167
 
170
168
  export const makeToggle = (options, name, defaultValue, className = 'toggle') => {
171
- const node = createNode('div', className);
169
+ const node = makeNode('div', className);
172
170
  options.forEach(([label, value, optionClassName]) => {
173
- const optionNode = createNode('div', 'toggle-item');
174
- const radioNode = createRadio(value, name, `${name}-${value}`, value===defaultValue);
175
- const labelNode = createLabel(label, radioNode.id, optionClassName);
171
+ const optionNode = makeNode('div', 'toggle-item');
172
+ const radioNode = makeRadio(value, name, `${name}-${value}`, value===defaultValue);
173
+ const labelNode = makeLabel(label, radioNode.id, optionClassName);
176
174
  optionNode.appendChild(radioNode);
177
175
  optionNode.appendChild(labelNode);
178
176
  node.appendChild(optionNode);
@@ -364,9 +362,9 @@ export class GameBase extends DisplayObject {
364
362
 
365
363
  super();
366
364
 
367
- this.node = createNode('div', name);
368
- this.gameOverNode = createNode('div', 'game-over');
369
- this.canvas = createNode('canvas', 'game-canvas');
365
+ this.node = makeNode('div', name);
366
+ this.gameOverNode = makeNode('div', 'game-over');
367
+ this.canvas = makeNode('canvas', 'game-canvas');
370
368
  this.ctx = this.canvas.getContext('2d');
371
369
  this.storage = new Storage(`me.jamesrock.${name}`);
372
370
 
@@ -417,7 +415,7 @@ export class BrickMaker extends DisplayObject {
417
415
  'hex': makeBitArray(size)
418
416
  };
419
417
 
420
- const node = this.node = createNode('div', 'brick-maker');
418
+ const node = this.node = makeNode('div', 'brick-maker');
421
419
  const bits = this.bits = [];
422
420
 
423
421
  this.setColor(color);
@@ -427,7 +425,7 @@ export class BrickMaker extends DisplayObject {
427
425
  makeArray(size).forEach((y) => {
428
426
  makeArray(size).forEach((x) => {
429
427
 
430
- const bit = createNode('div', 'brick-maker-bit');
428
+ const bit = makeNode('div', 'brick-maker-bit');
431
429
  bit.style.width = bit.style.height = `${scale}px`;
432
430
  bit.dataset.x = x;
433
431
  bit.dataset.y = y;
@@ -677,9 +675,9 @@ export class PlayingCard extends DisplayObject {
677
675
  make() {
678
676
 
679
677
  const
680
- node = createNode('div', 'card'),
681
- svg = createSVGNode('svg'),
682
- use = createSVGNode('use');
678
+ node = makeNode('div', 'card'),
679
+ svg = makeSVGNode('svg'),
680
+ use = makeSVGNode('use');
683
681
 
684
682
  use.setAttribute('href', `${this.deck.sprite}#${this.suit}${this.value}`);
685
683
 
@@ -834,3 +832,16 @@ export class DeckOfPlayingCards {
834
832
  'S': 'black'
835
833
  };
836
834
  };
835
+
836
+ // temporary alias
837
+ export const createNode = makeNode;
838
+ export const createSVGNode = makeSVGNode;
839
+ export const createButton = makeButton;
840
+ export const createInput = makeInput;
841
+ export const createOutput = makeOutput;
842
+ export const createContainer = makeContainer;
843
+ export const createSelect = makeSelect;
844
+ export const createOption = makeOption;
845
+ export const createHeading = makeHeading;
846
+ export const createRadio = makeRadio;
847
+ export const createLabel = makeLabel;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jamesrock/rockjs",
3
- "version": "1.29.0",
3
+ "version": "1.30.0",
4
4
  "description": "utility bliss",
5
5
  "license": "ISC",
6
6
  "author": "James Rock",