elc-mapper v0.2.0
ELC Mapper
You'll probably neveer nead to use this unless you have some "wierd" technical requirements.
experimenting with mappers and javascript's total lack of type system...
for implementation see ./lib/serialiser and ./lib/deserialiser
common opts
These options apply to the serialiser/deserialiser, are optional, and are the last argument
var elcMapper = require('elc-mapper')
var opts = {
treatNullAsUndefined: false
}
var mySerialiser = elcMapper.createSerialiser(mySpec, opts)
var myDeserialiser = elcMapper.createDeserialiser(factory, mySpec, opts)treatNullAsUndefined: boolean, default false. elc-mapper's standard behaviour is to treatnullandundefinedas different values, and copy/mapper/transformnulls as if they were any other desired value, whereasundefineds are ignored and never set on models/objects. By setting this value to true,serialisers/deserialiserswill not set a value on model/object if it's strictly (===) equivalent to null or undefined. If the value returned by mapper/transform/default/etc isnullthen it will not be set!
Serialiser
spec definition: If key is just an empty object '{}', or none of the optional keys below are found then the key ('propertyName') is just copied across (currently not cloned or anything)
{
propertyName : {
optional:
mapper:
transform:
getter:
sourceKey:
default:
}
}optional: boolean, default false, determines ifpropertyNameis optional. if true, thenpropertyNamewill not be created in the serialised data if it is not present on model. May one day throw error if false and no data found or returned bymapper/transform. Currently does nothing other than allow developer to signal intent.mapper: function, optional, function to operate on the model to produce the value for the propertyName. it's only arg is the model. the returned value from the function is then assigned to thepropertyName. ifundefinedis returned then acts likepropertyNamedidn't exist on model. Cannot be used withsourceKey.transform: function, optional, function to operate onmodel[propertyName]ormodel[getter]()to produce the value. Behaves likemapperexcept only arg is the value for thepropertyName/sourceKey/getter(). ifundefinedis returned then acts likepropertyName/sourceKey/getterdidn't exist on model. Cannot be used withmapper. Will not be called ifpropertyName/sourceKeyisundefined. ifsourceKeyis present then the value for that propertyName on the model will be usedgetter: string, optional, name of the function to use for getting, will work like thismodel[getter](). Cannot be used withmapper, orsourceKeysourceKey: string, optional, determines which propertyName on the model this value will come from. Cannot be used withmapper.default: primitive value or function that will be used if thepropertyName/sourceKeyis not found on the model, or isundefinedis returned by mapper. If a function (typeof 'function'), then result of calling that function will be assigned.nullvalues on the model will copied across.example:
var mySpec = { id: {}, name: { mapper: function(user){ return user.firstname + ' ' + user.lastname } } team: { sourceKey: companyName } } // The var mySerialiser = createSerialiser(mySpec, opts) // given a model of { id: 12, firstname: 'James', lastname: 'Butler', companyName: 'rofly', meta: 'some string?' } // it would serialise to { id: 12, name: 'James Butler', team: 'rofly' }
Deserialiser
factory = // function that will be called, should return a new instance of the model ready to be configured.
// is passed the optional initilisation data incase it wishes to use that for initialisation
// we have "initialisation data" just because some models needs it.
// object containing info on how to create/fill/whatever each property on the new model instance
properties: {
propertyName : {
optional:
mapper:
transform:
setter:
sourceKey:
default:
}
}-factory: function that will be called, should return a new instance of the model ready to be configured. Is passed the optional initilisation data incase it wishes to use that for initialisation
we have "initialisation data" just because some models needs it.
optional: boolean, default false, determines ifpropertyNameis optional. if true, thenpropertyNamewill not be created on the model if it is not present in the serialised data. May one day throw error if false and no data found or returned bymapper/transform. Currently does nothing other than allow developer to signal intent.mapper: function, optional, function to operate in the serialised data to produce the value for the propertyName. it's only arg is the serialised data. the returned value from the function is then assigned to thepropertyName. ifundefinedis returned then acts likepropertyNamedidn't exist in the serialised data. Cannot be used withsourceKey.transform: function, optional, function to operate ondata[propertyName]to produce the value. Behaves likemapperexcept only arg is the value for thepropertyName/sourceKey. ifundefinedis returned then acts likepropertyName/sourceKeydidn't exist on model. Cannot be used withmapper. Will not be called ifpropertyName/sourceKeyisundefined. ifsourceKeyis present then the value for thatpropertyNameon the serialised data will be usedsetter: string, optional, name of the function to use for setting, will work like thismodel[setter](value).sourceKey: string, optional, determines which propertyName on the the serialised data this value will come from. Cannot be used withmapper.default: primitive value or function that will be used if thepropertyName/sourceKeyis not found on the the serialised data, or isundefinedis returned by mapper. If a function (typeof 'function'), then result of calling that function will be assigned.nullvalues on the the serialised data will copied across.
example
var factory = function(){
return new UserModel()
}
var mySpec = {
id: {},
name: {
mapper: function(data){ return data.firstname + ' ' + data.lastname }
}
team: {
sourceKey: companyName
}
}
var myDeserialiser = createDeserialiser(factory, mySpec)
// given serialised data of
{
id: 12,
firstname: 'James',
lastname: 'Butler',
companyName: 'rofly',
meta: 'some string?'
}
// it would deserialise to a model something like
{
id: 12,
name: 'James Butler',
team: 'rofly'
}