UNPKG

immutable-js

Version:
42 lines (38 loc) 1.27 kB
/** * Copyright (c) 2015, Jan Biasi. * All rights reserved. * * This source code is licensed under the BSD-style license found in the * LICENSE file in the root directory of this source tree. An additional grant * of patent rights can be found in the PATENTS file in the same directory. */ import { IndexedSequence } from './Sequence'; import { NativeArray, isNative } from './Native'; import { invariant } from './util/toolset'; export class Range extends IndexedSequence { constructor(start, end, step) { if(!(this instanceof Range)) { return new Range(start, end, step); } invariant(step !== 0, 'Can\'t create a range with steps by 0'); start = start || 0; if(end === undefined) { end = Infinity; } step = step === undefined ? 1 : Math.abs(step); // Absolute value of number 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) { return EMPTY_RANGE; } EMPTY_RANGE = this; } } } var EMPTY_RANGE;