[Pkg-javascript-commits] [node-mocks-http] 10/296: Hiding the private _ variables in scope but out of object. Good idea?

Thorsten Alteholz alteholz at moszumanska.debian.org
Mon Feb 8 18:13:16 UTC 2016


This is an automated email from the git hooks/post-receive script.

alteholz pushed a commit to branch master
in repository node-mocks-http.

commit 32363fa99828c8be97ee70884e5627a7b9f23286
Author: Howard Abrams <howard at cloud-eco.com>
Date:   Sun Feb 19 10:58:14 2012 -0800

    Hiding the private _ variables in scope but out of object. Good idea?
---
 lib/mockResponse.js | 51 +++++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/lib/mockResponse.js b/lib/mockResponse.js
index b7f8559..40b572e 100644
--- a/lib/mockResponse.js
+++ b/lib/mockResponse.js
@@ -21,6 +21,7 @@
  *
  * Options:
  *
+ *   encoding - The default encoding for the response
  */
 
 exports.createResponse = function(options) {
@@ -28,11 +29,13 @@ exports.createResponse = function(options) {
         options = {};
     }
 
+    var _endCalled = false;
+    var _data      = "";
+    var _headers   = {};
+    var _encoding  = options.encoding;
+
     return {
-        _endCalled: false,
-        _data: "",
         statusCode: -1,
-        _headers: {},
 
         /**
          * Function: writeHead
@@ -46,7 +49,7 @@ exports.createResponse = function(options) {
          *               the HTTP headers.
          */
         writeHead: function( statusCode, phrase, headers ) {
-            if (this._endCalled) {
+            if (_endCalled) {
                 throw "The end() method has already been called.";
             }
             
@@ -55,11 +58,11 @@ exports.createResponse = function(options) {
             // Note: Not sure if the headers given in this function
             //       overwrite any headers specified earlier.
             if (headers) {
-                this._reasonPhrase = phrase;
-                this._headers = headers;
+                _reasonPhrase = phrase;
+                _headers = headers;
             }
             else {
-                this._headers = phrase;
+                _headers = phrase;
             }
         },
         
@@ -76,9 +79,9 @@ exports.createResponse = function(options) {
          *  encoding - Optional encoding value.
          */
         send: function( data, encoding ) {
-            this._data += data;
+            _data += data;
             if (encoding) {
-                this._encoding = encoding;
+                _encoding = encoding;
             }
         },
         
@@ -112,12 +115,12 @@ exports.createResponse = function(options) {
          *  encoding - Optional encoding value.
          */
         end: function( data, encoding ) {
-            this._endCalled = true;
+            _endCalled = true;
             if (data) {
-                this._data += data;
+                _data += data;
             }
             if (encoding) {
-                this._encoding = encoding;
+                _encoding = encoding;
             }
         },
 
@@ -128,7 +131,7 @@ exports.createResponse = function(options) {
          *   Returns a particular header by name.
          */
         getHeader: function(name) {
-            return this._headers[name];
+            return _headers[name];
         },
 
         /**
@@ -137,7 +140,7 @@ exports.createResponse = function(options) {
          *   Set a particular header by name.
          */
         setHeader: function(name, value) {
-            return this._headers[name] = value;
+            return _headers[name] = value;
         },
 
         /**
@@ -146,7 +149,7 @@ exports.createResponse = function(options) {
          *   Removes an HTTP header by name.
          */
         removeHeader: function(name) {
-            delete this._headers[name];
+            delete _headers[name];
         },
 
         /**
@@ -159,7 +162,7 @@ exports.createResponse = function(options) {
          *   encoding - The string representing the encoding value.
          */
         setEncoding: function(encoding) {
-            this._encoding = encoding;
+            _encoding = encoding;
         },
         
         
@@ -173,7 +176,7 @@ exports.createResponse = function(options) {
          *  returns true if it has been called. False otherwise.
          */
         _isEndCalled: function() {
-            return this._endCalled;
+            return _endCalled;
         },
 
         
@@ -184,7 +187,7 @@ exports.createResponse = function(options) {
          *  empty object, but probably will have "Content-Type" set.
          */
         _getHeaders: function() {
-            return this._headers;
+            return _headers;
         },
 
 
@@ -195,7 +198,7 @@ exports.createResponse = function(options) {
          *  The data sent to the user.
          */
         _getData: function() {
-            return this._data;
+            return _data;
         },
 
         /**
@@ -214,7 +217,7 @@ exports.createResponse = function(options) {
          *  It doesn't validate the data that was sent.
          */
         _isJSON: function() {
-            return (this._headers["Content-Type"] == "application/json");
+            return (_headers["Content-Type"] == "application/json");
         },
         
         /**
@@ -228,10 +231,10 @@ exports.createResponse = function(options) {
          *   False if the encoding wasn't set and wasn't set to "utf8".
          */
         _isUTF8: function() {
-            if ( !this._encoding ) {
+            if ( !_encoding ) {
                 return false;
             }
-            return ( this._encoding === "utf8" );
+            return ( _encoding === "utf8" );
         },
 
         /**
@@ -247,8 +250,8 @@ exports.createResponse = function(options) {
          *   set. Otherwise, it compares it.
          */
         _isDataLengthValid: function() {
-            if (this._headers["Content-Length"]) {
-                return (this._headers["Content-Length"] == this._data.length);
+            if (_headers["Content-Length"]) {
+                return (_headers["Content-Length"] == _data.length);
             }
             return true;
         }

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-mocks-http.git



More information about the Pkg-javascript-commits mailing list