Giter Club home page Giter Club logo

hydrate-schema's Introduction

hydrate-schema

Build an object by parsing another using different funcions for every new property

Installation

npm install hydrate-schema

Examples

This library is supposed to be used in a functional programing context, hence the use of ramda as an utility library in the following examples. However, using ramda, or an alternative, is not necessary.

  import hydrateSchema from 'hydrate-schema'
  // or const hydrateSchema = require('hydrate-schema').default

  const schema = {
    bar: '3',
    fn: R.pipe(R.prop('foo'), R.toUpper),
    arr: [ 1, R.pipe(R.prop('other'), Number, R.add(1)), 3],
    obj: {
      subbar: 42,
      subfn: R.pipe(R.path(['obj', 'subfoo']), String),
      emptyArr: []
    }
  }

  const obj = {
    foo: 'asd',
    other: '13',
    obj: {
      subfoo: 11
    }
  }

  hydrateSchema(schema, model)
  // or hydrateSchema(schema)(model)

  /* Expected result

  {
    bar: '3',
    fn: 'ASD',
    arr: [ 1, 14, 3 ],
    obj: {
      subbar: 42,
      subfn: '11',
      emptyArr: []
    }
  }

  */

Since hydrateSchema is curried, you can compose different schemas to create a bigger one


  const subSchema = hydrateSchema({
    a: '3',
    b: R.pipe(R.prop('foo'), R.toUpper)
  })

  const createFooObject = hydrateSchema({
    c: subSchema,
    d: Number
  })

Test

npm run prepare # Only if there are changes to be transcompiled
npm run test

hydrate-schema's People

Contributors

rodpepablo avatar

Watchers

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.