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

63 lines
1.6 KiB
JavaScript
Raw Normal View History

import _ from 'lodash';
import Q from 'q';
import Base from './base.js';
export default _.extend({}, Base, {
type: 'match',
_render() {
var start, end,
parts = this.parts(),
partPromises;
2014-12-11 00:58:36 +00:00
if (this.anchorStart()) {
2014-12-13 18:42:55 +00:00
start = this.renderLabel('Start of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
2014-12-11 00:58:36 +00:00
if (this.anchorEnd()) {
2014-12-13 18:42:55 +00:00
end = this.renderLabel('End of line')
.invoke('addClass', 'anchor');
2014-12-11 00:58:36 +00:00
}
if (start || end || parts.length !== 1) {
partPromises = _.map(parts, (function(part) {
return part.render(this.container.group());
}).bind(this));
return Q.all(_([start, partPromises, end]).flatten().compact().value())
.then(((items) => {
this.spaceHorizontally(items, {
padding: 10
});
}).bind(this));
} else {
return this.proxy(parts[0]);
}
},
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;
}, []);
}
});