Skip to content

Commit daa0d85

Browse files
committed
Convert appropriate functions to arrow function syntax
1 parent 66bc136 commit daa0d85

22 files changed

+110
-113
lines changed

lib/constructors/wp-request.js

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ function WPRequest( options ) {
3737
'username',
3838
'password',
3939
'nonce',
40-
].reduce( function( localOptions, key ) {
40+
].reduce( ( localOptions, key ) => {
4141
if ( options && options[ key ] ) {
4242
localOptions[ key ] = options[ key ];
4343
}
@@ -94,9 +94,7 @@ function WPRequest( options ) {
9494
* Identity function for use within invokeAndPromisify()
9595
* @private
9696
*/
97-
function identity( value ) {
98-
return value;
99-
}
97+
const identity = value => value;
10098

10199
/**
102100
* Process arrays of taxonomy terms into query parameters.
@@ -127,10 +125,10 @@ function prepareTaxonomies( taxonomyFilters ) {
127125

128126
return objectReduce( taxonomyFilters, function( result, terms, key ) {
129127
// Trim whitespace and concatenate multiple terms with +
130-
result[ key ] = terms.map( function( term ) {
128+
result[ key ] = terms
131129
// Coerce term into a string so that trim() won't fail
132-
return ( term + '' ).trim().toLowerCase();
133-
} ).join( '+' );
130+
.map( term => ( term + '' ).trim().toLowerCase() )
131+
.join( '+' );
134132

135133
return result;
136134
}, {} );
@@ -156,12 +154,16 @@ function populated( obj ) {
156154
if ( ! obj ) {
157155
return obj;
158156
}
159-
return objectReduce( obj, function( values, val, key ) {
160-
if ( val !== undefined && val !== null && val !== '' ) {
161-
values[ key ] = val;
162-
}
163-
return values;
164-
}, {} );
157+
return objectReduce(
158+
obj,
159+
( values, val, key ) => {
160+
if ( val !== undefined && val !== null && val !== '' ) {
161+
values[ key ] = val;
162+
}
163+
return values;
164+
},
165+
{}
166+
);
165167
}
166168

167169
/**
@@ -180,7 +182,7 @@ function validatePathLevel( levelDefinitions, levelContents ) {
180182
// One "level" may have multiple options, as a route tree is a branching
181183
// structure. We consider a level "valid" if the provided levelContents
182184
// match any of the available validators.
183-
const valid = levelDefinitions.reduce( function( anyOptionValid, levelOption ) {
185+
const valid = levelDefinitions.reduce( ( anyOptionValid, levelOption ) => {
184186
if ( ! levelOption.validate ) {
185187
// If there is no validator function, the level is implicitly valid
186188
return true;
@@ -194,9 +196,10 @@ function validatePathLevel( levelDefinitions, levelContents ) {
194196
levelContents,
195197
// awkward pluralization support:
196198
'does not match' + ( levelDefinitions.length > 1 ? ' any of' : '' ),
197-
levelDefinitions.reduce( function( components, levelOption ) {
198-
return components.concat( levelOption.component );
199-
}, [] ).join( ', ' ),
199+
levelDefinitions.reduce(
200+
( components, levelOption ) => components.concat( levelOption.component ),
201+
[]
202+
).join( ', ' ),
200203
].join( ' ' ) );
201204
}
202205
}
@@ -254,14 +257,12 @@ WPRequest.prototype._renderPath = function() {
254257

255258
const pathParts = this._path;
256259
const orderedPathParts = Object.keys( pathParts )
257-
.sort( function( a, b ) {
260+
.sort( ( a, b ) => {
258261
const intA = parseInt( a, 10 );
259262
const intB = parseInt( b, 10 );
260263
return intA - intB;
261264
} )
262-
.map( function( pathPartKey ) {
263-
return pathParts[ pathPartKey ];
264-
} );
265+
.map( pathPartKey => pathParts[ pathPartKey ] );
265266

266267
// Combine all parts of the path together, filtered to omit any components
267268
// that are unspecified or empty strings, to create the full path template
@@ -330,12 +331,8 @@ WPRequest.prototype.setPathPart = function( level, val ) {
330331
WPRequest.prototype.validatePath = function() {
331332
// Iterate through all _specified_ levels of this endpoint
332333
const specifiedLevels = Object.keys( this._path )
333-
.map( function( level ) {
334-
return parseInt( level, 10 );
335-
} )
336-
.filter( function( pathPartKey ) {
337-
return ! isNaN( pathPartKey );
338-
} );
334+
.map( level => parseInt( level, 10 ) )
335+
.filter( pathPartKey => ! isNaN( pathPartKey ) );
339336

340337
const maxLevel = Math.max.apply( null, specifiedLevels );
341338

@@ -391,7 +388,7 @@ WPRequest.prototype.param = function( props, value ) {
391388
}
392389

393390
// Iterate through the properties
394-
Object.keys( props ).forEach( function( key ) {
391+
Object.keys( props ).forEach( ( key ) => {
395392
let value = props[ key ];
396393

397394
// Arrays should be de-duped and sorted
@@ -401,7 +398,7 @@ WPRequest.prototype.param = function( props, value ) {
401398

402399
// Set the value
403400
this._params[ key ] = value;
404-
}.bind( this ) );
401+
} );
405402

406403
return this;
407404
};

lib/endpoint-factories.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ const objectReduce = require( './util/object-reduce' );
2727
*/
2828
function generateEndpointFactories( routesByNamespace ) {
2929

30-
return objectReduce( routesByNamespace, function( namespaces, routeDefinitions, namespace ) {
30+
return objectReduce( routesByNamespace, ( namespaces, routeDefinitions, namespace ) => {
3131

3232
// Create
33-
namespaces[ namespace ] = objectReduce( routeDefinitions, function( handlers, routeDef, resource ) {
33+
namespaces[ namespace ] = objectReduce( routeDefinitions, ( handlers, routeDef, resource ) => {
3434

3535
const handlerSpec = createResourceHandlerSpec( routeDef, resource );
3636

lib/endpoint-request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,20 @@ function createEndpointRequest( handlerSpec, resource, namespace ) {
4545
// Mix in all available shortcut methods for GET request query parameters that
4646
// are valid within this endpoint tree
4747
if ( typeof handlerSpec._getArgs === 'object' ) {
48-
Object.keys( handlerSpec._getArgs ).forEach( function( supportedQueryParam ) {
48+
Object.keys( handlerSpec._getArgs ).forEach( ( supportedQueryParam ) => {
4949
const mixinsForParam = mixins[ supportedQueryParam ];
5050

5151
// Only proceed if there is a mixin available AND the specified mixins will
5252
// not overwrite any previously-set prototype method
5353
if ( typeof mixinsForParam === 'object' ) {
54-
Object.keys( mixinsForParam ).forEach( function( methodName ) {
54+
Object.keys( mixinsForParam ).forEach( ( methodName ) => {
5555
applyMixin( EndpointRequest.prototype, methodName, mixinsForParam[ methodName ] );
5656
} );
5757
}
5858
} );
5959
}
6060

61-
Object.keys( handlerSpec._setters ).forEach( function( setterFnName ) {
61+
Object.keys( handlerSpec._setters ).forEach( ( setterFnName ) => {
6262
// Only assign setter functions if they do not overwrite preexisting methods
6363
if ( ! EndpointRequest.prototype[ setterFnName ] ) {
6464
EndpointRequest.prototype[ setterFnName ] = handlerSpec._setters[ setterFnName ];

lib/http-transport.js

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ function _setHeaders( request, options ) {
2828
return request;
2929
}
3030

31-
return objectReduce( options.headers, function( request, value, key ) {
32-
return request.set( key, value );
33-
}, request );
31+
return objectReduce(
32+
options.headers,
33+
( request, value, key ) => request.set( key, value ),
34+
request
35+
);
3436
}
3537

3638
/**
@@ -206,9 +208,9 @@ function createPaginationObject( result, options, httpTransport ) {
206208
* @returns {Promise} A promise to the superagent request
207209
*/
208210
function invokeAndPromisify( request, callback, transform ) {
209-
return new Promise( function( resolve, reject ) {
211+
return new Promise( ( resolve, reject ) => {
210212
// Fire off the result
211-
request.end( function( err, result ) {
213+
request.end( ( err, result ) => {
212214

213215
// Return the results as a promise
214216
if ( err || result.error ) {
@@ -217,14 +219,14 @@ function invokeAndPromisify( request, callback, transform ) {
217219
resolve( result );
218220
}
219221
} );
220-
} ).then( transform ).then( function( result ) {
222+
} ).then( transform ).then( ( result ) => {
221223
// If a node-style callback was provided, call it, but also return the
222224
// result value for use via the returned Promise
223225
if ( callback && typeof callback === 'function' ) {
224226
callback( null, result );
225227
}
226228
return result;
227-
}, function( err ) {
229+
}, ( err ) => {
228230
// If the API provided an error object, it will be available within the
229231
// superagent response object as response.body (containing the response
230232
// JSON). If that object exists, it will have a .code property if it is
@@ -312,9 +314,11 @@ function _httpPost( wpreq, data, callback ) {
312314

313315
if ( wpreq._attachment ) {
314316
// Data must be form-encoded alongside image attachment
315-
request = objectReduce( data, function( req, value, key ) {
316-
return req.field( key, value );
317-
}, request.attach( 'file', wpreq._attachment, wpreq._attachmentName ) );
317+
request = objectReduce(
318+
data,
319+
( req, value, key ) => req.field( key, value ),
320+
request.attach( 'file', wpreq._attachment, wpreq._attachmentName )
321+
);
318322
} else {
319323
request = request.send( data );
320324
}

lib/mixins/filters.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,14 +77,16 @@ filterMixins.taxonomy = function( taxonomy, term ) {
7777
/* jshint validthis:true */
7878
const termIsArray = Array.isArray( term );
7979
const termIsNumber = termIsArray ?
80-
term.reduce( function( allAreNumbers, term ) {
81-
return allAreNumbers && typeof term === 'number';
82-
}, true ) :
80+
term.reduce(
81+
( allAreNumbers, term ) => allAreNumbers && typeof term === 'number',
82+
true
83+
) :
8384
typeof term === 'number';
8485
const termIsString = termIsArray ?
85-
term.reduce( function( allAreStrings, term ) {
86-
return allAreStrings && typeof term === 'string';
87-
}, true ) :
86+
term.reduce(
87+
( allAreStrings, term ) => allAreStrings && typeof term === 'string',
88+
true
89+
) :
8890
typeof term === 'string';
8991

9092
if ( ! termIsString && ! termIsNumber ) {

lib/mixins/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const mixins = {
5252
'password',
5353
'status',
5454
'sticky',
55-
].forEach( function( mixinName ) {
55+
].forEach( ( mixinName ) => {
5656
mixins[ mixinName ] = {};
5757
mixins[ mixinName ][ mixinName ] = parameterMixins[ mixinName ];
5858
} );

lib/path-part-setter.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,11 @@ function createPathPartSetter( node ) {
2020
const nodeLevel = node.level;
2121
const nodeName = node.names[ 0 ];
2222
const supportedMethods = node.methods || [];
23-
const dynamicChildren = node.children ? Object.keys( node.children )
24-
.map( function( key ) {
25-
return node.children[ key ];
26-
} )
27-
.filter( function( childNode ) {
28-
return childNode.namedGroup === true;
29-
} ) : [];
23+
const dynamicChildren = node.children ?
24+
Object.keys( node.children )
25+
.map( key => node.children[ key ] )
26+
.filter( childNode => ( childNode.namedGroup === true ) ) :
27+
[];
3028
const dynamicChild = dynamicChildren.length === 1 && dynamicChildren[ 0 ];
3129
const dynamicChildLevel = dynamicChild && dynamicChild.level;
3230

lib/resource-handler-spec.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,12 @@ function assignSetterFnForNode( handler, node ) {
3434

3535
setterFn = createPathPartSetter( node );
3636

37-
node.names.forEach( function( name ) {
37+
node.names.forEach( ( name ) => {
3838
// Convert from snake_case to camelCase
39-
const setterFnName = name
40-
.replace( /[_-]+\w/g, function( match ) {
41-
return match.replace( /[_-]+/, '' ).toUpperCase();
42-
} );
39+
const setterFnName = name.replace(
40+
/[_-]+\w/g,
41+
match => match.replace( /[_-]+/, '' ).toUpperCase()
42+
);
4343

4444
// Don't overwrite previously-set methods
4545
if ( ! handler._setters[ setterFnName ] ) {
@@ -75,7 +75,7 @@ function extractSetterFromNode( handler, node ) {
7575

7676
if ( node.children ) {
7777
// Recurse down to this node's children
78-
Object.keys( node.children ).forEach( function( key ) {
78+
Object.keys( node.children ).forEach( ( key ) => {
7979
extractSetterFromNode( handler, node.children[ key ] );
8080
} );
8181
}
@@ -112,7 +112,7 @@ function createNodeHandlerSpec( routeDefinition, resource ) {
112112
};
113113

114114
// Walk the tree
115-
Object.keys( routeDefinition ).forEach( function( routeDefProp ) {
115+
Object.keys( routeDefinition ).forEach( ( routeDefProp ) => {
116116
if ( routeDefProp !== '_getArgs' ) {
117117
extractSetterFromNode( handler, routeDefinition[ routeDefProp ] );
118118
}

lib/route-tree.js

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,19 +84,16 @@ function reduceRouteComponents( routeObj, topLevel, parentLevel, component, idx,
8484

8585
// Only one validate function is maintained for each node, because each node
8686
// is defined either by a string literal or by a specific regular expression.
87-
currentLevel.validate = function( input ) {
88-
return groupPatternRE.test( input );
89-
};
87+
currentLevel.validate = input => groupPatternRE.test( input );
9088

9189
// Check to see whether to expect more nodes within this branch of the tree,
9290
if ( components[ idx + 1 ] ) {
9391
// and create a "children" object to hold those nodes if necessary
9492
currentLevel.children = currentLevel.children || {};
9593
} else {
9694
// At leaf nodes, specify the method capabilities of this endpoint
97-
currentLevel.methods = ( routeObj.methods || [] ).map( function( str ) {
98-
return str.toLowerCase();
99-
} );
95+
currentLevel.methods = ( routeObj.methods || [] ).map( str => str.toLowerCase() );
96+
10097
// Ensure HEAD is included whenever GET is supported: the API automatically
10198
// adds support for HEAD if you have GET
10299
if ( currentLevel.methods.indexOf( 'get' ) > -1 && currentLevel.methods.indexOf( 'head' ) === -1 ) {
@@ -108,13 +105,13 @@ function reduceRouteComponents( routeObj, topLevel, parentLevel, component, idx,
108105
// appropriate parameter mixins
109106
if ( routeObj.endpoints ) {
110107
topLevel._getArgs = topLevel._getArgs || {};
111-
routeObj.endpoints.forEach( function( endpoint ) {
108+
routeObj.endpoints.forEach( ( endpoint ) => {
112109
// `endpoint.methods` will be an array of methods like `[ 'GET' ]`: we
113110
// only care about GET for this exercise. Validating POST and PUT args
114111
// could be useful but is currently deemed to be out-of-scope.
115-
endpoint.methods.forEach( function( method ) {
112+
endpoint.methods.forEach( ( method ) => {
116113
if ( method.toLowerCase() === 'get' ) {
117-
Object.keys( endpoint.args ).forEach( function( argKey ) {
114+
Object.keys( endpoint.args ).forEach( ( argKey ) => {
118115
// Reference param definition objects in the top _getArgs dictionary
119116
topLevel._getArgs[ argKey ] = endpoint.args[ argKey ];
120117
} );
@@ -141,11 +138,13 @@ function reduceRouteComponents( routeObj, topLevel, parentLevel, component, idx,
141138
function reduceRouteTree( namespaces, routeObj, route ) {
142139
const nsForRoute = routeObj.namespace;
143140

144-
// Strip the namespace from the route string (all routes should have the
145-
// format `/namespace/other/stuff`) @TODO: Validate this assumption
146-
// Also strip any trailing "/?": the slash is already optional and a single
147-
// question mark would break the regex parser
148-
const routeString = route.replace( '/' + nsForRoute + '/', '' ).replace( /\/\?$/, '' );
141+
const routeString = route
142+
// Strip the namespace from the route string (all routes should have the
143+
// format `/namespace/other/stuff`) @TODO: Validate this assumption
144+
.replace( '/' + nsForRoute + '/', '' )
145+
// Also strip any trailing "/?": the slash is already optional and a single
146+
// question mark would break the regex parser
147+
.replace( /\/\?$/, '' );
149148

150149
// Split the routes up into hierarchical route components
151150
const routeComponents = splitPath( routeString );
@@ -180,7 +179,10 @@ function reduceRouteTree( namespaces, routeObj, route ) {
180179
// Recurse through the route components, mutating levels with information about
181180
// each child node encountered while walking through the routes tree and what
182181
// arguments (parameters) are available for GET requests to this endpoint.
183-
routeComponents.reduce( reduceRouteComponents.bind( null, routeObj, levels ), levels );
182+
routeComponents.reduce(
183+
reduceRouteComponents.bind( null, routeObj, levels ),
184+
levels
185+
);
184186

185187
return namespaces;
186188
}

0 commit comments

Comments
 (0)