Skip to content

Commit

Permalink
improve kebab case and camel case translations
Browse files Browse the repository at this point in the history
  • Loading branch information
maarteNNNN committed Jan 19, 2023
1 parent 16ea211 commit a3bbeff
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,7 @@ class REPLClient {
if (!str) return;
if (typeof str !== 'string')
throw new TypeError('Not a string or no string given');
return str.replace(/([a-z0-9]|(?=[A-Z]))([A-Z])/g, '$1-$2').toLowerCase();
return str.replace(/\B([A-Z])/g, '-$1').toLowerCase();
}

/**
Expand All @@ -601,7 +601,7 @@ class REPLClient {
throw new TypeError('Not a string or no string given');
return str.includes('--')
? str.replace('--', '')
: str.replace(/-./g, (x) => x.toUpperCase()[1]);
: str.replace(/-(\w)/g, (_, c) => (c ? c.toUpperCase() : ''));
}

/**
Expand Down
58 changes: 57 additions & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ describe('REPL Client tests', () => {

const commands = {
options: [
{ option: { short: 'v', long: 'version' }, help: 'Display current version' },
{
option: { short: 'v', long: 'version' },
help: 'Display current version',
},
],
command: {
execute: () => {},
Expand Down Expand Up @@ -298,4 +301,57 @@ describe('REPL Client tests', () => {
chai.expect(e).to.not.throw();
}
});

it('it correctly translates kebab-case to camelCase', async () => {
try {
const testCli = await initiateCli(
{ ...exampleOptions },
{
_: ['testing-this', 'testing-a-smaller-argument'],
// help: true,
},
);

const commands = {
testingThis: {
testingASmallerArgument: {
execute: ({ argument, options }) => {},
help: 'test this help',
},
},
};

await testCli.run(commands);
} catch (e) {
chai.expect(e).to.not.throw();
}
});

it('it throws if the kebab-case is off', async () => {
try {
const testCli = await initiateCli(
{ ...exampleOptions },
{
// Wrong
_: ['testing-this', 'testing-asmaller-argument'],
// help: true,
},
);

const commands = {
testingThis: {
testingASmallerArgument: {
execute: ({ argument, options }) => {},
help: 'test this help',
},
},
};

await testCli.run(commands);
} catch (e) {
chai
.expect(e.message)
.to.be.eql('command is invalid and needs more arguments');
}
});
});

0 comments on commit a3bbeff

Please sign in to comment.