regexper-static/src/js/parser/javascript/match.js

64 lines
1.6 KiB
JavaScript
Raw Normal View History

import _ from 'lodash';
import Base from './base.js';
export default _.extend({}, Base, {
type: 'match',
_render() {
2014-12-11 00:58:36 +00:00
this.contents = { parts: this.parts() };
2014-12-11 00:58:36 +00:00
if (this.anchorStart()) {
this.contents.anchor_start = this.renderLabel('Start of line')
.addClass('anchor');
}
2014-12-11 00:58:36 +00:00
if (this.anchorEnd()) {
this.contents.anchor_end = this.renderLabel('End of line')
.addClass('anchor');
}
2014-12-11 00:58:36 +00:00
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 {
2014-12-11 00:58:36 +00:00
this.proxy(this.contents.parts[0]);
}
},
_position() {
var items;
_.invoke(this.contents.parts, 'position');
2014-12-10 00:02:31 +00:00
2014-12-11 00:58:36 +00:00
items = _(this.contents).at('anchor_start', 'parts', 'anchor_end').flatten().compact().value();
this.spaceHorizontally(items, {
padding: 10
});
},
2014-12-09 23:08:40 +00:00
anchorStart() {
return this._anchor_start.textValue !== '';
},
2014-12-09 23:08:40 +00:00
anchorEnd() {
return this._anchor_end.textValue !== '';
},
parts() {
return _.reduce(this._parts.elements, function(result, node) {
2014-12-11 01:11:51 +00:00
var last = _.last(result);
if (last && node.elements[0].type === 'literal' && node.elements[1].textValue === '' && last.elements[0].type === 'literal' && last.elements[1].textValue === '') {
last.textValue += node.textValue;
last.elements[0].textValue += node.elements[0].textValue;
last.elements[0].literal.textValue += node.elements[0].literal.textValue;
} else {
result.push(node);
}
return result;
}, []);
}
});