-
-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: optimize enum keyword, option loopEnum, to typescript
- Loading branch information
1 parent
08d1224
commit 0c179f3
Showing
7 changed files
with
76 additions
and
3 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
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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import {KeywordDefinition} from "../../types" | ||
import {quotedString} from "../util" | ||
|
||
const def: KeywordDefinition = { | ||
keyword: "enum", | ||
schemaType: "array", | ||
$data: true, | ||
code({write, fail, scope, data, $data, schema, schemaCode, opts}) { | ||
const valid = scope.getName("valid") | ||
if ($data) { | ||
write(`let ${valid};`) | ||
// TODO trim whitespace | ||
write( | ||
`if (${schemaCode} === undefined) ${valid} = true; | ||
else { | ||
${valid} = false; | ||
if (Array.isArray(${schemaCode})) {` | ||
) | ||
loopEnum(<string>schemaCode) | ||
write("}}") | ||
fail(`!${valid}`) | ||
} else { | ||
if (schema.length === 0) throw new Error("enum must have non-empty array") | ||
const vSchema = scope.getName("schema") | ||
write(`const ${vSchema} = ${schemaCode};`) | ||
if (schema.length > (opts.loopEnum as number)) { | ||
write(`let ${valid} = false;`) | ||
loopEnum(vSchema) | ||
fail(`!${valid}`) | ||
} else { | ||
let cond: string = schema.reduce( | ||
(c, _, i) => (c += (c && "||") + equalCode(vSchema, i)), | ||
"" | ||
) | ||
fail(`!(${cond})`) | ||
} | ||
} | ||
|
||
function loopEnum(sch: string): void { | ||
// TODO trim whitespace | ||
write( | ||
`for (const v of ${sch}) { | ||
if (equal(${data}, v)) { | ||
${valid} = true; | ||
break; | ||
} | ||
}` | ||
) | ||
} | ||
|
||
function equalCode(vSchema: string, i: number): string { | ||
let sch = schema[i] | ||
if (sch && typeof sch === "object") { | ||
return `equal(${data}, ${vSchema}[${i}])` | ||
} | ||
if (typeof sch === "string") sch = quotedString(sch) | ||
return `${data} === ${sch}` | ||
} | ||
}, | ||
error: { | ||
message: () => '"should be equal to one of the allowed values"', | ||
params: ({schemaCode}) => `{allowedValues: ${schemaCode}}`, | ||
}, | ||
} | ||
|
||
module.exports = def |
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