Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
chuigda authored Jul 30, 2021
1 parent 4bae1d4 commit 59991f9
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,81 @@
# TypeAssert
Minimal JavaScript type assertions

## Basic usage
```javascript
const { typeAssert } = require('./typeAssert')

// simple types
typeAssert(1, 'number')
typeAssert(2, 'string')

// object
typeAssert({ x: 1, y: 3 }, 'object')
typeAssert({ x: 1, y: 3 }, {})

// array
typeAssert([1, 2, 3], 'Array')
typeAssert([1, 2, 3], [])

// object fields
typeAssert({
x: 1,
y: '2'
}, {
x: 'number',
y: 'string'
})

// array elements
typeAssert([1, 2, 3], ['number'])

// "sum" types
const assertion = 'string'.sumWith('number')
typeAssert('abc', assertion)
typeAssert(123, assertion)

// nullable types
const assertion = { x: 'number', y: 'function' }.orNull()
typeAssert({
x: 144,
y: () => {}
}, assertion)
typeAssert(null, assertion)

// nullable shorthand for simple types
typeAssert(114, 'number?')
typeAssert(null, 'number?)
// nested situation
const assertion = {
a: 'number',
b: 'string',
c: [
{
x: 'function',
y: 'function'
}.sumWith({
x: [],
y: {}
}).orNull()
]
}

typeAssert({
a: 114,
b: '514',
c: [
null,
{
x: [1, 2, 3],
y: {
z: '4'
}
},
{
x: console.log,
y: Array.prototype.push
}
]
}, assertion)
```

0 comments on commit 59991f9

Please sign in to comment.