This repository was archived by the owner on Oct 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget.js
84 lines (73 loc) · 2.65 KB
/
get.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const partial = require('../lib/partial')
, expect = require('must')
, each = require('../lib/each')
, get = require('../lib/get')
describe('`get`', function() {
describe('when given two parameters: `map` and `key`;', function() {
describe('and when `map` has `key`', function() {
it('should return its value', function () {
const map = { foo : 'wibble' }
expect(get(map, 'foo')).to.equal('wibble')
})
})
describe('and when the value of `map[key]` is logically false', function() {
it('should still return the value of `key`', function() {
each([null, undefined, false], function(x) {
const map = { foo: x }
expect(get(map, 'foo')).to.equal(x)
})
})
})
describe('but when `map` does not have `key`', function() {
it('should return `undefined`', function () {
const map = { foo : 'wibble' }
expect(get(map, 'bar')).to.equal(undefined)
})
})
describe('or when `map` is logically false', function() {
it('should return `undefined`', function() {
each([null, false, undefined], function(nil) {
expect(get(nil, 'whatever')).to.equal(undefined)
})
})
})
})
describe('when given three parameters: `map`, `key`, and `notFound`;', function() {
const nope = '¯\(º_o)/¯'
describe('and when `map` has `key`', function() {
it('should return its value', function () {
const map = { foo : 'wibble' }
expect(get(map, 'foo', nope)).to.equal('wibble')
})
})
describe('and when the value of `key` is logically false', function() {
it('should still return the value of `key`', function() {
each([null, undefined, false], function(x) {
const map = { foo: x }
expect(get(map, 'foo', nope)).to.equal(x)
})
})
})
describe('but when `map` does not have `key`', function() {
it('should return `notFound`', function () {
const map = { foo : 'wibble' }
expect(get(map, 'bar', nope)).to.equal(nope)
})
})
describe('or when `map` is logically false', function() {
it('should return `notFound`', function() {
each([null, false, undefined], function(nil) {
expect(get(nil, 'whatever', nope)).to.equal(nope)
})
})
})
})
describe('when given an array as `key`', function() {
it('should work recursively', function() {
const map = { a: { b: { c: { d: 'wibble' } } } }
, notFound = {}
expect(get(map, ['a', 'b', 'c', 'd'])).to.equal('wibble')
expect(get(map, ['a', 'b', 'c', 'x'], notFound)).to.equal(notFound)
})
})
})