add localStorage to polyfills; slightly refactor Ox.localStorage; remove whitespace; update example
This commit is contained in:
parent
41a3eaacd4
commit
5b6f161612
3 changed files with 74 additions and 28 deletions
|
@ -97,13 +97,54 @@ My.range = function() {
|
||||||
return a;
|
return a;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/*@
|
||||||
My.foo = {
|
My.localStorage <f> Returns a localStorage handler for a given namespace
|
||||||
array: [
|
(ns) -> storage <f> localStorage handler
|
||||||
{a: 1, b: 2},
|
() -> <o> Returns all key:value pairs
|
||||||
{c: 3, d: 4}
|
(key) -> <*> Returns one value
|
||||||
],
|
(key, value) -> <f> Sets one value, returns the handler
|
||||||
objects: {
|
({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;
|
||||||
|
};
|
||||||
|
}();
|
|
@ -139,8 +139,6 @@ Ox.load = function() {
|
||||||
/*@
|
/*@
|
||||||
Ox.localStorage <f> localStorage wrapper
|
Ox.localStorage <f> localStorage wrapper
|
||||||
(namespace) -> storage <f> localStorage function for a given namespace
|
(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
|
() -> <o> returns all key:value pairs
|
||||||
(key) -> <*> returns one value
|
(key) -> <*> returns one value
|
||||||
(key, val) -> <f> sets one value, returns localStorage object
|
(key, val) -> <f> sets one value, returns localStorage object
|
||||||
|
@ -151,7 +149,7 @@ Ox.localStorage = function(namespace) {
|
||||||
window.localStorage = {};
|
window.localStorage = {};
|
||||||
}
|
}
|
||||||
function storage(key, value) {
|
function storage(key, value) {
|
||||||
var args, ret;
|
var ret;
|
||||||
if (arguments.length == 0) {
|
if (arguments.length == 0) {
|
||||||
ret = {};
|
ret = {};
|
||||||
Ox.forEach(localStorage, function(value, key) {
|
Ox.forEach(localStorage, function(value, key) {
|
||||||
|
@ -164,8 +162,7 @@ Ox.localStorage = function(namespace) {
|
||||||
value = localStorage[namespace + '.' + key];
|
value = localStorage[namespace + '.' + key];
|
||||||
ret = value === void 0 ? void 0 : JSON.parse(value);
|
ret = value === void 0 ? void 0 : JSON.parse(value);
|
||||||
} else {
|
} else {
|
||||||
args = Ox.makeObject(arguments);
|
Ox.forEach(Ox.makeObject(arguments), function(value, key) {
|
||||||
Ox.forEach(args, function(value, key) {
|
|
||||||
localStorage[namespace + '.' + key] = JSON.stringify(value);
|
localStorage[namespace + '.' + key] = JSON.stringify(value);
|
||||||
});
|
});
|
||||||
ret = this;
|
ret = this;
|
||||||
|
@ -173,11 +170,14 @@ Ox.localStorage = function(namespace) {
|
||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
// IE 8 doesn't like `storage.delete`
|
// IE 8 doesn't like `storage.delete`
|
||||||
storage['delete'] = function(key) {
|
storage['delete'] = function() {
|
||||||
var keys = arguments.length == 0 ? Object.keys(storage()) : [key];
|
var keys = arguments.length == 0 ? Object.keys(storage())
|
||||||
|
: Ox.toArray(arguments)
|
||||||
keys.forEach(function(key) {
|
keys.forEach(function(key) {
|
||||||
delete localStorage[namespace + '.' + key];
|
delete localStorage[namespace + '.' + key];
|
||||||
});
|
});
|
||||||
|
var keys =
|
||||||
|
keys
|
||||||
return storage;
|
return storage;
|
||||||
};
|
};
|
||||||
return storage;
|
return storage;
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
Ox.polyfill = {};
|
Ox.polyfill = {};
|
||||||
|
|
||||||
(function(global) {
|
(function(window) {
|
||||||
/*@
|
/*@
|
||||||
Ox.polyfill.bind <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
|
Ox.polyfill.bind <f> see https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
|
||||||
<script>
|
<script>
|
||||||
|
@ -31,7 +31,7 @@ Ox.polyfill = {};
|
||||||
this_ = this,
|
this_ = this,
|
||||||
ret = function() {
|
ret = function() {
|
||||||
return this_.apply(
|
return this_.apply(
|
||||||
this instanceof fn ? this : that || global,
|
this instanceof fn ? this : that || window,
|
||||||
args.concat(Array.prototype.slice.call(arguments))
|
args.concat(Array.prototype.slice.call(arguments))
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
@ -236,6 +236,11 @@ Ox.polyfill.lastIndexOf = function(value) {
|
||||||
return ret;
|
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 <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; })
|
> 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, '');
|
return this.replace(/^\s+|\s+$/g, '');
|
||||||
};
|
};
|
||||||
|
|
||||||
(function(global) {
|
(function(window) {
|
||||||
var key, log, object;
|
var key, log, object;
|
||||||
for (key in Ox.polyfill) {
|
for (key in Ox.polyfill) {
|
||||||
object = key == 'bind' ? Function.prototype
|
object = key == 'bind' ? Function.prototype
|
||||||
: key == 'isArray' ? Array
|
: key == 'isArray' ? Array
|
||||||
: key == 'JSON' ? global
|
: key == 'JSON' || key == 'localStorage' ? window
|
||||||
: key == 'keys' ? Object
|
: key == 'keys' ? Object
|
||||||
: key == 'trim' ? String.prototype
|
: key == 'trim' ? String.prototype
|
||||||
: Array.prototype;
|
: Array.prototype;
|
||||||
|
@ -351,15 +356,15 @@ Ox.polyfill.trim = function() {
|
||||||
// In IE8, window.console.log is an object,
|
// In IE8, window.console.log is an object,
|
||||||
// in IE9, window.console.log.apply is undefined
|
// 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
|
// see http://stackoverflow.com/questions/5472938/does-ie9-support-console-log-and-is-it-a-real-function
|
||||||
if (global.console) {
|
if (window.console) {
|
||||||
if (typeof global.console.log !== 'function') {
|
if (typeof window.console.log !== 'function') {
|
||||||
log = global.console.log;
|
log = window.console.log;
|
||||||
global.console.log = function() {
|
window.console.log = function() {
|
||||||
log(Array.prototype.slice.call(arguments).join(' '));
|
log(Array.prototype.slice.call(arguments).join(' '));
|
||||||
};
|
};
|
||||||
} else if (!global.console.log.apply) {
|
} else if (!window.console.log.apply) {
|
||||||
global.console.log = Function.prototype.bind.call(
|
window.console.log = Function.prototype.bind.call(
|
||||||
global.console.log, global.console
|
window.console.log, window.console
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue