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 6e7068be..33007d19 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 @@ -223,4 +224,56 @@ PostsRequest.prototype.statuses = function() { return this; }; +/** + * Adds the Year filter into the request to retrieve posts for a given year + * + * @method year + * @chainable + * @param {Number} year integer representation of year requested + * @returns {CollectionRequest} The PostsRequest instance (for chaining) + */ +PostsRequest.prototype.year = function( year ) { + return this.filter( 'year', 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 {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 ); + } + return this; +}; + +/** + * Add the day filter into the request to retrieve posts for a given day + * + * @method day + * @chainable + * @param {Number} day Integer representation of the day requested + * @returns {CollectionRequest} The PostsRequest instance (for chaining) + */ +PostsRequest.prototype.day = function( 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 ]