Giter VIP home page Giter VIP logo

data-adapter's Introduction

Introduction

Library for data adaptation using decorators. You can declaratively state how your data should look when being sent to the server.

How to use?

npm install data-adapter

Example

Using class adapter

import {AdaptClass, denormalize, normalize} from 'data-adapter';

@AdaptClass({
  name: snakeCase
})
class Human {
  firstName: string = 'foo';
  lastName: string = 'bar';
  age: number = 42;
}

// { first_name: 'foo', last_name: 'bar', age: 42 }
denormalize(new Human());

// { firstName: 'foo', lastName: 'bar', age: 42 }
normalize({ first_name: 'foo', last_name: 'bar', age: 42 }, Human);

Using property adapters

import {Adapt, denormalize, normalize} from 'data-adapter';

const denormalize = (obj, field) => {
  return obj[field] === 'male' ? 0 : 1;
};

const normalize = (obj, field) => {
  return obj[field] === 0 ? 'male' : 'female';
};

class Kid {
  @Adapt({ normalize, denormalize })
  gender;
}

class Parent {
  @Adapt({ name: 'first_name' }) firstName;
  @Adapt({ name: 'last_name' }) lastName;
  @Adapt({ type: Kid })
  kids;
}

let parent = new Parent();
parent.firstName = 'John';
parent.lastName = 'Doe';

let kid1 = new Kid();
kid1.gender = 'male';
let kid2 = new Kid();
kid2.gender = 'female';

// { first_name: 'John', last_name: 'Doe', kids: [{ gender: 0 }, { gender: 1 }] }
const denormalized = denormalize(new Person());

// { firstName: 'John', lastName: 'Doe', kids: [{ gender: 'male' }, { gender: 'female' }] }
const normalized = normalize(denormalized);

License

MIT

data-adapter's People

Contributors

mgechev avatar lpmi-13 avatar

Stargazers

Unix Doughnut avatar  avatar Mariano Alvarez avatar Christopher Cortes avatar Marcus S. Abildskov avatar Ivan Ivanov avatar Brian Ramirez avatar Vijay Singh avatar Tane Piper avatar C.OG avatar Vitalii Bobrov avatar Pierre-Edouard Galtier avatar Thomas Pfeiffer avatar Svilen Velikov avatar João Vitor Paes de Barros do Carmo avatar Theophilos Matzavinos avatar Andrei Balasescu avatar Arthur Melo avatar Kirill Neruchev avatar Michael Hladky avatar Manoj Talagadadeevi avatar Artem Halas avatar Kevin Schuchard avatar Martin avatar Jose I. Rubio avatar Chia-Chun Chen avatar Christian M. Austria avatar Nikita Popov avatar Joel Rinkoff avatar John Simerlink avatar Benjamin RICHARD avatar Frederik Prijck avatar  avatar Anik Hasan avatar Jorge Rodríguez Galán avatar Quinntyne Brown avatar Mateusz Kocz avatar Anton Shubin avatar Dzmitry avatar alwayrun avatar  avatar Georgi Serev avatar Jørgen Arnor G. Lom avatar Jayson Harshbarger avatar Joshua Wiens avatar

Watchers

Martin avatar  avatar James Cloos avatar  avatar

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.