regexper-static/src/js/parser/javascript/match.js
2014-12-10 19:58:36 -05:00

70 lines
1.8 KiB
JavaScript

import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
type: 'match',
_render() {
this.contents = { parts: this.parts() };
if (this.anchorStart()) {
this.contents.anchor_start = this.renderLabel('Start of line')
.addClass('anchor');
}
if (this.anchorEnd()) {
this.contents.anchor_end = this.renderLabel('End of line')
.addClass('anchor');
}
if (this.contents.anchor_start || this.contents.anchor_end || this.contents.parts.length !== 1) {
_.each(this.contents.parts, (function(part) {
part.render(this.container.group());
}).bind(this));
} else {
this.proxy(this.contents.parts[0]);
}
},
_position() {
var items;
_.invoke(this.contents.parts, 'position');
items = _(this.contents).at('anchor_start', 'parts', 'anchor_end').flatten().compact().value();
this.spaceHorizontally(items, {
padding: 10
});
},
anchorStart() {
return this._anchor_start.textValue !== '';
},
anchorEnd() {
return this._anchor_end.textValue !== '';
},
parts() {
return _.reduce(this._parts.elements, function(result, node) {
var last = result.pop();
if (last) {
if (node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') {
last = _.clone(last, true);
last.textValue += node.textValue;
last.elements[0].textValue += node.elements[0].textValue;
last.elements[0].literal.textValue += node.elements[0].literal.textValue;
last.elements[1] = node.elements[1];
node = last;
} else {
result.push(last);
}
}
result.push(node);
return result;
}, []);
}
});