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

refactor: use export function #279

Merged
merged 1 commit into from
Dec 7, 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
2 changes: 1 addition & 1 deletion src/choose.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { Choice } from './choice'
* @returns choices[i].content
* @throws random <= 0, random < 1 or choices.weight are not natural integers.
*/
export const chooseOne = (choices: Choice[], random: number): string => {
export function chooseOne(choices: Choice[], random: number): string {
if (isNaN(random) || random < 0 || 1 <= random)
throw RangeError('random arg should be 0 <= random < 1.')
const sortedChoices = choices.sort((l, r) => r.weight - l.weight)
Expand Down
19 changes: 7 additions & 12 deletions src/input.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
import type { InputOptions } from '@actions/core'
import { getInput } from '@actions/core'

import type { Choice } from './choice'

/** Gets the values of an multi-line input.
* @param name name of the input to get
* @param options optional. See InputOptions.
* @returns string[]
*/
const getMultipleInput = (name: string, options?: InputOptions): string[] =>
getInput(name, options)
.split('\n')
.filter(x => x !== '')

/** Gets the values of user inputs.
* @returns Parsed user inputs
* @throws contents is empty, weights is not natural number,
* or contents length not equals weights one.
* @see {@link ../action.yml}
*/
export const getInputs = (): Choice[] => {
export function getInputs(): Choice[] {
type InputOptions = Parameters<typeof getInput>[1]
const getMultipleInput = (name: string, options?: InputOptions) =>
getInput(name, options)
.split('\n')
.filter(x => x !== '')

const contents = getMultipleInput('contents', { required: true })
const weights = getMultipleInput('weights').map(s => parseInt(s.trim(), 10))

Expand Down