Skip to content

Commit

Permalink
chore: merged with master
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinaldy Rafli committed Aug 16, 2021
2 parents d3e000b + 8e7f7c0 commit f6f5ddc
Show file tree
Hide file tree
Showing 12 changed files with 238 additions and 82 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
custom: ["https://saweria.co/teknologiumum"]
Empty file modified .husky/pre-commit
100755 → 100644
Empty file.
72 changes: 20 additions & 52 deletions README.md
Original file line number Diff line number Diff line change
@@ -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<String> 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

<a href="https://tldrlegal.com/license/mit-license" target="_blank">MIT</a> © <a href="https://github.com/ts95/" target="_blank">Toni Sučić</a>
[MIT](./LICENSE)
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 21 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -18,7 +28,7 @@
],
"repository": {
"type": "git",
"url": "https://github.com/teknologi-umum/language-detector"
"url": "https://github.com/teknologi-umum/flourite"
},
"keywords": [
"programming",
Expand All @@ -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",
Expand Down
12 changes: 0 additions & 12 deletions prepare.cjs

This file was deleted.

10 changes: 7 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { PHP } from './languages/php';
import { Python } from './languages/python';
import { Ruby } from './languages/ruby';
import { Rust } from './languages/rust';
import { SQL } from './languages/sql';
import { nearTop, getPoints } from './points';
import type { LanguagePattern, Options, StatisticOutput } from './types';

Expand Down Expand Up @@ -44,19 +45,21 @@ const languages: Record<string, LanguagePattern[]> = {
Python,
Ruby,
Rust,
SQL,
Unknown: [],
};

/**
* 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,
Expand Down Expand Up @@ -114,4 +117,5 @@ function detectLang(
return bestResult.language;
}

export type { Options, StatisticOutput };
export default detectLang;
22 changes: 22 additions & 0 deletions src/languages/sql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import type { LanguagePattern } from '../types';

export const SQL: LanguagePattern[] = [
{ pattern: /CREATE (TABLE|DATABASE)/, points: 2, nearTop: true },
{ pattern: /DROP (TABLE|DATABASE)/, points: 2, nearTop: true },
{ pattern: /SHOW DATABASES/, points: 2, nearTop: true },
{ pattern: /INSERT INTO/, points: 2 },
{ pattern: /(SELECT|SELECT DISTINCT)\s/, points: 1 },
{ pattern: /INNER JOIN/, points: 1 },
{ pattern: /GROUP BY/, points: 1 },
{ pattern: /(END;|COMMIT;)/, points: 1 },

{ pattern: /UPDATE\s+\w+\sSET/, points: 1 },
{ pattern: /VALUES+(\s+\(\w|\(\w)/, points: 1 },
// Comments
{ pattern: /--\s\w/, points: 1 },
// Data types
{ pattern: /(VARCHAR|CHAR|BINARY|VARBINARY|BLOB|TEXT)\([0-9]+\)/, points: 1 },
{ pattern: /(BIT|TINYINT|SMALLINT|MEDIUMINT|INT|INTEGER|BIGINT|DOUBLE)\([0-9]+\)/, points: 1 },
{ pattern: /(TINYBLOB|TINYTEXT|MEDIUMTEXT|MEDIUMBLOB|LONGTEXT|LONGBLOB)/, points: 1 },
{ pattern: /(BOOLEAN|BOOL|DATE|YEAR)/, points: 1 },
];
4 changes: 2 additions & 2 deletions src/points.ts
Original file line number Diff line number Diff line change
@@ -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}
Expand All @@ -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}
Expand Down
2 changes: 1 addition & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export interface LanguagePattern {

export interface Options {
heuristic: boolean;
statistics: false;
statistics: boolean;
}

export interface StatisticOutput {
Expand Down
Loading

0 comments on commit f6f5ddc

Please sign in to comment.