-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #52 from ChenCMD:fix/multi-line-gen
🚸 MultiLineGeneratorの諸々を修正
- Loading branch information
Showing
10 changed files
with
161 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,37 @@ | ||
import { locale } from '../../../locales'; | ||
import { ParsingError } from '../../../types'; | ||
import { listenInput } from '../../../utils'; | ||
import { listenInput, numberValidator, stringValidator } from '../../../utils'; | ||
import { Replacer } from '../types/Replacer'; | ||
|
||
export const expressionReplacer: Replacer = async (insertString, insertCount) => { | ||
const expression = await listenInput(locale('expression.expression')); | ||
const expression = await listenInput(locale('expression.expression'), undefined, 'y = 2x + 1'); | ||
const paddingLength = parseInt(await listenInput(locale('serial-number.padding-length'), v => numberValidator(v, { min: -1 }), -1), 10); | ||
const paddingChar = paddingLength >= 1 | ||
? await listenInput(locale('serial-number.padding-char'), v => stringValidator(v, { minLength: 1, maxLength: 1 })) | ||
: '0'; | ||
|
||
const ans: string[] = []; | ||
try { | ||
for (let i = 1; i < insertCount + 1; i++) { | ||
const replaceData = eval(expression | ||
const replaceData: number = eval(expression | ||
.replace(/\s/g, '') | ||
.replace(/\dx/g, m => `(${m.slice(0, 1)}*x)`) | ||
.replace(/[^+\-*/]\(/g, m => `${m.slice(0, 1)}*(`) | ||
.replace(/^.*=/, '') | ||
.replace(/\^/g, '**') | ||
.replace(/x/g, i.toString())); | ||
ans.push(insertString.replace(/%r/g, replaceData)); | ||
.replace(/\dx/g, m => `${m.slice(0, 1)}*x`) | ||
.replace(/^[x0-9]\(/g, m => `${m.slice(0, 1)}*(`) | ||
.replace(/[0-9]\(/g, m => `${m.slice(0, 1)}*(`) | ||
.replace(/[^a-zA-Z.]x\(/g, m => `${m.slice(0, 2)}*(`) | ||
.replace(/[^a-zA-Z.]x/g, m => `${m.slice(0, 1)}${i.toString()}`)); | ||
ans.push(insertString.replace(/%r/g, replaceData.toString(10))); | ||
} | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
} catch (e: any) { | ||
} catch { | ||
throw new ParsingError(locale('error.not-expression')); | ||
} | ||
return ans; | ||
|
||
const maxLength = ans.reduce((a, b) => Math.max(a, b.length), 0); | ||
|
||
return paddingLength !== -1 | ||
? ans.map(str => paddingChar.repeat(maxLength - str.length + paddingLength) + str) | ||
: ans; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters