Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/orodio/muoto
Declarative shape checking
https://github.com/orodio/muoto
Last synced: 16 days ago
JSON representation
Declarative shape checking
- Host: GitHub
- URL: https://github.com/orodio/muoto
- Owner: orodio
- Created: 2016-06-27T07:45:22.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2016-06-27T07:57:49.000Z (over 8 years ago)
- Last Synced: 2024-04-25T14:22:10.620Z (7 months ago)
- Language: JavaScript
- Size: 8.79 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# muoto
## install
```
$ npm intall --save muoto
```## usage
```javascript
import { shape } from "muoto"function bob (thing) {
if (!shape({
foo: 1,
bar: shape.number,
baz: shape.string,
fuzz: shape.bool,
})(thing).valid) throw new Error("Passed in the wrong thing")// do stuff
}bob({ foo:1, bar:1, baz:"a", fuzz:true }) // does stuff
bob() // throws
bob({}) // throws
bob({ foo:2, bar:1, baz:"a", fuzz:true }) // throws```
### using your own shape things
```javascript
function greater_than (n) {
return function (value, cursor, tools) {
if (tools.is.NUMBER(value)) return tools.Nope(cursor, value, cursor + " needs to be a number")
if (value <= n) return tools.Nope(cursor, value, cursor + " needs to be greater than " + n + ", but was " + value)
return tools.Just(cursor, value)
}
}var check = shape({ foo:greater_than(4) })
check({ foo:9 }).valid // true
check({ foo:3 }).valid // false
check({ foo:19 }).valid // true
```### some other stuff
```javascript
// arrays
shape([1])([1, 1, 1, 1]).valid // true
shape([1])([1, 2, 1, 1]).valid // false
shape([1, 2, 1, 1])([1, 2, 1, 1]).valid // true
shape([1, 2, 1, 1])([1, 1, 1, 1]).valid // false
shape([{ foo:shape.number }])([ { foo:1 }, { foo:2 }, { foo:3} ]).valid // trueshape(shape.number, { blocking: true })("b") // throws an error
shape(shape.number, { logging: true })("b") // logs an error messagevar shp = shape({ foo:[{ bar:1 }] })({ foo:[{ bar:1 }, { bar:1 }, { bar:2 }, { bar:1 }] })
shp.valid // false
shp.cursor // foo[2].barshape(shape.number, { bypass:true })("a").valid // true - wont execute any of the traversals (prod mode maybe)
```