Skip to content

Commit

Permalink
Lodash: Refactor away from _.isNil() (#41785)
Browse files Browse the repository at this point in the history
* Lodash: Refactor away from _.isNil()

* Remove typeof check
  • Loading branch information
tyxla authored Jun 20, 2022
1 parent a265d13 commit 2d3cfcc
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
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

0 comments on commit 2d3cfcc

Please sign in to comment.