Skip to content

Commit c0d57b1

Browse files
committed
Use objects instead of arrays for faster lookups.
1 parent 5c26a6e commit c0d57b1

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

typescript-json-schema.ts

+29-11
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,29 @@ export function getDefaultArgs() {
2222
}
2323

2424
export class JsonSchemaGenerator {
25-
private static validationKeywords = [
26-
"ignore", "description", "type", "minimum", "exclusiveMinimum", "maximum",
27-
"exclusiveMaximum", "multipleOf", "minLength", "maxLength", "format",
28-
"pattern", "minItems", "maxItems", "uniqueItems", "default",
29-
"additionalProperties", "enum"];
25+
/**
26+
* JSDoc keywords that should be used to annotate the JSON schema.
27+
*/
28+
private static validationKeywords = {
29+
ignore: true,
30+
description: true,
31+
type: true,
32+
minimum: true,
33+
exclusiveMinimum: true,
34+
maximum: true,
35+
exclusiveMaximum: true,
36+
multipleOf: true,
37+
minLength: true,
38+
maxLength: true,
39+
format: true,
40+
pattern: true,
41+
minItems: true,
42+
maxItems: true,
43+
uniqueItems: true,
44+
default: true,
45+
additionalProperties: true,
46+
enum: true
47+
};
3048

3149
private allSymbols: { [name: string]: ts.Type };
3250
private inheritingTypes: { [baseName: string]: string[] };
@@ -73,7 +91,7 @@ export class JsonSchemaGenerator {
7391
// jsdocs are separate from comments
7492
const jsdocs = symbol.getJsDocTags();
7593
jsdocs.forEach(doc => {
76-
if (JsonSchemaGenerator.validationKeywords.indexOf(doc.name) > 0 || JsonSchemaGenerator.validationKeywords.indexOf("TJS-" + doc.name) >= 0) {
94+
if (JsonSchemaGenerator.validationKeywords[doc.name] || JsonSchemaGenerator.validationKeywords["TJS-" + doc.name]) {
7795
definition[doc.name] = this.parseValue(doc.text);
7896
} else {
7997
// special annotations
@@ -453,14 +471,14 @@ export class JsonSchemaGenerator {
453471
}
454472
}
455473

456-
private simpleTypesAllowedProperties = [
457-
"type",
458-
"description"
459-
];
474+
private simpleTypesAllowedProperties = {
475+
type: true,
476+
description: true
477+
};
460478

461479
private addSimpleType(def: any, type: string) {
462480
for (let k in def) {
463-
if (this.simpleTypesAllowedProperties.indexOf(k) === -1) {
481+
if (!this.simpleTypesAllowedProperties[k]) {
464482
return false;
465483
}
466484
}

0 commit comments

Comments
 (0)