If you've been working as a developer for a while, you've probably found yourself processing data on a regular basis. I’m talking about the CSV-file you got from a client or the JSON file which needs some remapping of keys.
Here's a quick example of how to use Deno to transform data quickly using
Let’s start with something simple: Uppercase all the things (or in our case all the contact details)!
Let's unpack what is happening here:
First, we pipe the content of
deno run. This command accepts a path or URL and executes it. In our case, we use the URL to the
xeval example in Deno’s standard lib.
The last argument is the code that gets called for every line. The
$ variable holds a reference to the current line. From here it’s just plain code and we call
toUpperCase() and print out the result using
If you run that command, you will get an output where all contact details are uppercase.
From text to JSON
Before we proceed, lets setup an alias for for this command with
deno install. Special thanks to Lars Gyrup Brink Nielsen for pointing this out.
Let’s move on to a slightly more complex example and turn this plain text file into a JSON format.
Again, we parse the file line by line and use array deconstruction in combination with object shorthand syntax to convert this text into a JSON string.
Here's the output that you should be seeing when running the command.
As the code evolves, it gets harder to read and maintain this code string. My solution to this problem is as follows:
store the code in a separate file use subshell command to print the code while executing the Deno run command
I hope this is helpful! Before we wrap it up, I want to mention one limitation of
xeval: Operations need to be synchronous, but other than that
xeval.ts is one of my highlights in Deno.
Thanks for reading.