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

bumblebee-object-transformation

v0.3.9

Published

Object transformation tool inspired on mongoose models.

Downloads

23

Readme

Coverage Status Dependency Status

Bumblebee javascript object transformation tool

bumblebee

Object transformation tool inspired on mongoose models. The basic idea is to contruct a result object, form a model and a reference object (and also you can pass an object as base).

Getting Started

Install the module with: npm install bumblebee

    var bumblebee = require('bumblebee-object-transformation');
    bumblebee(initialObject, referenceObject, model, function(err, result){
        console.log(result);
        //Your code....
    });

Documentation

Basically you should use as model the final object, and field by field you can speficy:

  • __format : (to specify the default format)
  • __default : (if bumblebee dont find the field you can specify a default field)
  • __composer : (to make some operations predefined)
  • __scheema : (to compose the scheema of this field)
  • __originField: (to specify the orifinal field inside the object, remeber that any scheema that you define inside the composer use their own element or subobject as reference)
  • __clean : (to use over the object selected my object cleaner, that removes innecesarry arrays with lenght 0, really usefull when you convert from xml to js.)

For now the operations are:

  • RegEx : to find inside the object a fields and put in an array.
  • everyElement : to loop over the elements of an array and make the transformations
  • everyElementToArray : to loop over the elements of an array and create an object, you need to specify the key of the every element (__key)

Remember that this module is underconstruction, if you have some needs try to follow the essential structure of the application, and i try to merge all request. Usully i use this module, so im adapting frecuently, and sometimes wont be really well optimized, all suggestions are welcome.

Thanks!

Examples

###(From 0.3.X Examples)

    initialObject = {};
    var referenceObject = {
        'title' : 'title',
        'text': 'hey im a text',
        'position' : [ 41.3901566, 2.1355214 ],
        'you' : {
            'video' : {
                'active': true,
                'times': 4,
                'information':{
                    'duration': 10,
                    'format': 'min'
                }
            }
        },
        'youtube':{
            'expiration': 'DATE',
            'expirationTimezone': 'GMT+1'
        },
        'buyNowConfig': {
            'price': 100
        },
        'product':{
            'images':[
                { url: 'http://lkjljklkj/123'},
                { url: 'http://lkjljklkj/456'},
                { url: 'http://lkjljklkj/678'},
                { url: 'http://lkjljklkj/999'},
            ]
        },
        providers:[{
            '$': {
                key: 'acme',
                type: 'company'
            },
            ids: ['123']
        },{
            '$': {
                key: 'octo',
                type: 'company'
            },
            ids: '345'
        }]
    };
    var model = {
        'position': {
            __format: 'Object',
            __scheema: {
                lat: { __format: 'Float', __originField: 'position.0' },
                lng: { __format: 'Float', __originField: 'position.1' }
            }
        },
        'youtube': {
            __scheema:{
                'active': { __format: 'Boolean', __originField: 'you.video.active'},
                'times': { __format: 'Integer', __originField: 'you.video.times'},
                'duration': { __format: 'Integer', __originField: 'you.video.information.duration'},
                'expiration': { __format: 'String', __originField: 'youtube.expiration'},
                'expirationTimezone': { __format: 'String', __originField: 'youtube.expirationTimezone'},
            }
        },
        'products': {
            __format: 'Array',
            __scheema: {
                'title': { __format: 'Object' },
                'text': { __format: 'Object' },
                'imagePath': { __format: 'String', __originField: 'images.0.url', __outputMod: function (url) { return url+ ".jpg" } },
                'buyNowPrice': { __format: 'Float', __originField: 'buyNowConfig.price' }
            }
        },
        'images' : {
            __format: 'Array',
            __originField: 'product.images',
            __composer:{
                __type: 'everyElement',
                __scheema: {
                    'type': { __format: 'String', __default: 'internal' },
                    'fullUrl': { __format: 'String', __originField: 'url', __outputMod: function (url) { return url+ ".jpg" } }
                }
            }
        },
        'providers' : {
            __format: "Object",
            __composer:{
                __type: 'everyElementToObject',
                __originField: 'providers',
                __key: '$.key',
                __scheema: {
                    type:{
                        __format: 'String',
                        __originField: '$.type'
                    },
                    codes: {
                        __format: 'Array',
                        __originField: 'ids'
                    }
                },
            }
        }
    };

