/
writeIsValidDecimalInput.ts
48 lines (44 loc) · 1.51 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
47
48
import { type ContentWriterOptions } from '../../types';
export const writeIsValidDecimalInput = ({
fileWriter: { writer, writeImport },
dmmf,
getSingleFileContent = false,
}: ContentWriterOptions) => {
const { useMultipleFiles, prismaClientPath } = dmmf.generatorConfig;
if (useMultipleFiles && !getSingleFileContent) {
writeImport('type { Prisma }', `${prismaClientPath}`);
}
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 | Prisma.DecimalJsLike): v is string | number | Prisma.DecimalJsLike => `,
)
.inlineBlock(() => {
writer
.writeLine(`if (v === undefined || v === null) return false;`)
.writeLine(`return (`)
.withIndentationLevel(3, () => {
writer
.writeLine(
`(typeof v === 'object' && 'd' in v && 'e' in v && 's' in v && 'toFixed' 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;`);
}
};