string_score icon indicating copy to clipboard operation
string_score copied to clipboard

Don't patch string prototype

Open felixfbecker opened this issue 7 years ago • 7 comments

Patching prototypes of global objects is a bad practice. Would it be possible to get a version of this module that just exports the scoring function, with a UMD wrapper so it can be used in a CommonJS environment? That would also make it possible to pass the function directly to array.sort()

felixfbecker avatar Sep 15 '17 05:09 felixfbecker

const score = require('string_score')

let word = "Hello World"

console.log(word.score("Hell"))

thereis avatar Dec 06 '17 12:12 thereis

world would be undefined in this example so you cannot score it.

Sent from my iPhone

On Dec 6, 2017, at 6:08 AM, Lucas Reis [email protected] wrote:

const score = require('string_score')

let word = "Hello World"

console.log(world.score("Hell")) — You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

joshaven avatar Dec 06 '17 16:12 joshaven

Sorry for the typo hehe

thereis avatar Dec 06 '17 17:12 thereis

This would be nice! Planning on using this lib in a large production app but would like to get rid of the prototype patching. Created a local version which doesn't have the prototype patching, we could do an import like:

// Usage without prototype patching
const score = require('string_score/no-prototype')

const word = "Hello World"

console.log(score(word, "Hell")
// Normal usage with prototype patching
 require("string_score");

This will allow us to release a new non-breaking version of this lib. Got a local version ready to go, are you interested in me doing a PR for this?

@joshaven What's your opinion on this topic?

DannyvanderJagt avatar Jan 04 '18 13:01 DannyvanderJagt

I think you are right to recommend the change. I haven’t done much with the package lately, I’ll look into it and respond better later. Thanks.

Sent from my iPhone

On Jan 4, 2018, at 7:21 AM, Danny van der Jagt [email protected] wrote:

This would be nice! Planning on using this lib in a large production app but would like to get rid of the prototype patching. Created a local version which doesn't have the prototype patching, we could do an import like:

// Usage without prototype patching const score = require('string_score/no-prototype')

const word = "Hello World"

console.log(score(word, "Hell") // Normal usage with prototype patching require("string_score"); This will allow us to release a new non-breaking version of this lib. Got a local version ready to go, are you interested in me doing a PR for this?

@joshaven What's your opinion on this topic?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

joshaven avatar Jan 04 '18 15:01 joshaven

Thanks! Sounds good. I will make a PR so you can take a look at it, maybe it helps. It can be quite a small change.

DannyvanderJagt avatar Jan 04 '18 15:01 DannyvanderJagt

@DannyvanderJagt could you push your code on a PR? At least it would be accessible to the community!

BuonOmo avatar Dec 18 '19 12:12 BuonOmo