| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | id: 594faaab4e2a8626833e9c3d | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | title: Tokenize a string with escaping | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | challengeType: 5 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | forumTopicId: 302338 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | dashedName: tokenize-a-string-with-escaping | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | --- | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --description--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | Write a function or program that can split a string at each non-escaped occurrence of a separator character. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It should accept three input parameters: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  |   <li>The <strong>string</strong></li> | 
					
						
							|  |  |  |   <li>The <strong>separator character</strong></li> | 
					
						
							|  |  |  |   <li>The <strong>escape character</strong></li> | 
					
						
							|  |  |  | </ul> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | It should output a list of strings. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rules for splitting: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  |   <li>The fields that were separated by the separators, become the elements of the output list.</li> | 
					
						
							|  |  |  |   <li>Empty fields should be preserved, even at the start and end.</li> | 
					
						
							|  |  |  | </ul> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Rules for escaping: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <ul> | 
					
						
							|  |  |  |   <li>"Escaped" means preceded by an occurrence of the escape character that is not already escaped itself.</li> | 
					
						
							|  |  |  |   <li>When the escape character precedes a character that has no special meaning, it still counts as an escape (but does not do anything special).</li> | 
					
						
							|  |  |  |   <li>Each occurrences of the escape character that was used to escape something, should not become part of the output.</li> | 
					
						
							|  |  |  | </ul> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Demonstrate that your function satisfies the following test-case: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Given the string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <pre>one^|uno||three^^^^|four^^^|^cuatro|</pre> | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | and using `|` as a separator and `^` as escape character, your function should output the following array: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | <pre>  ['one|uno', '', 'three^^', 'four^|cuatro', ''] | 
					
						
							|  |  |  | </pre> | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --hints--
 | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `tokenize` should be a function. | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | ```js | 
					
						
							|  |  |  | assert(typeof tokenize === 'function'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `tokenize` should return an array. | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert(typeof tokenize('a', 'b', 'c') === 'object'); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `tokenize('one^|uno||three^^^^|four^^^|^cuatro|', '|', '^')` should return `['one|uno', '', 'three^^', 'four^|cuatro', '']` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert.deepEqual(tokenize(testStr1, '|', '^'), res1); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-06 04:42:36 +00:00
										 |  |  | `tokenize('a@&bcd&ef&&@@hi', '&', '@')` should return `['a&bcd', 'ef', '', '@hi']` | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | ```js | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | assert.deepEqual(tokenize(testStr2, '&', '@'), res2); | 
					
						
							| 
									
										
										
										
											2018-10-10 18:03:03 -04:00
										 |  |  | ``` | 
					
						
							| 
									
										
										
										
											2020-08-13 17:24:35 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | # --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; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-16 00:37:30 -07:00
										 |  |  | # --solutions--
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-13 03:31:00 +01:00
										 |  |  | ```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 | 
					
						
							|  |  |  |   ); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ``` |