/
githubmirror
/
immutable-js
Обзор
Документация
Войти
/
githubmirror
/
immutable-js
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v5.1.5
src/Range.js
137 строк
4 KB
Julien Deniau
sort all imports via eslint
12 июн 2025, 01:23
12 июн 2025, 01:23
67a4fa9
Код
Авторство
О чём код?
import { Iterator, iteratorDone, iteratorValue } from './Iterator'; import { IndexedSeq } from './Seq'; import { resolveBegin, resolveEnd, wholeSlice, wrapIndex } from './TrieUtils'; import deepEqual from './utils/deepEqual'; import invariant from './utils/invariant'; /** * Returns a lazy seq of nums from start (inclusive) to end * (exclusive), by step, where start defaults to 0, step to 1, and end to * infinity. When start is equal to end, returns empty list. */ export class Range extends IndexedSeq { constructor(start, end, step = 1) { if (!(this instanceof Range)) { // eslint-disable-next-line no-constructor-return return new Range(start, end, step); } invariant(step !== 0, 'Cannot step a Range by 0'); invariant( start !== undefined, 'You must define a start value when using Range' ); invariant( end !== undefined, 'You must define an end value when using Range' ); step = Math.abs(step); if (end < start) { step = -step; } this._start = start; this._end = end; this._step = step; this.size = Math.max(0, Math.ceil((end - start) / step - 1) + 1); if (this.size === 0) { if (EMPTY_RANGE) { // eslint-disable-next-line no-constructor-return return EMPTY_RANGE; } // eslint-disable-next-line @typescript-eslint/no-this-alias EMPTY_RANGE = this; } } toString() { return this.size === 0 ? 'Range []' : `Range [ ${this._start}...${this._end}${this._step !== 1 ? ' by ' + this._step : ''} ]`; } get(index, notSetValue) { return this.has(index) ? this._start + wrapIndex(this, index) * this._step : notSetValue; } includes(searchValue) { const possibleIndex = (searchValue - this._start) / this._step; return ( possibleIndex >= 0 && possibleIndex < this.size && possibleIndex === Math.floor(possibleIndex) ); } slice(begin, end) { if (wholeSlice(begin, end, this.size)) { return this; } begin = resolveBegin(begin, this.size); end = resolveEnd(end, this.size); if (end <= begin) { return new Range(0, 0); } return new Range( this.get(begin, this._end), this.get(end, this._end), this._step ); } indexOf(searchValue) { const offsetValue = searchValue - this._start; if (offsetValue % this._step === 0) { const index = offsetValue / this._step; if (index >= 0 && index < this.size) { return index; } } return -1; } lastIndexOf(searchValue) { return this.indexOf(searchValue); } __iterate(fn, reverse) { const size = this.size; const step = this._step; let value = reverse ? this._start + (size - 1) * step : this._start; let i = 0; while (i !== size) { if (fn(value, reverse ? size - ++i : i++, this) === false) { break; } value += reverse ? -step : step; } return i; } __iterator(type, reverse) { const size = this.size; const step = this._step; let value = reverse ? this._start + (size - 1) * step : this._start; let i = 0; return new Iterator(() => { if (i === size) { return iteratorDone(); } const v = value; value += reverse ? -step : step; return iteratorValue(type, reverse ? size - ++i : i++, v); }); } equals(other) { return other instanceof Range ? this._start === other._start && this._end === other._end && this._step === other._step : deepEqual(this, other); } } let EMPTY_RANGE;