Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lang-detector

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lang-detector

A library for detecting the programming language of a code snippet.

  • 1.0.6
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
808
decreased by-53.21%
Maintainers
1
Weekly downloads
 
Created
Source

lang-detector

NPM

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 lang-detector --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).
 */
var detectLang = require('lang-detector');

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.

License

MIT © Toni Sučić

Keywords

FAQs

Package last updated on 03 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc