stringvars

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

Usage no npm install needed!

<script type="module">
  import stringvars from 'https://cdn.skypack.dev/stringvars';
</script>

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!