-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
Remove underscore usage #40346
Remove underscore usage #40346
Changes from all commits
3a905c7
81f4716
2fe221d
84ab150
7c9704b
ac5a4dd
0cc0b12
861ea79
cca7dd7
c95933b
5995e6c
612b324
5901f28
583ccaa
880ef45
99760ff
b7bb995
26e3367
15c00f0
3c4bcf8
2b51b64
2be0ef2
60b1ade
c6ee94f
fbb7e8b
8ce0836
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 |
---|---|---|
|
@@ -72,10 +72,10 @@ Using arrow functions is the preferred way to write an anonymous function such a | |
|
||
```javascript | ||
// Bad | ||
_.map(someArray, function (item) {...}); | ||
someArray.map(function (item) {...}); | ||
|
||
// Good | ||
_.map(someArray, (item) => {...}); | ||
someArray.map((item) => {...}); | ||
``` | ||
|
||
Empty functions (noop) should be declare as arrow functions with no whitespace inside. Avoid _.noop() | ||
|
@@ -112,38 +112,9 @@ if (someCondition) { | |
} | ||
``` | ||
|
||
## Object / Array Methods | ||
|
||
We have standardized on using [underscore.js](https://underscorejs.org/) methods for objects and collections instead of the native [Array instance methods](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#instance_methods). This is mostly to maintain consistency, but there are some type safety features and conveniences that underscore methods provide us e.g. the ability to iterate over an object and the lack of a `TypeError` thrown if a variable is `undefined`. | ||
|
||
```javascript | ||
// Bad | ||
myArray.forEach(item => doSomething(item)); | ||
// Good | ||
_.each(myArray, item => doSomething(item)); | ||
|
||
// Bad | ||
const myArray = Object.keys(someObject).map(key => doSomething(someObject[key])); | ||
// Good | ||
const myArray = _.map(someObject, (value, key) => doSomething(value)); | ||
|
||
// Bad | ||
myCollection.includes('item'); | ||
// Good | ||
_.contains(myCollection, 'item'); | ||
|
||
// Bad | ||
const modifiedArray = someArray.filter(filterFunc).map(mapFunc); | ||
// Good | ||
const modifiedArray = _.chain(someArray) | ||
.filter(filterFunc) | ||
.map(mapFunc) | ||
.value(); | ||
``` | ||
|
||
## Accessing Object Properties and Default Values | ||
|
||
Use `lodashGet()` to safely access object properties and `||` to short circuit null or undefined values that are not guaranteed to exist in a consistent way throughout the codebase. In the rare case that you want to consider a falsy value as usable and the `||` operator prevents this then be explicit about this in your code and check for the type using an underscore method e.g. `_.isBoolean(value)` or `_.isEqual(0)`. | ||
Use `lodashGet()` to safely access object properties and `||` to short circuit null or undefined values that are not guaranteed to exist in a consistent way throughout the codebase. In the rare case that you want to consider a falsy value as usable and the `||` operator prevents this then be explicit about this in your code and check for the type. | ||
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'm not sure if this is still correct since we added 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 is a guideline for JS Style, I think we should remove it but there are few outdated guideline in this file, so not sure. (not related to underscore too) 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. hmm ok, let be it. 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. We are going to adjust this markdown in a separate PR, so we can leave it as it is now 👍 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. ok |
||
|
||
```javascript | ||
// Bad | ||
|
@@ -448,7 +419,7 @@ const propTypes = { | |
|
||
### Important Note: | ||
|
||
In React Native, one **must not** attempt to falsey-check a string for an inline ternary. Even if it's in curly braces, React Native will try to render it as a `<Text>` node and most likely throw an error about trying to render text outside of a `<Text>` component. Use `_.isEmpty()` instead. | ||
In React Native, one **must not** attempt to falsey-check a string for an inline ternary. Even if it's in curly braces, React Native will try to render it as a `<Text>` node and most likely throw an error about trying to render text outside of a `<Text>` component. Use `!!` instead. | ||
|
||
```javascript | ||
// Bad! This will cause a breaking an error on native platforms | ||
|
@@ -467,7 +438,7 @@ In React Native, one **must not** attempt to falsey-check a string for an inline | |
{ | ||
return ( | ||
<View> | ||
{!_.isEmpty(props.title) | ||
{!!props.title | ||
? <View style={styles.title}>{props.title}</View> | ||
: null} | ||
<View style={styles.body}>This is the body</View> | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
This file was deleted.
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 think we should update this to
standardized on using lodash or native method
instead of deleting itThere 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.
We have
you-dont-need-lodash-underscore
, so pretty sure we favor to use native object/array method. For example, instead oflodashKeys
, we preferObject.keys
.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.
Should we still add this information in here? Seems like there is a preference and posibility of using lodash so it makes sense to leave a guide here in terms of what is preferred