typescript-mysql-model
Version:
{ "version": "1.3.0", "name": "typescript-mysql-model", "description": "", "main": "index.js", "types": "index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "
82 lines • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
class EnumMatcher {
run(schema, viewColumn, viewName) {
const tableEnums = [];
for (const tableKey in schema.tables) {
const table = schema.tables[tableKey];
const enums = this.enumArr(table, tableKey);
tableEnums.push(...enums);
}
return this.findCorresponding({
field: viewColumn.field,
table: viewName,
options: viewColumn.enumValues || []
}, tableEnums);
}
findCorresponding(view, tableEnums) {
const matches = tableEnums.filter(t => (t.options.every((to, i) => to === view.options[i])));
if (matches.length === 1) {
return matches[0];
}
matches.sort((a, b) => {
const ascore = this.similarity(a.field, view.field);
const bscore = this.similarity(b.field, view.field);
return bscore - ascore;
});
return matches[0];
}
similarity(s1, s2) {
var longer = s1;
var shorter = s2;
if (s1.length < s2.length) {
longer = s2;
shorter = s1;
}
var longerLength = longer.length;
if (longerLength == 0) {
return 1.0;
}
return (longerLength - this.editDistance(longer, shorter)) / parseFloat(longerLength + "");
}
editDistance(s1, s2) {
s1 = s1.toLowerCase();
s2 = s2.toLowerCase();
var costs = new Array();
for (var i = 0; i <= s1.length; i++) {
var lastValue = i;
for (var j = 0; j <= s2.length; j++) {
if (i == 0)
costs[j] = j;
else {
if (j > 0) {
var newValue = costs[j - 1];
if (s1.charAt(i - 1) != s2.charAt(j - 1))
newValue = Math.min(Math.min(newValue, lastValue), costs[j]) + 1;
costs[j - 1] = lastValue;
lastValue = newValue;
}
}
}
if (i > 0)
costs[s2.length] = lastValue;
}
return costs[s2.length];
}
enumArr(table, tableName) {
const enums = [];
for (const colKey in table) {
const column = table[colKey];
if (column.type === "enum" && column.enumValues) {
enums.push({
field: column.field,
table: tableName,
options: column.enumValues
});
}
}
return enums;
}
}
exports.EnumMatcher = EnumMatcher;
//# sourceMappingURL=enum-matcher.js.map