Skip to content

Commit

Permalink
use const and let (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmywarting authored Nov 4, 2021
1 parent 68c0e37 commit dd5a4cb
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 77 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# brace-expansion

[Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html),
[Brace expansion](https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html),
as known from sh/bash, in JavaScript.

[![CI](https://github.com/juliangruber/brace-expansion/actions/workflows/ci.yml/badge.svg)](https://github.com/juliangruber/brace-expansion/actions/workflows/ci.yml)
Expand All @@ -9,7 +9,7 @@ as known from sh/bash, in JavaScript.
## Example

```js
var expand = require('brace-expansion');
const expand = require('brace-expansion');

expand('file-{a,b,c}.jpg')
// => ['file-a.jpg', 'file-b.jpg', 'file-c.jpg']
Expand Down Expand Up @@ -45,10 +45,10 @@ expand('ppp{,config,oe{,conf}}')
## API

```js
var expand = require('brace-expansion');
const expand = require('brace-expansion');
```

### var expanded = expand(str)
### const expanded = expand(str)

Return an array of all possible and valid expansions of `str`. If none are
found, `[str]` is returned.
Expand Down
2 changes: 1 addition & 1 deletion example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var expand = require('./');
const expand = require('./');

console.log(expand('http://any.org/archive{1996..1999}/vol{1..4}/part{a,b,c}.html'));
console.log(expand('http://www.numericals.com/file{1..100..10}.txt'));
Expand Down
87 changes: 43 additions & 44 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
var balanced = require('balanced-match');
const balanced = require('balanced-match');

module.exports = expandTop;

var escSlash = '\0SLASH'+Math.random()+'\0';
var escOpen = '\0OPEN'+Math.random()+'\0';
var escClose = '\0CLOSE'+Math.random()+'\0';
var escComma = '\0COMMA'+Math.random()+'\0';
var escPeriod = '\0PERIOD'+Math.random()+'\0';
const escSlash = '\0SLASH'+Math.random()+'\0';
const escOpen = '\0OPEN'+Math.random()+'\0';
const escClose = '\0CLOSE'+Math.random()+'\0';
const escComma = '\0COMMA'+Math.random()+'\0';
const escPeriod = '\0PERIOD'+Math.random()+'\0';

/**
* @return {number}
Expand Down Expand Up @@ -49,19 +47,17 @@ function parseCommaParts(str) {
if (!str)
return [''];

var parts = [];
var m = balanced('{', '}', str);
const parts = [];
const m = balanced('{', '}', str);

if (!m)
return str.split(',');

var pre = m.pre;
var body = m.body;
var post = m.post;
var p = pre.split(',');
const {pre, body, post} = m;
const p = pre.split(',');

p[p.length-1] += '{' + body + '}';
var postParts = parseCommaParts(post);
const postParts = parseCommaParts(post);
if (post.length) {
p[p.length-1] += postParts.shift();
p.push.apply(p, postParts);
Expand All @@ -85,8 +81,8 @@ function expandTop(str) {
// but a{},b}c will be expanded to [a}c,abc].
// One could argue that this is a bug in Bash, but since the goal of
// this module is to match Bash's rules, we escape a leading {}
if (str.substr(0, 2) === '{}') {
str = '\\{\\}' + str.substr(2);
if (str.slice(0, 2) === '{}') {
str = '\\{\\}' + str.slice(2);
}

return expand(escapeBraces(str), true).map(unescapeBraces);
Expand All @@ -98,6 +94,7 @@ function expandTop(str) {
function embrace(str) {
return '{' + str + '}';
}

/**
* @param {string} el
*/
Expand All @@ -112,6 +109,7 @@ function isPadded(el) {
function lte(i, y) {
return i <= y;
}

/**
* @param {number} i
* @param {number} y
Expand All @@ -126,27 +124,27 @@ function gte(i, y) {
*/
function expand(str, isTop) {
/** @type {string[]} */
var expansions = [];
const expansions = [];

var m = balanced('{', '}', str);
const m = balanced('{', '}', str);
if (!m) return [str];

// no need to expand pre, since it is guaranteed to be free of brace-sets
var pre = m.pre;
var post = m.post.length
const pre = m.pre;
const post = m.post.length
? expand(m.post, false)
: [''];

if (/\$$/.test(m.pre)) {
for (var k = 0; k < post.length; k++) {
var expansion = pre+ '{' + m.body + '}' + post[k];
for (let k = 0; k < post.length; k++) {
const expansion = pre+ '{' + m.body + '}' + post[k];
expansions.push(expansion);
}
} else {
var isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
var isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
var isSequence = isNumericSequence || isAlphaSequence;
var isOptions = m.body.indexOf(',') >= 0;
const isNumericSequence = /^-?\d+\.\.-?\d+(?:\.\.-?\d+)?$/.test(m.body);
const isAlphaSequence = /^[a-zA-Z]\.\.[a-zA-Z](?:\.\.-?\d+)?$/.test(m.body);
const isSequence = isNumericSequence || isAlphaSequence;
const isOptions = m.body.indexOf(',') >= 0;
if (!isSequence && !isOptions) {
// {a},b}
if (m.post.match(/,.*\}/)) {
Expand All @@ -156,7 +154,7 @@ function expand(str, isTop) {
return [str];
}

var n;
let n;
if (isSequence) {
n = m.body.split(/\.\./);
} else {
Expand All @@ -174,37 +172,37 @@ function expand(str, isTop) {

// at this point, n is the parts, and we know it's not a comma set
// with a single entry.
var N;
let N;

if (isSequence) {
var x = numeric(n[0]);
var y = numeric(n[1]);
var width = Math.max(n[0].length, n[1].length)
var incr = n.length == 3
const x = numeric(n[0]);
const y = numeric(n[1]);
const width = Math.max(n[0].length, n[1].length)
let incr = n.length == 3
? Math.abs(numeric(n[2]))
: 1;
var test = lte;
var reverse = y < x;
let test = lte;
const reverse = y < x;
if (reverse) {
incr *= -1;
test = gte;
}
var pad = n.some(isPadded);
const pad = n.some(isPadded);

N = [];

for (var i = x; test(i, y); i += incr) {
var c;
for (let i = x; test(i, y); i += incr) {
let c;
if (isAlphaSequence) {
c = String.fromCharCode(i);
if (c === '\\')
c = '';
} else {
c = String(i);
if (pad) {
var need = width - c.length;
const need = width - c.length;
if (need > 0) {
var z = new Array(need + 1).join('0');
const z = new Array(need + 1).join('0');
if (i < 0)
c = '-' + z + c.slice(1);
else
Expand All @@ -217,14 +215,14 @@ function expand(str, isTop) {
} else {
N = [];

for (var j = 0; j < n.length; j++) {
for (let j = 0; j < n.length; j++) {
N.push.apply(N, expand(n[j], false));
}
}

for (var j = 0; j < N.length; j++) {
for (var k = 0; k < post.length; k++) {
var expansion = pre + N[j] + post[k];
for (let j = 0; j < N.length; j++) {
for (let k = 0; k < post.length; k++) {
const expansion = pre + N[j] + post[k];
if (!isTop || isSequence || expansion)
expansions.push(expansion);
}
Expand All @@ -234,3 +232,4 @@ function expand(str, isTop) {
return expansions;
}

module.exports = expandTop;
16 changes: 8 additions & 8 deletions test/bash-comparison.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var test = require('tape');
var expand = require('..');
var fs = require('fs');
var resfile = __dirname + '/bash-results.txt';
var cases = fs.readFileSync(resfile, 'utf8').split('><><><><');
const test = require('tape');
const expand = require('..');
const fs = require('fs');
const resfile = __dirname + '/bash-results.txt';
const cases = fs.readFileSync(resfile, 'utf8').split('><><><><');

// throw away the EOF marker
cases.pop()

test('matches bash expansions', function(t) {
cases.forEach(function(testcase) {
var set = testcase.split('\n');
var pattern = set.shift();
var actual = expand(pattern);
let set = testcase.split('\n');
const pattern = set.shift();
const actual = expand(pattern);

// If it expands to the empty string, then it's actually
// just nothing, but Bash is a singly typed language, so
Expand Down
4 changes: 2 additions & 2 deletions test/dollar.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('ignores ${', function(t) {
t.deepEqual(expand('${1..3}'), ['${1..3}']);
Expand Down
4 changes: 2 additions & 2 deletions test/empty-option.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('empty option', function(t) {
t.deepEqual(expand('-v{,,,,}'), [
Expand Down
4 changes: 2 additions & 2 deletions test/negative-increment.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('negative increment', function(t) {
t.deepEqual(expand('{3..1}'), ['3', '2', '1']);
Expand Down
4 changes: 2 additions & 2 deletions test/nested.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('nested', function(t) {
t.deepEqual(expand('{a,b{1..3},c}'), [
Expand Down
4 changes: 2 additions & 2 deletions test/order.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('order', function(t) {
t.deepEqual(expand('a{d,c,b}e'), [
Expand Down
4 changes: 2 additions & 2 deletions test/pad.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('pad', function(t) {
t.deepEqual(expand('{9..11}'), [
Expand Down
8 changes: 4 additions & 4 deletions test/perf/bench.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
var expand = require('../../');
var fs = require('fs');
var resfile = __dirname + '/../cases.txt';
var cases = fs.readFileSync(resfile, 'utf8').split('\n');
const expand = require('../../');
const fs = require('fs');
const resfile = __dirname + '/../cases.txt';
const cases = fs.readFileSync(resfile, 'utf8').split('\n');

bench('Average', function() {
cases.forEach(function(testcase) {
Expand Down
4 changes: 2 additions & 2 deletions test/same-type.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('x and y of same type', function(t) {
t.deepEqual(expand('{a..9}'), ['{a..9}']);
Expand Down
4 changes: 2 additions & 2 deletions test/sequence.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var test = require('tape');
var expand = require('..');
const test = require('tape');
const expand = require('..');

test('numeric sequences', function(t) {
t.deepEqual(expand('a{1..2}b{2..3}c'), [
Expand Down

0 comments on commit dd5a4cb

Please sign in to comment.