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 _.reduce() #45460

Merged
merged 1 commit into from
Nov 7, 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 @@ -144,6 +144,7 @@ module.exports = {
'partial',
'partialRight',
'random',
'reduce',
'reject',
'repeat',
'reverse',
Expand Down
85 changes: 40 additions & 45 deletions packages/babel-plugin-makepot/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*/

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

Expand Down Expand Up @@ -324,50 +324,45 @@ module.exports = () => {
const files = Object.keys( strings ).sort();

// Combine translations from each file grouped by context.
const translations = reduce(
files,
( memo, file ) => {
for ( const context in strings[ file ] ) {
// Within the same file, sort translations by line.
const sortedTranslations = sortByReference(
Object.values( strings[ file ][ context ] )
);

sortedTranslations.forEach( ( translation ) => {
const { msgctxt = '', msgid } = translation;
if ( ! memo.hasOwnProperty( msgctxt ) ) {
memo[ msgctxt ] = {};
}

// Merge references if translation already exists.
if (
isSameTranslation(
translation,
memo[ msgctxt ][ msgid ]
)
) {
translation.comments.reference = [
...new Set(
[
memo[ msgctxt ][ msgid ]
.comments.reference,
translation.comments
.reference,
]
.join( '\n' )
.split( '\n' )
),
].join( '\n' );
}

memo[ msgctxt ][ msgid ] = translation;
} );
}

return memo;
},
{}
);
const translations = files.reduce( ( memo, file ) => {
for ( const context in strings[ file ] ) {
// Within the same file, sort translations by line.
const sortedTranslations = sortByReference(
Object.values( strings[ file ][ context ] )
);

sortedTranslations.forEach( ( translation ) => {
const { msgctxt = '', msgid } = translation;
if ( ! memo.hasOwnProperty( msgctxt ) ) {
memo[ msgctxt ] = {};
}

// Merge references if translation already exists.
if (
isSameTranslation(
translation,
memo[ msgctxt ][ msgid ]
)
) {
translation.comments.reference = [
...new Set(
[
memo[ msgctxt ][ msgid ]
.comments.reference,
translation.comments.reference,
]
.join( '\n' )
.split( '\n' )
),
].join( '\n' );
}

memo[ msgctxt ][ msgid ] = translation;
} );
}

return memo;
}, {} );

// Merge translations from individual files into headers
const data = merge( {}, baseData, { translations } );
Expand Down