cookie-getter
Advanced tools
Comparing version 1.0.1 to 2.0.0
// simple commonJS cookie reader, best perf according to http://jsperf.com/cookie-parsing | ||
module.exports = function (name) { | ||
var cookie = document.cookie, | ||
setPos = cookie.search(new RegExp('\\b' + name + '=')), | ||
stopPos = cookie.indexOf(';', setPos), | ||
res; | ||
if (!~setPos) return null; | ||
res = decodeURIComponent(cookie.substring(setPos, ~stopPos ? stopPos : undefined).split('=')[1]); | ||
return (res.charAt(0) === '{') ? JSON.parse(res) : res; | ||
}; | ||
if (typeof document === 'undefined') return null | ||
var cookie = document.cookie | ||
var setPos = cookie.search(new RegExp('\\b' + name + '=')) | ||
var stopPos = cookie.indexOf(';', setPos) | ||
var res | ||
if (!~setPos) return null | ||
res = decodeURIComponent(cookie.substring(setPos, ~stopPos ? stopPos : undefined).split('=')[1]) | ||
return (res.charAt(0) === '{') ? JSON.parse(res) : res | ||
} |
{ | ||
"name": "cookie-getter", | ||
"description": "Super light, high performance clientside cookie reader module.", | ||
"version": "2.0.0", | ||
"author": "Henrik Joreteg <henrik@andyet.net>", | ||
"name": "cookie-getter", | ||
"description": "Super light, high performance clientside cookie reader. Common JS and clientmodules compatible.", | ||
"version": "1.0.1", | ||
"tags": [ | ||
"bugs": "https://github.com/HenrikJoreteg/cookie-getter/issues", | ||
"devDependencies": { | ||
"precommit-hook": "^3.0.0", | ||
"standard": "8.6.0", | ||
"tap-spec": "^4.0.2", | ||
"tape": "^4.2.0" | ||
}, | ||
"homepage": "https://github.com/HenrikJoreteg/cookie-getter", | ||
"keywords": [ | ||
"client", | ||
"clientmodules", | ||
"cookie", | ||
"cookies", | ||
"client", | ||
"clientmodules" | ||
"cookies" | ||
], | ||
"license": "MIT", | ||
"main": "./cookie-getter.js", | ||
"pre-commit": [ | ||
"lint", | ||
"validate", | ||
"test" | ||
], | ||
"repository": { | ||
@@ -16,19 +31,7 @@ "type": "git", | ||
}, | ||
"main": "./cookie-getter.js", | ||
"scripts": { | ||
"lint": "standard", | ||
"test": "node test/index.js | tap-spec", | ||
"lint": "jshint .", | ||
"validate": "npm ls" | ||
}, | ||
"devDependencies": { | ||
"jshint": "^2.5.3", | ||
"precommit-hook": "^3.0.0", | ||
"tap-spec": "^4.0.2", | ||
"tape": "^4.2.0" | ||
}, | ||
"pre-commit": [ | ||
"lint", | ||
"validate", | ||
"test" | ||
] | ||
} | ||
} |
# cookie-getter | ||
Super simple, performant, clientside CommonJS cookie reader and nothing else. | ||
Minimalist (<10 LOC), performant, clientside cookie reader module and nothing else. It is tolerant of being required server-side. It will simply returns `null` if called within node. | ||
CommmonJS and [browserify](http://browserify.org/) compatible. | ||
## Why publish another cookie tool? | ||
Dead simple 4-line function that has the best performance according to: http://jsperf.com/cookie-parsing | ||
Dead simple function that has the best performance according to: http://jsperf.com/cookie-parsing | ||
Handy to have as a seperate module for easy installation with npm. | ||
Handy to have as a separate module for easy installation with npm. | ||
## Usage | ||
installing: | ||
installing: | ||
@@ -19,0 +15,0 @@ ``` |
@@ -1,35 +0,25 @@ | ||
var test = require('tape'), | ||
cookies = require('../cookie-getter'); | ||
var test = require('tape') | ||
var cookies = require('../cookie-getter') | ||
GLOBAL.document = { | ||
cookie: void 0 | ||
}; | ||
global.document = { | ||
cookie: void 0 | ||
} | ||
test("parsing some simple cookies", function(t) { | ||
t.plan(1); | ||
test('parsing some simple cookies', function (t) { | ||
t.plan(1) | ||
document.cookie = 'name1=val1;name2=val2;name3=val3' | ||
t.equal(cookies('name2'), 'val2') | ||
}) | ||
document.cookie = 'name1=val1;name2=val2;name3=val3'; | ||
test('parsing cookies with similar names', function (t) { | ||
t.plan(2) | ||
document.cookie = 'myconfig=someval;yourconfig=anotherval;config=yetanotherval;name3=val3' | ||
t.equal(cookies('config'), 'yetanotherval') | ||
t.equal(cookies('myconfig'), 'someval') | ||
}) | ||
t.equal(cookies('name2'), 'val2'); | ||
}); | ||
test('parsing cookies with similar names', function(t) { | ||
t.plan(2); | ||
document.cookie = 'myconfig=someval;yourconfig=anotherval;config=yetanotherval;name3=val3'; | ||
t.equal(cookies('config'), 'yetanotherval'); | ||
t.equal(cookies('myconfig'), 'someval'); | ||
}); | ||
test('parsing cookies separated by spaces', function(t) { | ||
t.plan(1); | ||
document.cookie = 'name1=val1; name2=val2; name3=val3'; | ||
t.equal(cookies('name2'), 'val2'); | ||
}); | ||
test('parsing cookies separated by spaces', function (t) { | ||
t.plan(1) | ||
document.cookie = 'name1=val1; name2=val2; name3=val3' | ||
t.equal(cookies('name2'), 'val2') | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
No License Found
License(Experimental) License information could not be found.
Found 1 instance in 1 package
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
2919
0
32
0
36