@modern-js/utils
Version:
A Progressive React Framework for modern web development.
42 lines (41 loc) • 1.68 kB
JavaScript
const pluginDagSort = (plugins, key = 'name', preKey = 'pre', postKey = 'post')=>{
let allLines = [];
function getPluginByAny(q) {
const target = plugins.find((item)=>'string' == typeof q ? item[key] === q : item[key] === q[key]);
if (!target) throw new Error(`plugin ${q} not existed`);
return target;
}
plugins.forEach((item)=>{
item[preKey]?.forEach((p)=>{
if (plugins.find((ap)=>ap.name === p)) allLines.push([
getPluginByAny(p)[key],
getPluginByAny(item)[key]
]);
});
item[postKey]?.forEach((pt)=>{
if (plugins.find((ap)=>ap.name === pt)) allLines.push([
getPluginByAny(item)[key],
getPluginByAny(pt)[key]
]);
});
});
let zeroEndPoints = plugins.filter((item)=>!allLines.find((l)=>l[1] === item[key]));
const sortedPoint = [];
while(zeroEndPoints.length){
const zep = zeroEndPoints.shift();
sortedPoint.push(getPluginByAny(zep));
allLines = allLines.filter((l)=>l[0] !== getPluginByAny(zep)[key]);
const restPoints = plugins.filter((item)=>!sortedPoint.find((sp)=>sp[key] === item[key]));
zeroEndPoints = restPoints.filter((item)=>!allLines.find((l)=>l[1] === item[key]));
}
if (allLines.length) {
const restInRingPoints = {};
allLines.forEach((l)=>{
restInRingPoints[l[0]] = true;
restInRingPoints[l[1]] = true;
});
throw new Error(`plugins dependencies has loop: ${Object.keys(restInRingPoints).join(',')}`);
}
return sortedPoint;
};
export { pluginDagSort };