Skip to content

Commit

Permalink
Data: Support deep value serialization of query key
Browse files Browse the repository at this point in the history
  • Loading branch information
aduth committed Aug 4, 2018
1 parent 37e0d7a commit 0368719
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
18 changes: 13 additions & 5 deletions packages/core-data/src/queried-data/get-query-parts.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';

/**
* Internal dependencies
*/
Expand Down Expand Up @@ -47,13 +52,16 @@ export function getQueryParts( query ) {
break;

default:
// While it's not required to be one, for simplicity's sake
// mimic querystring encoding for stable key.
// While it could be any deterministic string, for simplicity's
// sake mimic querystring encoding for stable key.
//
// TODO: For consistency with PHP implementation, addQueryArgs
// should accept a key value pair, which may optimize its
// implementation for our use here, vs. iterating an object
// with only a single key.
parts.stableKey += (
( parts.stableKey ? '&' : '' ) +
encodeURIComponent( key ) +
'=' +
encodeURIComponent( value )
addQueryArgs( '', { [ key ]: value } ).slice( 1 )
);
}
}
Expand Down
10 changes: 10 additions & 0 deletions packages/core-data/src/queried-data/test/get-query-parts.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ describe( 'getQueryParts', () => {
} );
} );

it( 'encodes deep values', () => {
const parts = getQueryParts( { a: [ 1, 2 ] } );

expect( parts ).toEqual( {
page: 1,
perPage: 10,
stableKey: 'a%5B0%5D=1&a%5B1%5D=2',
} );
} );

it( 'encodes stable string key with page data normalized to number', () => {
const first = getQueryParts( { b: 2, page: 1, perPage: 10 } );
const second = getQueryParts( { b: 2, page: '1', perPage: '10' } );
Expand Down

0 comments on commit 0368719

Please sign in to comment.