Skip to content

Commit

Permalink
132 str trim not a function (#209)
Browse files Browse the repository at this point in the history
* Fix and test for issue 11.

* Updated to use validation for empty string. Updated .gitignore (for work in Docker).

* Updated validator for empty strings to check String objects. Added more tests.
  • Loading branch information
awaterma committed Apr 26, 2021
1 parent 65e9a52 commit 1b25269
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Expand Up @@ -6,3 +6,6 @@ docker-compose.override.yml
.nyc_output
coverage
package-lock.json
.npm
.config
.bash_history
7 changes: 6 additions & 1 deletion lib/cookie.js
Expand Up @@ -459,6 +459,11 @@ function parse(str, options) {
if (!options || typeof options !== "object") {
options = {};
}

if (validators.isEmptyString(str) || !validators.isString(str)) {
return null
}

str = str.trim();

// We use a regex to parse the "name-value-pair" part of S5.2
Expand Down Expand Up @@ -659,7 +664,7 @@ function jsonParse(str) {
}

function fromJSON(str) {
if (!str) {
if (!str || validators.isEmptyString(str)) {
return null;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/validators.js
Expand Up @@ -41,11 +41,11 @@ function isDate(data) {
}

function isEmptyString(data) {
return data === '';
return data === '' || (data instanceof String && data.toString() === '');
}

function isString(data) {
return typeof data === 'string';
return typeof data === 'string' || data instanceof String
}

function isObject(data) {
Expand Down
32 changes: 32 additions & 0 deletions test/parsing_test.js
Expand Up @@ -691,6 +691,38 @@ vows
assert.equal(c.extensions, null);
}
}
},
"empty string": {
topic: function () {
return Cookie.parse('');
},
"is empty": function (c) {
assert.isNull(c);
}
},
"missing string": {
topic: function () {
return Cookie.parse();
},
"is empty": function (c) {
assert.isNull(c);
}
},
"some string object": {
topic: function() {
return Cookie.parse(new String(''))
},
"is empty": function(c) {
assert.isNull(c,null)
}
},
"some empty string object": {
topic: function() {
return Cookie.parse(new String())
},
"is empty": function(c) {
assert.isNull(c,null)
}
}
})
.export(module);

0 comments on commit 1b25269

Please sign in to comment.