Skip to content

Commit

Permalink
feat: cookie().remove
Browse files Browse the repository at this point in the history
  • Loading branch information
halldwang committed Aug 8, 2020
1 parent 42c3933 commit 8c61335
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
20 changes: 16 additions & 4 deletions src/cookie/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,32 @@
* bbo.cookie()
* https://github.com/hrout/onavo/blob/master/onavo.js#L209
*/

import extend from '../collection/extend';
import hasOwnProperty from '../args/has_own_property';
import size from '../collection/size';
import isNumber from '../lodash/is_number';

const cookie = () => {
function cookieAttrExtend() {
let i = 0;
let result = {};
for (; i < arguments.length; i++) {
let attributes = arguments[i];
for (let key in attributes) {
if (hasOwnProperty(attributes, key)) {
result[key] = attributes[key];
}
}
}
return result;
}
function init(converter) {
function api(key, value, attributes) {
let result;
if (typeof document === 'undefined') {
return;
}
if (size(arguments) > 1) {
attributes = extend(
attributes = cookieAttrExtend(
{
path: '/'
},
Expand Down Expand Up @@ -122,7 +134,7 @@ const cookie = () => {
api(
key,
'',
extend(attributes, {
cookieAttrExtend(attributes, {
expires: -1
})
);
Expand Down
35 changes: 23 additions & 12 deletions test/cookie/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@ import bbo from '../bbo';
describe('bbo.cookie()', () => {
const cookie = 'c=v; c1=v';
const cookieName = 'name={%22foo%22:%22bar%22}';
const object = {
c: 'v1',
c1: 'v',
name: '{"foo":"bar"}'
};
const objectJson = {
c: 'v1',
c1: 'v',
name: {
foo: 'bar'
}
};

test('bbo.cookie() is a Function', () => {
expect(bbo.cookie()).toBeInstanceOf(Function);
expect(bbo.cookie().set).toBeInstanceOf(Function);
expect(bbo.cookie().get).toBeInstanceOf(Function);
expect(bbo.cookie().getJSON).toBeInstanceOf(Function);
expect(bbo.cookie().getJson).toBeInstanceOf(Function);
expect(bbo.cookie().remove).toBeInstanceOf(Function);
});

test('cookie().set()', function() {
Expand All @@ -36,24 +49,22 @@ describe('bbo.cookie()', () => {
});

test('cookie().get()', function() {
const object = {
c: 'v1',
c1: 'v',
name: '{"foo":"bar"}'
};
const objectJson = {
c: 'v1',
c1: 'v',
name: {
foo: 'bar'
}
};
const c = bbo.cookie().get('c');
const get = bbo.cookie().get();
const getJson = bbo.cookie().getJSON();
const getkey = bbo.cookie().get('name');
const getkeyJson = bbo.cookie().getJson('name');

expect(c).toBe('v1');
expect(get).toEqual(object);
expect(getJson).toEqual(objectJson);
expect(getkey).toEqual(object.name);
expect(getkeyJson).toEqual(objectJson.name);
});

test('cookie().remove()', function() {
bbo.cookie().remove('c');
const c = bbo.cookie().get('c');
expect(c).toBe(undefined);
});
});

0 comments on commit 8c61335

Please sign in to comment.