Skip to content

Commit 556c2fb

Browse files
committed
Rewrote code to use tiny-cookie for handling cookies
- Cookie domain is now supported - Updated tests with a cookie-domain case - Added build folder and changed main file to be compiled minified
1 parent 4e0e688 commit 556c2fb

File tree

9 files changed

+242
-200
lines changed

9 files changed

+242
-200
lines changed

README.md

+32-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# vue-cookie
2-
A Vue.js plugin for manipulating cookies
2+
A Vue.js plugin for manipulating cookies tested up to ```Vue v2.0.5```
33

44
## Installation
55

@@ -13,7 +13,7 @@ npm install vue-cookie --save
1313
Include in ```<body>``` after loading Vue and it will automatically hook into Vue
1414

1515
``` html
16-
<script src="/node_modules/vue-cookie/src/vue-cookie.js'"></script>
16+
<script src="/node_modules/vue-cookie/build/vue-cookie.js'"></script>
1717

1818
```
1919

@@ -29,7 +29,10 @@ Vue.use(VueCookie);
2929
```
3030

3131
### Usage
32-
The plugin is available through ```this.$cookie``` in components
32+
The plugin is available through ```this.$cookie``` in components or ```Vue.cookie```
33+
34+
Rather than implementing my own Cookie handling logic the plugin now wraps the awesome
35+
[tiny-cookie](https://github.com/Alex1990/tiny-cookie "Tiny cookie documentation") package
3336

3437
###### Example
3538
``` javascript
@@ -45,12 +48,37 @@ this.$cookie.delete('test');
4548

4649
```
4750

51+
###### Advanced examples
52+
``` javascript
53+
54+
// Setting the cookie Domain
55+
this.$cookie.set('test', 'Random value', {expires: 1, domain: 'localhost'});
56+
57+
// As this cookie is set with a domain then if you wish to delete it you have to provide the domain when calling delete
58+
this.$cookie.delete('test', {domain: 'localhost'});
59+
60+
// Customizing expires
61+
var date = new Date;
62+
date.setDate(date.getDate() + 21);
63+
64+
Cookie.set('dateObject', 'A date object', { expires: date });
65+
Cookie.set('dateString', 'A parsable date string', { expires: date.toGMTString() });
66+
Cookie.set('integer', 'Seven days later', { expires: 7 });
67+
Cookie.set('stringSuffixY', 'One year later', { expires: '1Y' });
68+
Cookie.set('stringSuffixM', 'One month later', { expires: '1M' });
69+
Cookie.set('stringSuffixD', 'One day later', { expires: '1D' });
70+
Cookie.set('stringSuffixh', 'One hour later', { expires: '1h' });
71+
Cookie.set('stringSuffixm', 'Ten minutes later', { expires: '10m' });
72+
Cookie.set('stringSuffixs', 'Thirty seconds later', { expires: '30s' });
73+
74+
```
75+
4876
Thanks for using the plugin, I am happy to accept feedback/pull requests, do not forget to star if you like it!
4977

5078
Happy Coding! :D
5179

5280
### Tests
53-
This packacge uses the ´´´testem``` framework and jasmine assertion library
81+
This packacge uses the ´´´testem``` framework and ```jasmine``` assertion library
5482

5583
``` bash
5684
# Run npm install to fetch dependencies

build/vue-cookie.js

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

+1-11
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,9 @@ var elixir = require('laravel-elixir');
33
require('laravel-elixir-webpack-official');
44

55
require('laravel-elixir-vue');
6-
/*
7-
|--------------------------------------------------------------------------
8-
| Elixir Asset Management
9-
|--------------------------------------------------------------------------
10-
|
11-
| Elixir provides a clean, fluent API for defining some basic Gulp tasks
12-
| for your Laravel application. By default, we are compiling the Sass
13-
| file for our application, as well as publishing vendor resources.
14-
|
15-
*/
166

177
elixir(function(mix) {
188

19-
mix.webpack('vue-cookie-spec.js', 'test/bundle.js', 'test/spec');
9+
mix.webpack('vue-cookie.js', 'build', 'src');
2010

2111
});

package.json

