All Projects β†’ michal-perlakowski β†’ Range

michal-perlakowski / Range

Licence: mit
A JavaScript implementation of the Python's range() function.

Programming Languages

javascript
184084 projects - #8 most used programming language
python
139335 projects - #7 most used programming language

Labels

Projects that are alternatives of or similar to Range

React Range
🎚️Range input with a slider. Accessible. Bring your own styles and markup.
Stars: ✭ 545 (+395.45%)
Mutual labels:  range
Quadtree Js
A simple quadtree implementation for javascript and typescript (nodejs or browser).
Stars: ✭ 36 (-67.27%)
Mutual labels:  range
Functionalplus
Functional Programming Library for C++. Write concise and readable C++ code.
Stars: ✭ 1,286 (+1069.09%)
Mutual labels:  range
Period
PHP's time range API
Stars: ✭ 616 (+460%)
Mutual labels:  range
Singledateandtimepicker
You can now select a date and a time with only one widget !
Stars: ✭ 921 (+737.27%)
Mutual labels:  range
Fill Range
Fill in a range of numbers or letters, positive or negative, optionally passing an increment or multiplier to use.
Stars: ✭ 41 (-62.73%)
Mutual labels:  range
Pg pathman
Partitioning tool for PostgreSQL
Stars: ✭ 509 (+362.73%)
Mutual labels:  range
Spans
Spans is a pure Python implementation of PostgreSQL's range types.
Stars: ✭ 106 (-3.64%)
Mutual labels:  range
Angular Datetime Range
πŸ“… Angular directive for datetime range input
Stars: ✭ 27 (-75.45%)
Mutual labels:  range
Range Parser
Range header field parser
Stars: ✭ 78 (-29.09%)
Mutual labels:  range
React Slider
Accessible, CSS agnostic, slider component for React.
Stars: ✭ 627 (+470%)
Mutual labels:  range
Vue Ctk Date Time Picker
VueJS component to select dates & time, including a range mode
Stars: ✭ 707 (+542.73%)
Mutual labels:  range
Range Slider
The simplest JavaScript custom range slider ever!
Stars: ✭ 41 (-62.73%)
Mutual labels:  range
Traces.vim
Range, pattern and substitute preview for Vim
Stars: ✭ 568 (+416.36%)
Mutual labels:  range
Monster
The Art of Template MetaProgramming (TMP) in Modern C++♦️
Stars: ✭ 90 (-18.18%)
Mutual labels:  range
Ion2 Calendar
πŸ“… A date picker components for ionic2 /ionic3 / ionic4
Stars: ✭ 537 (+388.18%)
Mutual labels:  range
Fieldtop
Finds near-overflow columns in MySQL databases
Stars: ✭ 36 (-67.27%)
Mutual labels:  range
Easyiterator
πŸƒ Iterators made easy! Zero cost abstractions for designing and using C++ iterators.
Stars: ✭ 107 (-2.73%)
Mutual labels:  range
To Regex Range
Pass two numbers, get a regex-compatible source string for matching ranges. Fast compiler, optimized regex, and validated against more than 2.78 million test assertions. Useful for creating regular expressions to validate numbers, ranges, years, etc.
Stars: ✭ 97 (-11.82%)
Mutual labels:  range
Persianrangedatepicker
Persian range date picker for android.
Stars: ✭ 48 (-56.36%)
Mutual labels:  range

range

NPM Version Build Status dependency Status devDependency Status Coverage Status

A JavaScript implementation of the Python's range() function.

Installation

npm i python-range

Examples

Try it out in your browser.

Basic usage

import range from 'python-range';

const r = range(10);
console.log(r); // range(0, 10, 1)
console.log(Array.from(r)); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
console.log(r[3]); // 3
console.log(r.length); // 10

console.log(Array.from(range(2, 5))); // [2, 3, 4]
console.log(Array.from(range(5, 0, -1))); // [5, 4, 3, 2, 1]

Iteration

const r = range(3, 6);
for (const n of r) {
  console.log(n); // logs 3, 4 and 5
}
r.forEach(n => console.log(n)); // logs 3, 4, and 5

Lazy evaluation

Unlike other range modules, python-range has lazy evaluation.

