https://github.com/olsonpm/madonna-function
https://github.com/olsonpm/madonna-function
Last synced: about 1 year ago
JSON representation
- Host: GitHub
- URL: https://github.com/olsonpm/madonna-function
- Owner: olsonpm
- Created: 2016-06-02T03:14:26.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2016-08-01T18:10:55.000Z (almost 10 years ago)
- Last Synced: 2025-01-20T23:46:02.343Z (over 1 year ago)
- Language: JavaScript
- Size: 10.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Madonna Function
Creates a function that validates its arguments via
[madonna-fp](https://github.com/olsonpm/madonna-fp), passing them to another
function. You can optionally configure mapper functions to modify the arguments
after validation. I created this because I often want to call a function and
know for certain the arguments are valid. The optional mapper is for
convenience since I also often need arguments to come in as raw json and passed
through a class or constructor. The mapping functionality is provided by
[madonna-map](https://github.com/olsonpm/madonna-fp).
**Tested against**
- node 0.10.0 for the (default) es5 version
- node 6.0.0 for es6 @ `require('madonna-function/es6')`
## Table of Contents
- [Examples](#examples)
- [API](#api)
## Examples
```js
const createMadonnaFn = require('madonna-function').create
, printArgs = console.dir.bind(console);
// creates a function requiring the argument 'name' and prints it
let mfn = createMadonnaFn({
marg: { name: ['require', 'isLadenString'] }
, fn: printArgs
});
mfn({ name: 'matt' });
// prints
// { name: 'matt' }
// lets do the same but map name to all uppercase letters
let mfn = createMadonnaFn({
marg: { name: ['require', 'isLadenString'] }
, fn: printArgs
, argMap: { name: fp.toUpper }
});
mfn({ name: 'matt' });
// prints
// { name: 'MATT' }
// madonna-fp validation errors will be thrown if you pass invalid arguments
mfn({ name: 1 });
// throws the error
// Invalid Input: The following arguments didn't pass their criterion
// invalid arguments and values: {
// "name": 1
// }
// failed criterion per argument: {
// "name": {
// "flags": [
// "isLadenString"
// ]
// }
// }
```
## API
`require('madonna-function').create`
- 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.
- **fn**: `require` `fp.isFunction`
- The function called with the validated, and possibly mapped arguments
- **argMap**: `isLadenPlainObject`
- An object with keys present in **marg** to functions whose input
will be the validated value, and output will be passed to **fn**.
Examples above should clarify any confusion.
- Returns a function that validates its input against **marg**. Any properties
in **argMap** will pass through their mappers. All validated and mapped
values will be passed into **fn**.