-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathisSimpleCodeFile.ts
More file actions
30 lines (29 loc) · 1.13 KB
/
isSimpleCodeFile.ts
File metadata and controls
30 lines (29 loc) · 1.13 KB
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
import type { SimpleCodeFile } from "./updateCodeFile.ts";
/** Check if an object is a {@linkcode SimpleCodeFile}
*
* {@linkcode SimpleCodeFile} represents a code block in Scrapbox with:
* - filename: Name of the code file or block identifier
* - content: Code content as string or array of strings
* - lang: Optional programming language identifier
*
* This function performs runtime type checking to ensure:
* 1. Input is an object (not array or primitive)
* 2. filename is a string
* 3. content is either:
* - a string (single-line code)
* - an array of strings (multi-line code)
* - an empty array (empty code block)
* 4. lang is either undefined or a string
*/
export function isSimpleCodeFile(obj: unknown): obj is SimpleCodeFile {
if (Array.isArray(obj) || !(obj instanceof Object)) return false;
const code = obj as SimpleCodeFile;
const { filename, content, lang } = code;
return (
typeof filename == "string" &&
(typeof content == "string" ||
(Array.isArray(content) &&
(content.length == 0 || typeof content[0] == "string"))) &&
(typeof lang == "string" || lang === undefined)
);
}