See Why is β€œ1000000000000000 in range(1000000000000001)” so fast in Python 3?

console.log(range(1000000000000001).includes(1000000000000000)); // true
console.log(range(0, 100000000000001, 10).includes(100000000000000)); // true
console.log(range(0, -1000000000, -3)[12345678]); // -37037034

Documentation

Exported values

python-range exports two values: a PythonRange class, and a range function (a default export), which returns a PythonRange object.

import range, {PythonRange} from 'python-range';
console.log(range(10) instanceof PythonRange); // true

new PythonRange(<int> start, <int> stop[, <int> step])

The PythonRange constructor creates a range starting with start (inclusive) and ending with stop (exclusive). The step parameter defaults to 1 and specifies the distance between two elements. The step parameter must be different than 0.

new PythonRange(<int> stop)

When called with only one argument, the start parameter defaults to 0 and the passed argument becomes stop.

const r = range(3);
console.log(r.start); // 0
console.log(r.stop); // 3

Numeric properties

You can access range elements using array indices. Note that this requires native Proxy support.

const r = range(2, 5);
console.log(r[0]); // 2
console.log(r[2]); // 4

start, stop and step properties

The PythonRange constructor creates these properties based on the arguments passed to the constructor, or the default values. These properties are writable, and changing them automatically updates the range.

const r = range(5);
console.log(Array.from(r)); // [0, 1, 2, 3, 4]
r.step = 2;
console.log(Array.from(r)); // [0, 2, 4]

length property

The length property specifies the number of elements in the range. It's updated automatically when the start, stop and step properties are changed.

const r = range(5);
console.log(r.length); // 5
r.stop = 3;
console.log(r.length); // 3

PythonRange.prototype.get(<int> index)

Works the same as accessing numeric properties (e.g. r[2]). Use this method if you want to execute your code in environments without native Proxy support.

console.log(range(2, 5).get(1)); // 3

PythonRange.prototype.forEach(<callable> callback[, <object> thisArg])

Executes the callback once for each element with element, current index and the range as arguments. Works the same as Array.prototype.forEach().

PythonRange.prototype.includes(<int> value)

Returns true if the range contains the specified value; otherwise returns false.

console.log(range(3, 5).includes(3)); // true
console.log(range(10).includes(10)); // false

PythonRange.prototype.min()

Returns the smallest value in the range.

console.log(range(10).min()); // 0
console.log(range(0, -15, -1).min()); // -14

PythonRange.prototype.max()

Returns the largest value in the range.

console.log(range(10).max()); // 9
console.log(range(12, 0, -2).max()); // 12

PythonRange.prototype.reverse()

Reverses the range in-place and returns it.

console.log(range(2, 5).reverse()); // range(4, 1, -1)
console.log(range(10, 0, -1).reverse()); // range(1, 11, 1)

PythonRange.prototype.toString()

Returns a string range(<start>, <stop>, <step>), where <start>, <stop> and <step> are the start, stop and step properties of the range.

console.log(String(range(3, 6, 2))); // range(3, 6, 2)

PythonRange.prototype.valueOf()

Returns the result of the toString() method.

PythonRange.prototype[@@iterator]()

Returns the result of calling Array.prototype.values() on the range.

PythonRange.areEqual(<PythonRange> a, <PythonRange> b)

Returns true if the passed arguments are equal; otherwise returns false. Two ranges are considered equals if they contain the same set of values. For example, range(3, 3) and range(0) are equal, because they're both empty ranges. range(4, 5, 2) and range(4, 6, 3) are equal too, because they both contain only one element: 4.

const r = range(3);
console.log(PythonRange.areEqual(r, r)); // true
console.log(PythonRange.areEqual(range(10), range(10))); // true
console.log(PythonRange.areEqual(range(3, 3), range(0))); // true, because both ranges are empty
console.log(PythonRange.areEqual(range(4, 5, 2), range(4, 6, 3))); // true, because both ranges contain only one element: 4
console.log(PythonRange.areEqual(range(2), range(3))); // false

range(...args)

Returns an instance of PythonRange with the specified arguments.

Supported environments

In order to be able to access numeric properties (e.g. r[2]), native Proxy support is required (see compatibility table). If you want to execute your code in environments without Proxy support, you can use the get() method instead.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].