base-domain
Version:
simple module to help build Domain-Driven Design
112 lines (80 loc) • 2.19 kB
text/coffeescript
###*
define various data type, including model and array of models
key: typeName (String)
value: type (String|Function)
TYPES
base-domain
###
TYPES =
ANY : 'any'
STRING : 'string'
NUMBER : 'number'
BOOLEAN : 'boolean'
OBJECT : 'object'
ARRAY : 'array'
DATE : 'date'
BUFFER : 'buffer'
GEOPOINT : 'geopoint'
CREATED_AT : 'created'
UPDATED_AT : 'updated'
REV_TYPES = {}
REV_TYPES[v] = k for k, v of TYPES
###*
get type of single model name by model name
model => type
"notebook" => "m<notebook>"
MODEL
{String} modelName
{String} [idPropName] by default: xxxYyyId when modelName is xxx-yyy
{String} type
###
TYPES.MODEL = (modelName, idPropName) ->
idPropName ?= camelize(modelName) + 'Id'
"m<#{modelName},#{idPropName}>"
###*
get type of model name (array) by model name
model => type
"notebook" => "a<notebook>"
MODELS
{String} modelName
{String} [idPropName] by default: xxxYyyIds when modelName is xxx-yyy
{String} type
###
TYPES.MODELS = (modelName, idPropName) ->
idPropName ?= camelize(modelName) + 'Ids'
"a<#{modelName},#{idPropName}>"
###*
get information object by type
info
{String} type
{Object} info
{String} [info.name] type name
{String|null} [info.model] if model-related type, the name of the model
###
TYPES.info = (type) ->
return name: null unless type?
if match = type.match /([am])<([^,]+),([^>]+)>/
[all, m_or_a, modelName, idPropName] = match
typeName = if m_or_a is 'm' then 'MODEL' else 'MODELS'
return {
name: typeName
model: match[2]
idPropName: idPropName
}
else
return name: (REV_TYPES[type] ? null)
# 'shinout-no-macbook-pro => shinoutNoMacbookPro'
camelize = (str) ->
(for substr, i in str.split('-')
if i is 0
substr
else
substr.charAt(0).toUpperCase() + substr.slice(1)
).join('')
module.exports = TYPES