✨ ✨ An extensible 1.5KB JavaScript utility for generating integers ⚡ 💪 🔥
A simple Javascript utility that can generate a range of integers, from simple ranges like even and odd numbers, to multiples of a given number, all with configurable spacing between the number sequencies.
You can also easily extend the utility to generate special custom sequencies for your unique needs. See the bundled leap-year
plugin for sample extension plugin
This utility can be used anyhow you deem fit, but the codebase and repository exists mostly for demonstration and educational purposes. The goal is to show how this could be built, and maybe walk through the procees of doing so (with a youtube playlist 😏) while highlighting some best practices. If you feel this should become an NPM package, let me know on Twitter
-
To get started, clone this repository to your local machine and go into the int-range directory
git clone https://github.com/chalu/int-range.git cd int-range
-
Install all dependencies to complete the setup. Use NPM or Yarn depending on what you already have setup on your machine.
npm install --save
yarn install
-
View the examples by running any of the following commands and opening the displayed URL in a browser
npm run demo
yarn demo
See this demo from the docs folder for more usage samples
-
Generate 1 to 5
ints({ from: 1, till: 5 });
-
Generate 0 to 15, in steps of 3
ints({ from: 0, till: 15, stepsOf: 3 }); // output: 0, 3, 6, 9, 12, 15
-
Generate integers between 25 and 1, in descending order, each integer spaced out by 5
ints({ from: 25, till: 1, stepsOf: 5 }); // output: 25, 20, 15, 10, 5
-
Generate a sequence of every other multiple of 5 between 1 and 50
ints({ from: 1, till: 35, sequence: multiples({ of: 5, stepsOf: 2 }), // output: 1, 10, 20, 30 });
Bring Your Own Sequence (BYOS), a.k.a extensibility!
Have need for a custom or domain specific integer sequence? You simply need to make a validator function!
-
Import the util's sequencer engine
import { sequencer } from "../int-range.js";
-
Declare and export your custom range function. It will be called with an object parameter
export const myCustomIntRange = (opts = {}) => {};
-
Define a validator function. It should return
true
when itsvalue
parameter is a bonafide integer in your custom sequence. This validator function should be set as thevalidator
property of the object parameter passsed to your custom range functionexport const myCustomIntRange = (opts = {}) => { opts.validator = (value) => { // determine if value meets the requirement // for members of your custom range, // then return true, otherwise return false. }; };
-
Call the sequencer function with your input parameter and return the outcome. Your complete integer sequence extension should look like:
import { sequencer } from "../int-range.js"; export const myCustomIntRange = (opts = {}) => { opts.validator = (value) => { if(/* value meets member requirements */) return true; return false; } return sequencer(opts); }
-
Finally, use
myCustomIntRange
to generate your desired sequence of integersimport { ints } from "../int-range.js"; import { myCustomIntRange } from "./path/to/my-custom-int-range.js"; const numbers = ints({ from: 5, till: 200, sequence: myCustomIntRange({ stepsOf: 2 }), }); console.log(numbers);
Take a look at the leap year sample and see how super easy it is to extend int-range.js to sequence custom domain specific integer ranges.
MIT