--- id: 594faaab4e2a8626833e9c3d title: エスケープ文字のある文字列をトークン化する challengeType: 5 forumTopicId: 302338 dashedName: tokenize-a-string-with-escaping --- # --description-- エスケープ処理されていない区切り文字がある位置で、文字列を分割できる関数またはプログラムを記述してください。 次の 3 つの入力パラメータを受け取る必要があります:
one^|uno||three^^^^|four^^^|^cuatro|ここで、`|` を区切り文字として `^` をエスケープ文字として使用します。関数は次の配列を出力しなければなりません。
['one|uno', '', 'three^^', 'four^|cuatro', '']# --hints-- `tokenize` は関数とします。 ```js assert(typeof tokenize === 'function'); ``` `tokenize` は配列を返す必要があります。 ```js assert(typeof tokenize('a', 'b', 'c') === 'object'); ``` `tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')` は `['one|uno', '', 'three^^', 'four^|cuatro', '']` を返す必要があります。 ```js assert.deepEqual(tokenize(testStr1, '|', '^'), res1); ``` `tokenize('a@&bcd&ef&&@@hi', '&', '@')` は `['a&bcd', 'ef', '', '@hi']` を返す必要があります。 ```js assert.deepEqual(tokenize(testStr2, '&', '@'), res2); ``` # --seed-- ## --after-user-code-- ```js const testStr1 = 'one^|uno||three^^^^|four^^^|^cuatro|'; const res1 = ['one|uno', '', 'three^^', 'four^|cuatro', '']; // TODO add more tests const testStr2 = 'a@&bcd&ef&&@@hi'; const res2 = ['a&bcd', 'ef', '', '@hi']; ``` ## --seed-contents-- ```js function tokenize(str, sep, esc) { return true; } ``` # --solutions-- ```js // tokenize :: String -> Character -> Character -> [String] function tokenize(str, charDelim, charEsc) { const dctParse = str.split('') .reduce((a, x) => { const blnEsc = a.esc; const blnBreak = !blnEsc && x === charDelim; const blnEscChar = !blnEsc && x === charEsc; return { esc: blnEscChar, token: blnBreak ? '' : ( a.token + (blnEscChar ? '' : x) ), list: a.list.concat(blnBreak ? a.token : []) }; }, { esc: false, token: '', list: [] }); return dctParse.list.concat( dctParse.token ); } ```