extend String.prototype only if requested#6
Conversation
|
Nice changes! 👍 Code reuse is really what npm is about. But I have some doubts about the extending API.
Also, browsers' and Node's global objects have different names, and that may cause some problems for modules that try to work in both worlds (although browserify handles it automatically). Finally, if |
|
Thank you for your thoughtful feedback, @eush77.
I was thinking in terms of extending an environment (likely require('string-format').extend(String.prototype);
This is a matter of taste. I prefer I'll change the argument from an environment ( |
Good! One more thing. Have you though about returning var format = require('string-format').extend(String.prototype);
format('Hello, {}!', 'Alice'); // => 'Hello, Alice!'
'Hello, {}!'.format('Alice'); // => 'Hello, Alice!'And, finally, Thank you @davidchambers for bringing in this patch, it's a very important change! |
I have a very strong opinion on this matter. Side effects should occur in as few places as possible and, importantly, should draw attention to themselves. In general, functions with side effects should return undefined to make their impurity clear. Consider the following: var b = _.extend(a, {foo: 1, bar: 2});This is very confusing! -var b = _.extend(a, {foo: 1, bar: 2});
+var b = _.extend({}, a, {foo: 1, bar: 2});I have also seen this pattern many times: a = _.extend(a, {foo: 1, bar: 2});Here, the author assumed If _.extend(a, {foo: 1, bar: 2});Since the return value is not being used, we must be calling this function for side effects. The first example would become: var b = {};
_.extend(b, a, {foo: 1, bar: 2});Again, this implies that So to answer your question, yes, I have thought about having
Good point. This function should not be called multiple times, though, so the cost of calling
Thank you for your interest in this project, which has encouraged me to give it my time and attention. :) |
|
What's the progress on this? Some changes are not very relevant, and the rest seems fine to me. |
|
Thanks for following up, @eush77. Much of my free time of late has been spent contributing to Ramda and taking the Programming Languages class on Coursera (which is terrific). I'd love to wrap up this pull request and publish a new release one day this week. We'll see if it happens. :) |
eb4b268 to
cf55b49
Compare
|
I've added a file which tests the examples in the README, which I've translated from CoffeeScript to JavaScript. The only task remaining is to rework the README to introduce the |
ad3257a to
36b0584
Compare
3bbd413 to
0da66c5
Compare
e5645bb to
8dad929
Compare
extend String.prototype only if requested
This pull request is not yet ready to merge: the new API is not yet documented.
In short, prototype extension will become opt-in:
Or simply:
The other change is that transformers will no longer exist in a single namespace. Instead, a new
createfunction takes atransformersmap and returns a format function:This means two unrelated packages which define their own transformers can be used together without conflict.
These two changes make it possible, for the first time, to responsibly add
string-formatas a dependency of a library. I look forward to doing so!I'd love to hear your thoughts on these changes, @eush77.