Skip to content

Commit

Permalink
Utils: adding a addQueryArgs URL utils and using the built-in URL m…
Browse files Browse the repository at this point in the history
…odule for that
  • Loading branch information
youknowriad committed May 12, 2017
1 parent 298fe0d commit 0006673
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 22 deletions.
2 changes: 1 addition & 1 deletion components/button/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ function Button( { href, isPrimary, isLarge, isToggled, className, buttonRef, ..
...tagProps,
...additionalProps,
className: classes,
ref: buttonRef
ref: buttonRef,
} );
}

Expand Down
13 changes: 6 additions & 7 deletions editor/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
* External dependencies
*/
import { first, last } from 'lodash';
import qs from 'qs';

/**
* Internal dependencies
*/
import { addQueryArgs } from './utils/url';

export function getEditorMode( state ) {
return state.mode;
Expand Down Expand Up @@ -44,12 +48,7 @@ export function getEditedPostPreviewLink( state ) {
return null;
}

const queryStringPosition = link.indexOf( '?' );
const baseUrl = queryStringPosition !== -1 ? link.substring( 0, queryStringPosition ) : link;
const args = queryStringPosition !== -1 ? qs.parse( link.substring( queryStringPosition + 1 ) ) : {};
args.preview = 'true';

return `${ baseUrl }?${ qs.stringify( args ) }`;
return addQueryArgs( link, { preview: 'true' } );
}

export function getBlock( state, uid ) {
Expand Down
16 changes: 3 additions & 13 deletions editor/test/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,25 +199,15 @@ describe( 'selectors', () => {
expect( getEditedPostPreviewLink( state ) ).to.be.null();
} );

it( 'should return the correct url adding a query string', () => {
it( 'should return the correct url adding a preview parameter to the query string', () => {
const state = {
currentPost: {
link: 'https://andalouses.com/beach'
}
link: 'https://andalouses.com/beach',
},
};

expect( getEditedPostPreviewLink( state ) ).to.equal( 'https://andalouses.com/beach?preview=true' );
} );

it( 'should return the correct url concatening the query string', () => {
const state = {
currentPost: {
link: 'https://andalouses.com/?p=1'
}
};

expect( getEditedPostPreviewLink( state ) ).to.equal( 'https://andalouses.com/?p=1&preview=true' );
} );
} );

describe( 'getBlock', () => {
Expand Down
25 changes: 25 additions & 0 deletions editor/utils/test/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* External dependencies
*/
import { expect } from 'chai';

/**
* Internal dependencies
*/
import { addQueryArgs } from '../url';

describe( 'addQueryArgs', () => {
it( 'should append args to an URL without query string', () => {
const url = 'https://andalouses.com/beach';
const args = { sun: 'true', sand: 'false' };

expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?sun=true&sand=false' );
} );

it( 'should append args to an URL with query string', () => {
const url = 'https://andalouses.com/beach?night=false';
const args = { sun: 'true', sand: 'false' };

expect( addQueryArgs( url, args ) ).to.eql( 'https://andalouses.com/beach?night=false&sun=true&sand=false' );
} );
} );
20 changes: 20 additions & 0 deletions editor/utils/url.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* External dependencies
*/
import { parse, format } from 'url';

/**
* Appends arguments to the query string of the url
*
* @param {String} url URL
* @param {Object} args Query Args
*
* @return {String} Updated URL
*/
export function addQueryArgs( url, args ) {
const parsedUrl = parse( url, true );
const query = { ...parsedUrl.query, ...args };
delete parsedUrl.search;

return format( { ...parsedUrl, query } );
}
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"jed": "^1.1.1",
"js-beautify": "^1.6.12",
"lodash": "^4.17.4",
"qs": "^6.4.0",
"react": "^15.5.4",
"react-autosize-textarea": "^0.4.2",
"react-click-outside": "^2.3.0",
Expand Down

0 comments on commit 0006673

Please sign in to comment.