Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (75 loc) · 2.15 KB
/
Copy pathindex.js
File metadata and controls
81 lines (75 loc) · 2.15 KB
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
var test = require('../util/test')(__filename);
var partition = require('../../packages/array-partition');
// import partition from 'just-partition';
// partition([1, 5, 2, 4, 3], n => n > 3); // [[5, 4],[1, 2, 3]]
// partition(['a', 2, 3, '3'], x => typeof x == 'string'); // [['a', '3'],[2, 3]]
// partition([1, 2, 3, 4], x => typeof x == 'number'); // [[1, 2, 3, 4],[]]
// partition([1, 2, 3, 4], x => typeof x == 'string'); // [[], [1, 2, 3, 4]]
// partition([], n => n > 3); // [[], []]
// partition({a: 1, b: 2}, n => n > 1); // throws
// partition(null, n => n > 1); // throws
// partition(undefined, n => n > 1); // throws
test('populated arrays', function(t) {
t.plan(4);
var result;
result = partition([1, 5, 2, 4, 3], function(n) {
return n > 3;
});
t.deepEqual(result, [[5, 4], [1, 2, 3]]);
result = partition(['a', 2, 3, '3'], function(x) {
return typeof x == 'string';
});
t.deepEqual(result, [['a', '3'], [2, 3]]);
result = partition([1, 2, 3, 4], function(x) {
return typeof x == 'number';
});
t.deepEqual(result, [[1, 2, 3, 4], []]);
result = partition([1, 2, 3, 4], function(x) {
return typeof x == 'string';
});
t.deepEqual(result, [[], [1, 2, 3, 4]]);
t.end();
});
test('sparse arrays', function(t) {
t.plan(2);
var result;
result = partition([1, 5, 2, , , 4, 3], function(n) {
return n > 3;
});
t.deepEqual(result, [[5, 4], [1, 2, undefined, undefined, 3]]);
result = partition([1, 2, 3, , , 4], function(x) {
return typeof x == 'undefined';
});
t.deepEqual(result, [[undefined, undefined], [1, 2, 3, 4]]);
t.end();
});
test('empty array', function(t) {
t.plan(1);
var result = partition([], function(n) {
return n > 3;
});
t.deepEqual(result, [[], []]);
t.end();
});
test('illegal arguments', function(t) {
t.plan(4);
t.throws(function() {
partition({a: 1, b: 2}, function(n) {
return n > 3;
});
});
t.throws(function() {
partition(null, function(n) {
return n > 3;
});
});
t.throws(function() {
partition(undefined, function(n) {
return n > 3;
});
});
t.throws(function() {
partition([1, 2, 3], []);
});
t.end();
});