Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

132 str trim not a function #209

Merged
merged 4 commits into from
Apr 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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);