DEV Community

Cover image for Built-in-like Range in JavaScript

Built-in-like Range in JavaScript

Francesco Di Donato on October 06, 2021

Premise Make it possible to generate any range of integers with built-in-like syntax. Motivation? Honestly, none. Zero. Except for fun...
Collapse
 
jonrandy profile image
Jon Randy 🎖️ • Edited

I'm planning to build something similar, but with 'safe' monkey patching using Symbols. I think I'll end up with a syntax something like:

4[to](7) // [4, 5, 6, 7]
[ ...1[to](3), ...10[to](15) ] // [1, 2, 3, 10, 11, 12, 13, 14, 15]

(4)[to](7) // alternative way to write it
Enter fullscreen mode Exit fullscreen mode

Or maybe (not sure this is possible safely):

4[to(7)] // [4, 5, 6, 7]
[ ...1[to(3)], ...10[to(15)] ] // [1, 2, 3, 10, 11, 12, 13, 14, 15]
Enter fullscreen mode Exit fullscreen mode

Which syntax do you prefer? (I'm leaning towards the former)

Collapse
 
jonrandy profile image
Jon Randy 🎖️

Could even expand it to:

1[to](19, {step: 2})) // [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]
Enter fullscreen mode Exit fullscreen mode

etc.

Collapse
 
didof profile image
Francesco Di Donato • Edited

It's an amazing idea, Jon. I find the second option more readable. Or, at least, it feels "righter". But I'm not sure it could work since a function cannot be used as a key in a plain object.
Anyway, I'd like to work on it, together. Would you like to?

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

I already did a POC in about 5 lines of code - works perfectly

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

Still trying to think of a way to make the second syntax work without unsafe monkey patching. I'm not sure it's possible. My POC uses the a[to](b) syntax. Basically, to is a method on the number prototype... nothing particularly fancy going on

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️

I think there is a way to do the 2nd syntax... I'll get back to you

Thread Thread
 
jonrandy profile image
Jon Randy 🎖️ • Edited

It works 👍 And it's safe

Thread Thread
 
didof profile image
Francesco Di Donato

I'm glad to read it. If you like to share I'd love to see it!

Collapse
 
jonrandy profile image
Jon Randy 🎖️

I noticed in your header image, you have (3)(7) - that's not gonna work

Collapse
 
didof profile image
Francesco Di Donato

Ops, thank you man! This is what happens when you write posts at 2 a.m. :)
I'm gonna fix it

Collapse
 
Sloan, the sloth mascot
Comment deleted
 
poker profile image
Poker9x

shshshsh

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

This is too much magic for me, don't comment to argue, I don't like it I won't change my mind but you can like it.

Collapse
 
didof profile image
Francesco Di Donato

Hi Adam, I understand you point of view. As I stated in the post, this is something superfluous, a dev can definitely live without it :)

Collapse
 
adam_cyclones profile image
Adam Crockett 🌀

It's not that, I woke up on the wrong side of the bed and I saw prototype of native primitive type being modified and it's just not my bag. Sorry if this came across in any other way but grumpy 😜

Collapse
 
didof profile image
Francesco Di Donato

Me too! Thank you!