-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest.js
170 lines (146 loc) · 7.07 KB
/
test.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
162
163
164
165
166
167
168
169
170
/*!
* expand-range <https://github.com/jonschlinkert/expand-range>
*
* Copyright (c) 2014-2015, Jon Schlinkert.
* Licensed under the MIT License
*/
'use strict';
require('mocha');
var assert = require('assert');
var expand = require('./');
describe('expand range', function() {
it('should return the number as an array if no range is specified', function() {
assert.deepEqual(expand('1'), ['1']);
});
it('should throw when the first arg is not a string.', function() {
assert.throws(function() {
expand();
}, /expand-range expects a string/);
});
it('should expand numerical ranges', function() {
assert.deepEqual(expand('1..3'), ['1', '2', '3']);
assert.deepEqual(expand('5..8'), ['5', '6', '7', '8']);
});
it('should expand negative ranges', function() {
assert.deepEqual(expand('0..-5'), ['0', '-1', '-2', '-3', '-4', '-5']);
});
it('should expand alphabetical ranges', function() {
assert.deepEqual(expand('a..e'), ['a', 'b', 'c', 'd', 'e']);
assert.deepEqual(expand('A..E'), ['A', 'B', 'C', 'D', 'E']);
});
it('should fill in numerical ranges', function() {
assert.deepEqual(expand('1..3'), ['1', '2', '3']);
assert.deepEqual(expand('5..8'), ['5', '6', '7', '8']);
});
it('should fill in numerical ranges when numbers are passed as strings', function() {
assert.deepEqual(expand('1..3'), ['1', '2', '3']);
assert.deepEqual(expand('5..8'), ['5', '6', '7', '8']);
});
it('should fill in negative ranges', function() {
assert.deepEqual(expand('0..-5'), [ '0', '-1', '-2', '-3', '-4', '-5' ]);
assert.deepEqual(expand('0..-5'), [ '0', '-1', '-2', '-3', '-4', '-5' ]);
assert.deepEqual(expand('9..-4'), [ '9', '8', '7', '6', '5', '4', '3', '2', '1', '0', '-1', '-2', '-3', '-4' ]);
assert.deepEqual(expand('-10..-1'), [ '-10', '-9', '-8', '-7', '-6', '-5', '-4', '-3', '-2', '-1' ]);
});
it('should fill in rangines using the given increment', function() {
assert.deepEqual(expand('1..10'), [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]);
assert.deepEqual(expand('1..10..1'), [ '1', '2', '3', '4', '5', '6', '7', '8', '9', '10' ]);
assert.deepEqual(expand('1..10..2'), [ '1', '3', '5', '7', '9' ]);
assert.deepEqual(expand('1..10..2'), [ '1', '3', '5', '7', '9' ]);
assert.deepEqual(expand('1..10..2'), [ '1', '3', '5', '7', '9' ]);
assert.deepEqual(expand('1..20..2'), [ '1', '3', '5', '7', '9', '11', '13', '15', '17', '19' ]);
assert.deepEqual(expand('1..20..20'), [ '1' ]);
assert.deepEqual(expand('10..1..-2'), [ '10', '8', '6', '4', '2' ]);
assert.deepEqual(expand('10..1..2'), [ '10', '8', '6', '4', '2' ]);
assert.deepEqual(expand('1..9'), [ '1', '2', '3', '4', '5', '6', '7', '8', '9' ]);
assert.deepEqual(expand('2..10..2'), [ '2', '4', '6', '8', '10' ]);
assert.deepEqual(expand('2..10..1'), [ '2', '3', '4', '5', '6', '7', '8', '9', '10' ]);
assert.deepEqual(expand('2..10..2'), [ '2', '4', '6', '8', '10' ]);
assert.deepEqual(expand('2..10..3'), [ '2', '5', '8' ]);
});
it('should fill in negative ranges using the given increment', function() {
assert.deepEqual(expand('-1..-10..-2'), [ '-1', '-3', '-5', '-7', '-9' ]);
assert.deepEqual(expand('-1..-10..2'), [ '-1', '-3', '-5', '-7', '-9' ]);
assert.deepEqual(expand('10..1..-2'), [ '10', '8', '6', '4', '2' ]);
assert.deepEqual(expand('-10..-2..2'), [ '-10', '-8', '-6', '-4', '-2' ]);
assert.deepEqual(expand('-2..-10..1'), [ '-2', '-3', '-4', '-5', '-6', '-7', '-8', '-9', '-10' ]);
assert.deepEqual(expand('-2..-10..2'), [ '-2', '-4', '-6', '-8', '-10' ]);
assert.deepEqual(expand('-2..-10..3'), [ '-2', '-5', '-8' ]);
assert.deepEqual(expand('-9..9..3'), [ '-9', '-6', '-3', '0', '3', '6', '9' ]);
});
it('should fill in negative ranges using the given increment', function() {
assert.deepEqual(expand('1..10..2'), [ '1', '3', '5', '7', '9' ]);
assert.deepEqual(expand('-1..-10..2'), [ '-1', '-3', '-5', '-7', '-9' ]);
assert.deepEqual(expand('-1..-10..-2'), [ '-1', '-3', '-5', '-7', '-9' ]);
assert.deepEqual(expand('10..1..-2'), [ '10', '8', '6', '4', '2' ]);
assert.deepEqual(expand('10..1..2'), [ '10', '8', '6', '4', '2' ]);
assert.deepEqual(expand('1..20..2'), [ '1', '3', '5', '7', '9', '11', '13', '15', '17', '19' ]);
assert.deepEqual(expand('1..20..20'), [ '1' ]);
});
it('should fill in alphabetical ranges', function() {
assert.deepEqual(expand('a..e'), ['a', 'b', 'c', 'd', 'e']);
assert.deepEqual(expand('A..E'), ['A', 'B', 'C', 'D', 'E']);
assert.deepEqual(expand('E..A'), ['E', 'D', 'C', 'B', 'A']);
});
it('should use increments with alphabetical ranges', function() {
assert.deepEqual(expand('a..e..2'), ['a','c', 'e']);
assert.deepEqual(expand('E..A..2'), ['E', 'C', 'A']);
});
});
describe('character classes:', function() {
it('should return a string for a regex range when `true` is passed:', function() {
assert.deepEqual(expand('a..e', true), '[a-e]');
assert.deepEqual(expand('0..9', true), '[0-9]');
assert.deepEqual(expand('A..Z..5', true), '(A|F|K|P|U|Z)');
assert.deepEqual(expand('E..A', true), '[A-E]');
});
it('should flip order when a sequential range is backwards', function() {
assert.deepEqual(expand('1023..1021', true), '102[1-3]');
});
it('should return the string when arguments are invalid:', function() {
assert.equal(expand('1..1..2..1'), '1..1..2..1');
});
it('should return null for invalid patterns:', function() {
assert.deepEqual(expand('1.1..2.1'), []);
assert.deepEqual(expand('1.2..2'), []);
assert.deepEqual(expand('1.20..2'), []);
assert.deepEqual(expand('1..0f'), []);
assert.deepEqual(expand('1..10..ff'), []);
assert.deepEqual(expand('1..10.f'), []);
assert.deepEqual(expand('1..10f'), []);
assert.deepEqual(expand('1..20..2f'), []);
assert.deepEqual(expand('1..20..f2'), []);
assert.deepEqual(expand('1..2f..2'), []);
assert.deepEqual(expand('1..ff..2'), []);
assert.deepEqual(expand('1..ff'), []);
});
});
describe('when a custom function is used for expansions', function() {
it('should expose the current value being iterated over', function() {
var res = expand('1..5', function(val, a, b, step, idx, arr, options) {
return String(val);
});
assert.deepEqual(res, ['1', '2', '3', '4', '5']);
});
it('should expose options', function() {
var res = expand('001..003', function(val, a, b, step, idx, arr, options) {
return Array(options.maxLength - String(val).length).join('0') + val;
});
assert.deepEqual(res, ['001', '002', '003']);
});
it('should expose options.isNumber', function() {
var res = expand('a..e', function(val, a, b, step, idx, arr, options) {
if (options.isNumber) {
return String.fromCharCode(val);
}
return val;
});
assert.deepEqual(res, ['a', 'b', 'c', 'd', 'e']);
});
it('should expose the index', function() {
var res = expand('a..e', function(val, a, b, step, idx, arr, options) {
return String.fromCharCode(String(a)) + (idx - 1);
});
assert.deepEqual(res, ['a0', 'b1', 'c2', 'd3', 'e4']);
});
});