@rulecad/keyboard-layout 0.1.5 → 0.2.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.
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # @rulecad/keyboard-layout
2
2
 
3
3
  A keyboard layout as plain JSON that everything else can read: the plate, the keycaps, the case and the circuit board
4
- all take their positions from one file. No dependencies; browsers and Node 20+.
4
+ all take their positions from one file. No dependencies; browsers and Node 22.13 or later.
5
5
 
6
6
  ```json
7
7
  {
@@ -13,7 +13,8 @@
13
13
  /** @typedef {{ label: string, u: number, units: number, row: string | number, index: number, col: number, x: number, y: number, [property: string]: any }} Key */
14
14
 
15
15
  // A key as { label, u, …properties }
16
- const key = (k) => (typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k });
16
+ const key = (k) =>
17
+ typeof k === 'string' ? { label: k, u: 1 } : Array.isArray(k) ? { label: k[0], u: k[1] ?? 1 } : { u: 1, ...k };
17
18
  const rowOf = (r) => (Array.isArray(r) ? { keys: r } : r);
18
19
  // A gap between keys: { "gap": 0.5 } in a row, in key units (no key there, but the keys after it move along)
19
20
  const isGap = (k) => k && typeof k === 'object' && !Array.isArray(k) && k.gap != null && k.label == null;
@@ -26,16 +27,30 @@ const isGap = (k) => k && typeof k === 'object' && !Array.isArray(k) && k.gap !=
26
27
  * @returns {Key[]}
27
28
  */
28
29
  export function keys(layout) {
29
- const rows = (layout.rows || []).map(rowOf), defaults = layout.defaults || {};
30
- const across = width(layout), out = [];
30
+ const rows = (layout.rows || []).map(rowOf),
31
+ defaults = layout.defaults || {};
32
+ const across = width(layout),
33
+ out = [];
31
34
  rows.forEach((r, row) => {
32
35
  const { keys: list = [], ...shared } = r;
33
- let x = -across / 2, col = 0;
36
+ let x = -across / 2,
37
+ col = 0;
34
38
  list.forEach((k) => {
35
- if (isGap(k)) { x += k.gap; return; }
39
+ if (isGap(k)) {
40
+ x += k.gap;
41
+ return;
42
+ }
36
43
  const props = { ...defaults, ...shared, ...key(k) };
37
44
  for (const [n, v] of Object.entries(props)) if (v === '@label') props[n] = props.label; // e.g. legend: the key's label
38
- out.push({ ...props, row: props.row ?? row, index: row, col: col++, units: props.u, x: x + props.u / 2, y: (rows.length - 1) / 2 - row });
45
+ out.push({
46
+ ...props,
47
+ row: props.row ?? row,
48
+ index: row,
49
+ col: col++,
50
+ units: props.u,
51
+ x: x + props.u / 2,
52
+ y: (rows.length - 1) / 2 - row,
53
+ });
39
54
  x += props.u;
40
55
  });
41
56
  });
@@ -44,14 +59,20 @@ export function keys(layout) {
44
59
 
45
60
  // width(layout): the widest row, in key units; depth(layout): the number of rows
46
61
  /** The widest row, in key units. @param {Layout} layout @returns {number} */
47
- export const width = (layout) => Math.max(...(layout.rows || []).map(rowOf).map((r) => (r.keys || []).reduce((s, k) => s + (isGap(k) ? k.gap : key(k).u), 0)));
62
+ export const width = (layout) =>
63
+ Math.max(
64
+ ...(layout.rows || []).map(rowOf).map((r) => (r.keys || []).reduce((s, k) => s + (isGap(k) ? k.gap : key(k).u), 0)),
65
+ );
48
66
  /** The number of rows. @param {Layout} layout @returns {number} */
49
67
  export const depth = (layout) => (layout.rows || []).length;
50
68
 
51
69
  // project(layout, fields): only the fields something reads, key by key, so a change to anything else (a legend, when
52
70
  // only positions matter) isn't a change to it. Positions always count.
53
71
  /** Only the fields something reads, key by key (positions always). @param {Layout} layout @param {string[]} [fields] @returns {object[]} */
54
- export const project = (layout, fields = []) => keys(layout).map((k) => Object.fromEntries(['row', 'index', 'col', 'u', 'x', 'y', ...fields].map((f) => [f, k[f] ?? null]))); // (x and y too: a gap moves the keys after it)
72
+ export const project = (layout, fields = []) =>
73
+ keys(layout).map((k) =>
74
+ Object.fromEntries(['row', 'index', 'col', 'u', 'x', 'y', ...fields].map((f) => [f, k[f] ?? null])),
75
+ ); // (x and y too: a gap moves the keys after it)
55
76
 
56
77
  /**
57
78
  * A layout from keyboard-layout-editor.com: its JSON download, or the "Raw data" text. Rows, widths and the gaps
@@ -64,10 +85,14 @@ export const project = (layout, fields = []) => keys(layout).map((k) => Object.f
64
85
  export function fromKLE(kle) {
65
86
  const data = typeof kle === 'string' ? parseRaw(kle) : kle;
66
87
  if (!Array.isArray(data)) throw new Error("this isn't a keyboard-layout-editor layout (a list of rows)");
67
- const notes = new Set(), rows = [];
88
+ const notes = new Set(),
89
+ rows = [];
68
90
  let title;
69
91
  for (const row of data) {
70
- if (!Array.isArray(row)) { if (row && typeof row === 'object') title = row.name; continue; } // the layout's own settings
92
+ if (!Array.isArray(row)) {
93
+ if (row && typeof row === 'object') title = row.name;
94
+ continue;
95
+ } // the layout's own settings
71
96
  const keys = [];
72
97
  let next = {};
73
98
  for (const item of row) {
@@ -78,8 +103,10 @@ export function fromKLE(kle) {
78
103
  continue;
79
104
  }
80
105
  if (next.x) keys.push({ gap: next.x });
81
- if ((next.h && next.h !== 1) || next.w2 || next.h2 || next.x2 || next.y2) notes.add(`${legendOf(item).label || 'a key'} is tall or stepped: placed as ${next.w || 1}u wide`);
82
- const { label, legend2 } = legendOf(item), u = next.w || 1;
106
+ if ((next.h && next.h !== 1) || next.w2 || next.h2 || next.x2 || next.y2)
107
+ notes.add(`${legendOf(item).label || 'a key'} is tall or stepped: placed as ${next.w || 1}u wide`);
108
+ const { label, legend2 } = legendOf(item),
109
+ u = next.w || 1;
83
110
  keys.push(legend2 ? { label, u, legend2 } : u === 1 ? label : [label, u]);
84
111
  next = {};
85
112
  }
@@ -92,13 +119,24 @@ export function fromKLE(kle) {
92
119
  function parseRaw(text) {
93
120
  // (one row on its own is a list with no lists in it: a three-key macropad's raw data. It is a layout of one row)
94
121
  const rows = (data) => (Array.isArray(data) && data.length && !data.some(Array.isArray) ? [data] : data);
95
- try { return rows(JSON.parse(text)); } catch {}
122
+ try {
123
+ return rows(JSON.parse(text));
124
+ } catch {
125
+ // (not JSON as written: read below as a raw KLE text, its keys unquoted)
126
+ }
96
127
  const quoted = `[${text}]`.replace(/([{,]\s*)([A-Za-z_][\w]*)\s*:/g, '$1"$2":');
97
- try { return rows(JSON.parse(quoted)); } catch { throw new Error("this isn't keyboard-layout-editor data: paste its Raw data, or use its JSON download"); }
128
+ try {
129
+ return rows(JSON.parse(quoted));
130
+ } catch {
131
+ throw new Error("this isn't keyboard-layout-editor data: paste its Raw data, or use its JSON download");
132
+ }
98
133
  }
99
134
 
100
135
  // A key's legends: "Q", or "!\n1" (top-left shifted, bottom-left main), up to twelve positions
101
136
  function legendOf(text) {
102
- const all = String(text).split('\n').map((l) => l.trim()).filter(Boolean);
137
+ const all = String(text)
138
+ .split('\n')
139
+ .map((l) => l.trim())
140
+ .filter(Boolean);
103
141
  return all.length > 1 ? { label: all[all.length - 1], legend2: all[0] } : { label: all[0] || '' };
104
142
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rulecad/keyboard-layout",
3
- "version": "0.1.5",
3
+ "version": "0.2.0",
4
4
  "description": "A plain JSON keyboard layout format: rows of keys with widths and any per-key properties.",
5
5
  "type": "module",
6
6
  "exports": {
@@ -20,7 +20,7 @@
20
20
  "mechanical-keyboard"
21
21
  ],
22
22
  "engines": {
23
- "node": ">=20"
23
+ "node": ">=22.13"
24
24
  },
25
25
  "sideEffects": false,
26
26
  "repository": {