Giter VIP home page Giter VIP logo

js-object-transformation's Introduction

Intro

A tool to transform one js object into another

Word of warning

This code is at version 0.0.0 so its interface might be changed in the future

Code is written in es6 (a.k.a. js2015) and needs babel for transpiling and running unit tests

Is not published to any package manager repo

JSON path syntax is not fully supported (e.g. no relative paths, no array items by index etc)

Examples

import transform from '../src/main.js';

Simple

// get value from 'a' property
transform({ a: '_a_' }, '$.a'); // > '_a_'

// get value from the sub property
transform({ a: { b: '_b_' }}, '$.a.b'); // > '_b_'

// get array and apply some modificator to it
transform({ c: [1, 5, 2, 3] }, ['$.c[*]', c=>Math.max(...c) ]); // > 5

Object

let src = {
    a: { sub: 'bio' },
    b: 'hazard',
    c: [ 'c', 'e', 'l', 'l' ]
};

let mod = {
    // $ points to the source's root

    // just copy value from 'b' to 'y'
    y: '$.b',

    // get 'a.sub' from src and put it to 'x' 
    x: '$.a.sub',

    // get 'b' property and src itself
    // and then apply modificator
    // and put to z
    z: [ '$.a', '$', (a,$)=>`${a.sub.toUpperCase()} ${$.b}` ],

    // again, simple copying of array
    // but putting it to sub property
    c: { cell: '$.c[*]' },

    // apply some modificator to array from src
    // and put it to 'r'
    r: ['$.c[*]', c=>c.join('')]
};


let result = transform(src, mod);

result === {
    y: 'hazard',
    x: 'bio',
    z: 'BIO hazard',
    c: { cell: [ 'c', 'e', 'l', 'l'  ]  },
    r: 'cell'
};

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.