Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

woof command language selection works #1441

Merged
merged 2 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions src/cli/commands/woof.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { MethodArgs, ArgsOptions } from '../args';

interface WoofOptions {
language?: string; // ISO 639-1 code
}

const woofs = {
en: 'Woof!',
he: ' !הב ',
Expand All @@ -12,13 +8,22 @@ const woofs = {
cs: ' Haf!',
};

export = function woof(...args0: MethodArgs) {
const args = [...args0];
let options: WoofOptions = {};
if (typeof args[args.length - 1] === 'object') {
options = (args.pop() as ArgsOptions) as WoofOptions;
export function getWoofLanguage(args: MethodArgs): string {
const options = args.pop() as ArgsOptions;
let lang = 'en';

if (
typeof options.language === 'string' &&
Object.keys(woofs).includes(options.language)
) {
lang = options.language;
}
const lang = options.language || 'en';

return lang;
}

export default function woof(...args: MethodArgs) {
const lang = getWoofLanguage(args);
console.log(`
| |
/| |\\
Expand All @@ -35,4 +40,4 @@ export = function woof(...args0: MethodArgs) {
\\U/ --( ${woofs[lang]} )
\\-----/
`);
};
}
28 changes: 28 additions & 0 deletions test/woof.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { getWoofLanguage } from '../src/cli/commands/woof';

describe('Woof command - Language option', () => {
it('Default language is "en"', () => {
// $ snyk woof
expect(getWoofLanguage([{} as any])).toEqual('en');
});

it('Returns selected language', () => {
expect(
getWoofLanguage([
{
language: 'he',
} as any,
]),
).toEqual('he');
});

it('Returns default when selected language is invalid', () => {
expect(
getWoofLanguage([
{
language: 'toString',
} as any,
]),
).toEqual('en');
});
});