hardlounge/client/js/libs/handlebars/ircmessageparser/merge.js
Bonuspunkt 0b85ded53f Add bonuspunkt's parser
Fixes #15.
Fixes #199.
Fixes #583.
Fixes #654.
Fixes #928.
Fixes #1001.
2017-04-21 20:29:04 +03:00

48 lines
1.1 KiB
JavaScript

"use strict";
const anyIntersection = require("./anyIntersection");
const fill = require("./fill");
let Object_assign = Object.assign;
if (typeof Object_assign !== "function") {
Object_assign = function(target) {
Array.prototype.slice.call(arguments, 1).forEach(function(obj) {
Object.keys(obj).forEach(function(key) {
target[key] = obj[key];
});
});
return target;
};
}
function assign(textPart, fragment) {
const fragStart = fragment.start;
const start = Math.max(fragment.start, textPart.start);
const end = Math.min(fragment.end, textPart.end);
return Object_assign({}, fragment, {
start: start,
end: end,
text: fragment.text.slice(start - fragStart, end - fragStart)
});
}
function merge(textParts, styleFragments) {
const cleanText = styleFragments.map(fragment => fragment.text).join("");
const allParts = textParts
.concat(fill(textParts, cleanText))
.sort((a, b) => a.start - b.start);
return allParts.map(textPart => {
textPart.fragments = styleFragments
.filter(fragment => anyIntersection(textPart, fragment))
.map(fragment => assign(textPart, fragment));
return textPart;
});
}
module.exports = merge;