Skip to content

Commit

Permalink
feat: add ability to reset injected answers
Browse files Browse the repository at this point in the history
  • Loading branch information
bngo97 committed Aug 2, 2024
1 parent 095785d commit 76e51e7
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@ function inject(answers) {
prompt._injected = (prompt._injected || []).concat(answers);
}

function resetInjectedAnswers() {
prompt._injected = [];
}

function override(answers) {
prompt._override = Object.assign({}, answers);
}

module.exports = Object.assign(prompt, { prompt, prompts, inject, override });
module.exports = Object.assign(prompt, { prompt, prompts, inject, resetInjectedAnswers, override });
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@withgraphite/prompts",
"version": "0.0.3",
"version": "0.0.4",
"description": "Lightweight, beautiful and user-friendly prompts",
"license": "MIT",
"repository": {
Expand Down
25 changes: 25 additions & 0 deletions test/prompts.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,28 @@ test('injects', t => {
});
});
});

test('resetInjectedAnswers', t => {
let injected = [ 1, 2, 3 ];
prompt.inject(injected);
t.same(prompt._injected, injected, 'injects array of answers');

prompt({ type: 'text', name:'a', message: 'a message' })
.then(foo => {
t.same(foo, { a:1 }, 'immediately returns object with injected answer');
t.same(prompt._injected, [ 2, 3 ], 'deletes the first answer from internal array');

prompt.resetInjectedAnswers();
t.same(prompt._injected, [], 'deletes everything from internal array');

let secondInjected = [ 4, 5 ];
prompt.inject(secondInjected)
t.same(prompt._injected, secondInjected, 'injects second array of answers');
prompt([{ type: 'text', name:'b', message: 'b message' }, { type: 'text', name:'c', message: 'c message' }])
.then(bar => {
t.same(bar, { b:4, c:5 }, 'immediately handles two prompts at once with new injected answers');
t.same(prompt._injected, [], 'leaves behind empty internal array when exhausted');
t.end();
});
});
});

0 comments on commit 76e51e7

Please sign in to comment.