Skip to content

Commit

Permalink
Feat: add kebab-case for caseFile (sequelize#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray0427 authored Aug 24, 2021
1 parent be94883 commit 18a8c6d
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/auto-generator.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import _ from "lodash";
import { ColumnDescription } from "sequelize/types";
import { DialectOptions, FKSpec } from "./dialects/dialect-options";
import { AutoOptions, CaseOption, Field, IndexSpec, LangOption, qNameJoin, qNameSplit, recase, Relation, TableData, TSField, singularize, pluralize } from "./types";
import { AutoOptions, CaseFileOption, CaseOption, Field, IndexSpec, LangOption, qNameJoin, qNameSplit, recase, Relation, TableData, TSField, singularize, pluralize } from "./types";

/** Generates text from each table in TableData */
export class AutoGenerator {
Expand All @@ -18,7 +18,7 @@ export class AutoGenerator {
lang?: LangOption;
caseModel?: CaseOption;
caseProp?: CaseOption;
caseFile?: CaseOption;
caseFile?: CaseFileOption;
additional?: any;
schema?: string;
singularize: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/auto-writer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
import util from "util";
import { Utils } from "sequelize";
import { FKSpec, TableData } from ".";
import { AutoOptions, CaseOption, LangOption, qNameSplit, recase, Relation, pluralize } from "./types";
import { AutoOptions, CaseFileOption, CaseOption, LangOption, qNameSplit, recase, Relation, pluralize } from "./types";
const mkdirp = require('mkdirp');

/** Writes text into files from TableData.text, and writes init-models */
Expand All @@ -13,7 +13,7 @@ export class AutoWriter {
foreignKeys: { [tableName: string]: { [fieldName: string]: FKSpec } };
relations: Relation[];
options: {
caseFile?: CaseOption;
caseFile?: CaseFileOption;
caseModel?: CaseOption;
caseProp?: CaseOption;
directory: string;
Expand Down
17 changes: 15 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,20 @@ export declare type LangOption = "es5" | "es6" | "esm" | "ts" | "esmd";
* "u" UPPER_CASE */
export declare type CaseOption = "c" | "l" | "o" | "p" | "u";

/**
* "c" camelCase |
* "k" kebab-case |
* "l" lower_case |
* "o" original (db) |
* "p" PascalCase |
* "u" UPPER_CASE
*/
export declare type CaseFileOption = "k" | CaseOption;

export interface AutoOptions {
additional?: any;
/** Case of file names */
caseFile?: CaseOption;
caseFile?: CaseFileOption;
/** Case of model names */
caseModel?: CaseOption;
/** Case of property names */
Expand Down Expand Up @@ -186,7 +196,7 @@ export function singularize(s: string) {
}

/** Change casing of val string according to opt [c|l|o|p|u] */
export function recase(opt: CaseOption | undefined, val: string | null, singular = false) {
export function recase(opt: CaseOption | CaseFileOption | undefined, val: string | null, singular = false) {
if (singular && val) {
val = singularize(val);
}
Expand All @@ -196,6 +206,9 @@ export function recase(opt: CaseOption | undefined, val: string | null, singular
if (opt === 'c') {
return _.camelCase(val);
}
if (opt === 'k') {
return _.kebabCase(val);
}
if (opt === 'l') {
return _.snakeCase(val);
}
Expand Down
15 changes: 15 additions & 0 deletions test/types.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { expect } = require('chai');
const { describe, it } = require('mocha');
const { recase } = require('../lib/types');

describe('sequelize-auto types', function() {
it('recase kebab-case', function() {
const recasedString = recase('k', 'related_product');
expect(recasedString).to.be.equal('related-product');
});

it('recase UPPER_CASE', function() {
const recasedString = recase('u', 'related_product');
expect(recasedString).to.be.equal('RELATED_PRODUCT');
});
});

0 comments on commit 18a8c6d

Please sign in to comment.