@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
Maintainers
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 |