-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dmitryger
committed
Apr 10, 2024
1 parent
f98d053
commit e403d2a
Showing
7 changed files
with
257 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -116,3 +116,6 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
|
||
.idea |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { LanguagePattern } from "../types"; | ||
|
||
|
||
export const Typescript: LanguagePattern[] = [{ | ||
pattern: /(Readonly<|ReadonlyArray<|Array<|Record<|Pick<|Omit<|Exclude<|Extract<)/, | ||
type: "constant.dictionary" | ||
}, | ||
{ | ||
pattern: /\w+\[]/, | ||
type: "keyword.other" | ||
}, | ||
{ | ||
pattern: /:\s*\w+\s*</, | ||
type: "keyword.control" | ||
}, | ||
{ | ||
pattern: /\??:\s*(any|string|boolean|undefined|null|number|void|never|symbol|bigint)\s*/, | ||
type: "constant.type" | ||
}, | ||
{ | ||
pattern: /(let|const|var)*\s*\w*:\s*(any|string|boolean|undefined|null|number|void|never|symbol|bigint)\s*=/, | ||
type: "constant.type" | ||
}, | ||
{ pattern: /console\.log\s*\(/, type: "keyword.print" }, | ||
{ | ||
pattern: /interface\s*(\w+)\s*{/, | ||
type: "constant.type" | ||
}, | ||
{ | ||
pattern: /enum\s*\w+\s*=/, | ||
type: "constant.type" | ||
}, | ||
{ | ||
pattern: /type\s*(\w+)\s*=/, | ||
type: "constant.type" | ||
}, | ||
{ | ||
pattern: /function\s+\w+\(.*\):\s\w+\s*{/, | ||
type: "keyword.function" | ||
}, | ||
{ | ||
pattern: /\(.*\):\s\w+\s*=>\s*{/, | ||
type: "keyword.function" | ||
}, | ||
{ | ||
pattern: /(typeof|declare)\s+/, | ||
type: "keyword" | ||
}, | ||
{ | ||
pattern: /\s+as\s+/, | ||
type: "keyword" | ||
}, | ||
// Rust types | ||
{ | ||
pattern: /usize/, | ||
type: "not" | ||
}, | ||
// Kotlin | ||
{ | ||
pattern: /Array<String>/, | ||
type: "not" | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
import { test } from "uvu"; | ||
import * as assert from "uvu/assert"; | ||
import detectLang from "../src/index"; | ||
|
||
test("hello world", () => { | ||
const code = detectLang("console.log(\"Hello world!\" as string);"); | ||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test("fizz buzz", () => { | ||
const code = detectLang(` | ||
const length: number = 100 | ||
const numberArrays: number[] = Array.from({length: length}, (_, i: number) => i) | ||
numberArrays.forEach((item: number): void => { | ||
if (item % 15 === 0) console.log('FizzBuzz' as string) | ||
else if(item % 3 === 0) console.log('Fizz' as string) | ||
else if (item % 5 === 0) console.log('Buzz' as string) | ||
}) | ||
`); | ||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test("quick sort", () => { | ||
const code = detectLang(` | ||
function heapSort(arr: Array<number>): void { | ||
heapify(arr) | ||
let end: number = arr.length - 1 | ||
while (end > 0) { | ||
[arr[end], arr[0]] = [arr[0], arr[end]] | ||
end-- | ||
siftDown(arr, 0, end) | ||
} | ||
} | ||
function heapify(arr: Array<number>): void { | ||
let start: number = Math.floor(arr.length/2) - 1 | ||
while (start >= 0) { | ||
siftDown(arr, start, arr.length - 1) | ||
start-- | ||
} | ||
} | ||
function siftDown(arr: Array<number>, startPos: number, endPos: number): void { | ||
let rootPos: number = startPos | ||
while (rootPos * 2 + 1 <= endPos) { | ||
let childPos: number = rootPos * 2 + 1 | ||
if (childPos + 1 <= endPos && arr[childPos] < arr[childPos + 1]) { | ||
childPos++ | ||
} | ||
if (arr[rootPos] < arr[childPos]) { | ||
[arr[rootPos], arr[childPos]] = [arr[childPos], arr[rootPos]] | ||
rootPos = childPos | ||
} else { | ||
return | ||
} | ||
} | ||
} | ||
test('test code', (): void => { | ||
let arr: Array<number> = [12, 11, 15, 10, 9, 1, 2, 3, 13, 14, 4, 5, 6, 7, 8,] | ||
const result: Array<number> = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15] | ||
heapSort(arr) | ||
expect(arr).toStrictEqual(result) | ||
}) | ||
`); | ||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test("http server", () => { | ||
const code = detectLang(` | ||
const http = require('http'); | ||
interface Error { | ||
message: string | ||
} | ||
http.get('http://rosettacode.org', (resp: any): void => { | ||
let data: string = ''; | ||
resp.on('data', (chunk: any): void => { | ||
data += chunk; | ||
}); | ||
resp.on('end', (): void => { | ||
console.log("Data:", data); | ||
}); | ||
}).on("error", (err: Error): void => { | ||
console.log("Error: " + err.message); | ||
}); | ||
`); | ||
|
||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test("longest palindrome", () => { | ||
const code = detectLang(` | ||
function isPalindrome(s: string): boolean { | ||
return s === s.split('').reverse().join('') | ||
} | ||
function longestPalindrome(s: string): string { | ||
if (s.length < 2) | ||
return s | ||
const unique: Set<string> = new Set(s) | ||
if (unique.size === 1) { | ||
return s | ||
} | ||
let startPos: number = 0 | ||
let nextPos: number = 1 | ||
let startCharSubstring: string = s[0] | ||
let longestSubstring: string = s[0] | ||
let curSubString: string = s[0] + s[1] | ||
while (startPos !== s.length - 1) { | ||
if (startCharSubstring === s[nextPos]) { | ||
if (isPalindrome(curSubString) && curSubString.length > longestSubstring.length) { | ||
longestSubstring = curSubString | ||
} | ||
} | ||
if (nextPos === s.length - 1) { | ||
startPos += 1 | ||
nextPos = startPos + 1 | ||
curSubString = s[startPos] + s[nextPos] | ||
startCharSubstring = s[startPos] | ||
} else { | ||
nextPos += 1 | ||
curSubString += s[nextPos] | ||
} | ||
} | ||
return longestSubstring | ||
} | ||
`); | ||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test("vue 3 component code", () => { | ||
const code = detectLang(` | ||
enum VARIANT { | ||
'big'= 'BIG TITLE', | ||
'small'= 'SMALL TITLE' | ||
} | ||
type Variants = 'big' | 'small' | ||
interface Props { | ||
title?: string, | ||
isView: string, | ||
variant: Variants | ||
} | ||
interface Emits { | ||
(e: 'click', item: string): void | ||
} | ||
const props = defineProps<Props>() | ||
const emit = defineEmits<Emits>() | ||
const newTitle = ref<string>('') | ||
const changeTitle = (): void => { | ||
if (props.variant === 'big') { | ||
newTitle.value = VARIANT.big | ||
} else if (props.variant === 'small') { | ||
newTitle.value = VARIANT.small | ||
} | ||
} | ||
`); | ||
assert.equal(code.language, "Typescript"); | ||
}); | ||
|
||
test.run(); |