Skip to content

Commit 3e816b8

Browse files
Add migration notice and optional jquery support
Closes js-cookiegh-3.
1 parent a95cebc commit 3e816b8

File tree

10 files changed

+111
-15
lines changed

10 files changed

+111
-15
lines changed

.jshintrc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
"curly": true,
33
"eqeqeq": true,
44
"expr": true,
5-
// "maxlen": 130,
65
"newcap": true,
76
"noarg": true,
87
"nonbsp": true,
98
"trailing": true,
109
"undef": true,
1110
"unused": true,
1211
"globals": {
13-
"Cookies": true
12+
"Cookies": true,
13+
"require": true
1414
}
1515
}

Gruntfile.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,15 @@ module.exports = function (grunt) {
66
grunt.initConfig({
77
pkg: grunt.file.readJSON('package.json'),
88
qunit: {
9-
all: 'test/index.html'
9+
all: {
10+
options: {
11+
httpBase: 'http://127.0.0.1:9998'
12+
},
13+
src: ['test/index.html', 'test/amd.html']
14+
}
15+
},
16+
nodeunit: {
17+
all: 'test/node.js'
1018
},
1119
jshint: {
1220
options: {
@@ -53,6 +61,12 @@ module.exports = function (grunt) {
5361
base: ['.', 'test']
5462
}
5563
},
64+
build: {
65+
options: {
66+
port: 9998,
67+
base: ['.', 'test']
68+
}
69+
},
5670
tests: {
5771
options: {
5872
port: 9998,
@@ -69,6 +83,7 @@ module.exports = function (grunt) {
6983
urls: ['http://127.0.0.1:9999'],
7084
testname: 'Sauce Test for js-cookie',
7185
build: process.env.TRAVIS_JOB_ID,
86+
pollInterval: 5000,
7287
browsers: [
7388
// iOS
7489
{
@@ -164,7 +179,7 @@ module.exports = function (grunt) {
164179
}
165180

166181
grunt.registerTask('saucelabs', ['connect:saucelabs', 'saucelabs-qunit']);
167-
grunt.registerTask('test', ['jshint', 'qunit']);
182+
grunt.registerTask('test', ['jshint', 'connect:build', 'qunit', 'nodeunit']);
168183

169184
grunt.registerTask('dev', ['test', 'uglify', 'compare_size']);
170185
grunt.registerTask('ci', ['test', 'saucelabs']);

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
# Migrating from jquery-cookie
2+
3+
JavaScript Cookie 1.x internal behavior is totally backward compatible with jquery-cookie.
4+
To start migrating from jquery-cookie to JavaScript Cookie, just rename the API accordingly:
5+
6+
`$.cookie('name', 'value')` === `Cookies.set('name', 'value')`
7+
`$.cookie('name')` === `Cookies.get('name')`
8+
`$.removeCookie('name')` === `Cookies.remove('name')`
9+
`$.cookie()` === `Cookies.get()`
10+
111
# JavaScript Cookie [![Build Status](https://travis-ci.org/js-cookie/js-cookie.svg?branch=master)](https://travis-ci.org/js-cookie/js-cookie) [![Code Climate](https://codeclimate.com/github/js-cookie/js-cookie.svg)](https://codeclimate.com/github/js-cookie/js-cookie)
212

313
A simple, lightweight JavaScript API for handling cookies

package.json

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,27 @@
1717
"license": "MIT",
1818
"devDependencies": {
1919
"grunt": "0.4.5",
20+
"grunt-compare-size": "0.4.0",
21+
"grunt-contrib-connect": "0.10.1",
2022
"grunt-contrib-jshint": "0.11.1",
21-
"grunt-contrib-uglify": "0.8.0",
22-
"grunt-contrib-qunit": "0.5.2",
23+
"grunt-contrib-nodeunit": "0.4.1",
24+
"grunt-contrib-qunit": "0.7.0",
25+
"grunt-contrib-uglify": "0.9.1",
2326
"grunt-contrib-watch": "0.6.1",
24-
"grunt-compare-size": "0.4.x",
2527
"grunt-saucelabs": "8.6.0",
26-
"grunt-contrib-connect": "0.9.0",
27-
"gzip-js": "0.3.2"
28+
"gzip-js": "0.3.2",
29+
"jquery": "2.1.3",
30+
"qunitjs": "1.18.0",
31+
"requirejs": "2.1.17"
2832
},
2933
"volo": {
3034
"url": "https://raw.github.com/carhartl/jquery-cookie/v{version}/src/jquery.cookie.js"
3135
},
3236
"jspm": {
3337
"main": "js.cookie",
34-
"files": ["src/js.cookie.js"],
38+
"files": [
39+
"src/js.cookie.js"
40+
],
3541
"buildConfig": {
3642
"uglify": true
3743
}

src/js.cookie.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,21 @@
66
* Released under the MIT license
77
*/
88
(function (factory) {
9+
var jQuery;
910
if (typeof define === 'function' && define.amd) {
1011
// AMD (Register as an anonymous module)
11-
define(factory);
12+
define(['jquery'], factory);
1213
} else if (typeof exports === 'object') {
1314
// Node/CommonJS
14-
module.exports = factory();
15+
try {
16+
jQuery = require('jquery');
17+
} catch(e) {}
18+
module.exports = factory(jQuery);
1519
} else {
1620
// Browser globals
17-
window.Cookies = factory();
21+
window.Cookies = factory(window.jQuery);
1822
}
19-
}(function () {
23+
}(function ($) {
2024

2125
var pluses = /\+/g;
2226

@@ -129,5 +133,10 @@
129133
return !api(key);
130134
};
131135

136+
if ( $ ) {
137+
$.cookie = api;
138+
$.removeCookie = api.remove;
139+
}
140+
132141
return api;
133142
}));

test/amd-config.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*jshint unused:false */
2+
3+
var require = {
4+
paths: {
5+
'jquery': [
6+
'../node_modules/jquery/dist/jquery'
7+
],
8+
'qunit': [
9+
'../node_modules/qunitjs/qunit/qunit'
10+
]
11+
}
12+
};

test/amd.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<title>JavaScript Cookie Test Suite - AMD</title>
6+
<link href="../node_modules/qunitjs/qunit/qunit.css" rel="stylesheet">
7+
<script src="amd-config.js"></script>
8+
<script src="../node_modules/requirejs/require.js"></script>
9+
<script src="amd.js"></script>
10+
</head>
11+
<body>
12+
<div id="qunit"></div>
13+
<div id="qunit-fixture"></div>
14+
</body>
15+
</html>

test/amd.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
require(['qunit'], function (QUnit) {
2+
QUnit.module('amd');
3+
4+
QUnit.start();
5+
QUnit.test('with jquery dependency', function (assert) {
6+
QUnit.expect(3);
7+
var done = assert.async();
8+
require(['jquery', '/src/js.cookie.js'], function ($, Cookies) {
9+
assert.ok(!!$.cookie, 'Loaded $.cookie');
10+
assert.ok(!!$.removeCookie, 'Loaded $.removeCookie');
11+
assert.ok(!!Cookies.get, 'Loaded Cookies api');
12+
done();
13+
});
14+
});
15+
16+
});

test/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<html>
33
<head>
44
<meta charset="utf-8">
5-
<title>Javascript Cookie Test Suite</title>
5+
<title>JavaScript Cookie Test Suite</title>
66
<link href="http://code.jquery.com/qunit/qunit-1.14.0.css" rel="stylesheet">
77
<script src="http://code.jquery.com/qunit/qunit-1.14.0.js"></script>
88
<script src="../src/js.cookie.js"></script>

test/node.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*jshint node:true */
2+
exports.node = {
3+
should_load_js_cookie: function(test) {
4+
test.expect(3);
5+
var Cookies = require('../src/js.cookie');
6+
7+
test.ok( !!Cookies.get, 'should expose get api' );
8+
test.ok( !!Cookies.set, 'should expose set api' );
9+
test.ok( !!Cookies.remove, 'should expose remove api' );
10+
11+
test.done();
12+
}
13+
};

0 commit comments

Comments
 (0)