Permalink
Browse files

DRYness refactor in request.js

  • Loading branch information...
1 parent 3e02cf2 commit 7548645bbf8ece8a230b0ed4b5ca6acfccaaab4b @nrstott committed Feb 13, 2013
Showing with 24 additions and 19 deletions.
  1. +24 −19 lib/request.js
View
@@ -4,27 +4,32 @@ var util = require('./util');
module.exports = function(router, jsgiReq) {
return Object.create(jsgiReq, {
router: { value: router },
- search: {
- get: function() {
- return util.extractSearch(jsgiReq);
- },
- enumerable: true
- },
- isXMLHttpRequest: {
- get: function() {
- return !util.no(this.headers['x-requested-with']);
- },
- enumerable: true
- },
+ search: getter(function() {
+ return util.extractSearch(jsgiReq);
+ }),
+ isXMLHttpRequest: getter(function() {
+ return !util.no(this.headers['x-requested-with']);
+ }),
routeParams: {
value: {},
enumerable: true
},
- params: {
- get: function() {
- return util.merge({}, this.routeParams, this.search, this.body);
- },
- enumerable: true
- }
+ params: getter(function() {
+ return util.merge({}, this.routeParams, this.search, this.body);
+ })
});
-};
+};
+
+/**
+ * Helper function to create property descriptor that is enumerable
+ * and has a getter.
+ *
+ * @param fn {Function} The getter function.
+ * @api private
+ */
+function getter(fn) {
+ return {
+ get: fn,
+ enumerable: true
+ }
+}

0 comments on commit 7548645

Please sign in to comment.