Closed jankapunkt closed 4 years ago
Task list:
_.has
replace with item in object
_.clone
replaced with { ...object }
_.extend
replaced with { ...a, ...b }
or Object.assign(a, b)
_.filter
replaced with array.filter
_.contains
replaced with array.includes
_.isArray
replaced with Array.isArray
_.keys
replaced with Object.keys
_.unique
replaced with [...new Set(array)]
_.compact
replaced with array.filter(Boolean)
_.any
replaced with array.some
_.map
replaced with Object.entries(object).map
, Object.values(object).map
or array.map
_.throttle
replaced with [1]_.where
replaced with Object.values(object).filter
_.reject
replaced with array.filter
_.without
replaced with array.filter
and !array.includes
_.find
replaced with array.find
_.last
replaced with array.pop()
_.intersection
replaced with array.filter
and array.includes
_.isEmpty
replaced with !Object.keys(object).length
_.difference
replaced with Object.values(object).filter
_.isString
replaced with 'string' === typeof x
_.once
replaced with [2]_.isObject
replaced with [3]_.isFunction
replaced with [4]_.pick
replaced with ES6 destructuring _.omit
replaced with ES6 destructuring_.all
replaced with array.every
_.each
replaced with Object.entries(object).forEach
, Object.keys(object).forEach
or array.forEach
[1] throttle function
function throttle(fn, limit) {
let timeout
return function (...args) {
clearTimeout(timeout)
timeout = setTimeout(function () {
fn(...args)
}, limit)
}
}
[2] once implementation
const fnOnce = () => {
let alreadyRan = false
return d => {
if (alreadyRan) return
alreadyRan = true
return fn(d)
}
}
[3] isObject
export const isObject = obj => toString.call(obj) == '[object Object]' || Array.isArray(obj) || isFunction(obj)
[4] isFunction
export const isFunction = obj => toString.call(obj) == '[object Function]'
I could implement some of them. Do you need support on any of them?
Some of them have an obvious alternative, some of them don't. For example I don't know what's the best way to do _.isFunction
, should I do x instanceof Function
? Should I do "function" == typeof x
? For _.isObject
it's even more complicated. Should we replace them by the exact underscore definitions?
I think it depends on how "complete" our cases need these Functions to be. I have a gist with very close completeness: https://gist.github.com/jankapunkt/e43405ab19fd939ff058be1e9d4c177e
But this may be total overkill for many cases. Often Objects that are not a subclass of Object (like Array, String etc.) have the constructor [Object]
while Array for example has the constructor name [object Array]
(which is used via Array.isArray
) so for simple checks this may be sufficient for 99% of the cases.
Using instanceof
traverses the whole prototype chain and slows down things a lot. Maybe the most performant appraoch is to define the use cases, write some tests with most (better: all) occurring inputs and find the most minimal solution (like using 'function' === typeof x`)
@jankapunkt https://gist.github.com/jankapunkt/e43405ab19fd939ff058be1e9d4c177e breaks autoform.
I guess that's all of them!
Ok, we don't depend on underscore anymore.
Going to close this, this is done and if it causes any issues it should be followed in a new thread.
As discussed in the community packages forums, we should remove these utility package dependency.