lukeed / kleur

The fastest Node.js library for formatting terminal text with ANSI colors~!

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Kleur crashes when there's a global variable keys

ivan-kleshnin opened this issue · comments

Hi! My app uses global.keys constant. Unfortunately, it conflicts with Kleur internal magic.

function init(key) {
	return function (txt) {
		let isChain = !!this.keys; // <<<
		...
	};
}

As global.keys becomes something different:

this.keys.includes is not a function
    at /.../node_modules/kleur/index.js:95:26

I guess that's how you detect chaining. Is it possible to use "use strict" pragma and check if this is undefined instead of relying on global object field (which is not safe for libraries)?

Hey, thanks!

I can add the use strict, not a problem. What do you mean by the this check?

Also, can you show me how you're using the kleur method? That might be affecting this.

The use case causing the problem is simply: gray("whatever")

To test the case you can do:

import {gray} from "kleur"

global.keys = () => null

console.log(gray("whatever"))
TypeError: this.keys.includes is not a function

What do you mean by the this check?

I meant you detect whether it's blue() or gray().blue()in that line, right?

Great, thank you~!

Can you try this for me:

+ 'use strict';

// ..

- let isChain = !!this.keys;
+ let isChain = this !== void 0 && !!this.keys;

No problem. This way it works for me, yes.

Released in v3.0.2 – thank you~!