4 ms·
Someone linked me to 1-liners[0], which is - you guessed it - a bunch of one-liners. I think it's nice to have as a reference. But a dependency? Really? My lea
by torgard 4y ago
Someone linked me to 1-liners[0], which is - you guessed it - a bunch of one-liners. I think it's nice to have as a reference. But a dependency? Really?
My least favorite is assign.
Not only does JavaScript feature that natively (though I suppose the library may predate widespread support for Object.assign), the 1-liner assign flips the order of the parameters!
assign({ a: true }, { a: false }) -> { a: true }
Object.assign({ a: true }, { a: false }) -> { a: false }
And most of them are just straight-up pointless! Like, let's introduce a dependency for decrement lol
EDIT: In looking up whether the 1-liners assign predated widespread Object.assign support, I found that their implementation - confusingly named extend[1] at first - literally used Object.assign from the very beginning. And they still chose to mess with the parameter order. For shame lol
[0] https://github.com/1-liners/1-liners https://github.com/1-liners/1-liners
[1] https://github.com/1-liners/1-liners/blob/7c1f8d51df4b4b3e0a16d0ace8864eafcbac5483/module/extend.js#L17 https://github.com/1-liners/1-liners/blob/7c1f8d51df4b4b3e0a...
- jeroenhd 4y ago> Like, let's introduce a dependency for decrement lol Here's a package that basically does that: https://www.npmjs.com/package/number-precision https://www.npmjs.com/package/number-precision Not entirely unreasonable as all `number`s are floats by default in JS, but the implementation of the entire package (https://github.com/nefe/number-precision/blob/master/src/index.ts https://github.com/nefe/number-precision/blob/master/src/ind...) is less than 100 lines of code and actually contains a method called "minus". The very worst JS packages I've seen have got to be is-odd and is-even. 430,796 and 202,268 downloads every week, I kid you not!
- viraptor 4y agoSeems like a perfect candidate for a drive by fix. If you see it in your dependency tree, fix it in the project that uses it.
- samatman 4y agoIt would appear this argument order is due to Principle 5: Data comes last, for consistent currying. You might not like it, or the library (I don't write JS on purpose, so no opinion), but it's right there in the README.
- torgard 4y agoI was not aware of the data-last convention. Thanks! Makes a lot of sense. My mental model of assign was backwards, and it took me a while to comprehend why their implementation would be data-last. So, the data is the object that is being modified? And the idea is it lets you write stuff like this more easily: const addStuffToObject = stuff -> object -> assign({ "some_stuff": stuff }, object); const addWeirdStuff = addStuffToObject("weird stuff"); const weirdObj = addWeirdStuff({ "foo": "bar" });
- samatman 4y agoYes, and this sort of application (pun intended) is what the one-liner library is for. If you have a function isEqualTo(a, b), you can curry(isEqualTo, 5) and filter with it. `assign` could be used in the same way for assigning/overwriting the same field in an array of Objects, and so on.
- abraham 4y agoIt's got 2-3000 installs/week so not many people are using it. https://www.npmjs.com/package/1-liners https://www.npmjs.com/package/1-liners