Spaces:
Sleeping
Sleeping
Download web/src/languages.js from brianbellx/codehygiene: direct link, hf CLI and curl.
- Browser
- Download file 1.36 kB
-
https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/languages.js
- Command line
-
hf download hf://spaces/brianbellx/codehygiene/web/src/languages.js
-
curl -L -o languages.js https://huggingface.co/spaces/brianbellx/codehygiene/resolve/main/web/src/languages.js
1.36 kB
| const SNIFF_RULES = [ | |
| { | |
| extension: "go", | |
| linePrefixes: ["package ", "func "], | |
| fragments: [" := "], | |
| }, | |
| { | |
| extension: "rs", | |
| linePrefixes: ["fn ", "pub fn ", "let mut ", "impl ", "use std::"], | |
| fragments: ["-> Option<", "-> Result<"], | |
| }, | |
| { | |
| extension: "ts", | |
| linePrefixes: ["interface ", "export interface ", "type ", "export type "], | |
| fragments: [": string", ": number", ": boolean"], | |
| }, | |
| { | |
| extension: "js", | |
| linePrefixes: ["const ", "let ", "function ", "export function ", "export default "], | |
| fragments: [" => "], | |
| }, | |
| { | |
| extension: "py", | |
| linePrefixes: ["def ", "class ", "import ", "from ", "elif "], | |
| fragments: ["self."], | |
| }, | |
| ]; | |
| function matchesRule(rule, trimmedLines, text) { | |
| const startsRule = (line) => rule.linePrefixes.some((prefix) => line.startsWith(prefix)); | |
| return trimmedLines.some(startsRule) || rule.fragments.some((fragment) => text.includes(fragment)); | |
| } | |
| export function extensionOf(filename) { | |
| const dotIndex = filename.lastIndexOf("."); | |
| return dotIndex <= 0 ? "" : filename.slice(dotIndex + 1).toLowerCase(); | |
| } | |
| export function sniffExtension(text) { | |
| const trimmedLines = text.split("\n").map((line) => line.trimStart()); | |
| const rule = SNIFF_RULES.find((candidate) => matchesRule(candidate, trimmedLines, text)); | |
| return rule ? rule.extension : "py"; | |
| } | |