https://github.com/ts95/lang-detector
A library for detecting the programming language of a code snippet.
https://github.com/ts95/lang-detector
Last synced: over 1 year ago
JSON representation
A library for detecting the programming language of a code snippet.
- Host: GitHub
- URL: https://github.com/ts95/lang-detector
- Owner: ts95
- License: mit
- Created: 2015-03-29T08:24:30.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2022-12-13T20:06:06.000Z (over 3 years ago)
- Last Synced: 2025-04-12T03:38:18.870Z (over 1 year ago)
- Language: JavaScript
- Homepage:
- Size: 2.94 MB
- Stars: 52
- Watchers: 2
- Forks: 12
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
lang-detector [](https://travis-ci.org/hosein2398/lang-detector) [](https://www.npmjs.com/package/lang-detector) [](LICENSE)
=====

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.
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.
## Detectable languages
* JavaScript
* C
* C++
* Python
* Java
* HTML
* CSS
* Ruby
* Go
* PHP
## Install
```Shell
npm install lang-detector --save
```
## 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');
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
}
}
*/
```
## Unit tests
Run `npm test` in the root of the directory to run the tests.
## License
MIT © Toni Sučić