between
tests if a numeral value is between two bounds.
const x = 50
between(x, 10, 100) // true
cycle
returns the next value of a set of values, and defaults to the first one.
cycle(current, 'a', 'b', 'c') // if current === 'a', returns 'b'
cycle(current, 'a', 'b', 'c') // if current === 'b', returns 'c'
cycle(current, 'a', 'b', 'c') // if current === 'c', returns 'a'
cycle(current, 'a', 'b', 'c') // if current === 'x', returns 'a'
defined
returns true
if the passed value is not undefined
.
defined(undefined) // false
defined('abc') // true
either
returns true
if the first argument matches any of the other.
const something = 'bar'
either(something, 'foo', 'bar', 'baz') // true
exists
returns true
if its argument is not null
or undefined
.
exists('a') // true
exists(null) // false
exists(undefined) // false
global
is a set of functions to create and access global variables.
// In file A
setGlobal('store', store)
// In file B
getGlobal('store') // store
html
is a template string tag that does almost nothing. It's a normal template string, but your code editor or its plugins might support syntax highlighting for HTML when they are marked with an html
tag.
const name = 'Sven'
console.log(html`<div>Hello ${name}</div>`) // This HTML code has syntax highlighting
ifs
is a function that does an if
/ else if
/ else
chain. Since it doesn't have the rigid structure of if
and else
statements, you can inline if anywhere in your code.
ifs(
[value < 10, 'green'],
[value < 100, 'yellow'],
[value < 1000, 'orange'],
[value < 10000, 'red'],
'black',
)
Make sure you read the documentation to avoid side-effects.
inlineThrow
is a function that throw
s its argument. Unlike a regular throw
, it's an expression and can be used anywhere.
const number = isNan(string) ? inlineThrow(Error('Not a number')) : parseInt(string)
run
lets you run some imperative code anywhere. Useful for debugging in declarative code.
const Cmp = ({ something }) => (
<div data-something={run(() => {
console.log(something)
doWhatever()
return something
})}></div>
)
swit
is an inline and less verbose switch
.
const value = 2
swit(
value,
[1, 'one'],
[2, 'two'],
[3, 'three'],
'default'
)
// returns 'two'
Make sure you read the documentation to avoid side-effects.
toggle
toggles between two values, and defaults to the first one.
toggle(current, 'a', 'b') // if current === 'a', returns 'b'
toggle(current, 'a', 'b') // if current === 'b', returns 'a'
toggle(current, 'a', 'b') // if current === 'x', returns 'a'
tryCatch
is an inline try
/ catch
/ finally
function, which returns the result of the try
or catch
case.
tryCatch(() => success()) // some result
tryCatch(() => failure()) // undefined
tryCatch(() => failure(), err => err) // the error
tryCatch(() => failure(), () => {}) // undefined
tryCatch(() => whatever(), () => {}, () => cleanup())
wait
is a Promise
-based delay.
console.log('Legen - wait for it...')
await wait(3000)
console.log('...dary!')
This package is part of @sharynjs/sharyn.