Thank you for checking out my project! Before we proceed, this a small coding task intended for individual coders, not teams. You must be a JavaScript and regular expressions expert to complete this task.
This task requires you to produce a function which receives two inputs: an Object and Text. Your job is to parse the input text using given object and return expected result which is also shown below.
Please note that I tried my best not to make mistakes in the output example, but if something looks like it doesn't follow everything else or there's a character missing, it's probably a mistake. Please confirm with me. There should be NO exceptions in resulting object and if flattened, it should match the input text exactly.
Finally, result should not rely on any external libraries or code. Deliverable should be plain JavaScript with no external library dependencies of any kind except what is provided by ECMA.
## Deliverables
A few notes: (1) All matches, including begin and end are regular expressions. (2) The patterns could be specified directly or be reused from repository. (3) Recursion is allowed which is accomplished via "include": "#self".
**Object Input:**
{
"name": "source",
"patterns": [
{
"name": "keyword",
"match": "begin|end"
},
{
"name": "value",
"patterns": [
{ "include": "#colors" },
{ "include": "#numbers" }
]
},
{
"name": "string",
"begin": "'",
"end": "'",
"patterns": [
{
"name": "[login to view URL]",
"match": "\\\\."
}
]
},
{
"name": "script",
"begin": "#{",
"end": "}",
"patterns": [
{ "include": "#self" }
]
}
],
"repository": {
"colors": [
"name": "color",
"match": "red|blue"
],
"numbers": [
"name": "number",
"patterns": [
{
"name": "integer",
"match": "\\d+"
},
{
"name": "float",
"match": "[\\d\\.]+"
}
]
]
}
}
**Text Input:**
This is a string: "hello world, #{ this should all be a string }".
This is an escaped string: "Hello \"wonderful\" world. Lets end our troubles and begin new life!".
Here are some keywords: Lets end our troubles and begin new life!
Here are some values: Sky is blue, roses are red and so is 10000 and 10.42.
And here's the recursion test:
#{
This is a string: "hello world, #{ this should all be a string }".
This is an escaped string: "Hello \"wonderful\" world. Lets end our troubles and begin new life!".
Here are some keywords: Lets end our troubles and begin new life!
Here are some values: Sky is blue, roses are red and so is 10000 and 10.42.
And here's the recursion test: #{ "string" }
}
**Output:**
The name of a pattern is used as a key. If there's no pattern capturing, plain string is included. Begin/end are implicitly captured with "begin" and "end" names.
{
"source": [
"This is a string: ",
{
"string" : [
{ "begin": "\"" },
"hello world, #{ this should all be a string }",
{ "end": "\"" }
]
},
".\nThis is an escaped string: ",
{
"string" : [
{ "begin": "\"" },
"Hello \"wonderful\" world. Lets end our troubles and begin new life!",
{ "end": "\"" }
]
},
".\nHere are some keywords: Lets ",
{ "keyword": [ "end" ] },
" our troubles and ",
{ "keyword": [ "begin" ] },
" new life!\nHere are some values: Sky is ",
{
"value": [
{ "color": [ "blue" ] }
]
},
", roses are ",
{
"value": [
{ "color": [ "red" ] }
]
},
" and so is ",
{
"value": [
{
"number": [
{ "integer": "10000" }
]
}
]
},
" and ",
{
"value": [
{
"number": [
{ "float": "10.42" }
]
}
]
},
".\n\nAnd here's the recursion test:\n",
{
"script": [
{ "begin": "#{" },
{
"source": [
"\n\tThis is a string: ",
{
"string" : [
{ "begin": "\"" },
"hello world, #{ this should all be a string }",
{ "end": "\"" }
]
},
".\n\tThis is an escaped string: ",
{
"string" : [
{ "begin": "\"" },
"Hello \"wonderful\" world. Lets end our troubles and begin new life!",
{ "end": "\"" }
]
},
".\n\tHere are some keywords: Lets ",
{ "keyword": [ "end" ] },
" our troubles and ",
{ "keyword": [ "begin" ] },
" new life!\n\tHere are some values: Sky is ",
{
"value": [
{ "color": [ "blue" ] }
]
},
", roses are ",
{
"value": [
{ "color": [ "red" ] }
]
},
" and so is ",
{
"value": [
{
"number": [
{ "integer": "10000" }
]
}
]
},
" and ",
{
"value": [
{
"number": [
{ "float": "10.42" }
]
}
]
},
".\n\tAnd here's the recursion test:\n",
{
"script": [
{ "begin": "#{" },
" ",
{
"string" : [
{ "begin": "\"" },
"string",
{ "end": "\"" }
]
},
" ",
{ "end": "}" }
]
},
"\n"
]
},
{ "end": "}" }
]
}
]
}