@munierujp/val
v1.0.0
Published
Val class wraps value
Downloads
19
Readme
Val.js
Val class wraps value.
Installation
$ npm i @munierujp/val
or
$ yarn add @munierujp/val
Use in your script
const Val = require('@munierujp/val')
Usage
You can use below methods.
Creation
Val.empty()
const val = Val.empty()
console.log(val) // { _: null }
Val.of(value)
const val = Val.of('foo')
console.log(val) // { _: 'foo' }
Intermediate operation
Val.filter(matcher)
const val = Val.of('foo')
const filtered = val.filter(v => v.startsWith('f'))
console.log(filtered) // { _: 'foo' }
const val = Val.of('foo')
const filtered = val.filter(v => v.startsWith('b'))
console.log(filtered) // { _: null }
Val.map(mapper)
const val = Val.of('foo')
const mapped = val.map(v => v + '_')
console.log(mapped) // { _: 'foo_' }
const val = Val.empty()
const mapped = val.map(v => v + '_')
console.log(mapped) // { _: null }
Val.ifPresent(callback)
const val = Val.of('foo')
let tmp
const val2 = val.ifPresent(v => {
tmp = v
})
console.log(val2) // { _: 'foo' }
console.log(tmp) // foo
const val = Val.empty()
let tmp
const val2 = val.ifPresent(v => {
tmp = v
})
console.log(val2) // { _: null }
console.log(tmp) // undefined
Val.ifAbsent(callback)
const val = Val.of('foo')
let tmp
const val2 = val.ifAbsent(() => {
tmp = 'bar'
})
console.log(val2) // { _: 'foo' }
console.log(tmp) // undefined
const val = Val.empty()
let tmp
const val2 = val.ifAbsent(() => {
tmp = 'bar'
})
console.log(val2) // { _: null }
console.log(tmp) // bar
Val.or(other)
const val = Val.of('foo')
const value = val.or('bar')
console.log(value) // foo
const val = Val.empty()
const value = val.or('bar')
console.log(value) // bar
Val.is(value)
const val = Val.of('foo')
console.log(val.is('foo')) // true
const val = Val.of('foo')
console.log(val.is('bar')) // false
Val.isPresent()
const val = Val.of('foo')
console.log(val.isPresent()) // true
const val = Val.empty()
console.log(val.isPresent()) // false
Val.isAbsent()
const val = Val.of('foo')
console.log(val.isAbsent()) // false
const val = Val.empty()
console.log(val.isAbsent()) // true