Security News
Maven Central Adds Sigstore Signature Validation
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
super-transformer
Advanced tools
This package provides scripts to transform incoming strings and files using templates. It receives a file path or a string in various formats (JSON, CSV) and returns transformed data.
This package provides scripts to transform incoming strings using templates. It receives string of various formats (JSON, CSV) and returns a string after transforming the incoming data. This package can also transform entire files or a string passed as an argument.
A complete example of how to use this package in your own project is available at: https://github.com/valtech-sd/super-transformer-example
The NPM packages are committed to the repo, but if you're having problems, try npm i
.
This repository uses Handlebars templates. To learn more about handlebars/mustache template transformations, see the HandlebarsJS website.
The script transformJSON.js is a transformation script that operates on a single data object (a single "row" of data). This script uses a handlebars/mustache template to transform JSON passed as a string in the command line into another format determined by the template!
The transform script can be called like this:
$ node ./transformJSON.js -t "./template-examples/demo-simple.hbs" -i "{\"customer\": {\"name\": \"John\"}}"
The command line arguments are:
JSON.parse()
), the script will terminate and return an error.'.*'
instead of /.*/
). If passed, you must also pass -r --replacepattern containing a matching Regex replacement string.The script transformJSON-file.js is a transformation script that operates on an input file of any size (instead of a single data object). This script uses a handlebars/mustache template to transform each row of JSON into another format determined by the template!
The transform script can be called like this:
$ node ./transformJSON-file.js -t "./template-examples/demo-simple.hbs" -i "./tests/test-files/simple-json-01.txt"
The command line arguments are:
'.*'
instead of /.*/
). If passed, you must also pass -r --replacepattern containing a matching Regex replacement string.This script outputs all transformed rows to STDOUT. OS level redirection should be used capture results to file. For example:
$ node ./transformJSON-file.js -t "./template-examples/demo-simple.hbs" -i "./tests/test-files/simple-json-01.txt" > /path/to/an/output/file.json
What about lines in the file that can't be parsed as JSON? This script will skip those lines and output error messages via STDERR so that a redirected output file still contains the successful rows without any errors.
Handlebars supports many rich template substitutions.
The example template is very simple:
{ "customerName": "{{customer.name}}" }
This template will output all the text inside the file except the items wrapped in a mustache (the "{{}}"). For example, in the above, {{customer.name}} will be replaced with the object customer and the property name in the incoming data. If the incoming data does not have a value for the object, then the value is replaced with an empty string.
The example data in the command line above is:
"{\"customer\": {\"name\": \"John\"}}"
Note the string is in double quotes with the inner double quotes escaped. This is impractical if your data is coming from another tool, in which case, you can also pass it in single quotes:
'{"customer": {"name": "John"}}'
Regardless of how you pass the string with json data, the object above is represented as the following object once JSON.parse()
parses it.
{
"customer": {
"name": "John"
}
}
When you run this demo-simple.hbs template with the data above, the results are:
{ "customerName": "John" }
What just happened? The template mustached variables were replaced with data coming from the passed JSON object!
Of course, this is a very simple example, but in practice the data object can be any JSON structure even containing arrays, nested objects, etc.
In addition to standard Handlebars substitutions, the Handlebars API also supports custom helpers. This package exposes those as well to the CLI scripts and provides an easy mechanism for other code that uses this package directly to include custom handlebars helpers.
The example template is very simple:
{ "customerName": "{{yell customer.name}}" }
The string yell
in the above example will call a Handlebar helper called yell
.
You can define Handlebars helpers in a special JavaScript file. For example:
function loadHandlebarsHelpers(Handlebars) {
Handlebars.registerHelper('yell', (someString) => {
return someString.toUpperCase();
});
}
module.exports.loadHandlebarsHelpers = loadHandlebarsHelpers;
In the above, the blocks that start with Handlebars.registerHelper(...)
are standard Handlebars custom helpers as described here: https://handlebarsjs.com/guide/#custom-helpers.
However, to make these easy to use within this package, we wrap one or more such registerHelper()
instructions in a special syntax like this:
function loadHandlebarsHelpers(Handlebars) {
// As many Handlebars custom helper registrations as you want
}
module.exports.loadHandlebarsHelpers = loadHandlebarsHelpers;
The final step in order to use your custom helpers is to pass your helper's path in the transformJSON command line like using the -x
command line argument:
$ node ./transformJSON-file.js -t "./template-examples/demo-simple-with-handlebars-helpers.hbs" -i "./tests/test-files/simple-json-01.txt" -x template-examples/demo-handlebars-helpers.js > /path/to/an/output/file.json
When you run this demo-simple.hbs template with the data above, the results are that all the names are converted to UPPERCASE:
{ "customerName": "JOHN" }
{ "customerName": "MARY" }
{ "customerName": "PETE" }
The example data file contains one JSON object per row as follows:
{"customer": {"name": "John"}}
{"customer": {"name": "Mary"}}
{"customer": {"name": "Pete"}}
Note that this is NOT a well formed JSON file in that it is not an array of JSON objects strung together. This format, very common in logging scenarios, holds a single complete JSON object per row!
The script transformDelimited.js is a transformation script that operates on a single data object (a single "row" of data). This script uses a handlebars/mustache template to transform a delimited string (CSV or otherwise) passed as a string in the command line into another format determined by the template!
The transform script can be called like this:
$ node ./transformDelimited.js -t "./template-examples/demo-simple-flat.hbs" -i '"john", "smith", "Davenport, FL", 2017' -d "," -c "first_name, last_name, customer_city, hire_year"
The command line arguments are:
'.*'
instead of /.*/
). If passed, you must also pass -r --replacepattern containing a matching Regex replacement string.The script transformDelimited.js is a transformation script that operates on an input file of any size (instead of a single data object). This script uses a handlebars/mustache template to transform each delimited record row (CSV or otherwise) into another format determined by the template!
The transform script can be called like this:
$ node ./transformDelimited-file.js -t "./template-examples/demo-simple-flat.hbs" -i "./tests/test-files/simple-csv-01.txt" -d "," -n
The command line arguments are:
'.*'
instead of /.\*/
). If passed, you must also pass -r --replacepattern containing a matching Regex replacement string.This script outputs all transformed rows to STDOUT. OS level redirection should be used capture results to file. For example:
$ node ./transformDelimited-file.js -t "./template-examples/demo-simple-flat.hbs" -i "./tests/test-files/simple-csv-01.txt" -d "," -n > /path/to/an/output/file.json
What about lines in the file that can't be parsed as JSON? This script will skip those lines and output error messages via STDERR so that a redirected output file still contains the successful rows without any errors.
Handlebars supports many rich template substitutions. To learn more about handlebars/mustache template transformations, see the HandlebarsJS website.
The example template is very simple:
{
"customerName": "{{first_name}}{{#if last_name}} {{last_name}}{{/if}}",
"customerCity": "{{customer_city}}",
"fteSince": {{hire_year}}
}
This template will output all the text inside the file except the items wrapped in a mustache (the "{{}}"). For example, in the above, {{first_name}} will be replaced with the first_name column. Similarly the others are replaced with the corresponding named columns.
If the incoming data does not have a value for the object, then the value is replaced with an empty string.
The example column names in the above are:
"first_name, last_name, customer_city, hire_year"
This will represent to the parser that it should expect 3 columns and it will parse the values into an object with 3 properties: first_name, last_name, customer_city.
Once the columns are defined, we can pass in corresponding delimited data.
The example data in the command line above are:
"\"john\", \"smith\", \"Davenport, FL\", 2017"
Note the string is in double quotes with the inner double quotes escaped. This is impractical if your data is coming from another tool, in which case, you can also pass it in single quotes:
'"john", "smith", "Davenport, FL", 2017'
Note in both of the above examples, the column "Davenport, FL" is treated as a single column (and is not delimited by the comma since it is quoted.)
Regardless of how you pass the string, the object above is represented as the following object once it is parsed.
{
"first_name": "john",
"last_name": "smith",
"customer_city": "Davenport, FL",
"hire_year": "2017"
}
When you run this demo-simple.hbs template with the data above, the results are:
{ "customerName": "john smith", "customerCity": "Davenport, FL", "fteSince": 2017 }
What just happened? The template mustached variables were replaced with data coming from the passed delimited string!
Of course, this is a very simple example, but in practice the input data can be any delimited string with as many columns as necessary so long as a matching columnLayout is also passed.
The example data file contains a header row with column names plus delinited (CSV) record rows as follows:
first_name, last_name, customer_city, hire_year
"john", "smith", "Davenport, FL", 2017
"mary", "jones", "Orlando, FL", 2019
"pete", "parker", "Lakeland, FL", 2018
You would transform with the above file that contains a column header row using the -n command line argument to let the script infer your column names.
It is also possible to use a file without headers, for example:
"john", "smith", "Davenport, FL", 2017
"mary", "jones", "Orlando, FL", 2019
"pete", "parker", "Lakeland, FL", 2018
You would transform with the above file that does not have a column header row using the -c "first_name, last_name, customer_city, hire_year" command line argument.
Both the transformDelimited and transformJSON variants of this package support passing in a Regex replacement. This package supports standard JavaScript Regex patterns and replacements.
Regex replacements can be as simple or as complex as needed!
Considerations:
Example 1 - Simple
Passing: -m 'John' -r 'Johnnn'
When receiving the following input: (pretty printed for illustration only. Note matches should be on the same line of the file since the Regex will be applied one line at a time!)
{"customer": {"name": "John"}}
Results in the following JSON before the rest of the package acts on your data: (pretty printed for illustration only. the return will be exactly as you passed, but with replacements.)
{"customer": {"name": "Johnnn"}}
Example 2 - Complex
Passing: -m '"rv(\d\d)".*?:' -r '"rv":$1,"rvdata":'
When receiving the following input: (pretty printed for illustration only. Note matches should be on the same line of the file since the Regex will be applied one line at a time!)
{
"rvs": {
"rv18": {
"RvVgs": {"stops": 7}
}
}
}
Results in the following JSON before the rest of the package acts on your data: (pretty printed for illustration only. the return will be exactly as you passed, but with replacements.)
{
"rvs": {
"rv": 18,
"rvdata": {
"RvVgs": {"stops": 7}
}
}
}
Example 3 - Simple
Passing: -m 'John' -r 'Johnnn'
When receiving the following input: (pretty printed for illustration only. Note matches should be on the same line of the file since the Regex will be applied one line at a time!)
"john", "smith", "Davenport, FL", 2017
Results in the following JSON before the rest of the package acts on your data: (pretty printed for illustration only. the return will be exactly as you passed, but with replacements.)
"Johnnn", "smith", "Davenport, FL", 2017
Example 4 - Complex
Passing: -m '^(.*?),(.*?),(.*?),(.*?)$' -r '$1,$2,$3,$4,"full time"'
When receiving the following input: (pretty printed for illustration only. Note matches should be on the same line of the file since the Regex will be applied one line at a time!)
"john", "smith", "Davenport, FL", 2017
Results in the following JSON before the rest of the package acts on your data: (pretty printed for illustration only. the return will be exactly as you passed, but with replacements.)
"john", "smith", "Davenport, FL", 2017, "full time"
(Essentially, you add one more column to every row! Note that in this case, you would need to pass -c to define the columns properly otherwise, you'll get a column mismatch error.)
From the root of this project, run npm test
to execute all the tests in the tests sub-directory.
In order to publish to NPM, you must first login with:
$ npm login
You'll be prompted for username and password for npmjs.com. You should use the NPM user valtech-sd.
After login:
$ npm version patch
$ npm publish
FAQs
This package provides scripts to transform incoming strings and files using templates. It receives a file path or a string in various formats (JSON, CSV) and returns transformed data.
The npm package super-transformer receives a total of 0 weekly downloads. As such, super-transformer popularity was classified as not popular.
We found that super-transformer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Maven Central now validates Sigstore signatures, making it easier for developers to verify the provenance of Java packages.
Security News
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.