|
|
@@ -1,5 +1,5 @@
|
|
|
-const { pascal, title, camel, param } = require('change-case')
|
|
|
-const plural = require('plural')
|
|
|
+const { pascal, title, camel, param, constant } = require('change-case')
|
|
|
+const pluralLib = require('plural')
|
|
|
|
|
|
/** @define CrudOptions
|
|
|
* @property {string} titleName Type name in Title Case. This is used for labels like "New {Title Name}"
|
|
|
@@ -10,6 +10,8 @@ const plural = require('plural')
|
|
|
* @property {string} camelPlural Plural type name in camelCase.
|
|
|
* @property {string} paramName Type name in param-case.
|
|
|
* @property {string} paramPlural Plural type name in param-case. This is used for urls, like "/api/{param-plural}"
|
|
|
+ * @property {string} constantName Type name in CONSTANT_CASE. This is used for permissions like CONSTANT_NAME_READ.
|
|
|
+ * @property {string} constantPlural Plural type name in CONSTANT_CASE.
|
|
|
* @property {string} apiPrefix API url path prefix. Default: /api/{param-plural}
|
|
|
* @property {string} appPrefix APP url path prefix. Default: /{param-plural}
|
|
|
* @property {CrudColumnOptions[]} columns
|
|
|
@@ -24,24 +26,35 @@ const plural = require('plural')
|
|
|
* @property {boolean} inList Default: true. Includes column in list page.
|
|
|
*/
|
|
|
|
|
|
+const plural = text => {
|
|
|
+ words = title(text).split(' ')
|
|
|
+ words[words.length - 1] = pluralLib(words[words.length - 1].toLowerCase())
|
|
|
+ return words.join(' ')
|
|
|
+}
|
|
|
|
|
|
const defaults = (opts) => {
|
|
|
opts = Object.assign({}, opts)
|
|
|
if (!opts.pascalName) opts.pascalName = pascal(opts.titleName || opts.camelName || opts.paramName || (opts.Type && opts.Type.name) || '')
|
|
|
if (!opts.pascalName) throw new Error('pascalName is required')
|
|
|
if (opts.pascalName !== pascal(opts.pascalName)) throw new Error('pascalName should be PascalCased')
|
|
|
- if (!opts.pascalPlural) opts.pascalPlural = plural(opts.pascalName)
|
|
|
+ if (!opts.pascalPlural) opts.pascalPlural = pascal(plural(opts.pascalName))
|
|
|
if (opts.pascalPlural !== pascal(opts.pascalPlural)) throw new Error('pascalPlural should be PascalCased')
|
|
|
if (!opts.titleName) opts.titleName = title(opts.pascalName)
|
|
|
- if (!opts.titlePlural) opts.titlePlural = plural(opts.titleName)
|
|
|
+ if (!opts.titlePlural) opts.titlePlural = title(plural(opts.titleName))
|
|
|
if (!opts.camelName) opts.camelName = camel(opts.pascalName)
|
|
|
if (opts.camelName !== camel(opts.camelName)) throw new Error('camelName should be camelCased')
|
|
|
- if (!opts.camelPlural) opts.camelPlural = plural(opts.camelName)
|
|
|
+ if (!opts.camelPlural) opts.camelPlural = camel(plural(opts.camelName))
|
|
|
if (opts.camelPlural !== camel(opts.camelPlural)) throw new Error('camelPlural should be camelCased')
|
|
|
if (!opts.paramName) opts.paramName = param(opts.pascalName)
|
|
|
if (opts.paramName !== param(opts.paramName)) throw new Error('paramName should be param-cased')
|
|
|
- if (!opts.paramPlural) opts.paramPlural = plural(opts.paramName)
|
|
|
+ if (!opts.paramPlural) opts.paramPlural = param(plural(opts.paramName))
|
|
|
if (opts.paramPlural !== param(opts.paramPlural)) throw new Error('paramPlural should be param-cased')
|
|
|
+
|
|
|
+ if (!opts.constantName) opts.constantName = constant(opts.pascalName)
|
|
|
+ if (opts.constantName !== constant(opts.constantName)) throw new Error('constantName should be constant-cased')
|
|
|
+ if (!opts.constantPlural) opts.constantPlural = constant(plural(opts.constantName))
|
|
|
+ if (opts.constantPlural !== constant(opts.constantPlural)) throw new Error('constantPlural should be constant-cased')
|
|
|
+
|
|
|
if (!opts.apiPrefix) opts.apiPrefix = `/api/${opts.paramPlural}`
|
|
|
if (!opts.appPrefix) opts.appPrefix = `/${opts.paramPlural}`
|
|
|
if (!opts.routeParam) opts.routeParam = `${opts.camelName}Id`
|