elc-mapper
v0.2.0
Published
an experimental mapper library
Downloads
26
Readme
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 treatnull
andundefined
as different values, and copy/mapper/transformnull
s as if they were any other desired value, whereasundefined
s are ignored and never set on models/objects. By setting this value to true,serialisers
/deserialisers
will 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 isnull
then 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 ifpropertyName
is optional. if true, thenpropertyName
will 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
. ifundefined
is returned then acts likepropertyName
didn't exist on model. Cannot be used withsourceKey
.transform
: function, optional, function to operate onmodel[propertyName]
ormodel[getter]()
to produce the value. Behaves likemapper
except only arg is the value for thepropertyName
/sourceKey
/getter()
. ifundefined
is returned then acts likepropertyName
/sourceKey
/getter
didn't exist on model. Cannot be used withmapper
. Will not be called ifpropertyName
/sourceKey
isundefined
. ifsourceKey
is 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
, orsourceKey
sourceKey
: 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
/sourceKey
is not found on the model, or isundefined
is returned by mapper. If a function (typeof 'function'
), then result of calling that function will be assigned.null
values 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 ifpropertyName
is optional. if true, thenpropertyName
will 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
. ifundefined
is returned then acts likepropertyName
didn't exist in the serialised data. Cannot be used withsourceKey
.transform
: function, optional, function to operate ondata[propertyName]
to produce the value. Behaves likemapper
except only arg is the value for thepropertyName
/sourceKey
. ifundefined
is returned then acts likepropertyName
/sourceKey
didn't exist on model. Cannot be used withmapper
. Will not be called ifpropertyName
/sourceKey
isundefined
. ifsourceKey
is present then the value for thatpropertyName
on 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
/sourceKey
is not found on the the serialised data, or isundefined
is returned by mapper. If a function (typeof 'function'
), then result of calling that function will be assigned.null
values 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'
}