deno.land / std@0.167.0 / bytes / repeat.ts

نووسراو ببینە
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
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.// This module is browser compatible.import { copy } from "./copy.ts";
/** Returns a new Uint8Array composed of `count` repetitions of the `source` * array. * * If `count` is negative, a `RangeError` is thrown. * * ```ts * import { repeat } from "https://deno.land/std@$STD_VERSION/bytes/repeat.ts"; * const source = new Uint8Array([0, 1, 2]); * console.log(repeat(source, 3)); // [0, 1, 2, 0, 1, 2, 0, 1, 2] * console.log(repeat(source, 0)); // [] * console.log(repeat(source, -1)); // RangeError * ``` */export function repeat(source: Uint8Array, count: number): Uint8Array { if (count === 0) { return new Uint8Array(); }
if (count < 0) { throw new RangeError("bytes: negative repeat count"); } else if ((source.length * count) / count !== source.length) { throw new Error("bytes: repeat count causes overflow"); }
const int = Math.floor(count);
if (int !== count) { throw new Error("bytes: repeat count must be an integer"); }
const nb = new Uint8Array(source.length * count);
let bp = copy(source, nb);
for (; bp < nb.length; bp *= 2) { copy(nb.slice(0, bp), nb, bp); }
return nb;}
std

Version Info

Tagged at
a year ago