Skip to content

Commit 19a3d83

Browse files
committed
💪 Use unknownutil functions internally
1 parent 2664d95 commit 19a3d83

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

denops_std/buffer/buffer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Denops } from "https://deno.land/x/denops_core@v5.0.0/mod.ts";
2+
import { maybe } from "https://deno.land/x/unknownutil@v3.10.0/mod.ts#^";
23
import * as autocmd from "../autocmd/mod.ts";
34
import * as batch from "../batch/mod.ts";
45
import * as fn from "../function/mod.ts";
@@ -7,7 +8,7 @@ import { execute } from "../helper/mod.ts";
78
import {
89
FileFormat,
910
findFileFormat,
10-
maybeFileFormat,
11+
isFileFormat,
1112
splitText,
1213
} from "./fileformat.ts";
1314
import { tryDecode } from "./fileencoding.ts";
@@ -284,7 +285,7 @@ export async function decode(
284285
} else {
285286
[enc, text] = tryDecode(data, fileencodings);
286287
}
287-
const ff = maybeFileFormat(options.fileformat) ??
288+
const ff = maybe(options.fileformat, isFileFormat) ??
288289
findFileFormat(text, fileformats) ?? fileformat;
289290
return {
290291
content: splitText(text, ff),

denops_std/buffer/fileformat.ts

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,30 @@
1-
import { is } from "https://deno.land/x/unknownutil@v3.10.0/mod.ts#^";
1+
import {
2+
assert,
3+
ensure,
4+
is,
5+
maybe,
6+
PredicateType,
7+
} from "https://deno.land/x/unknownutil@v3.10.0/mod.ts#^";
28

3-
export type FileFormat = "unix" | "dos" | "mac";
9+
export const isFileFormat = is.LiteralOneOf(["unix", "dos", "mac"] as const);
10+
11+
export type FileFormat = PredicateType<typeof isFileFormat>;
12+
13+
export const assertFileFormat = (v: unknown): asserts v is FileFormat =>
14+
assert(v, isFileFormat);
15+
16+
export const ensureFileFormat = (v: unknown): FileFormat =>
17+
ensure(v, isFileFormat);
18+
19+
export const maybeFileFormat = (v: unknown): FileFormat | undefined =>
20+
maybe(v, isFileFormat);
421

522
const fileFormatDelimiters = {
623
unix: "\n",
724
dos: "\r\n",
825
mac: "\r",
926
};
1027

11-
export function isFileFormat(v: unknown): v is FileFormat {
12-
return is.String(v) && v in fileFormatDelimiters;
13-
}
14-
15-
export function assertFileFormat(v: unknown): asserts v is FileFormat {
16-
if (!isFileFormat(v)) {
17-
throw new Error(`Unknown fileformat '${v}' is specified`);
18-
}
19-
}
20-
21-
export function ensureFileFormat(v: unknown): FileFormat {
22-
assertFileFormat(v);
23-
return v;
24-
}
25-
26-
export function maybeFileFormat(v: unknown): FileFormat | undefined {
27-
if (isFileFormat(v)) {
28-
return v;
29-
}
30-
return undefined;
31-
}
32-
3328
/**
3429
* Split text as Text File in POSIX.
3530
*/

0 commit comments

Comments
 (0)