Skip to content

Commit

Permalink
v1.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottHamper committed Nov 6, 2016
1 parent b6649f5 commit b7357e1
Show file tree
Hide file tree
Showing 9 changed files with 228 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +0,0 @@
/dist
bower.json
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Change Log

## 1.2.3
- Fixed null object reference error when both the global `this` and `window` objects are undefined (e.g., when using webpack).

## 1.2.2
- Fixed errors caused when accessing a properly encoded cookie while another cookie had a malformed key/value.

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ The following browsers have passed all of the automated Cookies.js tests:

## Getting the Library
#### Direct downloads
- [v1.2.2 Minified](https://raw.github.com/ScottHamper/Cookies/1.2.2/dist/cookies.min.js) (~1 KB gzipped)
- [v1.2.2 Unminified](https://raw.github.com/ScottHamper/Cookies/1.2.2/dist/cookies.js) (~1.7 KB gzipped)
- [v1.2.3 Minified](https://raw.github.com/ScottHamper/Cookies/1.2.3/dist/cookies.min.js) (~1 KB gzipped)
- [v1.2.3 Unminified](https://raw.github.com/ScottHamper/Cookies/1.2.3/dist/cookies.js) (~1.7 KB gzipped)

#### Node Package Manager
`npm install cookies-js`
Expand Down
10 changes: 10 additions & 0 deletions bower.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"name" : "cookies-js",
"version" : "1.2.3",
"main" : "dist/cookies.js",
"ignore" : [
"**/*",
"!dist/*",
"!bower.json"
]
}
33 changes: 33 additions & 0 deletions dist/cookies.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Cookies.js TypeScript Declaration File
* https://github.com/ScottHamper/Cookies
*
* This is free and unencumbered software released into the public domain.
*/
interface CookieOptions {
path?: string;
domain?: string;
expires?: any;
secure?: boolean;
}

interface CookiesStatic {
(key:string, value?:any, options?:CookieOptions): any;

get(key:string): string;
set(key:string, value:any, options?:CookieOptions): CookiesStatic;
expire(key:string, options?:CookieOptions): CookiesStatic;

defaults: CookieOptions;
enabled: boolean;
}

declare var Cookies:CookiesStatic;

declare module "cookies" {
export = Cookies;
}

declare module "cookies-js" {
export = Cookies;
}
172 changes: 172 additions & 0 deletions dist/cookies.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* Cookies.js - 1.2.3
* https://github.com/ScottHamper/Cookies
*
* This is free and unencumbered software released into the public domain.
*/
(function (global, undefined) {
'use strict';

var factory = function (window) {
if (typeof window.document !== 'object') {
throw new Error('Cookies.js requires a `window` with a `document` object');
}

var Cookies = function (key, value, options) {
return arguments.length === 1 ?
Cookies.get(key) : Cookies.set(key, value, options);
};

// Allows for setter injection in unit tests
Cookies._document = window.document;

// Used to ensure cookie keys do not collide with
// built-in `Object` properties
Cookies._cacheKeyPrefix = 'cookey.'; // Hurr hurr, :)

Cookies._maxExpireDate = new Date('Fri, 31 Dec 9999 23:59:59 UTC');

Cookies.defaults = {
path: '/',
secure: false
};

Cookies.get = function (key) {
if (Cookies._cachedDocumentCookie !== Cookies._document.cookie) {
Cookies._renewCache();
}

var value = Cookies._cache[Cookies._cacheKeyPrefix + key];

return value === undefined ? undefined : decodeURIComponent(value);
};

Cookies.set = function (key, value, options) {
options = Cookies._getExtendedOptions(options);
options.expires = Cookies._getExpiresDate(value === undefined ? -1 : options.expires);

Cookies._document.cookie = Cookies._generateCookieString(key, value, options);

return Cookies;
};

Cookies.expire = function (key, options) {
return Cookies.set(key, undefined, options);
};

Cookies._getExtendedOptions = function (options) {
return {
path: options && options.path || Cookies.defaults.path,
domain: options && options.domain || Cookies.defaults.domain,
expires: options && options.expires || Cookies.defaults.expires,
secure: options && options.secure !== undefined ? options.secure : Cookies.defaults.secure
};
};

Cookies._isValidDate = function (date) {
return Object.prototype.toString.call(date) === '[object Date]' && !isNaN(date.getTime());
};

Cookies._getExpiresDate = function (expires, now) {
now = now || new Date();

if (typeof expires === 'number') {
expires = expires === Infinity ?
Cookies._maxExpireDate : new Date(now.getTime() + expires * 1000);
} else if (typeof expires === 'string') {
expires = new Date(expires);
}

if (expires && !Cookies._isValidDate(expires)) {
throw new Error('`expires` parameter cannot be converted to a valid Date instance');
}

return expires;
};

Cookies._generateCookieString = function (key, value, options) {
key = key.replace(/[^#$&+\^`|]/g, encodeURIComponent);
key = key.replace(/\(/g, '%28').replace(/\)/g, '%29');
value = (value + '').replace(/[^!#$&-+\--:<-\[\]-~]/g, encodeURIComponent);
options = options || {};

var cookieString = key + '=' + value;
cookieString += options.path ? ';path=' + options.path : '';
cookieString += options.domain ? ';domain=' + options.domain : '';
cookieString += options.expires ? ';expires=' + options.expires.toUTCString() : '';
cookieString += options.secure ? ';secure' : '';

return cookieString;
};

Cookies._getCacheFromString = function (documentCookie) {
var cookieCache = {};
var cookiesArray = documentCookie ? documentCookie.split('; ') : [];

for (var i = 0; i < cookiesArray.length; i++) {
var cookieKvp = Cookies._getKeyValuePairFromCookieString(cookiesArray[i]);

if (cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] === undefined) {
cookieCache[Cookies._cacheKeyPrefix + cookieKvp.key] = cookieKvp.value;
}
}

return cookieCache;
};

Cookies._getKeyValuePairFromCookieString = function (cookieString) {
// "=" is a valid character in a cookie value according to RFC6265, so cannot `split('=')`
var separatorIndex = cookieString.indexOf('=');

// IE omits the "=" when the cookie value is an empty string
separatorIndex = separatorIndex < 0 ? cookieString.length : separatorIndex;

var key = cookieString.substr(0, separatorIndex);
var decodedKey;
try {
decodedKey = decodeURIComponent(key);
} catch (e) {
if (console && typeof console.error === 'function') {
console.error('Could not decode cookie with key "' + key + '"', e);
}
}

return {
key: decodedKey,
value: cookieString.substr(separatorIndex + 1) // Defer decoding value until accessed
};
};

Cookies._renewCache = function () {
Cookies._cache = Cookies._getCacheFromString(Cookies._document.cookie);
Cookies._cachedDocumentCookie = Cookies._document.cookie;
};

Cookies._areEnabled = function () {
var testKey = 'cookies.js';
var areEnabled = Cookies.set(testKey, 1).get(testKey) === '1';
Cookies.expire(testKey);
return areEnabled;
};

Cookies.enabled = Cookies._areEnabled();

return Cookies;
};
var cookiesExport = (global && typeof global.document === 'object') ? factory(global) : factory;

// AMD support
if (typeof define === 'function' && define.amd) {
define(function () { return cookiesExport; });
// CommonJS/Node.js support
} else if (typeof exports === 'object') {
// Support Node.js specific `module.exports` (which can be a function)
if (typeof module === 'object' && typeof module.exports === 'object') {
exports = module.exports = cookiesExport;
}
// But always support CommonJS module 1.1.1 spec (`exports` cannot be a function)
exports.Cookies = cookiesExport;
} else {
global.Cookies = cookiesExport;
}
})(typeof window === 'undefined' ? this : window);
6 changes: 6 additions & 0 deletions dist/cookies.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name" : "cookies-js",
"version" : "1.2.3-pre",
"version" : "1.2.3",
"author" : "Scott Hamper",
"description" : "Client-Side Cookie Manipulation API",
"homepage" : "http://github.com/ScottHamper/Cookies",
Expand Down
2 changes: 1 addition & 1 deletion src/cookies.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Cookies.js - 1.2.3-pre
* Cookies.js - 1.2.3
* https://github.com/ScottHamper/Cookies
*
* This is free and unencumbered software released into the public domain.
Expand Down

0 comments on commit b7357e1

Please sign in to comment.