我有一個決議到這個陣列的 git diff 輸出字串,它在 package.json 檔案中提供了所有更改的依賴項:
const data = [
[ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
[ ' ', '@date-io/moment', '1.3.14', '1', '3', '14' ],
[ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
[ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
[ ' ', '@emotion/react', '11.8.2', '11', '8', '2' ],
[ ' ', '@emotion/styled', '11.8.1', '11', '8', '1' ]
]
第一個元素 ( / -) 顯示此更改是否已添加或洗掉。第二個元素是包名(這是連接兩個陣列的元素),接下來的欄位給我版本。現在我需要迭代這個陣列以找出每個包都進行了哪種版本更改。所以輸出應該是:
{
'@date-io/moment': 'patch',
'@emotion/react': 'minor',
'@emotion/react': 'minor'
}
我試圖通過做
const result = {}
data.forEach(d => {
result[d[1]] = bump // how to calculate `bump`?
})
但這不能處理 ,-也不能計算版本凹凸的型別。
uj5u.com熱心網友回復:
使用Array.prototype.reducereduce可以將陣列reduced成一個物件,并為每個包使用Array.prototype.find找到相應的變化。然后您可以比較版本并確定其更改。
const data = [
[ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
[ ' ', '@date-io/moment', '1.3.14', '1', '3', '14' ],
[ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
[ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
[ ' ', '@emotion/react', '11.8.2', '11', '8', '2' ],
[ ' ', '@emotion/styled', '11.8.1', '11', '8', '1' ]
];
const changes = data.reduce((t, [type, name, , major, minor, patch], i, arr) => {
if (type === " ") {
const match = arr
.find(([type, pName]) => type === "-" && pName === name);
if (!match) {
return t; // handle no match here
}
const [, , , pMajor, pMinor, pPatch] = match;
return {
...t,
[name]: pMajor !== major ? "major" :
pMinor !== minor ? "minor" :
"patch"
}
}
return t;
}, {});
console.log(changes);
uj5u.com熱心網友回復:
首先讓兩個像這樣的不同物件中的 和 - 分開
let added={},removed={};
for(let i=0;i<data.length;i ){
if(data[i][0]=='-'){
removed[data[i][1]]={
version:data[i][2],
major:data[i][3],
min:data[i][4],
patch:data[i][5]
}
}else{
added[data[i][1]]={
version:data[i][2],
major:data[i][3],
min:data[i][4],
patch:data[i][5]
}
}
}
然后創建一個函式獲取差異字串,如下所示
function getPackageChanges(ad,rm){
if(ad.major!==rm.major) return "major";
if(ad.min!==rm.min) return "minor";
if(ad.patch!==rm.patch)return "patch";
return "unknown";
}
然后你可以從它的鍵映射添加到這種方式來得到結果字串
let res={};
Object.keys(added).forEach(t=>{
if(removed[t]!==undefined){
res[t]=getPackageChanges(removed[t],added[t])
}
})
const data = [
[ '-', '@date-io/moment', '1.3.13', '1', '3', '13' ],
[ ' ', '@date-io/moment', '1.3.14', '1', '3', '14' ],
[ '-', '@emotion/react', '11.7.0', '11', '7', '0' ],
[ '-', '@emotion/styled', '11.6.0', '11', '6', '0' ],
[ ' ', '@emotion/react', '11.8.2', '11', '8', '2' ],
[ ' ', '@emotion/styled', '11.8.1', '11', '8', '1' ]
]
let added={},removed={};
for(let i=0;i<data.length;i ){
if(data[i][0]=='-'){
removed[data[i][1]]={
version:data[i][2],
major:data[i][3],
min:data[i][4],
patch:data[i][5]
}
}else{
added[data[i][1]]={
version:data[i][2],
major:data[i][3],
min:data[i][4],
patch:data[i][5]
}
}
}
function getPackageChanges(ad,rm){
if(ad.major!==rm.major) return "major";
if(ad.min!==rm.min) return "minor";
if(ad.patch!==rm.patch)return "patch";
return "unknown";
}
let res ={};
Object.keys(added).forEach(t=>{
if(removed[t]!==undefined){
res[t]=getPackageChanges(removed[t],added[t])
}
})
console.log("result",res)
uj5u.com熱心網友回復:
- 創建
Map所有減法 - 回圈所有添加并使用地圖將其減少到所需的物件。
const data = [
["-", "@date-io/moment", "1.3.13", "1", "3", "13"],
[" ", "@date-io/moment", "1.3.14", "1", "3", "14"],
["-", "@emotion/react", "11.7.0", "11", "7", "0"],
["-", "@emotion/styled", "11.6.0", "11", "6", "0"],
[" ", "@emotion/react", "11.8.2", "11", "8", "2"],
[" ", "@emotion/styled", "11.8.1", "11", "8", "1"],
];
const subMap = new Map(data.filter((d) => d[0] === "-").map((d) => [d[1], d]));
const additions = data.filter((d) => d[0] === " ");
const updates = additions.reduce((updates, addition) => {
const pkgName = addition[1];
const [maj, min, patch] = addition.slice(-3);
const [oldMaj, oldMin, oldPatch] = subMap.get(pkgName).slice(-3);
if (maj === oldMaj && min === oldMin && patch >= oldPatch) {
updates[pkgName] = "patch";
} else if (maj === oldMaj && min >= oldMin) {
updates[pkgName] = "minor";
} else {
updates[pkgName] = "major";
}
return updates;
}, {});
console.log(updates);
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/463383.html
標籤:javascript
上一篇:使用熊貓堆疊條形圖
