Skip to content

Commit

Permalink
ES2015ify the codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 20, 2017
1 parent cb3f230 commit 249b9ac
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 237 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Original file line Diff line number Diff line change
@@ -1,6 +1,6 @@
sudo: false
language: node_js language: node_js
node_js: node_js:
- '8'
- '6' - '6'
- '4' - '4'
after_success: npm run coveralls after_success: npm run coveralls
14 changes: 7 additions & 7 deletions benchmark.js
Original file line number Original file line Diff line number Diff line change
@@ -1,24 +1,24 @@
/* globals set bench */ /* globals set bench */
'use strict'; 'use strict';
var chalk = require('./'); const chalk = require('.');


suite('chalk', function () { suite('chalk', () => {
set('iterations', 100000); set('iterations', 100000);


bench('single style', function () { bench('single style', () => {
chalk.red('the fox jumps over the lazy dog'); chalk.red('the fox jumps over the lazy dog');
}); });


bench('several styles', function () { bench('several styles', () => {
chalk.blue.bgRed.bold('the fox jumps over the lazy dog'); chalk.blue.bgRed.bold('the fox jumps over the lazy dog');
}); });


var cached = chalk.blue.bgRed.bold; const cached = chalk.blue.bgRed.bold;
bench('cached styles', function () { bench('cached styles', () => {
cached('the fox jumps over the lazy dog'); cached('the fox jumps over the lazy dog');
}); });


bench('nested styles', function () { bench('nested styles', () => {
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!'); chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
}); });
}); });
114 changes: 60 additions & 54 deletions index.js
Original file line number Original file line Diff line number Diff line change
@@ -1,118 +1,124 @@
'use strict'; 'use strict';
var escapeStringRegexp = require('escape-string-regexp'); const escapeStringRegexp = require('escape-string-regexp');
var ansiStyles = require('ansi-styles'); const ansiStyles = require('ansi-styles');
var supportsColor = require('supports-color'); const supportsColor = require('supports-color');


var defineProps = Object.defineProperties; const defineProps = Object.defineProperties;
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM); const isSimpleWindowsTerm = process.platform === 'win32' && !process.env.TERM.toLowerCase().startsWith('xterm');


// supportsColor.level -> ansiStyles.color[name] mapping // `supportsColor.level` → `ansiStyles.color[name]` mapping
var levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m']; const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
// color-convert models to exclude from the Chalk API due to conflicts and such. // `color-convert` models to exclude from the Chalk API due to conflicts and such
var skipModels = ['gray']; const skipModels = ['gray'];


function Chalk(options) { function Chalk(options) {
// detect level if not set manually // Detect level if not set manually
this.level = !options || options.level === undefined ? supportsColor.level : options.level; this.level = !options || options.level === undefined ? supportsColor.level : options.level;
} }


// use bright blue on Windows as the normal blue color is illegible // Use bright blue on Windows as the normal blue color is illegible
if (isSimpleWindowsTerm) { if (isSimpleWindowsTerm) {
ansiStyles.blue.open = '\u001b[94m'; ansiStyles.blue.open = '\u001B[94m';
} }


var styles = {}; const styles = Object.create(null);


Object.keys(ansiStyles).forEach(function (key) { for (const key of Object.keys(ansiStyles)) {
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');


styles[key] = { styles[key] = {
get: function () { get() {
var codes = ansiStyles[key]; const codes = ansiStyles[key];
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key); return build.call(this, this._styles ? this._styles.concat(codes) : [codes], key);
} }
}; };
}); }


ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g'); ansiStyles.color.closeRe = new RegExp(escapeStringRegexp(ansiStyles.color.close), 'g');
Object.keys(ansiStyles.color.ansi).forEach(function (model) { for (const model of Object.keys(ansiStyles.color.ansi)) {
if (skipModels.indexOf(model) !== -1) { if (skipModels.indexOf(model) !== -1) {
return; continue;
} }


styles[model] = { styles[model] = {
get: function () { get() {
var level = this.level; const level = this.level;
return function () { return function () {
var open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments); const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
var codes = {open: open, close: ansiStyles.color.close, closeRe: ansiStyles.color.closeRe}; const codes = {
open,
close: ansiStyles.color.close,
closeRe: ansiStyles.color.closeRe
};
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model); return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
}; };
} }
}; };
}); }


ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g'); ansiStyles.bgColor.closeRe = new RegExp(escapeStringRegexp(ansiStyles.bgColor.close), 'g');
Object.keys(ansiStyles.bgColor.ansi).forEach(function (model) { for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
if (skipModels.indexOf(model) !== -1) { if (skipModels.indexOf(model) !== -1) {
return; continue;
} }


var bgModel = 'bg' + model.charAt(0).toUpperCase() + model.substring(1); const bgModel = 'bg' + model.charAt(0).toUpperCase() + model.slice(1);
styles[bgModel] = { styles[bgModel] = {
get: function () { get() {
var level = this.level; const level = this.level;
return function () { return function () {
var open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments); const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
var codes = {open: open, close: ansiStyles.bgColor.close, closeRe: ansiStyles.bgColor.closeRe}; const codes = {
open,
close: ansiStyles.bgColor.close,
closeRe: ansiStyles.bgColor.closeRe
};
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model); return build.call(this, this._styles ? this._styles.concat(codes) : [codes], model);
}; };
} }
}; };
}); }


