Skip to content

Commit

Permalink
refactor: return maskedValue to masked
Browse files Browse the repository at this point in the history
  • Loading branch information
Mist3rBru committed Sep 27, 2023
1 parent 9015ed7 commit 3192bf7
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
20 changes: 10 additions & 10 deletions packages/core/__tests__/prompts/password.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ describe('PasswordPrompt', () => {
makeSut();
mock.emit('value', value);

expect(mock.maskedValue).toBe(value.replace(/./g, '•'));
expect(mock.masked).toBe(value.replace(/./g, '•'));
});

it('should mask value with custom mask', () => {
Expand All @@ -37,7 +37,7 @@ describe('PasswordPrompt', () => {
});
mock.emit('value', value);

expect(mock.maskedValue).toBe(value.replace(/./g, '*'));
expect(mock.masked).toBe(value.replace(/./g, '*'));
});

it('should change cursor position when cursor changes', () => {
Expand All @@ -48,30 +48,30 @@ describe('PasswordPrompt', () => {
initialValue: value,
});

expect(mock.valueWithCursor).toBe(mock.maskedValue + cursor);
expect(mock.valueWithCursor).toBe(mock.masked + cursor);

cursorIndex--;
mock.setCursor(cursorIndex);
mock.emit('value', value);
expect(mock.valueWithCursor).toBe(
mock.maskedValue.slice(0, cursorIndex) +
color.inverse(mock.maskedValue[cursorIndex]) +
mock.maskedValue.slice(cursorIndex + 1)
mock.masked.slice(0, cursorIndex) +
color.inverse(mock.masked[cursorIndex]) +
mock.masked.slice(cursorIndex + 1)
);

cursorIndex--;
mock.setCursor(cursorIndex);
mock.emit('value', value);
expect(mock.valueWithCursor).toBe(
mock.maskedValue.slice(0, cursorIndex) +
color.inverse(mock.maskedValue[cursorIndex]) +
mock.maskedValue.slice(cursorIndex + 1)
mock.masked.slice(0, cursorIndex) +
color.inverse(mock.masked[cursorIndex]) +
mock.masked.slice(cursorIndex + 1)
);

cursorIndex += 2;
mock.setCursor(cursorIndex);
mock.emit('value', value);
expect(mock.valueWithCursor).toBe(mock.maskedValue + cursor);
expect(mock.valueWithCursor).toBe(mock.masked + cursor);
});

it('should submit value', () => {
Expand Down
14 changes: 7 additions & 7 deletions packages/core/src/prompts/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class PasswordPrompt extends Prompt {
return this._cursor;
}

get maskedValue(): string {
get masked(): string {
return this.value.replaceAll(/./g, this._mask);
}

Expand All @@ -24,22 +24,22 @@ export default class PasswordPrompt extends Prompt {

this.value = '';
this._mask = mask ?? '•';
this.valueWithCursor = this.maskedValue + color.inverse(color.hidden('_'));
this.valueWithCursor = this.masked + color.inverse(color.hidden('_'));

this.exposeTestUtils();

this.on('finalize', () => {
this.valueWithCursor = this.maskedValue;
this.valueWithCursor = this.masked;
this.exposeTestUtils();
});

this.on('value', (value) => {
this.value = value;
if (this.cursor >= this.value.length) {
this.valueWithCursor = `${this.maskedValue}${color.inverse(color.hidden('_'))}`;
this.valueWithCursor = `${this.masked}${color.inverse(color.hidden('_'))}`;
} else {
const s1 = this.maskedValue.slice(0, this.cursor);
const s2 = this.maskedValue.slice(this.cursor);
const s1 = this.masked.slice(0, this.cursor);
const s2 = this.masked.slice(this.cursor);
this.valueWithCursor = `${s1}${color.inverse(s2[0])}${s2.slice(1)}`;
}
this.exposeTestUtils();
Expand All @@ -49,7 +49,7 @@ export default class PasswordPrompt extends Prompt {
private exposeTestUtils() {
exposeTestUtils<PasswordPrompt>({
cursor: this.cursor,
maskedValue: this.maskedValue,
masked: this.masked,
valueWithCursor: this.valueWithCursor,
});
}
Expand Down
8 changes: 4 additions & 4 deletions packages/prompts/__tests__/prompts/password.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe('password', () => {
mock.submit(value);

expect(mock.state).toBe('submit');
expect(mock.frame).toBe(`${title}${color.gray(S_BAR)} ${color.dim(mock.maskedValue)}`);
expect(mock.frame).toBe(`${title}${color.gray(S_BAR)} ${color.dim(mock.masked)}`);
});

it('should render cancel', () => {
Expand All @@ -64,9 +64,9 @@ describe('password', () => {

expect(mock.state).toBe('cancel');
expect(mock.frame).toBe(
`${title}${color.gray(S_BAR)} ${color.strikethrough(
color.dim(mock.maskedValue)
)}\n${color.gray(S_BAR)}`
`${title}${color.gray(S_BAR)} ${color.strikethrough(color.dim(mock.masked))}\n${color.gray(
S_BAR
)}`
);
});

Expand Down
12 changes: 6 additions & 6 deletions packages/prompts/src/prompts/password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,21 @@ const password = (opts: PasswordOptions) => {
mask: opts.mask ?? S_PASSWORD_MASK,
render() {
const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`;
const maskedValue = this.valueWithCursor;
const masked = this.valueWithCursor;

switch (this.state) {
case 'error':
return `${title.trim()}\n${color.yellow(S_BAR)} ${maskedValue}\n${color.yellow(
return `${title.trim()}\n${color.yellow(S_BAR)} ${masked}\n${color.yellow(
S_BAR_END
)} ${color.yellow(this.error)}\n`;
case 'submit':
return `${title}${color.gray(S_BAR)} ${color.dim(maskedValue)}`;
return `${title}${color.gray(S_BAR)} ${color.dim(masked)}`;
case 'cancel':
return `${title}${color.gray(S_BAR)} ${
maskedValue ? color.strikethrough(color.dim(maskedValue)) : ''
}${maskedValue ? '\n' + color.gray(S_BAR) : ''}`;
masked ? color.strikethrough(color.dim(masked)) : ''
}${masked ? '\n' + color.gray(S_BAR) : ''}`;
default:
return `${title}${color.cyan(S_BAR)} ${maskedValue}\n${color.cyan(S_BAR_END)}\n`;
return `${title}${color.cyan(S_BAR)} ${masked}\n${color.cyan(S_BAR_END)}\n`;
}
},
}).prompt() as Promise<string | symbol>;
Expand Down

0 comments on commit 3192bf7

Please sign in to comment.