Skip to content

Commit 9bc79dd

Browse files
fix(toArrayFilter): handle non-objects
Closes #6 Closes #7
1 parent 8af2535 commit 9bc79dd

File tree

3 files changed

+32
-16
lines changed

3 files changed

+32
-16
lines changed

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ The filter iterates over the object's keys and creates an array containing the v
5858
property. By default, the filter also attaches a new property `$key` to the value containing
5959
the original key that was used in the object we are iterating over to reference the property.
6060

61+
6162
## Not adding the `$key` property
6263

6364
If you don't want the `$key` property to be attached to each of the property values, you simply
@@ -69,17 +70,24 @@ put an additional parameter on the `toArray` filter:
6970
</div>
7071
```
7172

73+
## Using `$key` with non-objects
74+
75+
Non-objects such as strings and numbers cannot have a new `$key` property attached to them.
76+
If the object properties you are iterating over are not objects then you must either disable
77+
the `$key` property or the filter will replace the non-object with a new object of the form:
78+
`{ $key: key, $value: value }`.
79+
80+
7281
## Caveats
7382

7483
There are always issues when trying to iterate over properties in JavaScript and the `toArray`
7584
filter has its own set of things to be aware of when using it:
7685

7786
* It only works with plain Objects - don't try to filter arrays and strings with it.
7887
* If you don't disable it, the filter will modify each property value with a new `$key` property.
79-
* If the object you are iterating over contains values that are not objects then you must disable
80-
the `$key` property or it will error - non-objects cannot have properties attached to them.
8188
* This filter is not compatible with IE8. (It uses `Object.keys` and `Object.defineProperty` which
82-
don't work well or at all in Internet Explorer 8 (IE8).
89+
don't work well or at all in Internet Explorer 8 (IE8).
90+
8391

8492
## Example
8593

test/toArrayFilter.spec.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,20 @@ describe("toArrayFilter", function() {
4646
}));
4747

4848

49+
it("should create new objects ({$key:...,$value:...}) from non-object properties if `addKey` is not false", inject(function(toArrayFilter) {
50+
var obj = {
51+
a: 'A',
52+
b: 'B',
53+
c: 'C'
54+
};
55+
expect(toArrayFilter(obj)).toEqual([
56+
{ $key: 'a', $value: 'A' },
57+
{ $key: 'b', $value: 'B' },
58+
{ $key: 'c', $value: 'C' }
59+
]);
60+
}));
61+
62+
4963
it("should work with an array", inject(function(toArrayFilter) {
5064
var obj = [
5165
{ name: 'A' },
@@ -62,17 +76,8 @@ describe("toArrayFilter", function() {
6276
it("should handle invalid inputs", inject(function(toArrayFilter) {
6377
expect(toArrayFilter(undefined)).toBeUndefined();
6478
expect(toArrayFilter(null)).toBe(null);
79+
expect(toArrayFilter('some string')).toBe('some string');
80+
expect(toArrayFilter(12345)).toBe(12345);
6581
}));
6682

67-
it("should not work with non-objects", inject(function(toArrayFilter) {
68-
var obj = 'some string';
69-
expect(function() {
70-
toArrayFilter(obj);
71-
}).toThrow();
72-
73-
obj = 12345;
74-
expect(function() {
75-
toArrayFilter(obj);
76-
}).toThrow();
77-
}));
7883
});

toArrayFilter.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ angular.module('angular-toArrayFilter', [])
22

33
.filter('toArray', function () {
44
return function (obj, addKey) {
5-
if (!obj) return obj;
5+
if (!angular.isObject(obj)) return obj;
66
if ( addKey === false ) {
77
return Object.keys(obj).map(function(key) {
88
return obj[key];
99
});
1010
} else {
1111
return Object.keys(obj).map(function (key) {
12-
return Object.defineProperty(obj[key], '$key', { enumerable: false, value: key});
12+
var value = obj[key];
13+
return angular.isObject(value) ?
14+
Object.defineProperty(value, '$key', { enumerable: false, value: key}) :
15+
{ $key: key, $value: value };
1316
});
1417
}
1518
};

0 commit comments

Comments
 (0)