Links
npm: ranging
GitHub: Crinax/ranging
Introduction
Have you ever worked with the range
function in Python
, or maybe heard of it? If you haven't, let me explain a bit. This function is used to create a range, with a number of arguments ranging from 1 to 3. If you give 1 argument, the function creates a range [0; n)
, where n
is a given number. If you set 2 arguments, they are treated by the function as the start and end of the range, e.g: range(1, 10)
- will create range [1, 10)
. If you set 3 arguments, the function will treat the third argument as a step.
Although I don't have much experience in working on any heavy projects. However, I noticed all the time that almost no one uses generators in JavaScript, although they can do a lot of useful things.
Problem
Lack of useful generator-based functions in vanilla JavaScript
.
Briefly about development
When I applied for a job as a programmer, I was given a test assignment in which I had to create tasks by dates. And then I thought how nice it would be to be able to generate a date range in a couple of lines. So I had the idea to create this library.
At first the development was done through the for
loop and the yield
operator. Later a friend of mine suggested that I add my own iterable classes. My own iterable classes allowed me to add more functionality.
During the creation, I constantly consulted with my friend, who helped me in many ways, for example, he is the owner of the function that allows you to correctly add floating point numbers. Thanks to him you can create ranges of floating point numbers with this library without fear.
A few examples
For a range of one type alone, there are 6 creation options. As the saying goes: "Everyone to his own taste".
const { Range, NumberRange } = require('ranging');
// Range [0; 9]
// 1 variant
const r = [...Range.numbers(9)];
// 2 variant
const a = [...Range.numbers({ end: 9 });
// 3 variant
const n = [...Range.numbers().end(9)];
// 4 variant
const g = [...new NumberRange(9)];
// 5 variant
const e = [...new NumberRange({ end: 9 })];
// 6 variant
const d = [...new NumberRange().end(9)];
Conclusion
As you can see, my library allows you to quickly create an array of numbers, symbols, dates. You can find more information on how to use it on GitHub under links
Top comments (0)