Skip to content

Commit

Permalink
Merge pull request #33 from imLymei/dev
Browse files Browse the repository at this point in the history
feat: add default value to spread
  • Loading branch information
imLymei authored Sep 27, 2024
2 parents 94ffc83 + b9ebd2a commit 471ac62
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,24 @@ export function formatCommand(

// TODO - make linting function

if (hasDefaultValue) {
if (isSpread) {
if (index !== commandArgs.length - 1)
error('You can only use a spread argument at the last position');

let name: string;
let defaultValue: string;

if (hasDefaultValue)
[name, defaultValue] = arg.split('??').map((str) => str.trim());

name = arg.replace('...', '').trim();
object.name = name;
object.isOptional = true;
object.isSpread = true;
object.defaultValue = defaultValue;

allowRequired = false;
} else if (hasDefaultValue) {
const [name, defaultValue] = arg.split('??');
object.name = name.trim();
object.defaultValue = defaultValue.trim();
Expand All @@ -143,16 +160,6 @@ export function formatCommand(
object.name = name;
object.isOptional = true;

allowRequired = false;
} else if (isSpread) {
if (index !== commandArgs.length - 1)
error('You can only use a spread argument at the last position');

const name = arg.replace('...', '').trim();
object.name = name;
object.isOptional = true;
object.isSpread = true;

allowRequired = false;
} else {
if (!allowRequired)
Expand All @@ -177,13 +184,17 @@ export function formatCommand(
for (let index = 0; index < commandArguments.length; index++) {
const arg = commandArguments[index];

let argValue = arg.isSpread
? args.slice(index).join(' ')
: arg.alternativeValue
? args[index]
? arg.defaultValue
: arg.alternativeValue
: args[index] ?? arg.defaultValue ?? '';
let argValue: string;

if (arg.isSpread) {
argValue = args.slice(index).join(' ');
} else if (arg.alternativeValue) {
argValue = args[index] ? arg.defaultValue : arg.alternativeValue;
} else {
argValue = args[index];
}

if (!argValue) argValue = arg.defaultValue ?? '';

splitCommand[index * 2 + 1] = argValue;
}
Expand Down

0 comments on commit 471ac62

Please sign in to comment.