Skip to content

Commit

Permalink
test negative indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcus Haddon committed Dec 20, 2019
1 parent d238318 commit 4baa491
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,36 @@ test('get value - simple array defined index', function (t) {
t.end();
});

test('get value - simple array negative index', function (t) {
var obj = ['foo', 'bar'];
var map = '[-1]';
var expect = 'bar';
var result = om.getKeyValue(obj, map);

t.deepEqual(result, expect);
t.end();
});

test('get value - simple array dot property', function (t) {
var obj = [{ name: 'foo' }, { name: 'bar' }];
var map = '[-1].name';
var expect = 'bar';
var result = om.getKeyValue(obj, map);

t.deepEqual(result, expect);
t.end();
});

test('get value - simple array negative index falls off array', function (t) {
var obj = ['foo', 'bar'];
var map = '[-3]';
var expect = null;
var result = om.getKeyValue(obj, map);

t.deepEqual(result, expect);
t.end();
});

test('get value - two levels deep', function (t) {
var key = 'foo.baz.fog';

Expand Down Expand Up @@ -384,6 +414,28 @@ test('get value - crazy', function (t) {
t.end();
});

test('get value - crazy negative', function (t) {
var key = 'foo.baz[-1].fog[1].baz';

var obj = {
"foo": {
"baz": [{
"fog": [, {
"baz": "bar"
}]
}]
}
};

var expect = "bar";

var result = om.getKeyValue(obj, key);

t.deepEqual(result, expect);
t.end();
});


test('select with array object where map is not an array 1', function (t) {
var obj = { foo: [{bar: 'a'}, {bar: 'b'}, {bar: 'c'}] }
var map = 'foo.bar'
Expand Down

0 comments on commit 4baa491

Please sign in to comment.