Skip to content

woowacourse-projects/javascript-mission-utils

Repository files navigation

mission-utils

npm tested with jest

Install

with CDN

  1. 스크립트 삽입하기
<script src="https://cdn.jsdelivr.net/npm/@woowacourse/mission-utils@1.0.1/dist/mission-utils.min.js"></script>
<script type="module" src="index.js"></script>
  • index.html에 해당 스크립트 태그를 삽입해주세요.
  • 유틸 라이브러리의 경우 어플리케이션 스크립트 이전에 작성해야 합니다.
  1. 유틸 사용하기
// example
console.log(MissionUtils.Random.pickNumberInList([1, 2, 3]));
  • 스크립트 태그로 삽입된 경우 전역에 할당되어 MissionUtils.[util] 형태로 사용할 수 있습니다.

with npm

  1. 모듈 다운로드
npm i @woowacourse/mission-utils
  1. 모듈 사용하기

ES Modules 방식으로 사용하는 경우

import { MissionUtils } from "@woowacourse/mission-utils";

console.log(MissionUtils.Random.pickNumberInList([1, 2, 3]));

CommonJS 방식으로 사용하는 경우

const MissionUtils = require("@woowacourse/mission-utils");

console.log(MissionUtils.Random.pickNumberInList([1, 2, 3]));

Features

Console

readLine(query, callback)

주어진 질문을 화면에 출력하고, 사용자가 답변을 입력할 때까지 기다린 다음 입력된 답변을 인수로 전달하는 콜백 함수를 호출한다.

Console.readLine('닉네임을 입력해주세요.', (answer) => {
  console.log(`닉네임: ${answer}`);
});

readLineAsync(query)

주어진 질문을 화면에 출력하고, 사용자가 입력한 답변을 Promise를 통해 반환한다.

async function getUsername() {
  try {
    const username = await Console.readLineAsync('닉네임을 입력해주세요.');
  } catch (error) {
    // reject 되는 경우
  }
}

print(message)

주어진 문자열을 콘솔에 출력한다.

Console.print('안녕하세요.');

Random

pickNumberInRange(startInclusive, endInclusive)

숫자 범위를 지정하면 시작 또는 끝 숫자를 포함하여 범위의 숫자를 반환한다.

Random.pickNumberInRange(1, 10); // 1
Random.pickNumberInRange(1, 10); // 10
Random.pickNumberInRange(1, 10); // 4
Random.pickNumberInRange(1, 10); // 5

pickNumberInList(array)

목록에 있는 숫자 중 하나를 반환한다.

Random.pickNumberInList([1, 3, 10]); // 1
Random.pickNumberInList([1, 3, 10]); // 10
Random.pickNumberInList([1, 3, 10]); // 3

pickUniqueNumbersInRange(startInclusive, endInclusive, count)

숫자 범위 내에서 지정된 개수만큼 겹치지 않는 숫자를 반환한다.

Random.pickUniqueNumbersInRange(1, 10, 2); // [1, 2]
Random.pickUniqueNumbersInRange(1, 10, 5); // [1, 10, 7, 8, 5]

shuffle(array)

무작위로 섞인 새 목록을 반환한다.

Random.shuffle([1, 2, 3, 4, 5]); // [2, 4, 1, 3, 5]

Contributors

2SOOY zereight