// eslint-disable-next-line func-names // eslint-disable-next-line func-names
var proto = defineProps(function chalk() {}, styles); const proto = defineProps(() => {}, styles);


function build(_styles, key) { function build(_styles, key) {
var builder = function () { const builder = function () {
return applyStyle.apply(builder, arguments); return applyStyle.apply(builder, arguments);
}; };


var self = this;

builder._styles = _styles; builder._styles = _styles;


const self = this;
Object.defineProperty(builder, 'level', { Object.defineProperty(builder, 'level', {
enumerable: true, enumerable: true,
get: function () { get() {
return self.level; return self.level;
}, },
set: function (level) { set(level) {
self.level = level; self.level = level;
} }
}); });


// see below for fix regarding invisible grey/dim combination on windows. // See below for fix regarding invisible grey/dim combination on Windows
builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey'; builder.hasGrey = this.hasGrey || key === 'gray' || key === 'grey';


// __proto__ is used because we must return a function, but there is // `__proto__` is used because we must return a function, but there is
// no way to create a function with a different prototype. // no way to create a function with a different prototype.
/* eslint-disable no-proto */ builder.__proto__ = proto; // eslint-disable-line no-proto
builder.__proto__ = proto;


return builder; return builder;
} }


function applyStyle() { function applyStyle() {
// support varags, but simply cast to string in case there's only one arg // Support varags, but simply cast to string in case there's only one arg
var args = arguments; const args = arguments;
var argsLen = args.length; const argsLen = args.length;
var str = argsLen !== 0 && String(arguments[0]); let str = argsLen !== 0 && String(arguments[0]);


if (argsLen > 1) { if (argsLen > 1) {
// don't slice `arguments`, it prevents v8 optimizations // Don't slice `arguments`, it prevents V8 optimizations
for (var a = 1; a < argsLen; a++) { for (let a = 1; a < argsLen; a++) {
str += ' ' + args[a]; str += ' ' + args[a];
} }
} }
Expand All @@ -121,19 +127,19 @@ function applyStyle() {
return str; return str;
} }


var nestedStyles = this._styles; const nestedStyles = this._styles;
var i = nestedStyles.length; let i = nestedStyles.length;


// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe, // Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
// see https://github.com/chalk/chalk/issues/58 // see https://github.com/chalk/chalk/issues/58
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop. // If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
var originalDim = ansiStyles.dim.open; const originalDim = ansiStyles.dim.open;
if (isSimpleWindowsTerm && this.hasGrey) { if (isSimpleWindowsTerm && this.hasGrey) {
ansiStyles.dim.open = ''; ansiStyles.dim.open = '';
} }


while (i--) { while (i--) {
var code = nestedStyles[i]; const code = nestedStyles[i];


// Replace any instances already present with a re-opening code // Replace any instances already present with a re-opening code
// otherwise only the part of the string until said closing code // otherwise only the part of the string until said closing code
Expand All @@ -143,10 +149,10 @@ function applyStyle() {
// Close the styling before a linebreak and reopen // Close the styling before a linebreak and reopen
// after next line to fix a bleed issue on macOS // after next line to fix a bleed issue on macOS
// https://github.com/chalk/chalk/pull/92 // https://github.com/chalk/chalk/pull/92
str = str.replace(/\r?\n/g, code.close + '$&' + code.open); str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
} }


// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue. // Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue
ansiStyles.dim.open = originalDim; ansiStyles.dim.open = originalDim;


return str; return str;
Expand Down
20 changes: 4 additions & 16 deletions license
Original file line number Original file line Diff line number Diff line change
@@ -1,21 +1,9 @@
The MIT License (MIT) MIT License


Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com) Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)


Permission is hereby granted, free of charge, to any person obtaining a copy Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:


The above copyright notice and this permission notice shall be included in The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
all copies or substantial portions of the Software.


THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
Loading

0 comments on commit 249b9ac

Please sign in to comment.