Skip to content

Commit

Permalink
Lodash: Refactor away from _.chunk() (#41777)
Browse files Browse the repository at this point in the history
  • Loading branch information
tyxla authored Jun 20, 2022
1 parent 2d3cfcc commit 7f07d8b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
{
name: 'lodash',
importNames: [
'chunk',
'clamp',
'concat',
'defaultTo',
Expand Down
15 changes: 10 additions & 5 deletions packages/core-data/src/batch/default-processor.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
/**
* External dependencies
*/
import { chunk } from 'lodash';

/**
* WordPress dependencies
*/
Expand All @@ -16,6 +11,16 @@ import apiFetch from '@wordpress/api-fetch';
*/
let maxItems = null;

function chunk( arr, chunkSize ) {
const tmp = [ ...arr ];
const cache = [];
while ( tmp.length ) {
cache.push( tmp.splice( 0, chunkSize ) );
}

return cache;
}

/**
* Default batch processor. Sends its input requests to /batch/v1.
*
Expand Down
13 changes: 11 additions & 2 deletions packages/e2e-test-utils-playwright/src/request-utils/rest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,23 @@
import * as fs from 'fs/promises';
import { dirname } from 'path';
import type { APIRequestContext } from '@playwright/test';
import { chunk } from 'lodash';

/**
* Internal dependencies
*/
import { WP_BASE_URL } from '../config';
import type { RequestUtils, StorageState } from './index';

function splitRequestsToChunks( requests: BatchRequest[], chunkSize: number ) {
const arr = [ ...requests ];
const cache = [];
while ( arr.length ) {
cache.push( arr.splice( 0, chunkSize ) );
}

return cache;
}

async function getAPIRootURL( request: APIRequestContext ) {
// Discover the API root url using link header.
// See https://developer.wordpress.org/rest-api/using-the-rest-api/discovery/#link-header
Expand Down Expand Up @@ -157,7 +166,7 @@ async function batchRest< BatchResponse >(
const maxBatchSize = await this.getMaxBatchSize();

if ( requests.length > maxBatchSize ) {
const chunks = chunk( requests, maxBatchSize );
const chunks = splitRequestsToChunks( requests, maxBatchSize );

const chunkResponses = await Promise.all(
chunks.map( ( chunkRequests ) =>
Expand Down

0 comments on commit 7f07d8b

Please sign in to comment.