+5-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,11 @@
44
"description": "A Vue.js plugin for manipulating cookies",
55
"author": "Alf Henderson <[email protected]>",
66
"private": false,
7-
"main": "src/vue-cookie.js",
7+
"main": "build/vue-cookie.js",
88
"license": "MIT",
99
"scripts": {
10-
"test": "./node_modules/testem/testem.js ci"
10+
"test": "./node_modules/testem/testem.js ci",
11+
"build": "gulp --production"
1112
},
1213
"repository": {
1314
"type": "git",
@@ -18,7 +19,8 @@
1819
"laravel-elixir": "^6.0.0-13",
1920
"laravel-elixir-vue": "^0.1.8",
2021
"laravel-elixir-webpack-official": "^1.0.9",
21-
"testem": "^1.13.0",
22+
"testem": "~1.8",
23+
"tiny-cookie": "^1.0",
2224
"vue": "^2.0.5"
2325
},
2426
"devDependencies": {}

src/vue-cookie.js

+15-24
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,32 @@
11
(function () {
22

3-
var VueCookie = {
3+
var Cookie = require('tiny-cookie');
44

5-
config: {
6-
domain: null
7-
},
5+
var VueCookie = {
86

97
install: function (Vue) {
108
Vue.prototype.$cookie = this;
119
Vue.cookie = this;
1210
},
13-
14-
set: function (name, value, days, domain) {
15-
let d = new Date;
16-
let cookieDomain = typeof domain !== 'undefined' ? domain : this.getCookieDomain();
17-
let domainString = cookieDomain !== null ? ";domain=" + cookieDomain : '';
18-
d.setTime(d.getTime() + 24*60*60*1000*days);
19-
window.document.cookie = name + "=" + value + domainString + ";path=/;expires=" + d.toGMTString();
11+
set: function (name, value, daysOrOptions) {
12+
let opts = daysOrOptions;
13+
if(Number.isInteger(daysOrOptions)) {
14+
opts = {expires: daysOrOptions};
15+
}
16+
return Cookie.set(name, value, opts);
2017
},
2118

2219
get: function (name) {
23-
var v = window.document.cookie.match('(^|;) ?' + name + '=([^;]*)(;|$)');
24-
return v ? v[2] : null;
20+
return Cookie.get(name);
2521
},
2622

27-
delete: function (name) {
28-
this.set(name, '', -1);
29-
},
30-
31-
setCookieDomain: function (domain) {
32-
this.config.cookieDomain = domain;
33-
},
34-
35-
getCookieDomain: function () {
36-
return this.config.cookieDomain;
23+
delete: function (name, options) {
24+
let opts = {expires: -1};
25+
if(options !== undefined) {
26+
opts = Object.assign(options, opts);
27+
}
28+
this.set(name, '', opts);
3729
}
38-
3930
};
4031

4132
if (typeof exports == "object") {

test.gulpfile.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
var elixir = require('laravel-elixir');
2+
3+
require('laravel-elixir-webpack-official');
4+
5+
require('laravel-elixir-vue');
6+
7+
elixir(function(mix) {
8+
9+
mix.webpack('vue-cookie-spec.js', 'test/bundle.js', 'test/spec');
10+
11+
});

test/spec/vue-cookie-spec.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,6 @@ describe('VueCookie', function(){
1111
this.cookieDomain = 'localhost';
1212
});
1313

14-
it('Should set cookie domain config and validate that it is set', function(){
15-
16-
Vue.cookie.setCookieDomain(this.cookieDomain);
17-
18-
expect(Vue.cookie.getCookieDomain())
19-
.toBe(this.cookieDomain);
20-
});
21-
2214
it('Should set and retrieve a Cookie with given value', function(){
2315

2416
Vue.cookie.set(this.cookieKey, this.cookieValue, 1);
@@ -38,12 +30,20 @@ describe('VueCookie', function(){
3830

3931
it('Should set and retrieve a Cookie with given value from a domain', function(){
4032

41-
Vue.cookie.set(this.cookieKey, this.cookieValue, 1, 'test' + this.cookieDomain);
33+
Vue.cookie.set(this.cookieKey, this.cookieValue, {expires: 1, domain: this.cookieDomain});
4234

4335
expect(Vue.cookie.get(this.cookieKey))
4436
.toBe(this.cookieValue);
4537
});
4638

39+
it('Should delete existing cookie with a domain and get null when fetching deleted cookie', function(){
40+
41+
Vue.cookie.delete(this.cookieKey, {domain: this.cookieDomain});
42+
43+
expect(Vue.cookie.get(this.cookieKey))
44+
.toBe(null);
45+
});
46+
4747

4848
});
4949

testem.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"serve_files": [
88
"test/bundle.js"
99
],
10-
"before_tests" : "gulp",
10+
"before_tests" : "gulp --gulpfile test.gulpfile.js",
1111
"after_tests" : "rm test/bundle.js",
1212
"launch_in_dev" : ["Chrome"]
1313
}

0 commit comments

Comments
 (0)