-
Notifications
You must be signed in to change notification settings - Fork 10
Avoid lodash #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Avoid lodash #11
Changes from all commits
75d950d
46cb4ad
5bd4fab
85be1d8
965f5cd
9fec11d
4202030
9c7dd8f
4a0456c
b2676f3
7a87af8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ React-specific guidelines described at [RingCentral React Style Guide](https://g | |
| 1. [Language statements and features](#language-statements-and-features) | ||
| 1. [Spaces and alignments](#spaces-and-alignments) | ||
| 1. [Naming](#naming) | ||
| 1. [External libraries](#external-libraries) | ||
|
|
||
| ## Overview | ||
|
|
||
|
|
@@ -825,3 +826,182 @@ export class MyCompositeComponent { | |
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## External libraries | ||
|
|
||
| ### Prefer native js vs lodash | ||
|
|
||
| > Reason: lodash is a great library which contains a lot of useful functions but the modern js has the short identical part of this functionality so some lodash functions are excess alternative. | ||
|
|
||
| > Don't forget to check necessary browsers compatibility and turn on polyfills if it's needed. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good note, I like it. I guess it would be better to clarify which variants is dangerous and add a link to MDN for every such method. We have to notify a users of this document.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suggest to not create a compatibility map, all developers understand about using different functionality and should think about each method itself depending which browser support is needed for an application |
||
| ```javascript | ||
| // BAD | ||
| _.each([1, 2, 3], callback); | ||
| // GOOD | ||
| [1, 2, 3].forEach(callback); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.filter([1, 2, 3], callback); | ||
| // GOOD | ||
| [1, 2, 3].filter(callback); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.compact([1, 2, 3, 0, false, '']); | ||
| // GOOD | ||
| [1, 2, 3, 0, false, ''].filter(Boolean); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.map([1, 2, 3], callback); | ||
| // GOOD | ||
| [1, 2, 3].map(callback); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.every([1, 2, 3], callback); | ||
| // GOOD | ||
| [1, 2, 3].every(callback); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.some([1, 2, 3], callback); | ||
| // GOOD | ||
| [1, 2, 3].some(callback); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.reduce([1, 2, 3], callback, 0); | ||
| // GOOD | ||
| [1, 2, 3].reduce(callback, 0); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.concat([1, 2, 3], [4, 5, 6]); | ||
| // GOOD | ||
| [1, 2, 3].concat([4, 5, 6]); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.indexOf([1, 2, 3], 1); | ||
| // GOOD | ||
| [1, 2, 3].indexOf(1); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.includes([1, 2, 3], 1); | ||
| // GOOD | ||
| [1, 2, 3].includes(1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will add a attention about polyfills |
||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| const first = _.first([1, 2, 3]); | ||
| // GOOD | ||
| const [first] = [1, 2, 3]; | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.join([1, 2, 3], '-'); | ||
| // GOOD | ||
| [1, 2, 3].join('-'); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.reverse([1, 2, 3]); | ||
| // GOOD | ||
| [1, 2, 3].reverse(); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.uniq([1, 2, 3, 3, 2]); | ||
| // GOOD | ||
| [...new Set([1, 2, 3, 3, 2])]; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This one does not looks better vs lodash variant.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Native and one line, easy to understand or to google purpose if not. I suggest it's a nice choice, IE should die, thinking about it is an atavism in 2k19. It will be attention about polyfills |
||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isArray([1, 2, 3]); | ||
| // GOOD | ||
| Array.isArray([1, 2, 3]); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.size([1, 2, 3]); | ||
| // GOOD | ||
| [1, 2, 3].length; | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isEmpty([]); | ||
| // GOOD | ||
| [].length === 0; | ||
| ```` | ||
| ```javascript | ||
| // BAD | ||
| _.bind(func, this); | ||
| // GOOD | ||
| func.bind(this); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isNaN(value); | ||
| // GOOD | ||
| Number.isNaN(value); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isUndefined(value); | ||
| // GOOD | ||
| value === undefined; | ||
ledamint marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isEmpty(object); | ||
| // GOOD | ||
| Object.keys(object).length === 0; | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| const foo = _.assign({}, object1, object2); | ||
| // GOOD | ||
| const foo = { | ||
| ...object1, | ||
| ...object2, | ||
| }; | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| const others = _.omit({a: 1, b: 2, c: 3, d: 4}, ['a', 'c']); | ||
| // GOOD | ||
| const {a, c, ...others} = {a: 1, b: 2, c: 3, d: 4}; | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.isEmpty(object); | ||
| // GOOD | ||
| Object.keys(object).length === 0; | ||
| ```` | ||
| ```javascript | ||
| // BAD | ||
| _.keys(object); | ||
| // GOOD | ||
| Object.keys(object); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.values(object); | ||
| // GOOD | ||
| Object.values(object); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.entries(object); | ||
| // GOOD | ||
| Object.entries(object); | ||
| ``` | ||
| ```javascript | ||
| // BAD | ||
| _.trim(' 123 '); | ||
| // GOOD | ||
| ' 123 '.trim(); | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
a good section, i like it. my common feedback - please add few words regarding to polyfills. Otherwise it looks unsafely to swapping to native methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
one more - is adding a few words when lodash is preferred, say reduce an Object, or find via object, is a shorthand's we prefer.
_.reduce({foo: 'FOO', bar: 'BAR'}, callback, {});_.find( [{baz: 10}, {baz: 30}, {baz: 50}], {foo: 30});Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we will leave only bad cases to not divide all lodash functionality to good or bad? I described popular and often used methods. In cases above, we can use
Object.(keys|values|entries)and after use array methods.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess we have to provide a list of all bad technics we have to replace to native alternatives. Regarding to using two methods vs one: using two methods vs one is not a good choice. We have to make our code simple and do not introduce extra boilerplate code. Lodash is created for this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a rare case where you need reduce to object and I think it's nice to directly show where is a plain array or where is an object which you need to use as an array. You use one short method with particular cases and your code remains strict, native and without external modules except using external functionality which eats any type