diff --git a/.eslintrc.json b/.eslintrc.json index f96c8f6..159b5a7 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,7 +4,7 @@ "es2021": true, "node": true }, - "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"], + "extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended", "prettier"], "parser": "@typescript-eslint/parser", "parserOptions": { "ecmaVersion": 12, diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..305ab0d --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +custom: ["https://saweria.co/teknologiumum"] diff --git a/README.md b/README.md index 4fa20ef..3e4ac20 100644 --- a/README.md +++ b/README.md @@ -1,73 +1,41 @@ -# Language Detector +# Flourite - Language detector A fork from [ts95/lang-detector](https://github.com/ts95/lang-detector), rewritten in Typescript with more language support. -A fast and small library for detecting the programming language of a code snippet. -Can be used for strings of code spanning multiple thousand lines. +Detects a programming language from a given string. -This library should only be used if you don't have anything else to go by to determine the language of the code, like a file extension. - -## Demo - -[Here](https://hosein2398.github.io/lang-detect/) you can see demo of this project. +- Built-in support for CommonJS and ESM format +- Built-in Typescript typings +- No external dependencies ## Detectable languages -- JavaScript -- C -- C++ -- Python -- Java -- HTML -- CSS -- Ruby -- Go -- PHP +| Languages | | | +| --------- | ---------- | ------ | +| C | Javascript | Python | +| C++ | Java | Ruby | +| HTML | CSS | PHP | ## Install -```Shell -npm install lang-detector --save +```bash +$ npm install flourite ``` ## Usage -```JavaScript -/** - * function detectLang(snippet, options) { ... } - * - * @snippet {String} The code snippet. - * @options {Object} (Optional) { - * heuristic: {Boolean} Enable heuristic optimisation for better performance. `true` by default. - * statistics: {Boolean} Return statistics. `false` by default. - * } - * @return {String} (Name of the detected language) or {Object} (Statistics). - */ -var detectLang = require('lang-detector'); +```js +import detectLang from 'flourite'; -detectLang('List things = new ArrayList<>();') - // => 'Java' -detectLang('console.log("Hello world");') - // => 'JavaScript' -detectLang('Hello world.', { statistics: true }) - /* => { - "detected": "Unknown", - "statistics": { - "JavaScript": 0, - "C": 0, - "C++": 0, - "Python": 0, - ... - "Unknown": 1 - } - } - */ +const code = detectLang('console.log("Hello World");'); // => Javascript ``` -## Unit tests +## Development -Run `npm test` in the root of the directory to run the tests. +- Use the Node.js version as defined on the `.nvmrc` file. +- Run `npm run test:tdd` to initiate a test driven development environment. +- Run `npm run lint` and `npm run format` before commit a change. ## License -MIT © Toni Sučić +[MIT](./LICENSE) diff --git a/package-lock.json b/package-lock.json index 41928df..613e731 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,11 @@ { - "name": "language-detector", - "version": "1.0.6", + "name": "flourite", + "version": "1.0.0", "lockfileVersion": 2, "requires": true, "packages": { "": { - "name": "language-detector", - "version": "1.0.6", + "version": "1.0.0", "license": "MIT", "devDependencies": { "@rollup/plugin-typescript": "^8.2.5", diff --git a/package.json b/package.json index e2b144d..9f3e390 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,22 @@ { - "name": "language-detector", - "version": "1.0.6", + "name": "flourite", + "version": "1.0.0", "description": "A library for detecting the programming language of a code snippet.", - "main": "dist/index.js", + "main": "dist/index.cjs", + "module": "dist/index.mjs", + "types": "dist/index.d.ts", + "exports": { + ".": { + "import": "./dist/index.mjs", + "require": "./dist/index.cjs" + }, + "./package.json": "./package.json", + "./*": "./*" + }, "scripts": { "lint": "eslint --fix --ignore-path .gitignore .", "format": "prettier --write --ignore-path .gitignore .", - "prepare": "node prepare.cjs", + "prepare": "husky install", "test:unit": "uvu -r esbuild-register tests \".(test|spec).ts\"", "test:coverage": "c8 npm run test:unit", "test:tdd": "npm run test:unit; watchlist src/ -- npm run test:unit", @@ -18,7 +28,7 @@ ], "repository": { "type": "git", - "url": "https://github.com/teknologi-umum/language-detector" + "url": "https://github.com/teknologi-umum/flourite" }, "keywords": [ "programming", @@ -45,9 +55,13 @@ ], "license": "MIT", "bugs": { - "url": "https://github.com/teknologi-umum/language-detector/issues" + "url": "https://github.com/teknologi-umum/flourite/issues" + }, + "homepage": "https://github.com/teknologi-umum/flourite#readme", + "directories": { + "lib": "./src", + "test": "./tests" }, - "homepage": "https://github.com/teknologi-umum/language-detector#readme", "devDependencies": { "@rollup/plugin-typescript": "^8.2.5", "@types/node": "^16.4.13", diff --git a/prepare.cjs b/prepare.cjs deleted file mode 100644 index 7e3d30f..0000000 --- a/prepare.cjs +++ /dev/null @@ -1,12 +0,0 @@ -// eslint-disable-next-line @typescript-eslint/no-var-requires -const { exec } = require('child_process'); - -if (process.env.NODE_ENV === 'production') { - process.exit(0); -} else { - exec('husky install', (error, stdout, stderr) => { - if (error) throw error; - console.log(stdout); - console.log(stderr); - }); -} diff --git a/src/index.ts b/src/index.ts index 0ec76c5..eb39914 100644 --- a/src/index.ts +++ b/src/index.ts @@ -48,15 +48,16 @@ const languages: Record = { }; /** - * TODO: FILL THIS + * Detects a programming language from a given string. * @param {String} snippet The code we're guessing * @param {Options} options Options - * @returns {String | StatisticOutput} + * @returns {String|StatisticOutput} A String or a StatisticOutput format if `statistics: true` * @example * ```js - * import detectLang from 'package-name'; + * import detectLang from 'flourite'; * const detect = detectLang(code); * ``` + * @see Supported Languages - https://github.com/teknologi-umum/flourite#detectable-languages */ function detectLang( snippet: string, @@ -114,4 +115,5 @@ function detectLang( return bestResult.language; } +export type { Options, StatisticOutput }; export default detectLang; diff --git a/src/points.ts b/src/points.ts index 5c9829f..78532d0 100644 --- a/src/points.ts +++ b/src/points.ts @@ -1,7 +1,7 @@ import type { LanguagePattern } from './types'; /** - * TODO: FILL THIS + * Get points from a language using regular expressions. * @param {String} lineOfCode * @param {LanguagePattern[]} checkers * @returns {Number} @@ -16,7 +16,7 @@ export function getPoints(lineOfCode: string, checkers: LanguagePattern[]): numb } /** - * TODO: FILL THIS + * Checks if a given string is near top of the code or not. * @param {Number} index * @param {String[]} linesOfCode * @returns {Boolean}