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
Lindsay Beer
Tobias Funke
cat list.txt | deno run -- "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.


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
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.


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.

Written by
Stefan Buck is a Staff Software Engineer. In 2013, he created OctoLinker, a browser extension for GitHub trusted and used by over 25,000 developers. Recently he started Pull Request Badge, a GitHub App that brings badges to your Pull Request.