Skip to content

Commit 29c8d32

Browse files
committedFeb 21, 2017
Merge branch 'master' of github.com:remy/undefsafe
* 'master' of github.com:remy/undefsafe: feat: * rule returns all matches (#7)
2 parents 9a1631a + 2d38e72 commit 29c8d32

File tree

2 files changed

+33
-10
lines changed

2 files changed

+33
-10
lines changed
 

‎lib/undefsafe.js

+16-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
function undefsafe(obj, path, value) {
3+
function undefsafe(obj, path, value, __res) {
44

55
// I'm not super keen on this private function, but it's because
66
// it'll also be use in the browser and I wont *one* function exposed
@@ -69,17 +69,26 @@ function undefsafe(obj, path, value) {
6969
if (key === '*') {
7070
// loop through each property
7171
var prop = '';
72+
var res = __res || [];
7273

7374
for (prop in parent) {
74-
var shallowObj = undefsafe(obj[prop], parts.slice(i + 1).join('.'), value);
75-
if (shallowObj) {
76-
if ((value && shallowObj === value) || (!value)) {
77-
return shallowObj;
75+
var shallowObj = undefsafe(obj[prop], parts.slice(i + 1).join('.'), value, res);
76+
if (shallowObj && shallowObj !== res) {
77+
if ((value && shallowObj === value) || (value === undefined)) {
78+
if (value !== undefined) {
79+
return shallowObj;
80+
}
81+
82+
res.push(shallowObj);
7883
}
7984
}
8085
}
81-
return undefined;
82-
key = prop;
86+
87+
if (res.length === 0) {
88+
return undefined;
89+
}
90+
91+
return res;
8392
}
8493

8594
obj = obj[key];

‎test/star-rule.test.js

+17-3
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,23 @@ var fixture = {
1818
]
1919
};
2020

21+
test('2.0.0: match all.*', function (t) {
22+
var res = undefsafe(fixture, '*.*.*.1');
23+
t.deepEqual(res, ['two', 'four']);
24+
t.end();
25+
});
26+
27+
28+
test('2.0.0: match all.*', function (t) {
29+
var res = undefsafe(fixture, 'commits.*.modified.*.b');
30+
t.deepEqual(res, ['one', 'two', 'two', 'four']);
31+
t.end();
32+
});
33+
34+
2135
test('get value on first * selector', function (t) {
22-
var res = undefsafe(fixture, 'commits.*.modified.*');
23-
t.equal(res, 'one');
36+
var res = undefsafe(fixture, 'commits.*.modified.0');
37+
t.deepEqual(res, ['one', 'two']);
2438
t.end();
2539
});
2640

@@ -44,7 +58,7 @@ test('match * selector returns undefined', function (t) {
4458
});
4559

4660
test('match * selector works on objects', function (t) {
47-
var res = undefsafe(fixture, '*.*.modified.*');
61+
var res = undefsafe(fixture, '*.*.modified.*', 'one');
4862
t.equal(res, 'one');
4963
t.end();
5064
});

0 commit comments

Comments
 (0)
Please sign in to comment.