immutable-js
Version:
Immutable types in JavaScript
66 lines (57 loc) • 1.71 kB
JavaScript
/**
* 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 { Sequence } from './Sequence';
import { equal, nullOrUndefined } from './util/is';
export class Repeat extends Sequence {
constructor(value, repeater) {
if(!(this instanceof Repeat)) {
return new Repeat(value, repeater);
}
this.__value = value;
this.size = repeater === undefined ? Infinity : Math.max(0, repeater);
if(this.size === 0) {
if(EMPTY_REPEAT) {
return EMPTY_REPEAT;
}
EMPTY_REPEAT = this;
}
}
toString() {
return this.__toString('Repeat [', this.__value + ' ' + this.size + ' times', ']');
}
includes(search) {
return equal(this.__value, search);
}
indexOf(search) {
if(this.includes(search)) {
return 0;
}
return -1;
}
__iterate(handle) {
for(var ii = 0; ii < this.size; ii++) {
if(handle(this.__value, ii, this) === false) {
return ii + 1;
}
}
return ii;
}
__ensureOwner(ownerID) {
if(ownerID === this.__ownerID) {
return this;
}
if(!ownerID || nullOrUndefined(ownerID)) {
this.__ownerID = ownerID;
this.__altered = false;
return this;
}
return new Repeat(this.__value, this.size, ownerID);
}
}
var EMPTY_REPEAT;