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
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue