-
Notifications
You must be signed in to change notification settings - Fork 1
/
get.js
161 lines (137 loc) · 4.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/**
* -----------------------------------------------------------------------------
* VITALS UNIT TESTS: vitals.get
* -----------------------------------------------------------------------------
* @section base
* @see [vitals.get docs](https://github.com/imaginate/vitals/wiki/vitals.get)
* @see [test api](https://github.com/imaginate/vitals/blob/master/test/setup/interface.js)
* @see [test helpers](https://github.com/imaginate/vitals/blob/master/test/setup/helpers.js)
*
* @author Adam Smith <adam@imaginate.life> (https://github.com/imaginate)
* @copyright 2017 Adam A Smith <adam@imaginate.life> (https://github.com/imaginate)
*
* Annotations:
* @see [JSDoc3](http://usejsdoc.org)
* @see [Closure Compiler JSDoc Syntax](https://developers.google.com/closure/compiler/docs/js-for-compiler)
*/
method('get', function() {
should('return array of all keys', function() {
test('<object>', function() {
var obj = { a: 1, b: 2, c: 3 };
var keys = vitals.get(obj);
assert( is.arr(keys) );
assert( hasVal(keys, 'a') );
assert( hasVal(keys, 'b') );
assert( hasVal(keys, 'c') );
assert( keys.length === 3 );
});
});
should('return array of values where key matches val', function() {
test('<object>', /^[0-9]$/, function() {
var obj = {
'a': 1, 'b': 2,
'3': 'c', '4': 'd'
};
var vals = vitals.get(obj, /^[0-9]$/);
assert( is.arr(vals) );
assert( hasVal(vals, 'c') );
assert( hasVal(vals, 'd') );
assert( vals.length === 2 );
});
});
should('return array of keys where value === val', function() {
test('<object>', 2, function() {
var obj = { a: 1, b: 2, c: 3 };
var keys = vitals.get(obj, 2);
assert( is.arr(keys) );
assert( keys[0] === 'b' );
assert( keys.length === 1 );
});
});
should('return array of all indexes', function() {
test('<array>', function() {
var arr = [ 'a', 'b', 'c' ];
var keys = vitals.get(arr);
assert( is.arr(keys) );
assert( keys[0] === 0 );
assert( keys[1] === 1 );
assert( keys[2] === 2 );
assert( keys.length === 3 );
});
});
should('return array of indexes where value === val', function() {
test('<array>', 2, function() {
var arr = [ 1, 2, 1, 2 ];
var keys = vitals.get(arr, 2);
assert( is.arr(keys) );
assert( keys[0] === 1 );
assert( keys[1] === 3 );
assert( keys.length === 2 );
});
});
should('return array of strings where substring matches val', function() {
test('abc123', /[a-z]/, function() {
var vals = vitals.get('abc123', /[a-z]/);
assert( is.arr(vals) );
assert( vals[0] === 'a' );
assert( vals[1] === 'b' );
assert( vals[2] === 'c' );
assert( vals.length === 3 );
});
});
should('return an array of indexes where substring matches val', function() {
test('abc123', 1, function() {
var keys = vitals.get('abc123', 1);
assert( is.arr(keys) );
assert( keys[0] === 3 );
assert( keys.length === 1 );
});
test('abc123abc123', 1, function() {
var keys = vitals.get('abc123abc123', 1);
assert( is.arr(keys) );
assert( keys[0] === 3 );
assert( keys[1] === 9 );
assert( keys.length === 2 );
});
test('abc123', 5, function() {
var keys = vitals.get('abc123', 5);
assert( is.arr(keys) );
assert( keys.length === 0 );
});
test('abc123abc123', 'a', function() {
var keys = vitals.get('abc123abc123', 'a');
assert( is.arr(keys) );
assert( keys[0] === 0 );
assert( keys[1] === 6 );
assert( keys.length === 2 );
});
test('abc*123', '*', function() {
var keys = vitals.get('abc*123', '*');
assert( is.arr(keys) );
assert( keys[0] === 3 );
assert( keys.length === 1 );
});
test('abc*123', '.*', function() {
var keys = vitals.get('abc*123', '.*');
assert( is.arr(keys) );
assert( keys.length === 0 );
});
});
should('throw an error', function() {
test(function() {
assert.throws(function() {
vitals.get();
}, validTypeErr);
});
test(null, function() {
assert.throws(function() {
vitals.get(null);
}, validTypeErr);
});
test('str', function() {
assert.throws(function() {
vitals.get('str');
}, validErr);
});
});
});