xterm
[VSoRC/.git] / node_modules / xterm / src / common / TypedArrayUtils.ts
1 /**
2  * Copyright (c) 2018 The xterm.js authors. All rights reserved.
3  * @license MIT
4  */
5
6 export type TypedArray = Uint8Array | Uint16Array | Uint32Array | Uint8ClampedArray
7   | Int8Array | Int16Array | Int32Array
8   | Float32Array | Float64Array;
9
10
11 /**
12  * polyfill for TypedArray.fill
13  * This is needed to support .fill in all safari versions and IE 11.
14  */
15 export function fill<T extends TypedArray>(array: T, value: number, start?: number, end?: number): T {
16   // all modern engines that support .fill
17   if (array.fill) {
18     return array.fill(value, start, end) as T;
19   }
20   return fillFallback(array, value, start, end);
21 }
22
23 export function fillFallback<T extends TypedArray>(array: T, value: number, start: number = 0, end: number = array.length): T {
24   // safari and IE 11
25   // since IE 11 does not support Array.prototype.fill either
26   // we cannot use the suggested polyfill from MDN
27   // instead we simply fall back to looping
28   if (start >= array.length) {
29     return array;
30   }
31   start = (array.length + start) % array.length;
32   if (end >= array.length) {
33     end = array.length;
34   } else {
35     end = (array.length + end) % array.length;
36   }
37   for (let i = start; i < end; ++i) {
38     array[i] = value;
39   }
40   return array;
41 }
42
43 /**
44  * Concat two typed arrays `a` and `b`.
45  * Returns a new typed array.
46  */
47 export function concat<T extends TypedArray>(a: T, b: T): T {
48   const result = new (a.constructor as any)(a.length + b.length);
49   result.set(a);
50   result.set(b, a.length);
51   return result;
52 }