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

stringvars

v1.0.1

Published

Pass variables as a human readable string and accurately read back the string converting all values to their respective datatypes

Downloads

7

Readme

Motivation

Have you ever wanted to transmit variables or arguments in a simple human readable string yet have them converted back to variables with their correct datatypes?

For example, the string: "Jeniffer,asl,24, female, Nairobi\\,Kenya" becomes

[ 'Jeniffer', 'asl', 24, ' female', ' Nairobi,Kenya' ] 

Note:

  • 24 was accurately detected as a number and type-cased accurately.
  • " female" was not trimmed so all white space is respected
  • The comma in "Nairobi,Kenya" needed to be escaped. This is because, when using "," as the separation character, then Nairobi and Kenya would be split to two strings.

Possible Use Cases

I created this module as a part of a larger project. I needed users to enter values as simple strings which then are read back into an array with correct type-casting for further use with Object Quiz.

Id you find this useful, I'm curious to hear your use case.

Gotchas

It is important to note that this module uses JSON.parse internally for type casting. As such, only data types supported by JSON can be correctly handled. Any other values will be casted to the string equivalent. Example:

"undefined" 
//becomes '"undefined"' because the undefined type is not supported

How To use Module

const stringVars = require('stringvars');
const query = 'Jeniffer,asl,24, female, Nairobi\\,Kenya'
const args = stringVars(query)
console.log(args);

// [ 'Jeniffer', 'asl', 24, ' female', ' Nairobi,Kenya' ] 
// You can now use the returned array as you wish

Want a different Separation Character?

If you have many string variables like "Nairobi,Kenya above, and are tired of escaping all the commas, you can alternatively change the separation character.

To do so, simply pass your desired character as the second argument like this:

const stringVars = require('stringvars');
const query = 'Jeniffer|asl|24| female| Nairobi,Kenya'
const args = stringVars(query, '|')
console.log(args);

// [ 'Jeniffer', 'asl', 24, ' female', ' Nairobi,Kenya' ] 

Organize values into blocks of arrays

You can group values into arrays by encapsulating them with square brackets.

const stringVars = require('stringvars');
const query =  "a,b,[1,2,453.55],f"
const args = stringVars(query)
console.log(args);
// [ 'a', 'b', [ 1, 2, 453.55 ], 'f' ]

Simple as that!