Skip to content

Commit

Permalink
Lodash: Refactor away from _.reduce()
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla committed Nov 1, 2022
1 parent 3e7a5e9 commit 1e05b51
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 45 deletions.
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

0 comments on commit 1e05b51

Please sign in to comment.