lagatun54

Untitled

May 8th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.81 KB | None | 0 0
  1. const brakets = ['{}', '<>']
  2.  
  3. function parse([v = '']) {
  4. v = v.trim()
  5. const output = {}
  6.  
  7. for (let row of v.split(/\n+/)) {
  8. let [name, value] = row.split(/\s*=\s*/)
  9. const opens = [...'']
  10. const opensIndex = [0].splice(1)
  11. const valueArray = [...'']
  12.  
  13. let start = -1
  14.  
  15. for (let j = 0; j < value.length; j++) {
  16. const s = value[j]
  17. const l = opens[opens.length - 1]
  18. let i = -1, f = brakets.find(e => (i = e.indexOf(s)) != -1)
  19.  
  20. if (f && !i) {
  21. opens.push(f)
  22. opensIndex.push(j)
  23. }
  24.  
  25. if (f && i) {
  26. if (f != l)
  27. throw new Error(`Syntax error an ${j} symbol!`)
  28.  
  29. opens.pop()
  30. opensIndex.pop()
  31. }
  32.  
  33. if (start == -1 && opens.length == 1)
  34. start = j + 1
  35.  
  36. if ((s == ',' && opens.length == 1) || !opens.length) {
  37. valueArray.push(value.substr(start, j - start))
  38. start = j + 1
  39. }
  40. }
  41.  
  42. if (opens.length != 0)
  43. throw new Error(`Not close brackets error an ${opensIndex.pop()} symbol!`)
  44.  
  45. output[name] = {
  46. value,
  47. valueArray
  48. }
  49. console.log(' ')
  50. }
  51.  
  52. return output
  53. }
  54.  
  55. const {A,B} = parse`
  56. A={a,b,{c,{d,e}}}
  57. B={f,g,{{d,e},c}}
  58. `
  59. //const diffValues = (a, b) => a.filter(av => !b.some(bv => bv === av))
  60. let diffValues =(a,b)=> a.filter(x => !b.includes(x))
  61.  
  62.  
  63.  
  64. function has (iterable, item) {
  65. return iterable.indexOf(item) != -1
  66. }
  67.  
  68. function difference(base, subtract) {
  69. var diff = []
  70.  
  71. base.forEach(function (item) {
  72. if (!has(subtract, item)) {
  73. diff.push(item)
  74. }
  75. })
  76.  
  77. return diff
  78. }
  79.  
  80. const x= difference(A.valueArray, B.valueArray)
  81. function myConcat(separator) {
  82. var args = Array.prototype.slice.call(arguments, 1);
  83. return args.join(separator);
  84. }
  85. var myVar = x.toString()
  86. console.log("{",myVar,"}" )
Advertisement
Add Comment
Please, Sign In to add comment