npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elc-mapper

v0.2.0

Published

an experimental mapper library

Downloads

9

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 treat null and undefined as different values, and copy/mapper/transform nulls as if they were any other desired value, whereas undefineds 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 is null 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 if propertyName is optional. if true, then propertyName 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 by mapper/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 the propertyName. if undefined is returned then acts like propertyName didn't exist on model. Cannot be used with sourceKey.
  • transform: function, optional, function to operate on model[propertyName] or model[getter]() to produce the value. Behaves like mapper except only arg is the value for the propertyName/sourceKey/getter(). if undefined is returned then acts like propertyName/sourceKey/getter didn't exist on model. Cannot be used with mapper. Will not be called if propertyName/sourceKey is undefined. if sourceKey is present then the value for that propertyName on the model will be used
  • getter: string, optional, name of the function to use for getting, will work like this model[getter](). Cannot be used with mapper, or sourceKey
  • sourceKey: string, optional, determines which propertyName on the model this value will come from. Cannot be used with mapper.
  • default: primitive value or function that will be used if the propertyName/sourceKey is not found on the model, or is undefined 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 if propertyName is optional. if true, then propertyName 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 by mapper/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 the propertyName. if undefined is returned then acts like propertyName didn't exist in the serialised data. Cannot be used with sourceKey.
  • transform: function, optional, function to operate on data[propertyName] to produce the value. Behaves like mapper except only arg is the value for the propertyName/sourceKey. if undefined is returned then acts like propertyName/sourceKey didn't exist on model. Cannot be used with mapper. Will not be called if propertyName/sourceKey is undefined. if sourceKey is present then the value for that propertyName on the serialised data will be used
  • setter: string, optional, name of the function to use for setting, will work like this model[setter](value).
  • sourceKey: string, optional, determines which propertyName on the the serialised data this value will come from. Cannot be used with mapper.
  • default: primitive value or function that will be used if the propertyName/sourceKey is not found on the the serialised data, or is undefined 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'
}