From e3767755a5fd246626273f45fdc101ba5392720e Mon Sep 17 00:00:00 2001 From: brad-decker Date: Mon, 16 Mar 2015 12:39:43 -0500 Subject: [PATCH 1/5] #61 - Added conveinence mthods to the posts request class. --- lib/posts.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/lib/posts.js b/lib/posts.js index aefa4a9e..931a6cc5 100644 --- a/lib/posts.js +++ b/lib/posts.js @@ -6,6 +6,7 @@ */ var CollectionRequest = require( './shared/collection-request' ); var inherit = require( 'util' ).inherits; +var _ = require( 'lodash' ); /** * PostsRequest extends CollectionRequest to handle the /posts API endpoint @@ -220,4 +221,48 @@ PostsRequest.prototype.statuses = function() { return this; }; +/** + * @method year + * @chainable + * @param year - Integer for year + * @returns {CollectionRequest} The PostsRequest instance (for chaining) + */ +PostsRequest.prototype.year = function ( year ) { + return this.filter( 'year', year ); +}; + +/** + * @method month + * @chainable + * @param month - Integer for month or month string ("january") + * @returns {CollectionRequest} The PostsRequest instance (for chaining) + */ +PostsRequest.prototype.month = function ( month ) { + if ( _.isString( month ) ) { + month = new Date( Date.parse(month +" 1, 2012") ); + + if ( isNaN(month) ) { + return this; + } + + month = month.getMonth() + 1; + } + + if ( _.isNumber( month ) ) { + return this.filter( 'monthnum', month ); + } + + return this; +}; + +/** + * @method day + * @chainable + * @param day - Integer for day + * @returns {CollectionRequest} The PostsRequest instance (for chaining) + */ +PostsRequest.prototype.day = function ( day ) { + return this.filter( 'day', day ); +}; + module.exports = PostsRequest; From 694c33e79ea132edc7e30156a971791d8cd65cda Mon Sep 17 00:00:00 2001 From: brad-decker Date: Tue, 17 Mar 2015 14:05:13 -0500 Subject: [PATCH 2/5] #61 - Updated comments to match existing documentation style. --- lib/posts.js | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/posts.js b/lib/posts.js index 931a6cc5..64bda380 100644 --- a/lib/posts.js +++ b/lib/posts.js @@ -222,9 +222,11 @@ PostsRequest.prototype.statuses = function() { }; /** + * Adds the Year filter into the request to retrieve posts for a given year + * * @method year * @chainable - * @param year - Integer for year + * @param {Number} year integer representation of year requested * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.year = function ( year ) { @@ -232,22 +234,30 @@ PostsRequest.prototype.year = function ( year ) { }; /** + * Add the monthnum filter into the request to retrieve posts for a given month + * + * Filters posts by a given month in either Integer or String Format. + * * @method month * @chainable - * @param month - Integer for month or month string ("january") + * @param {Number|String} month Integer for month or month string ("january") * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.month = function ( month ) { if ( _.isString( month ) ) { + // Append a arbitrary day and year to the month to parse the string into a Date month = new Date( Date.parse(month +" 1, 2012") ); + // If month is NaN, then the passed string is not a valid month. if ( isNaN(month) ) { return this; } + // Js Dates are 0 indexed, WP Api requires a 1 indexed integer. month = month.getMonth() + 1; } + // If month is a Number, add the monthnum filter to the request if ( _.isNumber( month ) ) { return this.filter( 'monthnum', month ); } @@ -256,9 +266,11 @@ PostsRequest.prototype.month = function ( month ) { }; /** + * Add the day filter into the request to retrieve posts for a given day + * * @method day * @chainable - * @param day - Integer for day + * @param {Number} day Integer representation of the day requested * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.day = function ( day ) { From 4ec3ec0e7da5a9d0e7f7d86e4c02d9349282a14a Mon Sep 17 00:00:00 2001 From: brad-decker Date: Tue, 17 Mar 2015 14:29:22 -0500 Subject: [PATCH 3/5] Removed Double quotes in code. --- lib/posts.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/posts.js b/lib/posts.js index 64bda380..69f92031 100644 --- a/lib/posts.js +++ b/lib/posts.js @@ -246,7 +246,7 @@ PostsRequest.prototype.year = function ( year ) { PostsRequest.prototype.month = function ( month ) { if ( _.isString( month ) ) { // Append a arbitrary day and year to the month to parse the string into a Date - month = new Date( Date.parse(month +" 1, 2012") ); + month = new Date( Date.parse(month + ' 1, 2012') ); // If month is NaN, then the passed string is not a valid month. if ( isNaN(month) ) { From 32cb69763c9a92c26dbdeb808aaef1128efde166 Mon Sep 17 00:00:00 2001 From: brad-decker Date: Tue, 17 Mar 2015 15:17:58 -0500 Subject: [PATCH 4/5] Fixing styles to match project styles. --- lib/posts.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/posts.js b/lib/posts.js index 69f92031..7edd5e0a 100644 --- a/lib/posts.js +++ b/lib/posts.js @@ -229,7 +229,7 @@ PostsRequest.prototype.statuses = function() { * @param {Number} year integer representation of year requested * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ -PostsRequest.prototype.year = function ( year ) { +PostsRequest.prototype.year = function( year ) { return this.filter( 'year', year ); }; @@ -243,13 +243,13 @@ PostsRequest.prototype.year = function ( year ) { * @param {Number|String} month Integer for month or month string ("january") * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ -PostsRequest.prototype.month = function ( month ) { +PostsRequest.prototype.month = function( month ) { if ( _.isString( month ) ) { // Append a arbitrary day and year to the month to parse the string into a Date - month = new Date( Date.parse(month + ' 1, 2012') ); + month = new Date( Date.parse( month + ' 1, 2012' ) ); // If month is NaN, then the passed string is not a valid month. - if ( isNaN(month) ) { + if ( isNaN( month ) ) { return this; } @@ -273,7 +273,7 @@ PostsRequest.prototype.month = function ( month ) { * @param {Number} day Integer representation of the day requested * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ -PostsRequest.prototype.day = function ( day ) { +PostsRequest.prototype.day = function( day ) { return this.filter( 'day', day ); }; From 2bd538e208ac547a978f320382ec98cb9bac606a Mon Sep 17 00:00:00 2001 From: brad-decker Date: Mon, 28 Dec 2015 16:05:49 -0600 Subject: [PATCH 5/5] fixed linting issue, upgraded qs package to latest version and added the ' brackets' option for handling arrays. Which will automatically support array query expressions like post__in --- .gitignore | 3 ++ lib/posts.js | 38 ++++++++++++-------------- lib/shared/collection-request.js | 2 +- package.json | 3 +- tests/lib/posts.js | 2 +- tests/lib/shared/collection-request.js | 7 +++++ 6 files changed, 31 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index ce7e0308..e28dd3ee 100644 --- a/.gitignore +++ b/.gitignore @@ -30,3 +30,6 @@ test.js # The rendered API docs files will live in the gh-pages branch docs/ index.html + +# Webstorm config folder +.idea diff --git a/lib/posts.js b/lib/posts.js index f9793fdc..33007d19 100644 --- a/lib/posts.js +++ b/lib/posts.js @@ -233,7 +233,7 @@ PostsRequest.prototype.statuses = function() { * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.year = function( year ) { - return this.filter( 'year', year ); + return this.filter( 'year', year ); }; /** @@ -247,25 +247,21 @@ PostsRequest.prototype.year = function( year ) { * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.month = function( month ) { - if ( _.isString( month ) ) { - // Append a arbitrary day and year to the month to parse the string into a Date - month = new Date( Date.parse( month + ' 1, 2012' ) ); - - // If month is NaN, then the passed string is not a valid month. - if ( isNaN( month ) ) { - return this; - } - - // Js Dates are 0 indexed, WP Api requires a 1 indexed integer. - month = month.getMonth() + 1; - } - - // If month is a Number, add the monthnum filter to the request - if ( _.isNumber( month ) ) { - return this.filter( 'monthnum', month ); - } - - return this; + if ( _.isString( month ) ) { + // Append a arbitrary day and year to the month to parse the string into a Date + month = new Date( Date.parse( month + ' 1, 2012' ) ); + // If month is NaN, then the passed string is not a valid month. + if ( isNaN( month ) ) { + return this; + } + // Js Dates are 0 indexed, WP Api requires a 1 indexed integer. + month = month.getMonth() + 1; + } + // If month is a Number, add the monthnum filter to the request + if ( _.isNumber( month ) ) { + return this.filter( 'monthnum', month ); + } + return this; }; /** @@ -277,7 +273,7 @@ PostsRequest.prototype.month = function( month ) { * @returns {CollectionRequest} The PostsRequest instance (for chaining) */ PostsRequest.prototype.day = function( day ) { - return this.filter( 'day', day ); + return this.filter( 'day', day ); }; module.exports = PostsRequest; diff --git a/lib/shared/collection-request.js b/lib/shared/collection-request.js index 15ba266b..57e06a07 100644 --- a/lib/shared/collection-request.js +++ b/lib/shared/collection-request.js @@ -179,7 +179,7 @@ CollectionRequest.prototype._renderQuery = function() { // Parse query parameters object into a query string, sorting the object // properties by alphabetical order (consistent property ordering can make // for easier caching of request URIs) - var queryString = qs.stringify( queryParams ) + var queryString = qs.stringify( queryParams, { arrayFormat: 'brackets' } ) .split( '&' ) .sort() .join( '&' ); diff --git a/package.json b/package.json index 195482ec..0230ba62 100644 --- a/package.json +++ b/package.json @@ -39,7 +39,7 @@ "li": "^1.0.0", "lodash": "^2.4.1", "node.extend": "^1.0.10", - "qs": "^2.3.3", + "qs": "^5.0.0", "route-parser": "0.0.2", "superagent": "^0.18.0" }, @@ -55,6 +55,7 @@ "grunt-newer": "^0.7.0", "grunt-simple-mocha": "^0.4.0", "istanbul": "^0.3.22", + "jscs": "^2.7.0", "jscs-stylish": "^0.3.0", "jshint-stylish": "^0.2.0", "load-grunt-tasks": "^0.6.0", diff --git a/tests/lib/posts.js b/tests/lib/posts.js index 573eff0a..e603c5af 100644 --- a/tests/lib/posts.js +++ b/tests/lib/posts.js @@ -184,7 +184,7 @@ describe( 'wp.posts', function() { 'page' ]); - var uri = '/wp-json/wp/v2/posts?type%5B0%5D=cpt1&type%5B1%5D=cpt2&type%5B2%5D=page'; + var uri = '/wp-json/wp/v2/posts?type%5B%5D=cpt1&type%5B%5D=cpt2&type%5B%5D=page'; expect( posts._renderURI() ).to.equal( uri ); }); diff --git a/tests/lib/shared/collection-request.js b/tests/lib/shared/collection-request.js index 93c31977..6a520cd9 100644 --- a/tests/lib/shared/collection-request.js +++ b/tests/lib/shared/collection-request.js @@ -588,6 +588,13 @@ describe( 'CollectionRequest', function() { .equal( '?filter%5Bpost_status%5D=publish&filter%5Bs%5D=Some%20search%20string' ); }); + it( 'propery parse array filters', function() { + request._filters = { post__in: [ 0, 1 ] }; + var query = request._renderQuery(); + expect( query ).to + .equal( '?filter%5Bpost__in%5D%5B%5D=0&filter%5Bpost__in%5D%5B%5D=1' ); + }); + it( 'correctly merges taxonomy and regular filters & renders them in order', function() { request._taxonomyFilters = { cat: [ 7, 10 ]