-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from seamapi/issue-#43
Support for adding array parameters e.g. access_codes/list editing parameter access_code_ids
- Loading branch information
Showing
2 changed files
with
67 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import prompts from "prompts" | ||
|
||
export const interactForArray = async ( | ||
array: string[], | ||
message: string | ||
): Promise<string[]> => { | ||
let updatedArray = [...array] | ||
|
||
const displayList = () => { | ||
console.log(`${message} Current list:`) | ||
if (updatedArray.length > 0) { | ||
updatedArray.forEach((item, index) => { | ||
console.log(`${index + 1}: ${item}`) | ||
}) | ||
} else { | ||
console.log("The list is currently empty.") | ||
} | ||
} | ||
|
||
let action: string | ||
do { | ||
displayList() | ||
|
||
const response = await prompts({ | ||
type: "select", | ||
name: "action", | ||
message: "Choose an action:", | ||
choices: [ | ||
{ title: "Add an item", value: "add" }, | ||
{ title: "Remove an item", value: "remove" }, | ||
{ title: "Finish editing", value: "done" }, | ||
], | ||
}) | ||
|
||
action = response.action | ||
|
||
if (action === "add") { | ||
const { newItem } = await prompts({ | ||
type: "text", | ||
name: "newItem", | ||
message: "Enter the new item:", | ||
}) | ||
if (newItem) { | ||
updatedArray.push(newItem) | ||
} | ||
} else if (action === "remove") { | ||
const { index } = await prompts({ | ||
type: "number", | ||
name: "index", | ||
message: "Enter the index of the item to remove:", | ||
validate: (value) => | ||
value > 0 && value <= updatedArray.length ? true : "Invalid index", | ||
}) | ||
if (index) { | ||
updatedArray.splice(index - 1, 1) | ||
} | ||
} | ||
} while (action !== "done") | ||
|
||
return updatedArray | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters