/
writeIsValidDecimalInput.ts
46 lines (42 loc) · 1.39 KB
/
writeIsValidDecimalInput.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import { type ContentWriterOptions } from '../../types';
export const writeIsValidDecimalInput = ({
fileWriter: { writer, writeImport },
dmmf,
getSingleFileContent = false,
}: ContentWriterOptions) => {
const { useMultipleFiles } = dmmf.generatorConfig;
if (useMultipleFiles && !getSingleFileContent) {
writeImport('type { DecimalJSLike }', `./DecimalJsLikeSchema`);
}
writer
.blankLine()
.writeLine(
`export const DECIMAL_STRING_REGEX = /^[0-9.,e+-bxffo_cp]+$|Infinity|NaN/;`,
)
.blankLine()
.writeLine(`export const isValidDecimalInput =`)
.withIndentationLevel(1, () => {
writer
.write(`(v?: null | string | number | DecimalJSLike) => `)
.inlineBlock(() => {
writer
.writeLine(`if (!v) return false;`)
.writeLine(`return (`)
.withIndentationLevel(3, () => {
writer
.writeLine(
`(typeof v === 'object' && 'd' in v && 'e' in v && 's' in v) ||`,
)
.writeLine(
`(typeof v === 'string' && DECIMAL_STRING_REGEX.test(v)) ||`,
)
.writeLine(`typeof v === 'number'`);
})
.write(`)`);
})
.write(`;`);
});
if (useMultipleFiles && !getSingleFileContent) {
writer.blankLine().writeLine(`export default isValidDecimalInput;`);
}
};