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

100% Statements 45/45
93.75% Branches 15/16
100% Functions 9/9
100% Lines 44/44

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 1101x               1x       36x 35x 35x     1x 1x   1x             1x 16x     1x 83x     1x 158240x     1x 553254x 553246x     553254x 553254x 553254x 553254x                                 1x   32x 32x 32x 32x 32x   32x   27x     5x 10x 10x 10x 5x       10x     5x                 1x 553254x 11x 11x     11x 11x 11x     1x  
import AbstractCharBuffer from './abstract-char-buffer';
 
/**
 * {@link AbstractCharBuffer} implementation using a [Typed Array][1] (more precisely an [Uint16Array][2]).
 *
 * [1]: https://www.khronos.org/registry/typedarray/specs/latest/
 * [2]: https://developer.mozilla.org/en-US/docs/Web/API/Uint16Array
 */
export default class TypedArrayBuffer extends AbstractCharBuffer {
	private _buffer: Uint16Array;
 
	public constructor(initCapacity: number) {
		super(initCapacity);
		initCapacity = initCapacity || 16;
		this._buffer = new Uint16Array(initCapacity);
	}
 
	public static get isSupported(): boolean {
		try {
			/* eslint-disable-next-line unicorn/prefer-spread */
			return String.fromCharCode.apply(null, Array.from(new Uint16Array())) === '';
		} catch (_) {
			/* istanbul ignore next */
			return false;
		}
	}
 
	public static fromString(string: string, transform?: (value: number, index: number, string: string) => number, thisArg?: any): TypedArrayBuffer {
		return super._fromString(new this(string.length), string, transform, thisArg);
	}
 
	public charAt(offset: number): string {
		return String.fromCharCode(this.read(offset));
	}
 
	public charCodeAt(offset: number): number {
		return this._buffer[offset];
	}
 
	public write(charCode: number, offset?: number): this {
		if (offset === undefined) {
			offset = this._length;
		}
 
		this._ensureCapacity(offset + 1);
		this._buffer[offset] = charCode;
		this._length = offset + 1 > this._length ? offset + 1 : this._length;
		return this;
	}
 
	// jshint -W101
	/**
	 * Returns the {@link String} represented by this buffer using {@link String#fromCharCode}.
	 *
	 * For details see:
	 *
	 * - [How to convert ArrayBuffer to and from String][1]
	 * - [WebKit Bug 80797 - Argument length limited to 65536 ][2]
	 *
	 * [1]: http://updates.html5rocks.com/2012/06/How-to-convert-ArrayBuffer-to-and-from-String
	 * [2]: https://bugs.webkit.org/show_bug.cgi?id=80797
	 *
	 * @return {String} The string.
	 */
	public toString(): string {
		// jshint +W101
		const ARGS_MAX = 65535;
		const len = this._length;
		let buf = '';
		let startPos = 0;
		let endPos = 0;
 
		if (len <= ARGS_MAX) {
			/* eslint-disable-next-line unicorn/prefer-spread */
			return String.fromCharCode.apply(null, Array.from(this._buffer.subarray(startPos, len)));
		}
 
		do {
			startPos = endPos;
			endPos += ARGS_MAX;
			if (endPos > len) {
				endPos = len;
			}
 
			/* eslint-disable-next-line unicorn/prefer-spread */
			buf += String.fromCharCode.apply(null, Array.from(this._buffer.subarray(startPos, endPos)));
		} while (endPos < len);
 
		return buf;
	}
 
	/**
	 * Ensures a minimum capacity.
	 * @param {Number} minCapacity The minimum capacity (i.e. the expected
	 *     {@link String#length length} of the {@link String} this buffer may
	 *     represent).
	 */
	protected _ensureCapacity(minCapacity: number): void {
		if (this._buffer.length < minCapacity) {
			Eif (minCapacity < this._buffer.length * 2) {
				minCapacity = this._buffer.length * 2; // I.e. double the capacity (!)
			}
 
			const buffer = new Uint16Array(minCapacity);
			buffer.set(this._buffer);
			this._buffer = buffer;
		}
	}
}