All files / src util.ts

100% Statements 10/10
100% Branches 0/0
100% Functions 6/6
100% Lines 7/7
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  1x               1x 56x                 1x 26x               1x 26x  
 
import pluralize = require("pluralize");
 
/**
 * Returns the pluralized table name for the specified string.
 * This trims an optional leading space, then converts the PascalCase
 * to pascal_case and pluralizes it. Converts _LeaderboardCategory into
 * leaderboard_categories for example.
 */
export function getTableName(name: string) {
    return pluralize(name.replace(/^_/, "").replace(/(?:^|\.?)([A-Z])/g, (_, x) => "_" + x.toLowerCase()).replace(/^_/, ""));
}
 
/**
 * Returns the automatic foreign key name for the specified string.
 * This trims an optional leading space, then converts the PascalCase
 * to pascal_case and appends _id. Converts _LeaderboardCategory into
 * leaderboard_category_id for example.
 */
export function getForeignKey(name: string) {
    return name.replace(/^_/, "").replace(/(?:^|\.?)([A-Z])/g, (_, x) => "_" + x.toLowerCase()).replace(/^_/, "") + "_id";
}
 
/**
 * Returns the table name for a pivot table between the two specified tables.
 * This takes the table names of the specified models, then sorts them alphabetically
 * and joins them with an underscore.
 */
export function getPivotTableName(a: string, b: string) {
    return [getTableName(a), getTableName(b)].sort((a, b) => a.localeCompare(b)).join("_");
}