Collection.js: handle localStorage object

This commit is contained in:
rolux 2012-06-17 16:41:39 +02:00
parent d793d7857d
commit 175c8b23d0

View file

@ -130,7 +130,7 @@ Ox.filter <f> Filters a collection by a given condition
Ox.filter = function(collection, iterator, that) {
var ret, type = Ox.typeOf(collection);
iterator = iterator || Ox.identity;
if (type == 'object') {
if (type == 'object' || type == 'storage') {
ret = {};
Ox.forEach(collection, function(value, key) {
if (iterator.call(that, value, key, collection)) {
@ -172,11 +172,8 @@ Ox.forEach <f> forEach loop
@*/
Ox.forEach = function(collection, iterator, that) {
var i = 0, key, type = Ox.typeOf(collection);
if (type != 'array' && type != 'object') {
collection = Ox.toArray(collection);
}
try {
if (type == 'object') {
if (type == 'object' || type == 'storage') {
for (key in collection) {
if (Ox.hasOwn(collection, key)) {
// iterator.call(that, collection[key], key, collection);
@ -188,6 +185,7 @@ Ox.forEach = function(collection, iterator, that) {
i++;
}
} else {
collection = Ox.toArray(collection);
for (i = 0; i < collection.length; i++) {
if (i in 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
`Ox` function (`1`).
> 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'
> Ox.len({a: 1, b: 2, c: 3})
3
> Ox.typeOf(Ox.len(localStorage))
'number'
> Ox.len('abc')
3
> Ox.len(function(a, b, c) {})
@ -296,7 +296,7 @@ Ox.len = function(collection) {
|| type == 'nodelist' || type == 'string'
) {
ret = collection.length;
} else if (type == 'object') {
} else if (type == 'object' || type == 'storage') {
ret = Object.keys(collection).length;
}
return ret;
@ -317,7 +317,7 @@ Ox.map <f> Transforms the values of an array, object or string
@*/
Ox.map = function(collection, iterator, that) {
var ret, type = Ox.typeOf(collection);
if (type == 'object') {
if (type == 'object' || type == 'storage') {
ret = {};
Ox.forEach(collection, function(value, key) {
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) {
var keys, ret, type = Ox.typeOf(collection), values;
if (type == 'object') {
if (type == 'object' || type == 'storage') {
keys = Object.keys(collection);
values = Ox.shuffle(Ox.values(collection));
ret = {};
@ -517,6 +517,8 @@ Ox.values <f> Returns the values of a collection
[1, 2, 3]
> Ox.values({a: 1, b: 2, c: 3})
[1, 2, 3]
> Ox.typeOf(Ox.values(localStorage))
'array'
> Ox.values('abc')
['a', 'b', 'c']
> Ox.values([1,,3])
@ -526,7 +528,7 @@ Ox.values = function(collection) {
var ret, type = Ox.typeOf(collection);
if (type == 'array') {
ret = Ox.clone(collection);
} else if (type == 'object') {
} else if (type == 'object' || type == 'storage') {
ret = [];
Ox.forEach(collection, function(value) {
ret.push(value);