core-graphics
Version:
A core library for creating shape-based graphic editors
69 lines (65 loc) • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.noop = noop;
exports.getPairs = getPairs;
exports.symmetricDifference2 = symmetricDifference2;
exports.symmetricDifference = symmetricDifference;
exports.hasInArr = hasInArr;
exports.uniq = uniq;
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
/* tslint:disable:no-empty */
function noop(a) {}
function getPairs(arr) {
var res = [];
arr.forEach(function (point) {
if (res.length === 0) {
res = [[point]];
return;
}
var last = res[res.length - 1];
if (last.length === 0) {
res[res.length - 1] = [point];
return;
}
if (last.length === 1) {
res[res.length - 1] = [].concat(_toConsumableArray(last), [point]);
return;
}
if (last.length === 2) {
res = [].concat(_toConsumableArray(res), [[point]]);
return;
}
});
return res;
}
var symFilterFunction = function symFilterFunction(arr1, arr2) {
return arr1.filter(function (item) {
return arr2.indexOf(item) === -1;
});
};
function symmetricDifference2(arr1, arr2) {
// Returns items in arr1 that don't exist in arr2
// Run filter function on each array against the other then get unique values
return [].concat(_toConsumableArray(symFilterFunction(arr1, arr2)), _toConsumableArray(symFilterFunction(arr2, arr1))).filter(function (item, idx, arr) {
return(
// Keep any items that are unique - the index of the current item === index of the first occurrence in the array
arr.indexOf(item) === idx
);
});
}
function symmetricDifference() {
for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return args.reduce(symmetricDifference2, []);
}
function hasInArr(what, where) {
return where.indexOf(what) > -1;
}
function uniq(arr) {
return arr.filter(function (value, index, self) {
return self.indexOf(value) === index;
});
}