chalk 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +26 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -7,6 +7,17 @@ var supportsColor = require('supports-color');
7
7
  var defineProps = Object.defineProperties;
8
8
  var chalk = module.exports;
9
9
 
10
+ function build(_styles) {
11
+ var builder = function builder() {
12
+ return applyStyle.apply(builder, arguments);
13
+ };
14
+ builder._styles = _styles;
15
+ // __proto__ is used because we must return a function, but there is
16
+ // no way to create a function with a different prototype.
17
+ builder.__proto__ = proto;
18
+ return builder;
19
+ }
20
+
10
21
  var styles = (function () {
11
22
  var ret = {};
12
23
 
@@ -17,8 +28,7 @@ var styles = (function () {
17
28
 
18
29
  ret[key] = {
19
30
  get: function () {
20
- this._styles.push(key);
21
- return this;
31
+ return build(this._styles.concat(key));
22
32
  }
23
33
  };
24
34
  });
@@ -26,15 +36,26 @@ var styles = (function () {
26
36
  return ret;
27
37
  })();
28
38
 
39
+ var proto = defineProps(function chalk() {}, styles);
40
+
29
41
  function applyStyle() {
30
42
  // support varags, but simply cast to string in case there's only one arg
31
- var str = arguments.length === 1 ? String(arguments[0]) : [].slice.call(arguments).join(' ');
43
+ var args = arguments;
44
+ var argsLen = args.length;
45
+ var str = argsLen !== 0 && String(arguments[0]);
46
+ if (argsLen > 1) {
47
+ // don't slice `arguments`, it prevents v8 optimizations
48
+ for (var a = 1; a < argsLen; a++) {
49
+ str += ' ' + args[a];
50
+ }
51
+ }
32
52
 
33
53
  if (!chalk.enabled || !str) {
34
54
  return str;
35
55
  }
36
56
 
37
- var nestedStyles = applyStyle._styles;
57
+ /*jshint validthis: true*/
58
+ var nestedStyles = this._styles;
38
59
 
39
60
  for (var i = 0; i < nestedStyles.length; i++) {
40
61
  var code = ansiStyles[nestedStyles[i]];
@@ -51,11 +72,9 @@ function init() {
51
72
  var ret = {};
52
73
 
53
74
  Object.keys(styles).forEach(function (name) {
54
- var style = defineProps(applyStyle, styles);
55
75
  ret[name] = {
56
76
  get: function () {
57
- style._styles = [];
58
- return style[name];
77
+ return build([name]);
59
78
  }
60
79
  };
61
80
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "chalk",
3
- "version": "0.5.0",
3
+ "version": "0.5.1",
4
4
  "description": "Terminal string styling done right. Created because the `colors` module does some really horrible things.",
5
5
  "license": "MIT",
6
6
  "repository": "sindresorhus/chalk",