feat(std/bytes): add hasSuffix and contains functions, update docs (#4801)

This commit is contained in:
Ali Hasani 2020-05-20 19:02:28 +04:30 committed by GitHub
parent 91d576aa5a
commit ef14d62462
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 86 additions and 29 deletions

View file

@ -5,8 +5,10 @@ import {
findLastIndex,
equal,
hasPrefix,
hasSuffix,
repeat,
concat,
contains,
} from "./mod.ts";
import { assertEquals, assertThrows, assert } from "../testing/asserts.ts";
import { encode, decode } from "../encoding/utf8.ts";
@ -24,6 +26,11 @@ Deno.test("[bytes] findIndex2", () => {
assertEquals(i, 1);
});
Deno.test("[bytes] findIndex3", () => {
const i = findIndex(encode("Deno"), encode("D"));
assertEquals(i, 0);
});
Deno.test("[bytes] findLastIndex1", () => {
const i = findLastIndex(
new Uint8Array([0, 1, 2, 0, 1, 2, 0, 1, 3]),
@ -47,6 +54,11 @@ Deno.test("[bytes] hasPrefix", () => {
assertEquals(v, true);
});
Deno.test("[bytes] hasSuffix", () => {
const v = hasSuffix(new Uint8Array([0, 1, 2]), new Uint8Array([1, 2]));
assertEquals(v, true);
});
Deno.test("[bytes] repeat", () => {
// input / output / count / error message
const repeatTestCase = [
@ -97,3 +109,11 @@ Deno.test("[bytes] concat empty arrays", () => {
assert(u1 !== joined);
assert(u2 !== joined);
});
Deno.test("[bytes] contain", () => {
const source = encode("deno.land");
const pattern = encode("deno");
assert(contains(source, pattern));
assert(contains(new Uint8Array([0, 1, 2, 3]), new Uint8Array([2, 3])));
});