All files / app/src abstract-char-buffer.ts

100% Statements 43/43
100% Branches 12/12
91.67% Functions 11/12
100% Lines 39/39

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190      5x           5x                 180x 5x     175x                                               5x 80x     80x 40x 790340x     40x 790340x       80x           5x 790574x                   40x               5x 2212984x                     5x 790619x                   5x 15x 5x     10x 20x                     5x 50x 5x     45x   45x 790350x     45x             5x 40x 20x     20x     5x 45x                                                                       5x  
/**
 * Base class for all CharBuffers.
 */
export default abstract class AbstractCharBuffer {
	protected _length: number;
 
	/**
	 * Indicates whether this CharBuffer is supported by the current platform.
	 */
	public static get isSupported(): boolean {
		/* istanbul ignore next */
		return false;
	}
 
	/**
	 * @param initCapacity The initial capacity (i.e. the expected {@link String#length length} of the {@link String} represented by this buffer).
	 */
	protected constructor(initCapacity: number) {
		if (initCapacity < 0) {
			throw new RangeError(`initCapacity must be non-negative, ${initCapacity} given.`);
		}
 
		this._length = 0;
	}
 
	/**
	 * Creates a new CharBuffer from a {@link String}.
	 *
	 * @param output
	 * @param string The string.
	 * @param transform Function that produces a charCode for the new CharBuffer from a charCode of the string parameter.
	 * @param thisArg
	 *
	 * ```
	 * var charBuffer;
	 *
	 * charBuffer = CharBuffer.fromString('abc');
	 * console.log(charBuffer.toString()); // output: abc
	 *
	 * charBuffer = CharBuffer.fromString('abc', function (charCode, index){
	 *     return charCode + 3;
	 * });
	 *
	 * console.log(charBuffer.toString()); // output: def
	 * ```
	 */
	protected static _fromString<T extends AbstractCharBuffer>(output: T, string: string, transform?: (value: number, index: number, string: string) => number, thisArg?: any): T {
		const len = string.length;
 
		// Manual loop optimization :-)
		if (transform) {
			for (let i = 0; i < len; i++) {
				output.append(transform.call(thisArg, string.charCodeAt(i), i, string));
			}
		} else {
			for (let i = 0; i < len; i++) {
				output.append(string.charCodeAt(i));
			}
		}
 
		return output;
	}
 
	/**
	 * Length of the {@link String} represented by this buffer.
	 */
	public get length(): number {
		return this._length;
	}
 
	/**
	 * Sets the length of the {@link String} represented by this buffer.
	 *
	 * @param {Number} newLength The new length.
	 * @throws {RangeError} if `newLength < 0 || newLength > this.length`
	 */
	public set length(newLength: number) {
		this.setLength(newLength);
	}
 
	/**
	 * Appends a charCode to the buffer. The length of the buffer increases by 1.
	 *
	 * @param charCode The charCode to append.
	 */
	public append(charCode: number): this {
		return this.write(charCode);
	}
 
	/**
	 * Reads the charCode at an offset.
	 *
	 * @param offset The zero based offset.
	 * @return The charCode.
	 *
	 * @throws if offset < 0 or offset >= this.length
	 */
	public read(offset: number): number {
		return this.charCodeAt(offset);
	}
 
	/**
	 * Executes a function once per charCode.
	 * See also {@link Array#forEach}
	 *
	 * @param callback Function to execute for each charCode.
	 * @param thisArg Value to use as this when executing callback.
	 */
	public forEach(callback: (value: number, index: number, buffer: this) => void, thisArg?: any): void {
		if (typeof callback !== 'function') {
			throw new TypeError(`${callback} is not a function`);
		}
 
		for (let i = 0; i < this.length; i++) {
			callback.call(thisArg, this.read(i), i, this);
		}
	}
 
	/**
	 * Creates a new CharBuffer with the results of calling a provided function on every charCode.
	 * See also {@link Array#map}
	 *
	 * @param callback Function to execute for each charCode.
	 * @param thisArg Value to use as this when executing callback.
	 */
	public map(callback: (value: number, index: number, buffer: this) => number, thisArg?: any): this {
		if (typeof callback !== 'function') {
			throw new TypeError(`${callback} is not a function`);
		}
 
		const output = this.clone();
 
		for (let i = 0; i < this.length; i++) {
			output.append(callback.call(thisArg, this.read(i), i, this));
		}
 
		return output;
	}
 
	/**
	 * Override this to observe changes.
	 * @param newLength
	 */
	protected setLength(newLength: number): void {
		if (newLength < 0 || newLength > this.length) {
			throw new RangeError(`newLength must be between 0 and ${this.length}, ${newLength} given.`);
		}
 
		this._length = newLength;
	}
 
	protected clone(): this {
		return new (this.constructor as any)(this.length);
	}
 
	/**
	 * Writes a charCode to the buffer at an offset.
	 *
	 * @param charCode charCode The charCode to write.
	 * @param offset offset The zero based offset to write at.
	 *
	 * @throws if offset < 0 or offset > this.length
	 */
	public abstract write(charCode: number, offset?: number): this;
 
	/**
	 * Reads the charCode at an offset.
	 *
	 * @param offset The zero based offset.
	 * @return The charCode.
	 *
	 * @throws if offset < 0 or offset >= this.length
	 */
	public abstract charCodeAt(offset: number): number;
 
	/**
	 * Reads the char at an offset.
	 *
	 * @param offset The zero based offset.
	 * @return The char.
	 * @throws {Error} if offset < 0 or offset >= this.length
	 */
	public abstract charAt(offset: number): string;
 
	/**
	 * Returns the {@link String} represented by this buffer.
	 */
	public abstract toString(): string;
}