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

command-tags

v1.1.6

Published

A way to parse custom tags/flags/input/options from a string.

Downloads

14

Readme

command-tags

Parse custom tags/input/options that appear anywhere in a string.

Options

string: The string to parse command tags from.

prefix: The prefix that would recognise a word as a tag. This can be a String or Regular Expression. e.g "--big", "--" being the prefix. Set to match any amount of -s by default.

numbersInStrings: Whether or not to match numbers too when you pass String into the Tag object. e.g "hello2" will match the whole word with this enabled, and will just match "hello" with this disabled. Set to true by default.

removeAllTags: Whether or not to should remove every word that starts with the prefix, but only match valid tags. Invalid tags will be removed but not added to the matches array. Set to false by default.

negativeNumbers: Whether or not negative numbers can be matched if only looking for a number. Set to true by default.

numberDoubles: Whether or not doubles can be matched, such as 23.90. Set to false by default.

tagData: Default types that matches tags should be parsed into.

lowercaseTags: Whether or not matched tags should be returned in lowercase. e.g: match HELLOWORLD and helloWorld and return as helloworld. Defaults to true

Examples

const Tagify = require("command-tags")

// Check for tags
Tagify({
  string: "Hello world! --bold --underline",
  prefix: "--"
}, "bold", "italic", "strikethrough", "underline")

/*
Returns:
{
  string: "Hello world! --bold --underline",
  newString: "Hello world!",
  matches: ["bold", "underline"]
  data: {}
}
*/


// Check for tags and values
Tagify({
  string: "Draw -name Painting -price 20 -width 1080 -height 1440 -paintbrush",
  prefix: "-"
}, "paintbrush", { tag: "price", value: Number }, { tag: "width", value: Number }, { tag: "name", value: String }, { tag: "height", value: Number })

// or..
Tagify({
  string: "Draw -name Painting -price 20 -width 1080 -height 1440 -paintbrush",
  prefix: "-"
}, "paintbrush", { price: Number }, { width: Number }, { name: String }, { height: Number })

// OR..
Tagify({
  string: "Draw -name Painting -price 20 -width 1080 -height 1440 -paintbrush",
  prefix: "-"
}, "paintbrush", { price: Number, width: Number, name: String, height: Number })

/*
Returns:
{
  string: "Draw -name Painting -price 20 -width 1080 -height 1440",
  newString: "Draw",
  matches: ["name", "price", "width", "height", "paintbrush"]
  data: { name: "Painting", price: 20, width: 1080, height: 1440 }
}
*/



// Invalid tag use example
Tagify("Does fhing work lol --invalidtag wait its --tag1 isnt it!", "tag1")

/*
{
  string: "Does fhing work lol --invalidtag wait its --tag1 isnt it!"
  newString: "Does fhing work lol --invalidtag wait its isnt it!"
  matches: ["tag1"]
  data: {}
}
*/
// Invalid tag use example, but with removeAllTags option enabled.
Tagify({
  string: "Does fhing work lol --invalidtag wait its --tag1 isnt it!",
  removeAllTags: true
}, "tag1")

/*
{
  string: "Does fhing work lol --invalidtag wait its --tag1 isnt it!"
  newString: "Does fhing work lol wait its isnt it!"
  matches: ["tag1"]
  data: {}
}
*/


// Match any tag
Tagify({
  string: "Hello --tag1 --tag2",
  prefix: "--"
}, "\\w+")

/*
Returns:
{
  string: "Hello --tag1 --tag2",
  newString: "Hello",
  matches: ["tag1", "tag2"],
  data: {}
}
*/


// Match any tag with values. Set resolve to false in the tag object to avoid resolving the value to match a string.
Tagify({
  string: "Hello --tag1 1 --tag2 1",
  prefix: "--"
}, { tag: "\\w+", value: "\\d+", resolve: false })

/*
Returns:
{
  string: "Hello --tag1 1 --tag2 1",
  newString: "Hello",
  matches: ["tag1", "tag2"],
  data: { tag1: 1, tag2: 1 }
}
*/

// Match values of different types
Tagify({
  string: "Convert colours --rgb [255, 53, 179] --num 519340 --hexes {\"hexadecimal\": \"0xFFFFFE\", \"hex\": \"#ec0d23\"}",
  prefix: "--",
  numbersInStrings: true
}, { rgb: Array }, { num: Number }, { hexes: Object })

/*
{
  string: "Convert colours --rgb [255, 53, 179] --num 519340 --hexes {\"hexadecimal\": \"0xFFFFFE\", \"hex\": \"#ec0d23\"}",
  newString: "Convert colours",
  matches: ["rgb", "num", "hexes"],
  data: {
    rgb: [255, 53, 179], 
    num: 519340, 
    hexes: {
      hexadecimal: "0xFFFFFE", hex: "#ec0d23"
    }
  }
}
*/

// Specify tag types
Tagify({
  string: "Colour --hex 0xECD558",
  prefix: "--",
  numbersInStrings: true,
  tagData: {
    hex: Number // without this, data.hex would be "0xecd558"
    // if the value is truthy and not Number|String|Object|Boolean (e.g. RegExp) then an attempt to json parse the matched tag will be made
  }
}, { hex: String }) // get X from the input like its a string, parse it like a number
/*
{
  string: "Colour --hex 0xECD558",
  newString: "Colour",
  matches: ["hex"],
  data: {
    hex: 15521112
  }
}
*/