https://github.com/olsonpm/madonna-map
https://github.com/olsonpm/madonna-map
Last synced: 11 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/olsonpm/madonna-map
- Owner: olsonpm
- Created: 2016-06-01T04:55:48.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T18:12:35.000Z (almost 10 years ago)
- Last Synced: 2025-05-07T21:02:23.661Z (about 1 year ago)
- Language: JavaScript
- Size: 15.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Madonna Map
Creates a function that validates its arguments via
[madonna-fp](https://github.com/olsonpm/madonna-fp) before mapping them to
something else. I created this to validate raw json then map it through a
class or constructor. Note `madonna-map` only allows you to map top-level
properties. Allowing otherwise would create for unnecessarily complex code.
**Tested against**
- node 0.10.0 for the (default) es5 version
- node 6.0.0 for es6 @ `require('madonna-map/es6')`
## Table of Contents
- [Examples](#examples)
- [API](#api)
## Examples
```js
const madonnaMap = require('madonna-map');
// creates a function requiring the argument 'name' and maps its value to uppercase
let mapperFn = madonnaMap.createMapper({
marg: { name: ['require', 'isLadenString'] }
, argMap: { name: fp.toUpper }
});
mapperFn({ name: 'phil' });
// returns
// { name: 'PHIL' }
mapperFn({ name: {} });
// throws an error
// Invalid Input: The following arguments didn't pass their criterion
// invalid arguments and values: {
// "name": {}
// }
// failed criterion per argument: {
// "name": {
// "flags": [
// "isLadenString"
// ]
// }
// }
// don't mismatch the argMap property names with those declared in marg
mapperFn = madonnaMap.createMapper({
marg: { name: ['require', 'isLadenString'] }
, argMap: { notName: fp.toUpper }
});
// throws an error
// Invalid Input: argMap can only contain keys present in schema.
// invalid keys: notName
// keys allowed: name
// a slightly larger example
mapperFn = madonnaMap.createMapper({
marg: {
name: ['require', 'isLadenString']
, age: { betweenI: [0, 120] }
}
, argMap: { name: fp.toUpper }
});
mapperFn({
name: 'phil'
, age: 28
});
// returns
// { name: 'PHIL', age: 28 }
```
## API
`require('madonna-map').createMapper`
- Takes the following arguments
- **marg**: `require` `isLadenPlainObject`
- marg stands for 'madonna-fp argument'. It is passed to madonna-fp which
is used to validate the arguments in calls to the returned function.
- **argMap**: `require` `isLadenPlainObject`
- This object can only contain keys found in **marg** and their values
must be functions. Valid input will pass through these functions
and returned. Not all properties in **marg** have to be mapped.
- Returns a function that validates its input against **marg**. Any properties
in **argMap** will pass through their mappers. All validated arguments will
be returned.