All files / app/src string-array-buffer.ts

100% Statements 23/23
100% Branches 8/8
100% Functions 8/8
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 511x         1x       36x 35x 35x     1x 1x     1x 16x                 1x 553254x 553246x     553254x 553254x 553254x     1x 158157x     1x 83x     1x 32x   1x  
import AbstractCharBuffer from './abstract-char-buffer';
 
/**
 * {@link AbstractCharBuffer} implementation using an {@link Array} of {@link String}s.
 */
export default class StringArrayBuffer extends AbstractCharBuffer {
	private _buffer: string[];
 
	public constructor(initCapacity: number) {
		super(initCapacity);
		initCapacity = initCapacity || 16;
		this._buffer = new Array(initCapacity);
	}
 
	public static get isSupported(): boolean {
		return true;
	}
 
	public static fromString(string: string, transform?: (value: number, index: number, string: string) => number, thisArg?: any): StringArrayBuffer {
		return super._fromString(new this(string.length), string, transform, thisArg);
	}
 
	/**
	 * Write a charCode to the buffer using {@link String#fromCharCode} and {@link Array#push []}.
	 *
	 * @param charCode The charCode to append.
	 * @param offset The zero based offset to write at.
	 */
	public write(charCode: number, offset?: number): this {
		if (typeof offset === 'undefined') {
			offset = this._length;
		}
 
		this._buffer[offset] = String.fromCharCode(charCode);
		this._length = offset + 1 > this._length ? offset + 1 : this._length;
		return this;
	}
 
	public charCodeAt(offset: number): number {
		return this._buffer[offset].charCodeAt(0);
	}
 
	public charAt(offset: number): string {
		return this._buffer[offset];
	}
 
	public toString(): string {
		return this._buffer.slice(0, this._length).join('');
	}
}