Code Monkey home page Code Monkey logo

dataadapter's Introduction

Data adapter

Convert data to suitable keys by defining a json object.

Usage

const data = {
    one: {
        one: 'this is one',
        two: 'this is two',
        renderAdd: { 'one': 1, two: 2 }
    }
}

const schema = [
    { dataKey: 'one.one', key: 'first' },
    { dataKey: 'one.two', key: 'second' },
    { 
      dataKey: 'one.renderAdd', 
      key: 'addition', 
      renderer: data => parseInt(data.one, 10) + parseInt(data.two, 10)
    }
]

const entity = {
    three: 'this is third',
    four: {
        five: 'this is nested'
    }
}

Pass the data and schema to the adapter. The transformed adapter will be mapped to the entity.

const transformedData = buildEntity({ data, schema, entity })

Output

{
    first: 'this is one',
    second: 'this is two',
    three: 'this is third',
    addition: 3,
    four: {
        five: 'this is nested'
    }
}

If the input data is an array of data, use mapDataToAdapter({ data, schema}), which will map and call the buildEntity internally. In this case, it will allow entity to be mapped with the object as there are too many different use cases.

Build schema object

Alternatively, you might want a flexible schema output that could be utilised for rendering in the UI.

Usage

const schema = [
    { dataKey: 'one.one', key: 'first' , uiRenderer: data => <em>data</em> },
    { dataKey: 'one.two', key: 'second' },
    { 
      dataKey: 'one.renderAdd', 
      key: 'addition', 
      renderer: data => parseInt(data.one, 10) + parseInt(data.two, 10)
    }
]

buildSchema({ data, schema })

You can also define a default schema to be added to each schema. By default, we add a uiRenderer key to render '-' if no data is found.

const defaultSchema = { uiRenderer: _data => _data || '-' }; 
buildSchema({ data, schema, defaultSchema })

Output

{
	first: {
		dataKey: 'one.one',
		key: 'first',
		data: 'this is one',
        uiRenderer: _data => _data || '-' 
	},
	second: {
		dataKey: 'one.two',
		key: 'second',
		data: 'this is two',
        uiRenderer: _data => _data || '-' 
	},
};

Dependencies

Immutable JS

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.