Skip to content

Commit

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

* Handle nullish values in `isURLLike()`

Co-authored-by: Nik Tsekouras <[email protected]>

* Handle nullish values in LinkControl search handler

Co-authored-by: Nik Tsekouras <[email protected]>

Co-authored-by: Nik Tsekouras <[email protected]>
  • Loading branch information
tyxla and ntsekouras authored Aug 9, 2022
1 parent 88f80c6 commit 82b524b
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 20 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ module.exports = {
'reverse',
'size',
'snakeCase',
'startsWith',
'stubFalse',
'stubTrue',
'sum',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { startsWith } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -20,6 +15,6 @@ import { isURL } from '@wordpress/url';
* @return {boolean} whether or not the value is potentially a URL.
*/
export default function isURLLike( val ) {
const isInternal = startsWith( val, '#' );
const isInternal = val?.startsWith( '#' );
return isURL( val ) || ( val && val.includes( 'www.' ) ) || isInternal;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ import { getProtocol, prependHTTP } from '@wordpress/url';
import { useCallback } from '@wordpress/element';
import { useSelect } from '@wordpress/data';

/**
* External dependencies
*/
import { startsWith } from 'lodash';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -38,7 +33,7 @@ export const handleDirectEntry = ( val ) => {
type = TEL_TYPE;
}

if ( startsWith( val, '#' ) ) {
if ( val?.startsWith( '#' ) ) {
type = INTERNAL_TYPE;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/blocks/src/api/serializer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { isEmpty, reduce, castArray, startsWith } from 'lodash';
import { isEmpty, reduce, castArray } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -320,7 +320,7 @@ export function getCommentDelimitedContent(
: '';

// Strip core blocks of their namespace prefix.
const blockName = startsWith( rawBlockName, 'core/' )
const blockName = rawBlockName?.startsWith( 'core/' )
? rawBlockName.slice( 5 )
: rawBlockName;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, startsWith, get, camelCase, has } from 'lodash';
import { find, get, camelCase, has } from 'lodash';
import { Dimensions } from 'react-native';

/**
Expand Down Expand Up @@ -104,7 +104,7 @@ export function getBlockColors(

// Custom colors.
Object.entries( blockStyleAttributes ).forEach( ( [ key, value ] ) => {
const isCustomColor = startsWith( value, '#' );
const isCustomColor = value?.startsWith?.( '#' );
let styleKey = key;

if ( BLOCK_STYLE_ATTRIBUTES_MAPPING[ styleKey ] ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
pickBy,
reduce,
set,
startsWith,
} from 'lodash';

/**
Expand Down Expand Up @@ -50,7 +49,7 @@ function compileStyleValue( uncompiledValue ) {
const VARIABLE_REFERENCE_PREFIX = 'var:';
const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
if ( startsWith( uncompiledValue, VARIABLE_REFERENCE_PREFIX ) ) {
if ( uncompiledValue?.startsWith( VARIABLE_REFERENCE_PREFIX ) ) {
const variable = uncompiledValue
.slice( VARIABLE_REFERENCE_PREFIX.length )
.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
Expand Down
4 changes: 2 additions & 2 deletions test/integration/full-content/full-content.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import glob from 'fast-glob';
import { startsWith, get } from 'lodash';
import { get } from 'lodash';
import { format } from 'util';

/**
Expand Down Expand Up @@ -237,7 +237,7 @@ describe( 'full post content fixture', () => {
.filter(
( basename ) =>
basename === nameToFilename ||
startsWith( basename, nameToFilename + '__' )
basename.startsWith( nameToFilename + '__' )
)
.map( ( basename ) => {
const { filename: htmlFixtureFileName } =
Expand Down

0 comments on commit 82b524b

Please sign in to comment.