-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassert_string.ts
More file actions
28 lines (26 loc) · 864 Bytes
/
assert_string.ts
File metadata and controls
28 lines (26 loc) · 864 Bytes
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
// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license.
// This module is browser compatible.
import { isString } from "https://deno.land/x/isx@1.3.1/is_string.ts";
import type { ErrorConstructorLike } from "./types.ts";
/** Assert the input is `string`.
* @param input - Any input.
* @example
* ```ts
* import { assertString } from "https://deno.land/x/assertion@$VERSION/assert_string.ts";
* import {
* assertFalse,
* assertThrows,
* } from "https://deno.land/std/testing/asserts.ts";
* assertFalse(assertString("hello world"));
* assertThrows(() => assertString(1000));
* ```
*
* @throws {Error} If the input is not `string`.
*/
export function assertString(
input: unknown,
msg?: string,
constructor: ErrorConstructorLike = Error,
): asserts input is string {
if (!isString(input)) throw new constructor(msg);
}