├── .gitignore ├── .travis.yml ├── test └── testCookie.js ├── package.json ├── LICENSE ├── README.md ├── my.conf.js └── simpleCookie.js /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.10" 4 | script: node_modules/karma/bin/karma start my.conf.js --single-run 5 | before_install: 6 | - export DISPLAY=:99.0 7 | - sh -e /etc/init.d/xvfb start -------------------------------------------------------------------------------- /test/testCookie.js: -------------------------------------------------------------------------------- 1 | 2 | describe('CRUDCookie', function() { 3 | it('Should exist', function() { 4 | _smplCke.set('test', {'foo' : 'bar', 1 : true}); 5 | expect(_smplCke.is('test')).toBe(true); 6 | expect(typeof _smplCke.get('test')).toBe('object'); 7 | _smplCke.del('test'); 8 | expect(_smplCke.is('test')).toBe(false); 9 | 10 | _smplCke.set('test3', 'a string'); 11 | expect(_smplCke.is('test3')).toBe(true); 12 | expect(typeof _smplCke.get('test3')).toBe('string'); 13 | }); 14 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "simplecookie", 3 | "version": "1.0.1", 4 | "description": "A small javascript utility to make handling cookies simple", 5 | "main": "simpleCookie.js", 6 | "repository": { 7 | "type": "git", 8 | "url": "https://github.com/tevko/simpleCookie.git" 9 | }, 10 | "keywords": [ 11 | "javascript", 12 | "cookie" 13 | ], 14 | "author": "Tim Evko", 15 | "license": "ISC", 16 | "bugs": { 17 | "url": "https://github.com/tevko/simpleCookie/issues" 18 | }, 19 | "homepage": "https://github.com/tevko/simpleCookie", 20 | "devDependencies": { 21 | "jasmine-core": "^2.3.4", 22 | "karma": "^0.13.14", 23 | "karma-firefox-launcher": "^0.1.6", 24 | "karma-jasmine": "^0.3.6" 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Tim Evko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simpleCookie [![buld status](https://travis-ci.org/tevko/simpleCookie.svg)](https://travis-ci.org/tevko/simpleCookie) [![Rate on Openbase](https://badges.openbase.com/js/rating/simplecookie.svg)](https://openbase.com/js/simplecookie?utm_source=embedded&utm_medium=badge&utm_campaign=rate-badge) 2 | A small utility to make handling cookies simple 3 | ----------------------------------------------- 4 | 5 | 307 bytes minified & gzipped 6 | 7 | ## Useage 8 | 9 | **After minifying/uglifying simpleCookie.js** 10 | 11 | ### Setting a cookie 12 | 13 | `_smplCke.set('test', {'foo' : 'bar', 1 : true}, 'Thu, 18 Dec 2019 12:00:00 UTC', '/login');` 14 | 15 | Parameters one and two are mandatory, three and four are optional, and default to `session` and `/` respectively. The value of a cookie can be a string or an object. 16 | 17 | ### Getting a cookie 18 | 19 | `_smplCke.get('test')` 20 | 21 | If the value of the cookie is an object, an object will be returned. Otherwise, this will return a string. 22 | 23 | ### Deleting a cookie 24 | 25 | `_smplCke.del('test')` 26 | 27 | ### Finding a cookie 28 | 29 | `_smplCke.is('test')` 30 | 31 | Will return `true` or `false` 32 | 33 | ## Contributing 34 | 35 | This library is meant to provide a minimal API, which can be extended upon by the user if needed. With that in mind, please do not submit pull requests containing additional features, unless they allow for necessary functionality which is not currently provided. 36 | -------------------------------------------------------------------------------- /my.conf.js: -------------------------------------------------------------------------------- 1 | // Karma configuration 2 | // Generated on Sun Nov 01 2015 17:16:05 GMT-0500 (Eastern Standard Time) 3 | 4 | module.exports = function(config) { 5 | config.set({ 6 | 7 | // base path that will be used to resolve all patterns (eg. files, exclude) 8 | basePath: '', 9 | 10 | 11 | // frameworks to use 12 | // available frameworks: https://npmjs.org/browse/keyword/karma-adapter 13 | frameworks: ['jasmine'], 14 | 15 | 16 | // list of files / patterns to load in the browser 17 | files: [ 18 | 'simpleCookie.js','test/testCookie.js' 19 | ], 20 | 21 | 22 | // list of files to exclude 23 | exclude: [ 24 | ], 25 | 26 | 27 | // preprocess matching files before serving them to the browser 28 | // available preprocessors: https://npmjs.org/browse/keyword/karma-preprocessor 29 | preprocessors: { 30 | }, 31 | 32 | 33 | // test results reporter to use 34 | // possible values: 'dots', 'progress' 35 | // available reporters: https://npmjs.org/browse/keyword/karma-reporter 36 | reporters: ['progress'], 37 | 38 | 39 | // web server port 40 | port: 9876, 41 | 42 | 43 | // enable / disable colors in the output (reporters and logs) 44 | colors: true, 45 | 46 | 47 | // level of logging 48 | // possible values: config.LOG_DISABLE || config.LOG_ERROR || config.LOG_WARN || config.LOG_INFO || config.LOG_DEBUG 49 | logLevel: config.LOG_INFO, 50 | 51 | 52 | // enable / disable watching file and executing tests whenever any file changes 53 | autoWatch: false, 54 | 55 | 56 | // start these browsers 57 | // available browser launchers: https://npmjs.org/browse/keyword/karma-launcher 58 | browsers: ['Firefox'], 59 | 60 | 61 | // Continuous Integration mode 62 | // if true, Karma captures browsers, runs the tests and exits 63 | singleRun: false, 64 | 65 | // Concurrency level 66 | // how many browser should be started simultanous 67 | concurrency: Infinity 68 | }) 69 | } 70 | -------------------------------------------------------------------------------- /simpleCookie.js: -------------------------------------------------------------------------------- 1 | /* simpleCookie.js 2 | * 3 | * A small javascript utility to make handling cookies simple 4 | * 5 | * Author: Tim Evko 6 | * https://twitter.com/tevko 7 | * https://timevko.website 8 | * https://github.com/tevko/simpleCookie 9 | * License: MIT 10 | * 11 | */ 12 | 13 | ;(function() { 14 | 'use strict'; 15 | window._smplCke = { 16 | /** 17 | * returns true if a cookie exists 18 | * @param {string} name - the name of the cookie 19 | * @return {Boolean} - returns true if the cookie exists 20 | */ 21 | is: function(name) { 22 | return document.cookie.indexOf(name + '=') > -1; 23 | }, 24 | /** 25 | * returns the value of a cookie as an object 26 | * @param {string} name - the name of the cookie to be returned 27 | * @return {object} - a JS object representation of the cookie 28 | */ 29 | get: function(name) { 30 | // if the cookie value is a normal string, just return it 31 | try { 32 | var jsonString = decodeURIComponent(document.cookie.split(name + '=')[1].split(';')[0]); 33 | return JSON.parse(jsonString); 34 | } catch(e) { 35 | return document.cookie.split(name + '=')[1].split(';')[0]; 36 | } 37 | }, 38 | /** 39 | * sets a cookie 40 | * @param {string} name - the name of the cookie 41 | * @param {string | object} value - the body of the cookie 42 | * @param OPTIONAL {date string} expiration - a valid UTC date string "Thu, 18 Dec 2013 12:00:00 UTC" DEFAULT - Session 43 | * @param OPTIONAL {string} path - a URL path where the cookie should exist DEFAULT - '/' 44 | */ 45 | set: function(name, value, expiration, path, httpOnly) { 46 | if (typeof path === 'undefined') { 47 | path = '/'; 48 | } 49 | if (typeof value === 'object') { 50 | value = JSON.stringify(value); 51 | } 52 | if (typeof expiration === 'undefined') { 53 | document.cookie = name + '=' + value + '; path=' + path; 54 | } else { 55 | document.cookie = name + '=' + value + ';expires=' + expiration + '; path=' + path; 56 | } 57 | }, 58 | /** 59 | * deletes a cookie 60 | * @param {string} name - the name of the cookie 61 | */ 62 | del: function(name) { 63 | document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;'; 64 | } 65 | } 66 | })(); --------------------------------------------------------------------------------