UNPKG

1.24 kBJavaScriptView Raw
1/*!
2 * expand-range <https://github.com/jonschlinkert/expand-range>
3 *
4 * Copyright (c) 2014-2015, 2017, Jon Schlinkert.
5 * Released under the MIT License.
6 */
7
8'use strict';
9
10var extend = require('extend-shallow');
11var fill = require('fill-range');
12
13module.exports = function expandRange(str, options, fn) {
14 if (typeof str !== 'string') {
15 throw new TypeError('expand-range expects a string.');
16 }
17
18 if (typeof options === 'function') {
19 fn = options;
20 options = undefined;
21 }
22
23 // shallow clone options, to ensure we
24 // don't mutate the object upstream
25 var opts = extend({}, options);
26
27 if (typeof fn === 'function') {
28 opts.transform = fn;
29 }
30
31 if (typeof options === 'boolean') {
32 opts.makeRe = true;
33 }
34
35 // create arguments to pass to fill-range
36 var segs = str.split('..');
37 var len = segs.length;
38 if (len > 3) { return str; }
39
40 // if only one segment, it can't expand so return it
41 if (len === 1) { return segs; }
42
43 // if fn is "true", tell fill-range to regexify the string
44 if (fn === true) {
45 opts.toRegex = true;
46 fn = undefined;
47 }
48
49 // wrap the result in parentheses, when regexified and necessary
50 opts.capture = true;
51 segs.push(opts);
52
53 return fill.apply(null, segs);
54};