@jinshuju/cli 0.29.1 → 0.31.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/dist/help.js CHANGED
@@ -91,11 +91,42 @@ export function commandHelp(command, style = PLAIN) {
91
91
  }
92
92
  return `${parts.join('\n')}\n`;
93
93
  }
94
+ /**
95
+ * How far a command is from what was typed after the resource: the words it
96
+ * shares with it count most, and the edits that turn one word into the other
97
+ * break ties. `artifact get <id>` is nearest `artifact share get`, `field rmove`
98
+ * nearest `field remove`.
99
+ */
100
+ function distanceTo(command, words) {
101
+ const wanted = command.path.slice(1);
102
+ const typed = words.slice(1, 1 + Math.max(wanted.length, 1));
103
+ const shared = wanted.filter((word) => typed.includes(word)).length;
104
+ const edits = wanted.reduce((sum, word, index) => sum + editDistance(word, typed[index] ?? ''), 0);
105
+ return edits - 100 * shared;
106
+ }
107
+ function editDistance(a, b) {
108
+ let previous = Array.from({ length: b.length + 1 }, (_, index) => index);
109
+ for (let i = 1; i <= a.length; i += 1) {
110
+ const current = [i];
111
+ for (let j = 1; j <= b.length; j += 1) {
112
+ current[j] = Math.min(previous[j] + 1, current[j - 1] + 1, previous[j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
113
+ }
114
+ previous = current;
115
+ }
116
+ return previous[b.length];
117
+ }
94
118
  /** What the caller typed that matched nothing, and the nearest things that do. */
95
119
  export function unknownCommandHelp(words, commands = COMMANDS, style = PLAIN) {
96
120
  const typed = words.join(' ');
97
121
  const resource = words[0];
98
- const siblings = commands.filter((command) => command.path[0] === resource).slice(0, 10);
122
+ // Nearest first: a resource has more commands than the list shows, and the one
123
+ // meant used to be cut off whenever it was declared late.
124
+ const siblings = commands
125
+ .filter((command) => command.path[0] === resource)
126
+ .map((command, order) => ({ command, order, distance: distanceTo(command, words) }))
127
+ .sort((a, b) => a.distance - b.distance || a.order - b.order)
128
+ .slice(0, 10)
129
+ .map(({ command }) => command);
99
130
  return [
100
131
  `Unknown command: jinshuju ${typed}`,
101
132
  ...(siblings.length > 0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@jinshuju/cli",
3
- "version": "0.29.1",
3
+ "version": "0.31.0",
4
4
  "description": "Command line interface for Jinshuju Open API v1",
5
5
  "homepage": "https://github.com/jinshuju/cli#readme",
6
6
  "bugs": {