add localStorage to polyfills; slightly refactor Ox.localStorage; remove whitespace; update example

This commit is contained in:
rolux 2012-05-30 16:21:07 +02:00
parent 41a3eaacd4
commit 5b6f161612
3 changed files with 74 additions and 28 deletions

View file

@ -97,13 +97,54 @@ My.range = function() {
return a;
};
My.foo = {
array: [
{a: 1, b: 2},
{c: 3, d: 4}
],
objects: {
/*@
My.localStorage <f> Returns a localStorage handler for a given namespace
(ns) -> storage <f> localStorage handler
() -> <o> Returns all key:value pairs
(key) -> <*> Returns one value
(key, value) -> <f> Sets one value, returns the handler
({key: value, ...}) -> <f> Sets one or more values, returns the handler
key <s> Any string
value <*> Any value that can be JSON-serialized
.delete <f> Delete method
() -> <f> Deletes all key:value pairs, returns the handler
(key[, ...]) -> <f> Deletes one or more pairs, returns the handler
key <s> Any string
ns <s> Namespace
*/
My.localStorage = (function() {
if (!window.localStorage) {
window.localStorage = {};
}
}
return function(ns) {
function storage(key, value) {
var args, ret;
if (arguments.length == 0) {
ret = {};
Ox.forEach(localStorage, function(value, key) {
if (Ox.startsWith(key, ns + '.')) {
ret[key.slice(ns.length + 1)] = JSON.parse(value);
}
});
} else if (arguments.length == 1 && typeof key == 'string') {
value = localStorage[ns + '.' + key];
ret = Ox.isUndefined(value) ? void 0 : JSON.parse(value);
} else {
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
localStorage[ns + '.' + key] = JSON.stringify(value);
});
ret = this;
}
return ret;
};
storage.delete = function() {
var keys = arguments.length == 0 ? Object.keys(storage())
: Ox.toArray(arguments)
keys.forEach(function(key) {
delete localStorage[ns + '.' + key];
});
return storage;
};
return storage;
};
}();

View file

@ -83,7 +83,7 @@ Some conventions:
@*/
global.Ox = function(value) {
return Ox.wrap(value);
};
};
})(this);
/*@
@ -139,8 +139,6 @@ Ox.load = function() {
/*@
Ox.localStorage <f> localStorage wrapper
(namespace) -> storage <f> localStorage function for a given namespace
FIXME: there is a bug in Ox.doc here,
will use "(namespace)" as function name
() -> <o> returns all key:value pairs
(key) -> <*> returns one value
(key, val) -> <f> sets one value, returns localStorage object
@ -151,7 +149,7 @@ Ox.localStorage = function(namespace) {
window.localStorage = {};
}
function storage(key, value) {
var args, ret;
var ret;
if (arguments.length == 0) {
ret = {};
Ox.forEach(localStorage, function(value, key) {
@ -164,8 +162,7 @@ Ox.localStorage = function(namespace) {
value = localStorage[namespace + '.' + key];
ret = value === void 0 ? void 0 : JSON.parse(value);
} else {
args = Ox.makeObject(arguments);
Ox.forEach(args, function(value, key) {
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
localStorage[namespace + '.' + key] = JSON.stringify(value);
});
ret = this;
@ -173,11 +170,14 @@ Ox.localStorage = function(namespace) {
return ret;
};
// IE 8 doesn't like `storage.delete`
storage['delete'] = function(key) {
var keys = arguments.length == 0 ? Object.keys(storage()) : [key];
storage['delete'] = function() {
var keys = arguments.length == 0 ? Object.keys(storage())
: Ox.toArray(arguments)
keys.forEach(function(key) {
delete localStorage[namespace + '.' + key];
});
var keys =
keys
return storage;
};
return storage;

View file

@ -2,7 +2,7 @@
Ox.polyfill = {};
(function(global) {
(function(window) {
/*@
Ox.polyfill.bind <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
<script>
@ -31,7 +31,7 @@ Ox.polyfill = {};
this_ = this,
ret = function() {
return this_.apply(
this instanceof fn ? this : that || global,
this instanceof fn ? this : that || window,
args.concat(Array.prototype.slice.call(arguments))
);
};
@ -236,6 +236,11 @@ Ox.polyfill.lastIndexOf = function(value) {
return ret;
};
/*@
Ox.polyfill.localStorage <o> Empty object, to be used instead of localStorage
*/
Ox.polyfill.localStorage = {};
/*@
Ox.polyfill.map <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/map
> Ox.polyfill.map.call([2, 1, 0], function(v, i) { return v == i; })
@ -335,12 +340,12 @@ Ox.polyfill.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
(function(global) {
(function(window) {
var key, log, object;
for (key in Ox.polyfill) {
object = key == 'bind' ? Function.prototype
: key == 'isArray' ? Array
: key == 'JSON' ? global
: key == 'JSON' || key == 'localStorage' ? window
: key == 'keys' ? Object
: key == 'trim' ? String.prototype
: Array.prototype;
@ -351,15 +356,15 @@ Ox.polyfill.trim = function() {
// In IE8, window.console.log is an object,
// in IE9, window.console.log.apply is undefined
// see http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function
if (global.console) {
if (typeof global.console.log !== 'function') {
log = global.console.log;
global.console.log = function() {
if (window.console) {
if (typeof window.console.log !== 'function') {
log = window.console.log;
window.console.log = function() {
log(Array.prototype.slice.call(arguments).join(' '));
};
} else if (!global.console.log.apply) {
global.console.log = Function.prototype.bind.call(
global.console.log, global.console
} else if (!window.console.log.apply) {
window.console.log = Function.prototype.bind.call(
window.console.log, window.console
);
}
}