-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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 _.orderBy()
#45146
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
18d14b0
Block Editor: Introduce simple sorting tool with tests
tyxla 6891fbe
Block Editor: Use custom orderBy() instead of Lodash one
tyxla 4c8e6ae
ESLint: Deprecate _.orderBy
tyxla 74ece1d
Cast block layouts to array before sorting
tyxla 8d31520
Stable sort
tyxla f63990c
Rename functions and arguments to make more sense
tyxla 2fa1f0a
Fix inline docs
tyxla File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 5 additions & 3 deletions
8
packages/block-editor/src/components/rich-text/format-toolbar/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Recursive stable sorting comparator function. | ||
* | ||
* @param {string|Function} field Field to sort by. | ||
* @param {Array} items Items to sort. | ||
* @param {string} order Order, 'asc' or 'desc'. | ||
* @return {Function} Comparison function to be used in a `.sort()`. | ||
*/ | ||
const comparator = ( field, items, order ) => { | ||
return ( a, b ) => { | ||
let cmpA, cmpB; | ||
|
||
if ( typeof field === 'function' ) { | ||
cmpA = field( a ); | ||
cmpB = field( b ); | ||
} else { | ||
cmpA = a[ field ]; | ||
cmpB = b[ field ]; | ||
} | ||
|
||
if ( cmpA > cmpB ) { | ||
return order === 'asc' ? 1 : -1; | ||
} else if ( cmpB > cmpA ) { | ||
return order === 'asc' ? -1 : 1; | ||
} | ||
|
||
const orderA = items.findIndex( ( item ) => item === a ); | ||
const orderB = items.findIndex( ( item ) => item === b ); | ||
|
||
// Stable sort: maintaining original array order | ||
if ( orderA > orderB ) { | ||
return 1; | ||
} else if ( orderB > orderA ) { | ||
return -1; | ||
} | ||
|
||
return 0; | ||
}; | ||
}; | ||
|
||
/** | ||
* Order items by a certain key. | ||
* Supports decorator functions that allow complex picking of a comparison field. | ||
* Sorts in ascending order by default, but supports descending as well. | ||
* Stable sort - maintains original order of equal items. | ||
* | ||
* @param {Array} items Items to order. | ||
* @param {string|Function} field Field to order by. | ||
* @param {string} order Sorting order, `asc` or `desc`. | ||
* @return {Array} Sorted items. | ||
*/ | ||
export function orderBy( items, field, order = 'asc' ) { | ||
return items.concat().sort( comparator( field, items, order ) ); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { orderBy } from '../sorting'; | ||
|
||
describe( 'orderBy', () => { | ||
it( 'should not mutate original input', () => { | ||
const input = []; | ||
expect( orderBy( input, 'x' ) ).not.toBe( input ); | ||
} ); | ||
|
||
it( 'should sort items by a field when it is specified as a string', () => { | ||
const input = [ { x: 2 }, { x: 1 }, { x: 3 } ]; | ||
const expected = [ { x: 1 }, { x: 2 }, { x: 3 } ]; | ||
expect( orderBy( input, 'x' ) ).toEqual( expected ); | ||
} ); | ||
|
||
it( 'should support functions for picking the field', () => { | ||
const input = [ { x: 2 }, { x: 1 }, { x: 3 } ]; | ||
const expected = [ { x: 1 }, { x: 2 }, { x: 3 } ]; | ||
expect( orderBy( input, ( item ) => item.x ) ).toEqual( expected ); | ||
} ); | ||
|
||
it( 'should support sorting in a descending order', () => { | ||
const input = [ { x: 2 }, { x: 1 }, { x: 3 } ]; | ||
const expected = [ { x: 3 }, { x: 2 }, { x: 1 } ]; | ||
expect( orderBy( input, 'x', 'desc' ) ).toEqual( expected ); | ||
} ); | ||
|
||
it( 'should maintain original order of equal items', () => { | ||
const a = { x: 1, a: 1 }; | ||
const b = { x: 1, b: 2 }; | ||
const c = { x: 0 }; | ||
const d = { x: 1, b: 4 }; | ||
const input = [ a, b, c, d ]; | ||
const expected = [ c, a, b, d ]; | ||
expect( orderBy( input, 'x' ) ).toEqual( expected ); | ||
} ); | ||
|
||
it( 'should maintain original order of equal items in descencing order', () => { | ||
const a = { x: 1, a: 1 }; | ||
const b = { x: 1, b: 2 }; | ||
const c = { x: 0 }; | ||
const d = { x: 1, b: 4 }; | ||
const input = [ a, b, c, d ]; | ||
const expected = [ a, b, d, c ]; | ||
expect( orderBy( input, 'x', 'desc' ) ).toEqual( expected ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is how Lodash
_.orderBy()
works - it maintains original array order, regardless of whether we're sorting in ascending or descending order:We're opting to keep that behavior and supporting it with a couple of unit tests.