Last change
on this file since 1ad8e64 was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
2.3 KB
|
Line | |
---|
1 | "use strict";
|
---|
2 |
|
---|
3 | function _createForOfIteratorHelperLoose(o, allowArrayLike) { var it; if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; return function () { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } it = o[Symbol.iterator](); return it.next.bind(it); }
|
---|
4 |
|
---|
5 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
---|
6 |
|
---|
7 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
---|
8 |
|
---|
9 | function last(array) {
|
---|
10 | return array[array.length - 1];
|
---|
11 | }
|
---|
12 |
|
---|
13 | var brackets = {
|
---|
14 | /**
|
---|
15 | * Parse string to nodes tree
|
---|
16 | */
|
---|
17 | parse: function parse(str) {
|
---|
18 | var current = [''];
|
---|
19 | var stack = [current];
|
---|
20 |
|
---|
21 | for (var _iterator = _createForOfIteratorHelperLoose(str), _step; !(_step = _iterator()).done;) {
|
---|
22 | var sym = _step.value;
|
---|
23 |
|
---|
24 | if (sym === '(') {
|
---|
25 | current = [''];
|
---|
26 | last(stack).push(current);
|
---|
27 | stack.push(current);
|
---|
28 | continue;
|
---|
29 | }
|
---|
30 |
|
---|
31 | if (sym === ')') {
|
---|
32 | stack.pop();
|
---|
33 | current = last(stack);
|
---|
34 | current.push('');
|
---|
35 | continue;
|
---|
36 | }
|
---|
37 |
|
---|
38 | current[current.length - 1] += sym;
|
---|
39 | }
|
---|
40 |
|
---|
41 | return stack[0];
|
---|
42 | },
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Generate output string by nodes tree
|
---|
46 | */
|
---|
47 | stringify: function stringify(ast) {
|
---|
48 | var result = '';
|
---|
49 |
|
---|
50 | for (var _iterator2 = _createForOfIteratorHelperLoose(ast), _step2; !(_step2 = _iterator2()).done;) {
|
---|
51 | var i = _step2.value;
|
---|
52 |
|
---|
53 | if (typeof i === 'object') {
|
---|
54 | result += "(" + brackets.stringify(i) + ")";
|
---|
55 | continue;
|
---|
56 | }
|
---|
57 |
|
---|
58 | result += i;
|
---|
59 | }
|
---|
60 |
|
---|
61 | return result;
|
---|
62 | }
|
---|
63 | };
|
---|
64 | module.exports = brackets; |
---|
Note:
See
TracBrowser
for help on using the repository browser.