@lionad/port-key 0.2.0 → 0.4.1

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
@@ -96,6 +96,7 @@ npx -y @lionad/port-key-mcp
96
96
  - `-m, --map <object>`: custom mapping (JSON or JS-like object literal)
97
97
  - `--lang <code>`: output language (currently only `en` and `cn`, default: `cn`)
98
98
  - `-d, --digits <count>`: preferred digit count for port (4 or 5, default: 4)
99
+ - `--padding-zero <true|false>`: Pad short ports with zero (default: true). e.g. "air" -> 1840
99
100
  - `-h, --help`: show help
100
101
 
101
102
  Examples:
@@ -122,6 +123,8 @@ A full Example:
122
123
  {
123
124
  // Preferred digit count for port (4 or 5)
124
125
  "preferDigitCount": 5,
126
+ // Pad short ports with zero (default: true)
127
+ "paddingZero": true,
125
128
  // Custom letter-to-digit mapping
126
129
  "blockedPorts": [3000, 3001, 3002, 6666],
127
130
  // Port range limits (inclusive)
package/locales/cn.json CHANGED
@@ -7,6 +7,7 @@
7
7
  "optMap": " -m, --map <对象> 自定义映射(JSON 或 JS 对象字面量)",
8
8
  "optLang": " --lang <代码> 输出语言(en 或 cn)",
9
9
  "optDigits": " -d, --digits <位数> 端口优先位数(4 或 5,默认:4)",
10
+ "optPadding": " --padding-zero <true|false> 是否为短端口号补零(默认:true)",
10
11
  "optHelp": " -h, --help 显示帮助",
11
12
  "examples": "示例:",
12
13
  "ex1": " npx @lionad/port-key cfetch # -> 34353",
package/locales/en.json CHANGED
@@ -7,6 +7,7 @@
7
7
  "optMap": " -m, --map <object> Custom mapping (JSON or JS-like object literal)",
8
8
  "optLang": " --lang <code> Output language (en or cn)",
9
9
  "optDigits": " -d, --digits <count> Preferred digit count for port (4 or 5, default: 4)",
10
+ "optPadding": " --padding-zero <true|false> Pad short ports with zero (default: true)",
10
11
  "optHelp": " -h, --help Show help",
11
12
  "examples": "Examples:",
12
13
  "ex1": " npx @lionad/port-key cfetch # -> 34353",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lionad/port-key",
3
- "version": "0.2.0",
3
+ "version": "0.4.1",
4
4
  "description": "A simple, practical port naming strategy",
5
5
  "type": "module",
6
6
  "types": "./src/port-key.d.ts",
package/src/cli.js CHANGED
@@ -18,6 +18,7 @@ function formatHelp(lang = 'cn') {
18
18
  t.optMap,
19
19
  t.optLang,
20
20
  t.optDigits,
21
+ t.optPadding,
21
22
  t.optHelp,
22
23
  '',
23
24
  t.examples,
@@ -35,6 +36,7 @@ function parseArgv(argv) {
35
36
  let showHelp = false;
36
37
  let lang;
37
38
  let preferDigitCount;
39
+ let paddingZero;
38
40
  const positionals = [];
39
41
 
40
42
  let i = 0;
@@ -78,6 +80,25 @@ function parseArgv(argv) {
78
80
  continue;
79
81
  }
80
82
 
83
+ if (token === '--padding-zero') {
84
+ const value = args[i + 1];
85
+ if (value === 'false') {
86
+ paddingZero = false;
87
+ i += 2;
88
+ } else if (value === 'true') {
89
+ paddingZero = true;
90
+ i += 2;
91
+ } else if (!value || value.startsWith('-')) {
92
+ // Treated as boolean flag (true) if no value provided
93
+ paddingZero = true;
94
+ i += 1;
95
+ } else {
96
+ // Fallback for unexpected values
97
+ throw new Error('Invalid value for --padding-zero. Use true or false.');
98
+ }
99
+ continue;
100
+ }
101
+
81
102
  positionals.push(token);
82
103
  i += 1;
83
104
  }
@@ -87,6 +108,7 @@ function parseArgv(argv) {
87
108
  lang,
88
109
  showHelp,
89
110
  preferDigitCount,
111
+ paddingZero,
90
112
  input: positionals.join(' '),
91
113
  };
92
114
  }
@@ -125,6 +147,7 @@ function runCli(argv, stdout = process.stdout, stderr = process.stderr, deps = {
125
147
  map: parsed.map !== DEFAULT_MAP ? parsed.map : undefined,
126
148
  lang: parsed.lang,
127
149
  preferDigitCount: parsed.preferDigitCount,
150
+ paddingZero: parsed.paddingZero,
128
151
  });
129
152
 
130
153
  const lang = getLangOrDefault(effective.lang);
@@ -151,6 +174,7 @@ function runCli(argv, stdout = process.stdout, stderr = process.stderr, deps = {
151
174
  maxPort: effective.maxPort,
152
175
  preferredRanges: effective.preferredRanges,
153
176
  preferDigitCount: effective.preferDigitCount,
177
+ paddingZero: effective.paddingZero,
154
178
  });
155
179
 
156
180
  if (result.port === null) {
package/src/config.js CHANGED
@@ -54,6 +54,7 @@ function mergeConfig(base, override) {
54
54
  maxPort: b.maxPort ?? a.maxPort,
55
55
  preferredRanges: b.preferredRanges ?? a.preferredRanges,
56
56
  preferDigitCount: b.preferDigitCount ?? a.preferDigitCount,
57
+ paddingZero: b.paddingZero ?? a.paddingZero,
57
58
  lang: b.lang ?? a.lang,
58
59
  };
59
60
  }
package/src/port-key.js CHANGED
@@ -97,19 +97,45 @@ function isPortBlocked(port, blockedPorts) {
97
97
  function pickPortFromDigits(digits, options = {}) {
98
98
  const raw = String(digits || '').replace(/[^0-9]/g, '');
99
99
  if (!raw) return { port: null, reason: 'No digits found in input' };
100
- if (raw.length < 2) return { port: null, reason: 'Not enough digits to form a candidate' };
100
+
101
+ const paddingZero = options.paddingZero !== false; // Default true
102
+
103
+ // If not padding, enforce strict length check
104
+ // If padding, allow short length as it will be padded
105
+ if (!paddingZero && raw.length < 2) return { port: null, reason: 'Not enough digits to form a candidate' };
101
106
 
102
107
  const minPort = Number.isFinite(options.minPort) ? options.minPort : 0;
103
108
  const maxPort = Number.isFinite(options.maxPort) ? options.maxPort : 65535;
104
109
  const blockedPorts = options.blockedPorts || DEFAULT_BLOCKED_PORTS;
105
110
  const preferDigitCount = options.preferDigitCount || 4;
111
+ // paddingZero is already defined above
106
112
 
107
113
  const candidates = [];
108
114
  const normalized = raw.replace(/^0+/, '');
115
+
116
+ // Padding Logic for short inputs like "air" (184)
117
+ // If normalized length is small, we can pad it with zeros to match preferDigitCount or more
118
+ const paddedCandidates = [];
119
+ if (paddingZero && normalized.length > 0 && normalized.length < preferDigitCount) {
120
+ let current = normalized;
121
+ // Fix: start padding loop immediately
122
+ while (current.length <= 5) {
123
+ // Only push if length >= 2 (valid port min length logic) and >= preferDigitCount (if we want to respect preference)
124
+ // Actually, the original requirement says "pad ... to match preferDigitCount or more".
125
+ if (current.length >= preferDigitCount) {
126
+ paddedCandidates.push(current);
127
+ }
128
+ current += '0';
129
+ }
130
+ }
131
+
109
132
  if (preferDigitCount && normalized.length >= preferDigitCount) {
110
133
  candidates.push(normalized.slice(0, preferDigitCount));
111
134
  candidates.push(normalized.slice(normalized.length - preferDigitCount));
112
135
  } else {
136
+ // If not padding, try smaller lengths (if >= 2)
137
+ // Note: if paddedCandidates is empty, we must rely on this.
138
+ // But if we have paddedCandidates, we can still add these as fallbacks.
113
139
  for (let len = Math.min(normalized.length, preferDigitCount); len >= 2; len -= 1) {
114
140
  candidates.push(normalized.slice(0, len));
115
141
  }
@@ -117,6 +143,9 @@ function pickPortFromDigits(digits, options = {}) {
117
143
  candidates.push(normalized.slice(normalized.length - len));
118
144
  }
119
145
  }
146
+
147
+ // Merge padded candidates
148
+ candidates.push(...paddedCandidates);
120
149
 
121
150
  const unique = Array.from(new Set(candidates));
122
151
  const rejectedCandidates = [];