Skip to content

Commit

Permalink
Use let, const
Browse files Browse the repository at this point in the history
  • Loading branch information
danez committed Apr 30, 2018
1 parent 13e0d90 commit 354cfbf
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 70 deletions.
54 changes: 26 additions & 28 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
* Local dependencies
*/

var compilers = require('./lib/compilers');
var parsers = require('./lib/parsers');
const compilers = require('./lib/compilers');
const parsers = require('./lib/parsers');

/**
* Module dependencies
*/

var Snapdragon = require('snapdragon');
var toRegex = require('to-regex');
const Snapdragon = require('snapdragon');
const toRegex = require('to-regex');

/**
* Parses the given POSIX character class `pattern` and returns a
Expand All @@ -25,7 +25,7 @@ var toRegex = require('to-regex');
*/

function brackets(pattern, options) {
var res = brackets.create(pattern, options);
const res = brackets.create(pattern, options);
return res.output;
}

Expand All @@ -34,7 +34,7 @@ function brackets(pattern, options) {
* array with only the strings that matched the pattern.
*
* ```js
* var brackets = require('expand-brackets');
* const brackets = require('expand-brackets');
* console.log(brackets.match(['1', 'a', 'ab'], '[[:alpha:]]'));
* //=> ['a']
*
Expand All @@ -50,22 +50,22 @@ function brackets(pattern, options) {

brackets.match = function(arr, pattern, options) {
arr = [].concat(arr);
var opts = Object.assign({}, options);
var isMatch = brackets.matcher(pattern, opts);
var len = arr.length;
var idx = -1;
var res = [];
const opts = Object.assign({}, options);
const isMatch = brackets.matcher(pattern, opts);
const len = arr.length;
const res = [];
let idx = -1;

while (++idx < len) {
var ele = arr[idx];
const ele = arr[idx];
if (isMatch(ele)) {
res.push(ele);
}
}

if (res.length === 0) {
if (opts.failglob === true) {
throw new Error('no matches found for "' + pattern + '"');
throw new Error(`no matches found for "${pattern}"`);
}

if (opts.nonull === true || opts.nullglob === true) {
Expand All @@ -80,7 +80,7 @@ brackets.match = function(arr, pattern, options) {
* brackets `pattern`.
*
* ```js
* var brackets = require('expand-brackets');
* const brackets = require('expand-brackets');
*
* console.log(brackets.isMatch('a.a', '[[:alpha:]].[[:alpha:]]'));
* //=> true
Expand All @@ -103,8 +103,8 @@ brackets.isMatch = function(str, pattern, options) {
* function takes the string to match as its only argument.
*
* ```js
* var brackets = require('expand-brackets');
* var isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
* const brackets = require('expand-brackets');
* const isMatch = brackets.matcher('[[:lower:]].[[:upper:]]');
*
* console.log(isMatch('a.a'));
* //=> false
Expand All @@ -118,18 +118,16 @@ brackets.isMatch = function(str, pattern, options) {
*/

brackets.matcher = function(pattern, options) {
var re = brackets.makeRe(pattern, options);
return function(str) {
return re.test(str);
};
const re = brackets.makeRe(pattern, options);
return (str) => re.test(str);
};

/**
* Create a regular expression from the given `pattern`.
*
* ```js
* var brackets = require('expand-brackets');
* var re = brackets.makeRe('[[:alpha:]]');
* const brackets = require('expand-brackets');
* const re = brackets.makeRe('[[:alpha:]]');
* console.log(re);
* //=> /^(?:[a-zA-Z])$/
* ```
Expand All @@ -140,8 +138,8 @@ brackets.matcher = function(pattern, options) {
*/

brackets.makeRe = function(pattern, options) {
var res = brackets.create(pattern, options);
var opts = Object.assign({strictErrors: false}, options);
const res = brackets.create(pattern, options);
const opts = Object.assign({strictErrors: false}, options);
return toRegex(res.output, opts);
};

Expand All @@ -150,7 +148,7 @@ brackets.makeRe = function(pattern, options) {
* with the compiled `output` and optional source `map`.
*
* ```js
* var brackets = require('expand-brackets');
* const brackets = require('expand-brackets');
* console.log(brackets('[[:alpha:]]'));
* // { options: { source: 'string' },
* // input: '[[:alpha:]]',
Expand Down Expand Up @@ -182,13 +180,13 @@ brackets.makeRe = function(pattern, options) {
*/

brackets.create = function(pattern, options) {
var snapdragon = (options && options.snapdragon) || new Snapdragon(options);
const snapdragon = (options && options.snapdragon) || new Snapdragon(options);
compilers(snapdragon);
parsers(snapdragon);

var ast = snapdragon.parse(pattern, options);
const ast = snapdragon.parse(pattern, options);
ast.input = pattern;
var res = snapdragon.compile(ast, options);
const res = snapdragon.compile(ast, options);
res.input = pattern;
return res;
};
Expand Down
10 changes: 5 additions & 5 deletions lib/compilers.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

var posix = require('posix-character-classes');
const posix = require('posix-character-classes');

module.exports = function(brackets) {
brackets.compiler
Expand Down Expand Up @@ -30,7 +30,7 @@ module.exports = function(brackets) {
return this.emit('\\[::\\]', node);
}

var val = posix[node.inner];
let val = posix[node.inner];
if (typeof val === 'undefined') {
val = '[' + node.inner + ']';
}
Expand All @@ -48,7 +48,7 @@ module.exports = function(brackets) {
return this.emit(node.val, node);
})
.set('bracket.inner', function(node) {
var inner = node.val;
let inner = node.val;

if (inner === '[' || inner === ']') {
return this.emit('\\' + node.val, node);
Expand All @@ -64,7 +64,7 @@ module.exports = function(brackets) {
inner = inner.split('-').join('\\-');
}

var isNegated = inner.charAt(0) === '^';
const isNegated = inner.charAt(0) === '^';
// add slashes to negated brackets, per spec
if (isNegated && inner.indexOf('/') === -1) {
inner += '/';
Expand All @@ -78,7 +78,7 @@ module.exports = function(brackets) {
return this.emit(inner, node);
})
.set('bracket.close', function(node) {
var val = node.val.replace(/^\\/, '');
const val = node.val.replace(/^\\/, '');
if (node.parent.escaped === true) {
return this.emit('\\' + val, node);
}
Expand Down
62 changes: 31 additions & 31 deletions lib/parsers.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict';

var utils = require('./utils');
const utils = require('./utils');

/**
* Text regex
*/

var TEXT_REGEX = '(\\[(?=.*\\])|\\])+';
var not = utils.createRegex(TEXT_REGEX);
const TEXT_REGEX = '(\\[(?=.*\\])|\\])+';
const not = utils.createRegex(TEXT_REGEX);

/**
* Brackets parsers
Expand All @@ -20,8 +20,8 @@ function parsers(brackets) {

.set('escape', function() {
if (this.isInside('bracket')) return;
var pos = this.position();
var m = this.match(/^\\(.)/);
const pos = this.position();
const m = this.match(/^\\(.)/);
if (!m) return;

return pos({
Expand All @@ -36,8 +36,8 @@ function parsers(brackets) {

.set('text', function() {
if (this.isInside('bracket')) return;
var pos = this.position();
var m = this.match(not);
const pos = this.position();
const m = this.match(not);
if (!m || !m[0]) return;

return pos({
Expand All @@ -51,8 +51,8 @@ function parsers(brackets) {
*/

.set('posix', function() {
var pos = this.position();
var m = this.match(/^\[:(.*?):\](?=.*\])/);
const pos = this.position();
const m = this.match(/^\[:(.*?):\](?=.*\])/);
if (!m) return;

var inside = this.isInside('bracket');
Expand All @@ -79,13 +79,13 @@ function parsers(brackets) {
*/

.set('bracket.open', function() {
var parsed = this.parsed;
var pos = this.position();
var m = this.match(/^\[(?=.*\])/);
const parsed = this.parsed;
const pos = this.position();
const m = this.match(/^\[(?=.*\])/);
if (!m) return;

var prev = this.prev();
var last = utils.last(prev.nodes);
const prev = this.prev();
const last = utils.last(prev.nodes);

if (parsed.slice(-1) === '\\' && !this.isInside('bracket')) {
last.val = last.val.slice(0, last.val.length - 1);
Expand All @@ -95,7 +95,7 @@ function parsers(brackets) {
});
}

var open = pos({
const open = pos({
type: 'bracket.open',
val: m[0]
});
Expand All @@ -107,7 +107,7 @@ function parsers(brackets) {
return open;
}

var node = pos({
const node = pos({
type: 'bracket',
nodes: []
});
Expand All @@ -123,24 +123,24 @@ function parsers(brackets) {

.set('bracket.inner', function() {
if (!this.isInside('bracket')) return;
var pos = this.position();
var m = this.match(not);
const pos = this.position();
const m = this.match(not);
if (!m || !m[0]) return;

var next = this.input.charAt(0);
var val = m[0];
const next = this.input.charAt(0);
let val = m[0];

var node = pos({
const node = pos({
type: 'bracket.inner',
val: val
val
});

if (val === '\\\\') {
return node;
}

var first = val.charAt(0);
var last = val.slice(-1);
const first = val.charAt(0);
const last = val.slice(-1);

if (first === '!') {
val = '^' + val.slice(1);
Expand All @@ -160,13 +160,13 @@ function parsers(brackets) {
*/

.set('bracket.close', function() {
var parsed = this.parsed;
var pos = this.position();
var m = this.match(/^\]/);
const parsed = this.parsed;
const pos = this.position();
const m = this.match(/^\]/);
if (!m) return;

var prev = this.prev();
var last = utils.last(prev.nodes);
const prev = this.prev();
const last = utils.last(prev.nodes);

if (parsed.slice(-1) === '\\' && !this.isInside('bracket')) {
last.val = last.val.slice(0, last.val.length - 1);
Expand All @@ -177,7 +177,7 @@ function parsers(brackets) {
});
}

var node = pos({
const node = pos({
type: 'bracket.close',
rest: this.input,
val: m[0]
Expand All @@ -189,7 +189,7 @@ function parsers(brackets) {
return node;
}

var bracket = this.pop('bracket');
const bracket = this.pop('bracket');
if (!this.isType(bracket, 'bracket')) {
if (this.options.strict) {
throw new Error('missing opening "["');
Expand Down
12 changes: 6 additions & 6 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

var toRegex = require('to-regex');
var regexNot = require('regex-not');
var cached;
const toRegex = require('to-regex');
const regexNot = require('regex-not');
let cached;

/**
* Get the last element from `array`
Expand All @@ -20,9 +20,9 @@ exports.last = function(arr) {

exports.createRegex = function(pattern, include) {
if (cached) return cached;
var opts = {contains: true, strictClose: false};
var not = regexNot.create(pattern, opts);
var re;
const opts = {contains: true, strictClose: false};
const not = regexNot.create(pattern, opts);
let re;

if (typeof include === 'string') {
re = toRegex('^(?:' + include + '|' + not + ')', opts);
Expand Down

0 comments on commit 354cfbf

Please sign in to comment.