Giter VIP home page Giter VIP logo

data-filler's Introduction

data-filler ๐Ÿ‹

make your backend response data shape reliable, with data-filler you can stay away from optional chain.

Quick Start

install

npm i data-filler
  • example 1
    missed level 1 key filled by template
import { fillDataByTemplate } from 'data-filler';

const data = { a: 1, b: 2 };
const template = { a: 0, b: 0, c: 0, d: [] };
const filled = fillDataByTemplate(data, template);
// filled is: { a: 1, b: 2, c: 0, d: [] }
  • example 2
    fixed incorrect type of key values by template
import { fillDataByTemplate } from 'data-filler';

const data = { a: 1, b: 2, c: { c1: 100 } };
const template = { a: 0, b: 0, c: 0 };
const filled = fillDataByTemplate(data, template);
// filled is: { a: 1, b: 2, c: 0 }
  • example 3
    missed child level key filled by template
import { fillDataByTemplate } from 'data-filler';

const data = { a: 1, b: 2, c: { c1: 100 } };
const template = { a: 0, b: 0, c: { c1: 0, c2: 0, c3: 0 } };
const filled = fillDataByTemplate(data, template);
// filled is: { a: 1, b: 2, c: { c1: 100, c2: 0, c3: 0 } }
  • example 4
    missed array item key filled by template
import { fillDataByTemplate } from 'data-filler';

const data = { a: 1, b: 2, c: [{ c1: 1, c2: 1 }] };
const template = { a: 0, b: 0, c: [] };
const filled = fillDataByTemplate(data, template, { 'c': { c1: 100, c2: 100, c3: 100 } });
// filled is: { a: 1, b: 2, c: [{ c1: 1, c2: 1, c3: 100 }] }

use 3rd param arrItemTemplates to describe array item data shape

  • example 5
    missed child array item key filled by template
import { fillDataByTemplate } from 'data-filler';

const data = { a: 1, b: 2, c: [{ c1: 1, c2: [{ d1: 1 }] }] };
const template = { a: 0, b: 0, c: [] };
const filled = fillDataByTemplate(data, template, {
  'c': { c1: 100, c2: [], c3: 100 },
  'c.c2': { d1: 0, d2: 100 },
});
// filled is: { a: 1, b: 2, c: [{ c1: 1, c2: [{ d1: 1, d2: 100 }], c3: 100 }] }

3rd param arrItemTemplates support key path to describe nested array item data shape

License

concent is released under the MIT License. http://www.opensource.org/licenses/mit-license

data-filler's People

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

amyyo

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.