-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathtest-map.js
141 lines (116 loc) · 3.68 KB
/
test-map.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
QUnit.test("map num array", function (assert) {
var data = [1, 2, 3, 4];
var result = Stream(data)
.map(function (num) {
return "obj" + num;
})
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0], 'obj1');
assert.equal(result[1], 'obj2');
assert.equal(result[2], 'obj3');
assert.equal(result[3], 'obj4');
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0], 1);
assert.equal(data[1], 2);
assert.equal(data[2], 3);
assert.equal(data[3], 4);
});
QUnit.test("map object array", function (assert) {
var data = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
var result = Stream(data)
.map(function (obj) {
return {b: obj.a};
})
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0].b, 1);
assert.equal(result[1].b, 2);
assert.equal(result[2].b, 3);
assert.equal(result[3].b, 4);
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0].a, 1);
assert.equal(data[1].a, 2);
assert.equal(data[2].a, 3);
assert.equal(data[3].a, 4);
});
QUnit.test("map empty array", function (assert) {
var result = Stream([])
.map(function (num) {
return "obj" + num;
})
.toArray();
assert.equal(result.length, 0);
});
QUnit.test("map with null", function (assert) {
var data = [1, null, undefined, 4];
var result = Stream(data)
.map(function (val) {
return "map_" + val;
})
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0], 'map_1');
assert.equal(result[1], 'map_null');
assert.equal(result[2], 'map_undefined');
assert.equal(result[3], 'map_4');
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0], 1);
assert.equal(data[1], null);
assert.equal(data[2], undefined);
assert.equal(data[3], 4);
});
QUnit.test("map via path (depth 1)", function (assert) {
var data = [{a: 1}, {a: 2}, {a: 3}, {a: 4}];
var result = Stream(data)
.map("a")
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0], 1);
assert.equal(result[1], 2);
assert.equal(result[2], 3);
assert.equal(result[3], 4);
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0].a, 1);
assert.equal(data[1].a, 2);
assert.equal(data[2].a, 3);
assert.equal(data[3].a, 4);
});
QUnit.test("map via path (depth 3)", function (assert) {
var data = [{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}, {a: {b: 4}}];
var result = Stream(data)
.map("a.b")
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0], 1);
assert.equal(result[1], 2);
assert.equal(result[2], 3);
assert.equal(result[3], 4);
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0].a.b, 1);
assert.equal(data[1].a.b, 2);
assert.equal(data[2].a.b, 3);
assert.equal(data[3].a.b, 4);
});
QUnit.test("map via path (depth 3)", function (assert) {
var data = [{a: {b: {c: 1}}}, {a: {b: {c: 2}}}, {a: {b: {c: 3}}}, {a: {b: {c: 4}}}];
var result = Stream(data)
.map("a.b.c")
.toArray();
assert.equal(result.length, 4);
assert.equal(result[0], 1);
assert.equal(result[1], 2);
assert.equal(result[2], 3);
assert.equal(result[3], 4);
// assert original data is untouched
assert.equal(data.length, 4);
assert.equal(data[0].a.b.c, 1);
assert.equal(data[1].a.b.c, 2);
assert.equal(data[2].a.b.c, 3);
assert.equal(data[3].a.b.c, 4);
});