which-programming-lang
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

which-programming-lang

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.

Detectable languages

  • JavaScript
  • C
  • C++
  • Python
  • Java
  • HTML
  • CSS
  • Ruby
  • Go
  • PHP

Install

npm install which-programming-lang --save

Usage

/**
 * 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).
 */
import {detectLang} from 'which-programming-lang';

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
                }
            } 
     */

Unit tests

Run npm test in the root of the directory to run the tests.

Credits

Thanks to:

  • The lang-detector project created by @ts95 which is the pioneer of programming language detector and inspired which-programming-lang.

License

MIT © wflixu

Package Sidebar

Install

npm i which-programming-lang

Weekly Downloads

2

Version

0.1.0

License

MIT

Unpacked Size

91.8 kB

Total Files

11

Last publish

Collaborators

  • wflixu