browser-cookies
Advanced tools
Comparing version 1.0.8 to 1.1.0
{ | ||
"name": "browser-cookies", | ||
"description": "Tiny cookies library for the browser", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"main": "src/browser-cookies.js", | ||
@@ -13,24 +13,25 @@ "files": [ | ||
"devDependencies": { | ||
"browserify": "9.0.x", | ||
"co": "4.5.x", | ||
"coveralls": "2.11.x", | ||
"browserify": "13.1.1", | ||
"concurrently": "3.1.0", | ||
"coveralls": "2.11.15", | ||
"gulp": "git://github.com/gulpjs/gulp.git#4.0", | ||
"gulp-rename": "1.2.x", | ||
"gulp-size": "1.2.x", | ||
"gulp-uglify": "1.2.x", | ||
"gulp-wrap": "0.11.x", | ||
"jasmine-core": "2.2.x", | ||
"karma": "0.12.x", | ||
"karma-browserify": "4.1.x", | ||
"karma-coverage": "0.3.x", | ||
"karma-jasmine": "0.3.x", | ||
"karma-phantomjs-launcher": "0.1.x", | ||
"karma-sauce-launcher": "0.2.x", | ||
"karma-spec-reporter": "0.0.x" | ||
"gulp-rename": "1.2.2", | ||
"gulp-size": "2.1.0", | ||
"gulp-uglify": "2.0.0", | ||
"gulp-wrap": "0.13.0", | ||
"karma": "1.3.0", | ||
"karma-coverage": "1.1.1", | ||
"karma-jasmine": "0.1.6", | ||
"karma-phantomjs-launcher": "1.0.2", | ||
"karma-spec-reporter": "0.0.26", | ||
"phantomjs-prebuilt": "2.1.14", | ||
"watchify": "3.8.0", | ||
"zuul": "3.11.1", | ||
"zuul-ngrok": "4.0.0" | ||
}, | ||
"scripts": { | ||
"build": "node --harmony ./node_modules/gulp/bin/gulp.js build", | ||
"test:local": "node --harmony ./node_modules/gulp/bin/gulp.js test:local", | ||
"test:full": "node --harmony ./node_modules/gulp/bin/gulp.js test:full", | ||
"test": "node --harmony ./node_modules/gulp/bin/gulp.js test:full" | ||
"build": "node ./node_modules/gulp/bin/gulp.js build", | ||
"test:local": "node ./node_modules/gulp/bin/gulp.js localtest", | ||
"test:full": "concurrently \"node ./node_modules/gulp/bin/gulp.js build\" \"node ./node_modules/zuul/bin/zuul -- test/*.js\"", | ||
"test": "concurrently \"node ./node_modules/gulp/bin/gulp.js coverage\" \"node ./node_modules/zuul/bin/zuul -- test/*.js\"" | ||
}, | ||
@@ -37,0 +38,0 @@ "author": { |
@@ -10,3 +10,2 @@ <img width="425" height="200" src="https://raw.githubusercontent.com/voltace/browser-cookies/master/browser-cookies.png"/> | ||
[![Coveralls Status][coveralls-image]][coveralls-url] | ||
[![Dev Dependencies Status][david-image]][david-url] | ||
@@ -28,3 +27,2 @@ - [Features](#features) | ||
- Cross browser support | ||
- Unit tests | ||
- Supports CommonJS (e.g. Browserify) | ||
@@ -35,3 +33,3 @@ | ||
[![Sauce Test Status][saucelabs-image]][saucelabs-url] | ||
Or [run the unit tests][ref-unittests] for your current browser right now. | ||
Or [run the unit tests][ref-unittests] right now in your current browser. | ||
@@ -63,2 +61,3 @@ ### Installation | ||
- method [cookies.get(`name`)](#cookies-get) | ||
- method [cookies.all()](#cookies-all) | ||
- method [cookies.erase(`name`, [, `options`])](#cookies-erase) | ||
@@ -90,3 +89,9 @@ - property [cookies.defaults](#cookies-defaults) | ||
<hr/><a name="cookies-all"></a> | ||
[cookies.all()](#cookies-all) | ||
<br/> | ||
Method to get all cookies. | ||
Returns an object containing all cookie values with the cookie names used as keys. Percent encoded names and values will automatically be decoded. | ||
<hr/><a name="cookies-erase"></a> | ||
@@ -163,2 +168,19 @@ | ||
The `cookies.all` method can be used for more advanced functionality, for example to erase all cookies except one: | ||
```javascript | ||
var cookies = require('browser-cookies'); | ||
var cookieToKeep = 'FirstName'; // Name of the cookie to keep | ||
// Get all cookies as an object | ||
var allCookies = cookies.all(); | ||
// Iterate over all cookie names | ||
for (var cookieName in allCookies) { | ||
// Erase the cookie (except if it's the cookie that needs to be kept) | ||
if(allCookies.hasOwnProperty(cookieName) && cookieName != cookieToKeep) { | ||
cookies.erase(cookieName); | ||
} | ||
} | ||
``` | ||
### How to use with PHP | ||
@@ -174,15 +196,4 @@ Use [setrawcookie()][ref-php-setrawcookie] instead of `setcookie()` to prevent PHP from replacing spaces with `+` characters: | ||
### Todo's | ||
- Additional testing: | ||
- Mobile browser testing (Disabled automated testing for mobile browsers because the results varied per run). | ||
- Manually verify support on old browsers that that still need to be supported (i.e. IE6)? | ||
- Perform compatibility testing with nodejs and document server-side usage. | ||
- Distribution: | ||
- Generate build for use without a loader (development build + minified version). | ||
- Cross browser consistency: | ||
- When a domain is not specified most browsers only allow an exact domain match, but [IE sends cookies to all subdomains][ref-ie-cookies]. Could ensure cookies are saved to all subdomains by default for consistent behavior amongst all browsers? or perhaps add a note ti set the domain explicitly for proper cross-browser consistency? | ||
### Development | ||
This design goal is to provide to smallest possible size (when minified and gzipped) for the given API, while remaining compliant to RFC6265 and providing cross-browser compatibility and consistency. | ||
The design goal is to provide the smallest possible size (when minified and gzipped) for the given API while remaining compliant to RFC6265 and providing cross-browser compatibility and consistency. | ||
@@ -203,2 +214,3 @@ Development setup (requires [node][ref-node-download] and [git][ref-git-setup] to be installed): | ||
[ref-browser-cookies-shim]: https://www.github.com/voltace/browser-cookies-shim | ||
[ref-cookie-domain]: https://stackoverflow.com/questions/1062963/how-do-browser-cookie-domains-work | ||
@@ -225,6 +237,3 @@ [ref-date-parse]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse | ||
[david-url]: https://david-dm.org/voltace/browser-cookies#info=devDependencies | ||
[david-image]: https://img.shields.io/david/dev/voltace/browser-cookies.svg | ||
[saucelabs-url]: https://saucelabs.com/u/browser-cookies | ||
[saucelabs-image]: https://saucelabs.com/browser-matrix/browser-cookies.svg | ||
[saucelabs-image]: https://rawgit.com/voltace/browser-cookies/master/browser_compatibility.svg |
@@ -9,7 +9,7 @@ exports.defaults = {}; | ||
// Apply default value for unspecified options | ||
var expires = opts.expires || defaults.expires; | ||
var domain = opts.domain || defaults.domain; | ||
var path = opts.path != undefined ? opts.path : (defaults.path != undefined ? defaults.path : '/'); | ||
var secure = opts.secure != undefined ? opts.secure : defaults.secure; | ||
var httponly = opts.httponly != undefined ? opts.httponly : defaults.httponly; | ||
var expires = opts.expires || defaults.expires; | ||
var domain = opts.domain || defaults.domain; | ||
var path = opts.path !== undefined ? opts.path : (defaults.path !== undefined ? defaults.path : '/'); | ||
var secure = opts.secure !== undefined ? opts.secure : defaults.secure; | ||
var httponly = opts.httponly !== undefined ? opts.httponly : defaults.httponly; | ||
@@ -20,3 +20,3 @@ // Determine cookie expiration date | ||
// in case expires is an integer, it should specify the number of days till the cookie expires | ||
typeof expires == 'number' ? new Date().getTime() + (expires * 864e5) : | ||
typeof expires === 'number' ? new Date().getTime() + (expires * 864e5) : | ||
// else expires should be either a Date object or in a format recognized by Date.parse() | ||
@@ -52,4 +52,6 @@ expires | ||
// Decode the cookie name and remove any leading/trailing spaces, then compare to the requested cookie name | ||
if (decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+|\s+$/g, '')) == name) { | ||
var cookie_name = decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+/, '')); | ||
// Return cookie value if the name matches | ||
if (cookie_name === name) { | ||
return decodeURIComponent(cookie.substring(separatorIndex + 1, cookieLength)); | ||
@@ -59,2 +61,3 @@ } | ||
// Return `null` as the cookie was not found | ||
return null; | ||
@@ -72,1 +75,24 @@ }; | ||
}; | ||
exports.all = function() { | ||
var all = {}; | ||
var cookies = document.cookie.split(';'); | ||
// Iterate all cookies | ||
for(var i = 0; i < cookies.length; i++) { | ||
var cookie = cookies[i]; | ||
var cookieLength = cookie.length; | ||
// Determine separator index ("name=value") | ||
var separatorIndex = cookie.indexOf('='); | ||
// IE<11 emits the equal sign when the cookie value is empty | ||
separatorIndex = separatorIndex < 0 ? cookieLength : separatorIndex; | ||
// add the cookie name and value to the `all` object | ||
var cookie_name = decodeURIComponent(cookie.substring(0, separatorIndex).replace(/^\s+/, '')); | ||
all[cookie_name] = decodeURIComponent(cookie.substring(separatorIndex + 1, cookieLength)); | ||
} | ||
return all; | ||
}; |
Sorry, the diff of this file is not supported yet
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
16375
75
231
17