Skip to content
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

Lodash: Refactor away from _.isNil() #41785

Merged
merged 2 commits into from
Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ module.exports = {
'isArray',
'isFinite',
'isFunction',
'isNil',
'isNumber',
'isObjectLike',
'isUndefined',
Expand Down
6 changes: 4 additions & 2 deletions packages/blocks/src/api/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import {
camelCase,
isEmpty,
isNil,
isObject,
isString,
mapKeys,
Expand Down Expand Up @@ -177,7 +176,10 @@ export function unstable__bootstrapServerSideBlockDefinitions( definitions ) {
continue;
}
serverSideBlockDefinitions[ blockName ] = mapKeys(
pickBy( definitions[ blockName ], ( value ) => ! isNil( value ) ),
pickBy(
definitions[ blockName ],
( value ) => value !== null && value !== undefined
),
( value, key ) => camelCase( key )
);
}
Expand Down
16 changes: 8 additions & 8 deletions packages/components/src/elevation/hook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { css } from '@emotion/react';
import { isNil } from 'lodash';

/**
* WordPress dependencies
Expand All @@ -16,6 +15,7 @@ import { useContextSystem } from '../ui/context';
import * as styles from './styles';
import { CONFIG, reduceMotion } from '../utils';
import { useCx } from '../utils/hooks/use-cx';
import { isValueDefined } from '../utils/values';

/**
* @param {number} value
Expand Down Expand Up @@ -49,13 +49,13 @@ export function useElevation( props ) {

const classes = useMemo( () => {
/** @type {number | undefined} */
let hoverValue = ! isNil( hover ) ? hover : value * 2;
let hoverValue = isValueDefined( hover ) ? hover : value * 2;
/** @type {number | undefined} */
let activeValue = ! isNil( active ) ? active : value / 2;
let activeValue = isValueDefined( active ) ? active : value / 2;

if ( ! isInteractive ) {
hoverValue = ! isNil( hover ) ? hover : undefined;
activeValue = ! isNil( active ) ? active : undefined;
hoverValue = isValueDefined( hover ) ? hover : undefined;
activeValue = isValueDefined( active ) ? active : undefined;
}

const transition = `box-shadow ${ CONFIG.transitionDuration } ${ CONFIG.transitionTimingFunction }`;
Expand All @@ -76,23 +76,23 @@ export function useElevation( props ) {
reduceMotion( 'transition' )
);

if ( ! isNil( hoverValue ) ) {
if ( isValueDefined( hoverValue ) ) {
sx.hover = css`
*:hover > & {
box-shadow: ${ getBoxShadow( hoverValue ) };
}
`;
}

if ( ! isNil( activeValue ) ) {
if ( isValueDefined( activeValue ) ) {
sx.active = css`
*:active > & {
box-shadow: ${ getBoxShadow( activeValue ) };
}
`;
}

if ( ! isNil( focus ) ) {
if ( isValueDefined( focus ) ) {
sx.focus = css`
*:focus > & {
box-shadow: ${ getBoxShadow( focus ) };
Expand Down
6 changes: 3 additions & 3 deletions packages/components/src/h-stack/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
* Internal dependencies
*/
import { isNil } from 'lodash';
import { isValueDefined } from '../utils/values';

/** @type {import('./types').Alignments} */
const ALIGNMENTS = {
Expand Down Expand Up @@ -41,7 +41,7 @@ const V_ALIGNMENTS = {
*/
/* eslint-enable jsdoc/valid-types */
export function getAlignmentProps( alignment, direction = 'row' ) {
if ( isNil( alignment ) ) {
if ( ! isValueDefined( alignment ) ) {
return {};
}
const isVertical = direction === 'column';
Expand Down
8 changes: 5 additions & 3 deletions packages/components/src/truncate/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
* Internal dependencies
*/
import { isNil } from 'lodash';
import { isValueDefined } from '../utils/values';

export const TRUNCATE_ELLIPSIS = '…';
export const TRUNCATE_TYPE = {
Expand Down Expand Up @@ -38,7 +38,9 @@ export function truncateMiddle( word, headLength, tailLength, ellipsis ) {
// eslint-disable-next-line no-bitwise
const backLength = ~~tailLength;
/* istanbul ignore next */
const truncateStr = ! isNil( ellipsis ) ? ellipsis : TRUNCATE_ELLIPSIS;
const truncateStr = isValueDefined( ellipsis )
? ellipsis
: TRUNCATE_ELLIPSIS;

if (
( frontLength === 0 && backLength === 0 ) ||
Expand Down