npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@itassistors/langline

v1.1.0

Published

Node JS library to look up programming languages based on file extensions, file name and actual file

Downloads

24

Readme

langline

Node library for getting information about programming languages by supplying either of the following options,

  • Name of the language
  • File extension of the language (E.g: js, py)
  • Fully qualified file name with the extension
  • The actual file which holds the code

Add langline to a project

npm i @itassistors/langline

Importing LangLine to a project

// ES5 require
const { LangLine } = require("@itassistors/langline");

// ES6 import
import { LangLine } from "@itassistors/langline";
  • Fetching language with Extension
const { LangLine } = require("@itassistors/langline");

const extension = "js"; //file extension
const language = new LangLine().withExtension(extension);

console.log(language);
  • Fetching language with File Name
const { LangLine } = require("@itassistors/langline");

const fileName = "addRepoApi.js"; //file name with extension
const language = new LangLine().withFileName(fileName);

console.log(language);
  • Fetching language with Language Name
const { LangLine } = require("@itassistors/langline");
const language = new LangLine().withLanguageName("javascript");
console.log(language);
  • Fetching language by supplying an actual file

Note: In this method, the supplied file will be validated to check its existence and to confirm its type. If the validation fails, then the language will not be returned. This is supported only when referred in the backend as it relies on the fs module and it is not supported when used in the client side (E.g: React)

const { LangLine } = require("@itassistors/langline");
const path = require("path");

async function langline() {
  const fileName = path.join(__dirname, "..", "server.js"); //file name with extension
  const language = await new LangLine()
    .withFile(fileName)
    .then((lang) => {
      return lang;
    })
    .catch((err) => {
      console.log("ERROR: ", err);
    });

  console.log(language);
}

langline();

Output

{
  name: 'JavaScript',
  extensions: [
    '.js',   '._js',   '.bones',
    '.cjs',  '.es',    '.es6',
    '.frag', '.gs',    '.jake',
    '.jsb',  '.jscad', '.jsfl',
    '.jsm',  '.jss',   '.mjs',
    '.njs',  '.pac',   '.sjs',
    '.ssjs', '.xsjs',  '.xsjslib'
  ],
  prismIndicator: 'javascript',
  founder: ["Brendan Eich"],
  year: ["1995"]
}

The programming language dataset is adapted from github linguist language list. The founder and year data were collected using a google search web scrapping module which is available in the addons directory

Langline CLI Support

The library can also be accessed via the CLI, provided langline is installed as a global npm module. To use langline's CLI capability, install it using the following command

npm i -g @itassistors/langline

In CLI mode, langline supports the following options to fetch the details of a language

| Alias | Option | Function | | ---- | ---- | ---- | |-we| --with-extension | Lookup for a language data with file extension | |-wfn| --with-file-name | Lookup for a language data with file name | |-wf | --with-file | Lookup for a language data with actual file |

$ langline -h
Usage: langline 


  langline [option] [argument]
  langline [option] [argument] --format=[argument]


Langline CLI - Use any of the options from below

Options:
  -V, --version                  output the version number
  -we, --with-extension <type>   Lookup for a language data with file extension
  -wfn, --with-file-name <type>  Lookup for a language data with file name
  -wf, --with-file <type>        Lookup for a language data with actual file
  --format <value>               To format the output based (choices: "json", "csv", "table")
  -h, --help                     display help for command


E.g:

  # Without explicit formatting (defaults to table output)
  $ langline --with-extension c

  Output  :

  ╔══════╤════════════════╤═══════════════╤═════════════════╤════════════╗
  ║ Name │ Founder        │ Year          │ Prism Indicator │ Extensions ║
  ╟──────┼────────────────┼───────────────┼─────────────────┼────────────╢
  ║ C    │ Dennis Ritchie │ 1972 and 1973 │ c               │ .c         ║
  ║      │                │               │                 │ .cats      ║
  ║      │                │               │                 │ .h         ║
  ║      │                │               │                 │ .idc       ║
  ╚══════╧════════════════╧═══════════════╧═════════════════╧════════════╝

  # Specified formatting
  $ langline --with-file main.c --format=csv

  Output :

  "name","founder","year","prismIndicator","extensions"
  "C","Dennis Ritchie","1972 and 1973","c",".c;.cats;.h;.idc"

Prism Indicator Field

This field is for prismjs users who relies on the framework for syntax highlighting. This field will come in handy for dynamic syntax highlighting when using prism with react or other frontend development frameworks

Client side support

From v1.0.1, the library was tweaked to access the data from linguistDataSet.ts instead of reading from the JSON file to provide client side support for the library. This makes LangLine methods compatible with front-end develoment as well *

* Except the withFile method

Add a new Language

The languages are maintained in a JSON file and a CSV file. If you wish to add a new language, fork the repo and submit a PR by updating either the JSON data file or the CSV data file

JSON File

Update the JSON file linguistDataSet.json with the language specific entries

{
    "name": "NAME OF THE LANGUAGE",
    "prismIndicator": "LANGUAGE COMPONENT NAME AS SUCH IN PRISMJS LIBRARY",
    "extensions": [".extension, "..."]
}

CSV File

Update the CSV file langData.csv with the following entries

| name | extensions | prismIndicator | founder | year | | -------------------- | ------------------------------------------------------------- | --------------------------------------------------------------------- | ------------------------------------------------- | ----------------------------------- | | NAME OF THE LANGUAGE | LANGUAGE FILE EXTENSIONS. SEPARATE MULTIPLE ENTRIES WITH PIPE | IF THE LANGUAGE IS SUPPORTED BY PRISM THEN THE PRISMJS COMPONENT NAME | FOUNDER NAME. SEPARATE MULTIPLE ENTRIES WITH PIPE | INITIAL RELEASE YEAR IN YYYY FORMAT |