Skip to content

rodpepablo/hydrate-schema

Repository files navigation

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

About

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

Resources

License

Stars

Watchers

Forks

Packages

No packages published