The Hidden Superpower of Deno: xeval

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.

The first time I stumbled across xeval was in Rayn Dahl’s Deno talk. I was glad to see an alternative to my countless Node.js scripts and a new tool for my developer tool belt.

Here's a quick example of how to use Deno to transform data quickly using xeval.

What does xeval.ts do?

xeval.ts is a line by line parsing utility that allows you to run TypeScript or JavaScript code for each line from a given input.

Let’s start with something simple: Uppercase all the things (or in our case all the contact details)!

Michael Wayne michael.wayne@gardner.com
Lindsay Beer lindsay@hotmail.com
Tobias Funke tobias.funke@gmail.com
list.txt
cat list.txt | deno run https://deno.land/std@0.71.0/examples/xeval.ts -- "console.log($.toUpperCase())"
Deno run command

Let's unpack what is happening here:

First, we pipe the content of list.txt to 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 console.log.

If you run that command, you will get an output where all contact details are uppercase.

MICHAEL WAYNE MICHAEL.WAYNE@GARDNER.COM
LINDSAY BEER LINDSAY@HOTMAIL.COM
TOBIAS FUNKE TOBIAS.FUNKE@GMAIL.COM
Output

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.

deno install --name xeval https://deno.land/std@0.71.0/examples/xeval.ts
Install xeval.ts as an executable

Let’s move on to a slightly more complex example and turn this plain text file into a JSON format.

cat list.txt | xeval -- "const [firstname, lastname, email] =$.split(' '); console.log(JSON.stringify({ firstname, lastname, email}))"
Deno run command

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.

{"firstname":"Michael","lastname":"Wayne","email":"michael.wayne@gardner.com"}
{"firstname":"Lindsay","lastname":"Beer","email":"lindsay@hotmail.com"}
{"firstname":"Tobias","lastname":"Funke","email":"tobias.funke@gmail.com"}
Output

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

cat list.txt | xeval -- "$(cat data-massage.js)"
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.

Was this article interesting or helpful?

Stefan Buck
Written by
Stefan Buck is Software Engineer since 2006. In 2013, he created OctoLinker, a browser extension for GitHub trusted and used by over 30,000 developers. Since then, constantly striving to enhance the developer experience with tools such as Pull Request Badge, Jumpcat, and more recently Tentacle,