Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const brakets = ['{}', '<>']
- function parse([v = '']) {
- v = v.trim()
- const output = {}
- for (let row of v.split(/\n+/)) {
- let [name, value] = row.split(/\s*=\s*/)
- const opens = [...'']
- const opensIndex = [0].splice(1)
- const valueArray = [...'']
- let start = -1
- for (let j = 0; j < value.length; j++) {
- const s = value[j]
- const l = opens[opens.length - 1]
- let i = -1, f = brakets.find(e => (i = e.indexOf(s)) != -1)
- if (f && !i) {
- opens.push(f)
- opensIndex.push(j)
- }
- if (f && i) {
- if (f != l)
- throw new Error(`Syntax error an ${j} symbol!`)
- opens.pop()
- opensIndex.pop()
- }
- if (start == -1 && opens.length == 1)
- start = j + 1
- if ((s == ',' && opens.length == 1) || !opens.length) {
- valueArray.push(value.substr(start, j - start))
- start = j + 1
- }
- }
- if (opens.length != 0)
- throw new Error(`Not close brackets error an ${opensIndex.pop()} symbol!`)
- output[name] = {
- value,
- valueArray
- }
- console.log(' ')
- }
- return output
- }
- const {A,B} = parse`
- A={a,b,{c,{d,e}}}
- B={f,g,{{d,e},c}}
- `
- //const diffValues = (a, b) => a.filter(av => !b.some(bv => bv === av))
- let diffValues =(a,b)=> a.filter(x => !b.includes(x))
- function has (iterable, item) {
- return iterable.indexOf(item) != -1
- }
- function difference(base, subtract) {
- var diff = []
- base.forEach(function (item) {
- if (!has(subtract, item)) {
- diff.push(item)
- }
- })
- return diff
- }
- const x= difference(A.valueArray, B.valueArray)
- function myConcat(separator) {
- var args = Array.prototype.slice.call(arguments, 1);
- return args.join(separator);
- }
- var myVar = x.toString()
- console.log("{",myVar,"}" )
Advertisement
Add Comment
Please, Sign In to add comment