Skip to content

Commit 471ac62

Browse files
authored
Merge pull request #33 from imLymei/dev
feat: add default value to spread
2 parents 94ffc83 + b9ebd2a commit 471ac62

File tree

1 file changed

+29
-18
lines changed

1 file changed

+29
-18
lines changed

src/lib/utils.ts

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,24 @@ export function formatCommand(
123123

124124
// TODO - make linting function
125125

126-
if (hasDefaultValue) {
126+
if (isSpread) {
127+
if (index !== commandArgs.length - 1)
128+
error('You can only use a spread argument at the last position');
129+
130+
let name: string;
131+
let defaultValue: string;
132+
133+
if (hasDefaultValue)
134+
[name, defaultValue] = arg.split('??').map((str) => str.trim());
135+
136+
name = arg.replace('...', '').trim();
137+
object.name = name;
138+
object.isOptional = true;
139+
object.isSpread = true;
140+
object.defaultValue = defaultValue;
141+
142+
allowRequired = false;
143+
} else if (hasDefaultValue) {
127144
const [name, defaultValue] = arg.split('??');
128145
object.name = name.trim();
129146
object.defaultValue = defaultValue.trim();
@@ -143,16 +160,6 @@ export function formatCommand(
143160
object.name = name;
144161
object.isOptional = true;
145162

146-
allowRequired = false;
147-
} else if (isSpread) {
148-
if (index !== commandArgs.length - 1)
149-
error('You can only use a spread argument at the last position');
150-
151-
const name = arg.replace('...', '').trim();
152-
object.name = name;
153-
object.isOptional = true;
154-
object.isSpread = true;
155-
156163
allowRequired = false;
157164
} else {
158165
if (!allowRequired)
@@ -177,13 +184,17 @@ export function formatCommand(
177184
for (let index = 0; index < commandArguments.length; index++) {
178185
const arg = commandArguments[index];
179186

180-
let argValue = arg.isSpread
181-
? args.slice(index).join(' ')
182-
: arg.alternativeValue
183-
? args[index]
184-
? arg.defaultValue
185-
: arg.alternativeValue
186-
: args[index] ?? arg.defaultValue ?? '';
187+
let argValue: string;
188+
189+
if (arg.isSpread) {
190+
argValue = args.slice(index).join(' ');
191+
} else if (arg.alternativeValue) {
192+
argValue = args[index] ? arg.defaultValue : arg.alternativeValue;
193+
} else {
194+
argValue = args[index];
195+
}
196+
197+
if (!argValue) argValue = arg.defaultValue ?? '';
187198

188199
splitCommand[index * 2 + 1] = argValue;
189200
}

0 commit comments

Comments
 (0)