access :: Any (String) -> Any
Access a property deep via a dot object notation on any kind of Object.
This function is used internally to implement shortcut notations for .map
/ .filter
/ .find
/ .findIndex
/ .sort
user = await (await fetch('https://api.github.com/users/torvalds')).json()
repos = await (await fetch('https://api.github.com/users/torvalds/repos')).json()
Object.access(user, 'name')
// Accessing Object
user.access('name')
// Accessing Array
repos.access('0.owner.id')
// Warning: can't access a property containing either a `.` or a `[]`
Object.access({ 'a.b': 1 }, 'a.b')
equal :: Any (Any) -> Boolean
Compare any kind of object together, returns true in case of deep equality.
This function is used internally to implement shortcut notations for .filter
/ .find
/ .findIndex
const a = [null, { a: { b: /c/ }, c: [{ d: 1 }] }, new Date('2020')]
const b = a
b[1].a.b = /diff/
Object.eq(a, b) // false
JSON.stringify(a) === JSON.stringify(b) // true
extend :: Object (Boolean) -> Array
This is the core function:
- If called with
true
, extends [Object, Array, Function, String, Number, Date, RegExp] prototypes with their function properties. - If called with
false|undefined
, add shortcuts to [Object, Array, Function, String, Number, Date, RegExp] function properties.
// Adding a function and extending
Array.last = arr => arr.slice(-1)[0]
Object.extends(true)
[1, 2, 3].last() === 3
// Adding a shortcut without extending
Object.extend.shortcuts.max = (fn, ...args) => {
if (args[1] === true) args[0] = args[0].filter(v => typeof v === 'number')
return fn(...args)
}
Object.extend()
[1, 2, '3'].max(true)
filter :: Object (Any) -> Object
Create a new object with properties that pass the test implemented by the function or shortcut.
user.filter(/Li/i)
user.filter((v, k) => v > 1000 || k === 'name')
// TODO: user.filter(['name', 'followers'])
find :: Object (Any) -> String|null
Returns the value of the first element in the object that satisfies the testing function.
user.find(/Li/)
user.find((v, k) => v > 1000 || k === 'name')
find :: Object (Any) -> String|null
Returns the key of the first element in the object that satisfies the testing function.
user.findIndex(/Li/)
user.findIndex((v, k) => v > 1000 || k === 'name')
keys :: Object () -> Array
Returns the object keys.
map :: Object (Any) -> Object
Creates a new object with every values passed thru the mapping function.
user.map('length')
user.map((v, k) => Number(v) ? v * 100 : k)
reduce :: Object (Function, Any) -> Any
Executes a reducer function on each properties of the object, resulting in a single output value.
user.reduce((acc, v, k) => {
if (v > 100) acc.push(v)
if (k === 'login') acc.push(v)
return acc
}, [])
values :: Object () -> Array
Returns the object values.
filter :: Array (Any) -> Array
Creates a new array with all elements that pass the test implemented by the function.
repos.filter(v => v.forks > 50)
[1, '3', 2].filter(Number) // [1, 2] // TODO
find :: Array (Any) -> Any
Returns the value of the first element in the array that satisfies the testing function.
repos.find(v => v.forks > 50)
Returns the index of the first element in the array that satisfies the testing function.
findIndex :: Array (Any) -> Number|null
repos.findIndex({ name: /linux/i })
group :: Array (Any) -> Any
Creates a new object with keys equals to elements passed thru the grouping function and values an array of elements.
repos.group('fork')
Creates a new array with every elements passed thru the mapping function.
map :: Array (Any) -> Array
repos.map({ name: 'name', fame: 'forks' })
// TODO: repos.map({ name: 'name', famous: v => v.forks > 1000 })
Returns the max
max :: Array () -> Number
repos.map('forks').max()
mean :: Array () -> Number
repos.map('forks').mean()
min :: Array () -> Number
repos.map('forks').min()
reduce :: Array (Function, Any) -> Any
repos.reduce((acc, v) => {
acc[v.name] = v.forks
return acc
}, {})
sort :: Array (Function) -> Any
repos.sort('forks').map('name')
repos.sort(['fork', '-forks']).map(['fork', 'forks'])
sum :: Array () -> Number
repos.map('forks').sum()
unique :: Array () -> Array
repos.map('forks').min()
debounce :: Function (Number) -> Void
const log = () => console.log('It will never be called')
setInterval(log.debounce(1000), 100)
every :: Function (Number, Number, Boolean) -> This
const log = () => console.log('It will never be called')
log.every(100)
memoize :: Function (Function) -> Function
const log = () => console.log('It will be called once only with the same signature')
const logmem = log.memoize()
logmem(1)
logmem(1)
partial :: Function (Any[]) -> Function
const log = (a, b, c) => console.log(a, b, c)
const logp = log.partial('a!', null, 'c!')
logp(1) // a! 1 c!
wait :: Function (Number) -> This
const log = () => console.log('It will be called once after a delay')
log.wait(1000)
decorate :: Function (Function) -> Function
const log = () => console.log('It will be called if first argument is true')
const logdecorate = log.decorate((fn, ...args) => args[0] && fn())
logdecorate()
logdecorate(true)
capitalize :: String () -> String
format :: String (Any[]) -> String
lower :: String () -> String
upper :: String () -> String
words :: String (RegExp|String) -> Array
access :: Number () -> String
format :: Number (String) -> String|Number
end :: Date (String) -> Date
format :: Date (String) -> String
getLastDate :: Date () -> Number
getQuarter :: Date () -> Number
getWeek :: Date () -> Number
minus :: Date (String) -> Date
modify :: Date (String, String) -> Date
plus :: Date (String) -> Date
relative :: Date () -> String
start :: Date (String) -> Date
escape :: RegExp () -> RegExp
minus :: RegExp (String) -> RegExp
plus :: RegExp (String) -> RegExp