-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimize mappers function to load selective conversion rules for perf…
…ormance improvement
- Loading branch information
Showing
1 changed file
with
19 additions
and
16 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
// mappers to store all the mappers of all drafts from draft3 to draft7 | ||
const drafts = ['draft3', 'draft4', 'draft6', 'draft7', '2019-09', '2020-12'] | ||
const mappers = drafts.flatMap((draft, index) => { | ||
const rules = [require(`../../rules/jsonschema-${draft}-to-${draft}.json`)] | ||
if (index + 1 < drafts.length) { | ||
rules.push(require(`../../rules/jsonschema-${draft}-to-${drafts[index + 1]}.json`)) | ||
} | ||
return rules | ||
}) | ||
|
||
// indexMapper maps drafts to their index in the mappers array. This is used to find the subarray of mappers to be returned. | ||
const indexMapper = new Map(drafts.map((draft, index) => [draft, index * 2])) | ||
|
||
const loadRequiredRules = (fromIndex, toIndex) => { | ||
const mappers = []; | ||
for (let index = fromIndex; index <= toIndex; index++) { | ||
const draft = drafts[index]; | ||
const rule = require(`../../rules/jsonschema-${draft}-to-${draft}.json`); | ||
mappers.push(rule); | ||
if (index < toIndex) { | ||
const nextDraft = drafts[index + 1]; | ||
const nextRule = require(`../../rules/jsonschema-${draft}-to-${nextDraft}.json`); | ||
mappers.push(nextRule); | ||
} | ||
} | ||
return mappers; | ||
}; | ||
exports.builtin = (from, to) => { | ||
if (!indexMapper.has(from)) { | ||
throw new Error(`Invalid "from": ${from}`) | ||
} else if (!indexMapper.has(to)) { | ||
} | ||
if (!indexMapper.has(to)) { | ||
throw new Error(`Invalid "to": ${to}`) | ||
} | ||
|
||
const fromIndex = indexMapper.get(from) | ||
const toIndex = indexMapper.get(to) | ||
return mappers.slice(fromIndex, toIndex + 1) | ||
} | ||
|
||
exports.drafts = drafts | ||
return loadRequiredRules(fromIndex, toIndex); | ||
}; | ||
exports.drafts = drafts |