Похожие чаты

I am rewriting some methods of lodash because I am

learning FP stuffs. I was rewriting implementation of _.drop https://lodash.com/docs/4.17.15#drop

export const drop = (array: unknown[], n = 1) => array.slice(n);

I have written something like above snippet and I found why I really need drop? It's doing same thing as .slice but when I check lodash source code they have written something differently

https://github.com/lodash/lodash/blob/4.17.15/lodash.js#L4045

I am just curious why they have written almost 20 lines of code if they can do using .slice()
also, why they have written _.drop since .slice will do same thing

6 ответов

13 просмотров
decoder- Автор вопроса

get more context from here

decoder- Автор вопроса

I thought may be they have written this method because slice not supported older browsers but I see almost all browsers are supports

there is no fun with that. got to wrap JavaScript with more JavaScript to enjoy life

Cause it's doing a lot more than just running the function. It needs to check if all the parameters are okay, make sure you didn't pass through an empty array, check the size, check if the value even fits in the array, make sure you're working with a positive value if you pass through a negative, etc. If you know that your data is gonna be clean and it can't be anything else than what you provide then just pass it through slice for a performance gain instead of relying on the checks c:

J : Vaccinated 🇨🇦
Cause it's doing a lot more than just running the ...

I understand using the library is a personal/teams choice. But why not follow the single responsibility principle (unwritten rule)¿? similar to unix philosophy

Похожие вопросы

Обсуждают сегодня

Карта сайта