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 _.sortBy() #43479

Merged
merged 2 commits into from
Aug 22, 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 @@ -134,6 +134,7 @@ module.exports = {
'reverse',
'size',
'snakeCase',
'sortBy',
'startCase',
'startsWith',
'stubFalse',
Expand Down
6 changes: 4 additions & 2 deletions bin/plugin/commands/changelog.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
const { groupBy, escapeRegExp, flow, sortBy } = require( 'lodash' );
const { groupBy, escapeRegExp, flow } = require( 'lodash' );
const Octokit = require( '@octokit/rest' );
const { sprintf } = require( 'sprintf-js' );
const semver = require( 'semver' );
Expand Down Expand Up @@ -823,7 +823,9 @@ function getContributorPropsMarkdownList( ftcPRs ) {
* @return {IssuesListForRepoResponseItem[]} The sorted list of pull requests.
*/
function sortByUsername( items ) {
return sortBy( items, ( item ) => item.user.login.toLowerCase() );
return [ ...items ].sort( ( a, b ) =>
a.user.login.toLowerCase().localeCompare( b.user.login.toLowerCase() )
);
}

/**
Expand Down
29 changes: 17 additions & 12 deletions packages/babel-plugin-makepot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,7 @@
*/

const { po } = require( 'gettext-parser' );
const {
pick,
reduce,
forEach,
sortBy,
isEqual,
merge,
isEmpty,
} = require( 'lodash' );
const { pick, reduce, forEach, isEqual, merge, isEmpty } = require( 'lodash' );
const { relative, sep } = require( 'path' );
const { writeFileSync } = require( 'fs' );

Expand Down Expand Up @@ -196,6 +188,20 @@ function isSameTranslation( a, b ) {
);
}

/**
* Sorts multiple translation objects by their reference.
* The reference is where they occur, in the format `file:line`.
*
* @param {Array} translations Array of translations to sort.
*
* @return {Array} Sorted translations.
*/
function sortByReference( translations = [] ) {
return [ ...translations ].sort( ( a, b ) =>
a.comments.reference.localeCompare( b.comments.reference )
);
}

module.exports = () => {
const strings = {};
let nplurals = 2,
Expand Down Expand Up @@ -323,9 +329,8 @@ module.exports = () => {
( memo, file ) => {
for ( const context in strings[ file ] ) {
// Within the same file, sort translations by line.
const sortedTranslations = sortBy(
strings[ file ][ context ],
'comments.reference'
const sortedTranslations = sortByReference(
strings[ file ][ context ]
);

forEach(
Expand Down
25 changes: 15 additions & 10 deletions packages/block-library/src/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import { Platform } from 'react-native';
import { sortBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -161,16 +160,22 @@ export const registerBlock = ( block ) => {
const registerBlockVariations = ( block ) => {
const { metadata, settings, name } = block;

sortBy( settings.variations, 'title' ).forEach( ( v ) => {
registerBlockType( `${ name }-${ v.name }`, {
...metadata,
name: `${ name }-${ v.name }`,
...settings,
icon: v.icon(),
title: v.title,
variations: [],
if ( ! settings.variations ) {
return;
}

[ ...settings.variations ]
.sort( ( a, b ) => a.title.localeCompare( b.title ) )
.forEach( ( v ) => {
registerBlockType( `${ name }-${ v.name }`, {
...metadata,
name: `${ name }-${ v.name }`,
...settings,
icon: v.icon(),
title: v.title,
variations: [],
} );
} );
} );
};

// Only enable code block for development
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { sortBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -40,7 +35,9 @@ function mapMenuItemsToBlocks( menuItems ) {
let mapping = {};

// The menuItem should be in menu_order sort order.
const sortedItems = sortBy( menuItems, 'menu_order' );
const sortedItems = [ ...menuItems ].sort(
( a, b ) => a.menu_order - b.menu_order
);

const innerBlocks = sortedItems.map( ( menuItem ) => {
if ( menuItem.type === 'block' ) {
Expand Down
8 changes: 6 additions & 2 deletions packages/block-library/src/page-list/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* External dependencies
*/
import classnames from 'classnames';
import { sortBy } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -133,7 +132,12 @@ function usePageData() {
// TODO: Once the REST API supports passing multiple values to
// 'orderby', this can be removed.
// https://core.trac.wordpress.org/ticket/39037
const sortedPages = sortBy( pages, [ 'menu_order', 'title.rendered' ] );
const sortedPages = [ ...( pages ?? [] ) ].sort( ( a, b ) => {
if ( a.menu_order === b.menu_order ) {
return a.title.rendered.localeCompare( b.title.rendered );
}
return a.menu_order - b.menu_order;
} );
const pagesByParentId = sortedPages.reduce( ( accumulator, page ) => {
const { parent } = page;
if ( accumulator.has( parent ) ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { sortBy } from 'lodash';

/**
* WordPress dependencies
*/
Expand Down Expand Up @@ -41,7 +36,9 @@ function mapMenuItemsToBlocks( menuItems ) {
let mapping = {};

// The menuItem should be in menu_order sort order.
const sortedItems = sortBy( menuItems, 'menu_order' );
const sortedItems = [ ...menuItems ].sort(
( a, b ) => a.menu_order - b.menu_order
);

const innerBlocks = sortedItems.map( ( menuItem ) => {
const attributes = menuItemToBlockAttributes( menuItem );
Expand Down
6 changes: 4 additions & 2 deletions packages/edit-navigation/src/store/transform.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get, omit, sortBy } from 'lodash';
import { get, omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -168,7 +168,9 @@ export function menuItemsToBlocks( menuItems ) {
*/
function mapMenuItemsToBlocks( menuItems ) {
// The menuItem should be in menu_order sort order.
const sortedItems = sortBy( menuItems, 'menu_order' );
const sortedItems = [ ...menuItems ].sort(
( a, b ) => a.menu_order - b.menu_order
);

const blocks = sortedItems.map( ( menuItem ) => {
if ( menuItem.type === 'block' ) {
Expand Down