Skip to content

Commit

Permalink
fix(prompt): fix default value (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Jul 15, 2020
1 parent 6eacfa3 commit 805f5a1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
10 changes: 5 additions & 5 deletions packages/prompt/lib/generic-prompt.ts
Expand Up @@ -148,16 +148,16 @@ export abstract class GenericPrompt<T, V, S extends GenericPromptSettings<T, V>>
}

protected transformValue( value: V ): T | undefined {

if ( !value && typeof this.settings.default !== 'undefined' ) {
return this.settings.default;
}

return this.settings.transform ? this.settings.transform( value ) : this.transform( value );
}

protected async validateValue( value: V ): Promise<boolean> {

if ( !value && typeof this.settings.default !== 'undefined' ) {
this.value = this.settings.default;
return true;
}

const validation = await ( this.settings.validate ? this.settings.validate( value ) : this.validate( value ) );

if ( validation === false ) {
Expand Down
15 changes: 13 additions & 2 deletions packages/prompt/test/input_test.ts
Expand Up @@ -11,12 +11,23 @@ Deno.test( 'prompt input: value', async () => {

Deno.test( 'prompt input: validate option', async () => {
console.log();
Input.inject( 'a'.repeat( 9 ) );
Input.inject( 'foo' );
const result: string | undefined = await Input.prompt( {
message: 'message',
validate: value => value.length < 10
} );
assertEquals( result, 'a'.repeat( 9 ) );
assertEquals( result, 'foo' );
} );

Deno.test( 'prompt input: default value', async () => {
console.log();
Input.inject( '' );
const result: string | undefined = await Input.prompt( {
message: 'message',
default: 'default',
validate: value => value.length < 10
} );
assertEquals( result, 'default' );
} );

Deno.test( 'prompt input: empty value', async () => {
Expand Down

0 comments on commit 805f5a1

Please sign in to comment.