excelのように、テキストに数字のある値を規則的に充填します.
2070 ワード
文字列リストを受信し、塗りつぶし規則を返します.すべての数値を規則的に塗りつぶします.
getTextFillRule = (textList) => {
let textNumberMatrix = [];
let RemainTextMatrix = [];
textList.forEach(textString => {
let newTextString = textString || '';
let Matrix = newTextString.match(/\d+/g) || [];
textNumberMatrix.push(Matrix);
Matrix.forEach(item => {
newTextString = newTextString.replace(item, '');
});
RemainTextMatrix.push(newTextString);
});
let remainSame = RemainTextMatrix.every(item => item === RemainTextMatrix[0]);
if (remainSame) {
let textNumber0Len =textNumberMatrix[0].length;
let isSquareMatrix = textNumberMatrix.every(item => item.length === textNumber0Len);
if (isSquareMatrix) {
let textNumberMatrixT = [];
textNumberMatrix.forEach((item, index) => {
item.forEach((v, i) => {
if (index === 0) {
textNumberMatrixT[i] = [];
}
textNumberMatrixT[i][index] = v;
});
});
let rules = [];
textNumberMatrixT.forEach(item => {
let value1 = Number(item[1]);
let value0 = Number(item[0]);
if (this.isArithmeticSequence(item)) { // 。 ,
rules.push(({ n }) => {
return n * (value1 - value0) + value0;
});
} else {
rules.push(this.getLeastSquares(item.map(value => Number(value)))); // ,
}
});
return ({ n }) => {
let newString = textList[0] || '';
rules.forEach((item, index) => {
let baseNumber = textNumberMatrix[0][index];
let replaceValue = baseNumber[0] === '0' ? `0${item({ n })}` : item({ n });
newString = newString.replace(baseNumber, replaceValue);
});
return newString;
};
}
}
return ({ value }) => {
return value;
};
}
```