Passing this 2 objects the module reconstruct this in the output:


result = {
    position: {
        lat: 41.3901566,
        lng: 2.1355214
    },
    youtube : {
        active: true,
        times: 4,
        duration: 10,
        expiration: 'DATE',
        expirationTimezone: 'GMT+1'
    },
    products: [
        {
        title: 'title',
        text: 'hey im a text',
        imagePath: 'http://lkjljklkj/123.jpg',
        buyNowPrice: 100
        }
    ],
    images: [
        { type: 'internal', fullUrl: 'http://lkjljklkj/123.jpg' },
        { type: 'internal', fullUrl: 'http://lkjljklkj/456.jpg' },
        { type: 'internal', fullUrl: 'http://lkjljklkj/678.jpg' },
        { type: 'internal', fullUrl: 'http://lkjljklkj/999.jpg' }
    ],
    providers:{
        acme: {
            type: 'company',
            codes: ['123']
        },
        octo: {
            type: 'company',
            codes: ['345']
        }
    }
}

###(From 0.2.1 Examples)

    initialObject = {
        _id: "000001"
    };
    referenceObject = {
        Name: 'dummy',
        number: '1243',
        latitud: '12.43',
        Active: 'true',
        image1: 'http://urltoimage1',
        image2: 'http://urltoimage2',
        image3: 'http://urltoimage3',
        image4: 'http://urltoimage4',
        codes:[
            {cod1: '111', cod2:'11'},
            {cod1: '333', cod2:'11'},
            {cod1: '444', cod2:'22'},
            {cod1: '555', cod2:'22'}
        ]
    };
    model = {
        name: {type: 'String', originField: 'Name'},
        code: {type: 'Integer', originField: 'number'},
        active: {type: 'Boolean', originField: 'Active'},
        images: {type: 'Array', composer:{type: 'RegEx', reg: 'image', use: 'value'}},
        latitud: {type: 'Float'},
        maybeValue: {type: 'Integer', default: 0},
        propertyCodes: {
            type: 'Array',
            originField: 'codes',
            composer:{
                type: 'everyElement',
                scheema: {
                    providerCode: {type: 'String', originField: 'cod1'},
                    providerType: {type: 'String', originField: 'cod2'}
                }
            }
        }
    };

Passing this 2 objects the module reconstruct this in the output:

result = {
  _id: "000001",
    name: 'dummy',
    code: 1243,
    active: true,
    images: [
      'http://urltoimage1',
      'http://urltoimage2',
      'http://urltoimage3',
      'http://urltoimage4'
   ],
    latitud: 12.43,
    maybeValue: 0,
    propertyCodes:[
      { providerCode: '111', providerType: '11' },
      { providerCode: '333', providerType: '11' },
      { providerCode: '444', providerType: '22' },
      { providerCode: '555', providerType: '22' }
  ]
}

Contributing

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using Grunt.

Release History

####(0.3.9 Lastest)

  • Added everyElementToObject basically its the same that every element and you need to specify the key of the object for every element
  • Added inside the everyElement of an array the posibilty to use a "__outputMod" function
  • Added inside the everyElement with __scheema the __condition function, if true the element should be inject on the final everyElement array
  • Fixed little problem with composer and array without __scheema field.

####(0.3.0) Models:

  • (0.3.0) (From 0.2.1 to 0.3) Not more type to define the output format, simply format.
  • (0.3.0) (From 0.2.1 to 0.3) All internal operators are rewrite with "__" to remove comflicts

Operations:

  • (0.3.0) Now added the scheema operation that allows you to compose objects.

License

Copyright (c) 2014 Daniel Biedma Ramos Licensed under the MIT license.