1.0.8 • Published 2 years ago

hydrate-schema v1.0.8

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
2 years ago

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
1.0.2

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.0

3 years ago