Collection.js: handle localStorage object
This commit is contained in:
parent
d793d7857d
commit
175c8b23d0
1 changed files with 12 additions and 10 deletions
|
@ -130,7 +130,7 @@ Ox.filter <f> Filters a collection by a given condition
|
||||||
Ox.filter = function(collection, iterator, that) {
|
Ox.filter = function(collection, iterator, that) {
|
||||||
var ret, type = Ox.typeOf(collection);
|
var ret, type = Ox.typeOf(collection);
|
||||||
iterator = iterator || Ox.identity;
|
iterator = iterator || Ox.identity;
|
||||||
if (type == 'object') {
|
if (type == 'object' || type == 'storage') {
|
||||||
ret = {};
|
ret = {};
|
||||||
Ox.forEach(collection, function(value, key) {
|
Ox.forEach(collection, function(value, key) {
|
||||||
if (iterator.call(that, value, key, collection)) {
|
if (iterator.call(that, value, key, collection)) {
|
||||||
|
@ -172,11 +172,8 @@ Ox.forEach <f> forEach loop
|
||||||
@*/
|
@*/
|
||||||
Ox.forEach = function(collection, iterator, that) {
|
Ox.forEach = function(collection, iterator, that) {
|
||||||
var i = 0, key, type = Ox.typeOf(collection);
|
var i = 0, key, type = Ox.typeOf(collection);
|
||||||
if (type != 'array' && type != 'object') {
|
|
||||||
collection = Ox.toArray(collection);
|
|
||||||
}
|
|
||||||
try {
|
try {
|
||||||
if (type == 'object') {
|
if (type == 'object' || type == 'storage') {
|
||||||
for (key in collection) {
|
for (key in collection) {
|
||||||
if (Ox.hasOwn(collection, key)) {
|
if (Ox.hasOwn(collection, key)) {
|
||||||
// iterator.call(that, collection[key], key, collection);
|
// iterator.call(that, collection[key], key, collection);
|
||||||
|
@ -188,6 +185,7 @@ Ox.forEach = function(collection, iterator, that) {
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
collection = Ox.toArray(collection);
|
||||||
for (i = 0; i < collection.length; i++) {
|
for (i = 0; i < collection.length; i++) {
|
||||||
if (i in collection) {
|
if (i in collection) {
|
||||||
// iterator.call(that, collection[i], i, collection);
|
// iterator.call(that, collection[i], i, collection);
|
||||||
|
@ -270,7 +268,7 @@ Ox.isEmpty = function(value) {
|
||||||
};
|
};
|
||||||
|
|
||||||
/*@
|
/*@
|
||||||
Ox.len <f> Returns the length of an array, node list, object or string
|
Ox.len <f> Returns the length of an array, nodelist, object, storage or string
|
||||||
Not to be confused with `Ox.length`, which is the `length` property of the
|
Not to be confused with `Ox.length`, which is the `length` property of the
|
||||||
`Ox` function (`1`).
|
`Ox` function (`1`).
|
||||||
> Ox.len((function() { return arguments; }(1, 2, 3)))
|
> Ox.len((function() { return arguments; }(1, 2, 3)))
|
||||||
|
@ -283,6 +281,8 @@ Ox.len <f> Returns the length of an array, node list, object or string
|
||||||
'number'
|
'number'
|
||||||
> Ox.len({a: 1, b: 2, c: 3})
|
> Ox.len({a: 1, b: 2, c: 3})
|
||||||
3
|
3
|
||||||
|
> Ox.typeOf(Ox.len(localStorage))
|
||||||
|
'number'
|
||||||
> Ox.len('abc')
|
> Ox.len('abc')
|
||||||
3
|
3
|
||||||
> Ox.len(function(a, b, c) {})
|
> Ox.len(function(a, b, c) {})
|
||||||
|
@ -296,7 +296,7 @@ Ox.len = function(collection) {
|
||||||
|| type == 'nodelist' || type == 'string'
|
|| type == 'nodelist' || type == 'string'
|
||||||
) {
|
) {
|
||||||
ret = collection.length;
|
ret = collection.length;
|
||||||
} else if (type == 'object') {
|
} else if (type == 'object' || type == 'storage') {
|
||||||
ret = Object.keys(collection).length;
|
ret = Object.keys(collection).length;
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
|
@ -317,7 +317,7 @@ Ox.map <f> Transforms the values of an array, object or string
|
||||||
@*/
|
@*/
|
||||||
Ox.map = function(collection, iterator, that) {
|
Ox.map = function(collection, iterator, that) {
|
||||||
var ret, type = Ox.typeOf(collection);
|
var ret, type = Ox.typeOf(collection);
|
||||||
if (type == 'object') {
|
if (type == 'object' || type == 'storage') {
|
||||||
ret = {};
|
ret = {};
|
||||||
Ox.forEach(collection, function(value, key) {
|
Ox.forEach(collection, function(value, key) {
|
||||||
ret[key] = iterator.call(that, value, key, collection);
|
ret[key] = iterator.call(that, value, key, collection);
|
||||||
|
@ -397,7 +397,7 @@ Ox.shuffle <f> Randomizes the order of values within a collection
|
||||||
@*/
|
@*/
|
||||||
Ox.shuffle = function(collection) {
|
Ox.shuffle = function(collection) {
|
||||||
var keys, ret, type = Ox.typeOf(collection), values;
|
var keys, ret, type = Ox.typeOf(collection), values;
|
||||||
if (type == 'object') {
|
if (type == 'object' || type == 'storage') {
|
||||||
keys = Object.keys(collection);
|
keys = Object.keys(collection);
|
||||||
values = Ox.shuffle(Ox.values(collection));
|
values = Ox.shuffle(Ox.values(collection));
|
||||||
ret = {};
|
ret = {};
|
||||||
|
@ -517,6 +517,8 @@ Ox.values <f> Returns the values of a collection
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
> Ox.values({a: 1, b: 2, c: 3})
|
> Ox.values({a: 1, b: 2, c: 3})
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
|
> Ox.typeOf(Ox.values(localStorage))
|
||||||
|
'array'
|
||||||
> Ox.values('abc')
|
> Ox.values('abc')
|
||||||
['a', 'b', 'c']
|
['a', 'b', 'c']
|
||||||
> Ox.values([1,,3])
|
> Ox.values([1,,3])
|
||||||
|
@ -526,7 +528,7 @@ Ox.values = function(collection) {
|
||||||
var ret, type = Ox.typeOf(collection);
|
var ret, type = Ox.typeOf(collection);
|
||||||
if (type == 'array') {
|
if (type == 'array') {
|
||||||
ret = Ox.clone(collection);
|
ret = Ox.clone(collection);
|
||||||
} else if (type == 'object') {
|
} else if (type == 'object' || type == 'storage') {
|
||||||
ret = [];
|
ret = [];
|
||||||
Ox.forEach(collection, function(value) {
|
Ox.forEach(collection, function(value) {
|
||||||
ret.push(value);
|
ret.push(value);
|
||||||
|
|
Loading…
Reference in a new issue