Skip to content

Commit

Permalink
- change api to make Kizzy lowercase to kizzy
Browse files Browse the repository at this point in the history
- remove stupid old and busted build process
- add smoosh to build
- new make task for building
- rearrange tests
- set commonjs module exports to cache
- update readme with extra usage notes (twice)
- output kizzy.js to root of project
- add package.json
  • Loading branch information
ded committed Apr 26, 2011
1 parent 7f2f89f commit b1df597
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 85 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.DS_Store
node_modules
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
boosh:
node make/build.js
36 changes: 25 additions & 11 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
Kizzy - a Local Storage Utility
-------------------------------

Kizzy is a light-weight, cross-browser, JavaScript local storage utility. It utilizes the HTML5 localStorage api when available, as well as Internet Explorer's persistent XML store — wrapped up in a easy to use, memcached-like interface. When neither of these features are available (unlikely), it falls back to an in-browser object store.
Kizzy is a light-weight, cross-browser, JavaScript local storage utility. It leverages the HTML5 localStorage API when available, as well as Internet Explorer's persistent XML store — wrapped up in a easy to use, memcached-like interface. When neither of these features are available (unlikely), it falls back to an in-browser object store.

It looks like this

var cache = Kizzy('users');
var cache = kizzy('users');
var agent = cache.get('Agent');
if (agent) {
alert('Welcome back ' + agent.name);
Expand All @@ -17,14 +17,14 @@ It looks like this

Furthermore, a call to 'set' will return the value, making it quite easy for assignment.

var cache = Kizzy('users');
var cache = kizzy('users');
var agent = cache.get('Agent') || cache.set('Agent', {
name: 'Agent Diaz'
});

Lastly, you can pass an optional third argument to 'set' that tells the cache how long to live

var cache = Kizzy('users');
var cache = kizzy('users');

var agent = cache.get('Agent') || cache.set('Agent', {
name: 'Agent Diaz'
Expand All @@ -41,10 +41,6 @@ Lastly, you can pass an optional third argument to 'set' that tells the cache ho
cache.get('Agent').name // => expired
}, 6000);

Kizzy whu?
----------
The name comes from Kunta Kinte, a Mandinka African warrior from the 1700's. After being brought into slavery, he had a daughter whom he named Kizzy, which translates to *stay put* in hopes that the family would stay together, but not stay a slave.

Browser support
---------------

Expand All @@ -53,12 +49,30 @@ Browser support
* Chrome
* Safari 4+

Building Kizzy Yourself
Building Kizzy
---------------------------------

Building Kizzy requires node, (eg: port install node), a *submodule update --init* (for Uglify, gzip, JSHint, and sink tests). Then simply run *node Makefile.js*
$ submodule update --init
& make

Running tests
------------------
Tests will not currently pass if run on a file:/// protocol. Otherwise...

$ open tests/test.html

Ender integration
--------
Install Kizzy as an Ender module

$ ender add kizzy

Use it as such:

$.cache('user').get('name')

Kizzy whu?
----------
The name comes from Kunta Kinte, a Mandinka African warrior from the 1700's. After being brought into slavery, he had a daughter whom he named Kizzy, which translates to *stay put* in hopes that the family would stay together, but not stay a slave.

To run tests, run tests/test.html in a browser uploaded to a real web-server. Otherwise they won't work in Firefox on a file:/// protocol.
Happy Caching!
2 changes: 1 addition & 1 deletion build/sink
194 changes: 194 additions & 0 deletions kizzy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,194 @@
/*! Kizzy - a cross-browser LocalStorage API
* Copyright: Dustin Diaz 2011
* https://github.com/ded/kizzy
* License: MIT
*/
!function (win, doc, localStorage, store) {

function noop() {}

var hasLocalStorage,
html5 = 0,
writeThrough = function () {
return 1;
};


try {
// HTML5 local storage
hasLocalStorage = !!localStorage || !!globalStorage;
localStorage = localStorage || globalStorage[store];
html5 = 1;
} catch (ex1) {
html5 = 0;
// IE local storage
try {
// this try / if is required. trust me
if (doc.documentElement.addBehavior) {
html5 = 0;
hasLocalStorage = 1;
var dataStore = doc.documentElement;
dataStore.addBehavior('#default#userData');
dataStore.load(store);
var xmlDoc = dataStore.xmlDocument;
var xmlDocEl = xmlDoc.documentElement;
}
} catch (ex2) {
hasLocalStorage = false;
}
}

var setLocalStorage = noop,
getLocalStorage = noop,
removeLocalStorage = noop,
clearLocalStorage = noop;

if (hasLocalStorage) {
setLocalStorage = html5 ? html5setLocalStorage : setUserData;
getLocalStorage = html5 ? html5getLocalStorage : getUserData;
removeLocalStorage = html5 ? html5removeLocalStorage : removeUserData;
clearLocalStorage = html5 ? html5clearLocalStorage : clearUserData;

writeThrough = function (inst) {
try {
setLocalStorage(inst.ns, JSON.stringify(inst._));
return 1;
} catch (x) {
return 0;
}
};
}


function time() {
return +new Date();
}

function checkExpiry(inst, k) {
if (inst._[k] && inst._[k].e && inst._[k].e < time()) {
inst.remove(k);
}
}

function isNumber(n) {
return typeof n === 'number' && isFinite(n);
}

function html5getLocalStorage(k) {
return localStorage[k];
}

function html5setLocalStorage(k, v) {
localStorage[k] = v;
return v;
}

function html5removeLocalStorage(k) {
delete localStorage[k];
}

function html5clearLocalStorage() {
localStorage.clear();
}

function getNodeByName(name) {
var childNodes = xmlDocEl.childNodes,
node,
returnVal = null;

for (var i = 0, len = childNodes.length; i < len; i++) {
node = childNodes.item(i);
if (node.getAttribute("key") == name) {
returnVal = node;
break;
}
}
return returnVal;
}

function getUserData(name) {
var node = getNodeByName(name);
var returnVal = null;
if (node) {
returnVal = node.getAttribute("value");
}
return returnVal;
}

function setUserData(name, value) {
var node = getNodeByName(name);
if (!node) {
node = xmlDoc.createNode(1, "item", "");
node.setAttribute("key", name);
node.setAttribute("value", value);
xmlDocEl.appendChild(node);
}
else {
node.setAttribute("value", value);
}
dataStore.save(store);
return value;
}

function removeUserData(name) {
getNodeByName(name) && xmlDocEl.removeChild(node);
dataStore.save(store);
}

function clearUserData() {
while (xmlDocEl.firstChild) {
xmlDocEl.removeChild(xmlDocEl.firstChild);
}
dataStore.save(store);
}

function _Kizzy() {
this._ = {};
}

_Kizzy.prototype = {

set: function (k, v, optTtl) {
this._[k] = {
value: v,
e: isNumber(optTtl) ? time() + optTtl : 0
};
writeThrough(this) || this.remove(k);
return v;
},

get: function (k) {
checkExpiry(this, k);
return this._[k] ? this._[k].value : undefined;
},

remove: function (k) {
delete this._[k];
writeThrough(this);
},

clear: function () {
this._ = {};
writeThrough(this);
}
};

function Kizzy(ns) {
this.ns = ns;
this._ = JSON.parse(getLocalStorage(ns) || '{}');
}

Kizzy.prototype = _Kizzy.prototype;

function kizzy(ns) {
return new Kizzy(ns);
}

kizzy.remove = removeLocalStorage;
kizzy.clear = clearLocalStorage;

win.kizzy = kizzy;

typeof module !== 'undefined' && module.exports && (module.exports.cache = kizzy);

}(this, document, localStorage, document.domain);
6 changes: 6 additions & 0 deletions kizzy.min.js

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

24 changes: 24 additions & 0 deletions make/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require('smoosh').config({
"JAVASCRIPT": {
"DIST_DIR": "./",
"kizzy": [
"./src/header.js",
"./src/kizzy.js"
]
},
"JSHINT_OPTS": {
"boss": true,
"forin": false,
"curly": true,
"debug": false,
"devel": false,
"evil": false,
"regexp": false,
"undef": false,
"sub": false,
"white": true,
"indent": 2,
"whitespace": true,
"asi": false
}
}).run().build().analyze();
13 changes: 13 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"name": "kizzy",
"description": "x-browser LocalStorage API with a memcached interface",
"keywords": ["cache", "local storage", "caching", "expiration", "dom"],
"version": "0.0.1",
"homepage": "https://github.com/ded/kizzy",
"author": "Dustin Diaz <@ded>",
"repository": {
"type": "git",
"url": "https://github.com/ded/kizzy.git"
},
"main": "./kizzy.js"
}
8 changes: 4 additions & 4 deletions src/header.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*! Kizzy v1.0
* https://github.com/polvero/kizzy
* Copyright: @ded Dustin Diaz 2011
* License: CC Attribution: http://creativecommons.org/licenses/by/3.0/
/*! Kizzy - a cross-browser LocalStorage API
* Copyright: Dustin Diaz 2011
* https://github.com/ded/kizzy
* License: MIT
*/
Loading

0 comments on commit b1df597

Please sign in to comment.