@igorskyflyer/comment-it
v3.2.0
Published
📜 Formats the provided string as a comment, either a single or a multi line comment for the given programming language. 💻
Maintainers
Readme
📃 Table of Contents
🤖 Features
- 💬 comments out code or strings using each language's native style
- 🌍 supports tons of languages via dynamic formatter lookup
- 🔍 finds formatters with fuzzy, case-insensitive matching
- 🧪 checks if a language is supported before formatting
- 🏷️ allows custom aliases for formatter reuse
- 📦 exposes all formatters for direct use or inspection
- 🔄 handles mixed separators and normalizes input
- 🚫 rejects invalid or duplicate aliases
🕵🏼 Usage
Install it by executing any of the following, depending on your preferred package manager:
pnpm add @igorskyflyer/comment-ityarn add @igorskyflyer/comment-itnpm i @igorskyflyer/comment-it🤹🏼 API
getLanguageIds()
getLanguageIds(): string[]Gets IDs of all available language formatters. Language formatters are callable as comment.<languageId>. See more below at comment.
language()
language(id: string): CommentFormatter | nullPerforms a case-insensitive search for a language formatter with the provided id and returns it - if one is found - else returns null.
supportsLanguage()
supportsLanguage(id: string): booleanReturns whether the given language formatter is supported, case-insensitive.
See the comment object below for available valid identifiers.
alias()
alias(id: CommentLanguage, alias: string): booleanAdds an alias for an existing comment formatter. Returns true upon success, false otherwise.
comment
An object where all formatters are stored.
comment's properties/formatters:
comment.ada- Adacomment.bash- Bashcomment.batch- Batchcomment.c- Ccomment.carbon- Carboncomment.cSharp- C#comment.coffeeScript- CoffeeScriptcomment.cpp- C++comment.crystal- Crystalcomment.css- CSScomment.dart- Dartcomment.delphi- Delphicomment.dockerFile- Dockerfilecomment.elixir- Elixircomment.erlang- Erlangcomment.euphoria- Euphoriacomment.fortran- Fortrancomment.fSharp- F#comment.genie- Geniecomment.go- Gocomment.groovy- Groovycomment.hack- Hackcomment.haskell- Haskellcomment.html- HTMLcomment.icon- Iconcomment.java- Javacomment.javaScript- JavaScriptcomment.jsx- JSXcomment.julia- Juliacomment.kotlin- Kotlincomment.lisp- Lispcomment.liveCode- LiveCodecomment.lua- Luacomment.maple- Maplecomment.matlab- MATLABcomment.mercury- Mercurycomment.mql4- MQL4comment.objectiveC- Objective-Ccomment.objectiveCpp- Objective-C++comment.oz- Ozcomment.pascal- Pascalcomment.perl- Perlcomment.php- PHPcomment.powerShell- PowerShellcomment.pug- Pug/Jadecomment.python- Pythoncomment.q- Qcomment.r- Rcomment.razor- Razorcomment.red- Redcomment.ring- Ringcomment.ruby- Rubycomment.rust- Rustcomment.scala- Scalacomment.sql- SQLcomment.swift- Swiftcomment.typeScript- TypeScriptcomment.vala- Valacomment.visualBasic- VisualBasiccomment.vue- Vuecomment.vueHtml- VueHtmlcomment.xml- XML
Each formatter exposes two functions, single() for single-line comments and multi() for multi-line comments.
single()
single(value: string): stringReturns a single-line comment formatted for the selected language.
multi()
multi(value: string): stringReturns a multi-line comment formatted for the selected language.
🗒️ Examples
import { comment, supportsLanguage } from '@igorskyflyer/comment-it'
const singleLine: string = 'hello world'
const multiLine: string = `hello
world
this is a test`
console.log(comment.javaScript.single(singleLine)) // prints '// hello world'
console.log(comment.jsx.single(singleLine)) // prints '{/* hello world */}'
console.log(comment.coffeeScript.multi(multiLine)) // prints '###\nhello\n\n\nworld\n\nthis is a test\n###'
// note: new lines in the example results are written as-is for brevity
console.log(supportsLanguage('lua')) // prints true
console.log(supportsLanguage('TYPEscript')) // prints true
console.log(supportsLanguage('foo')) // prints false📝 Changelog
📑 The changelog is available here, CHANGELOG.md.
🪪 License
Licensed under the MIT license which is available here, MIT license.
💖 Support
🧬 Related
🗺 Zero-dependency Map and RegExp based string replacer with Unicode support. 🍁
🕶️ Reads a JSON file into a Map. 🌻
@igorskyflyer/strip-html-headings
🍛 Strips HTML headings! 🍤
⛸ Strips Markdown headings! 🏹
🥽 Provides ways of parsing UNC paths and checking whether they are valid. 🎱
👨🏻💻 Author
Created by Igor Dimitrijević (@igorskyflyer).
