diff --git a/src/argBody.ts b/src/argBody.ts index 97880fd..e2d8b92 100644 --- a/src/argBody.ts +++ b/src/argBody.ts @@ -2,13 +2,18 @@ import { LambdaBody } from "./Types/lambda"; import { ParseBody } from "./Types/utils"; export const parse = (body: LambdaBody): ParseBody => { - const args = body.text?.split(" "); - if (args === undefined) { + if (body.text === undefined) { throw new Error( "引数が正しく渡されていません。もう一度helpを見てください。" ); } + if (body.text?.match(/.*\u3000.*/g) !== null) { + throw new Error("全角スペースが含まれています。引数から削除してください。"); + } + + const args = body.text?.split(" "); + // 引数がないもしくは'--'が入ってるときはhelp const isHelp = args[0] === "" || args[0].includes("--"); // locationIdは数値5桁(prefecturesId2桁 + placeId3桁)のみ指定されている場合 diff --git a/test/argBody.test.ts b/test/argBody.test.ts new file mode 100644 index 0000000..339e318 --- /dev/null +++ b/test/argBody.test.ts @@ -0,0 +1,26 @@ +import * as argBody from "../src/argBody"; +import { LambdaBody } from "../src/Types/lambda"; + +test("bodyがundefinedの場合は例外を吐く", async () => { + const body: LambdaBody = { + text: undefined, + }; + + const expectError = new Error( + "引数が正しく渡されていません。もう一度helpを見てください。" + ); + + expect(() => argBody.parse(body)).toThrowError(expectError); +}); + +test("bodyに全角スペースが含まれる場合は例外を吐く", async () => { + const body: LambdaBody = { + text: " 01101", + }; + + const expectError = new Error( + "全角スペースが含まれています。引数から削除してください。" + ); + + expect(() => argBody.parse(body)).toThrowError(expectError); +});