How to write a Javascript expression generator?

Undraw Data Processing

How to write a Javascript expression generator?

About

This howto will show you how to write an expression for a expression generator.

An expression generator generates data from another column based on an expression.

Example

This example generate a times table (known also a time dimension in analytics).

It's using a:

  • time sequence: a sequence that generates a date (ie time at the day level)
  • and expressions to generate the next columns:
    • month
    • year
With the following generator resource
kind: generator
spec:
  MaxRecordCount: 10
  Columns:
    # Generate 10 days sequentially
    - name: d_date
      comment: A date
      Type: date
      data-supplier:
        type: sequence
        arguments:
          start: 2025-06-06 # by default, today
    # Generate the month of the year from the date with an expression
    - name: d_moy
      comment: the month number in year
      Type: Integer
      data-supplier:
        type: expression
        arguments:
          column-variable: d_date
          expression: "d_date.getMonth()+1"
    # Generate the year from the date with an expression
    - name: d_year
      comment: The year number
      Type: Varchar
      Precision: 4
      data-supplier:
        type: expression
        arguments:
          column-variable: d_date
          expression: "d_date.getFullYear()"



You would get the following data:

tabul data print generator/expression--generator.yml@howto
d_date       d_moy   d_year
----------   -----   ------
2025-06-05       6   2025
2025-06-04       6   2025
2025-06-03       6   2025
2025-06-02       6   2025
2025-06-01       6   2025
2025-05-31       5   2025
2025-05-30       5   2025
2025-05-29       5   2025
2025-05-28       5   2025
2025-05-27       5   2025

What are Expressions ?

Expressions are Javascript expressions.

Tabulify pass the data as native javascript data type in a variable with the name of the column. You don't need to instantiate the variables, Tabulify do.

Note:

  • For date/timestamp/time data type, we create a Date as this is the only temporal data type of javascript.
  • For all numbers (Double, Integer, …) , we create a Number as this is also the only number data type of javascript.

Example for 2009/10/01 and the column name date, Tabulify would create the date variable like this:

date = new Date("2009","10","01")

And if you want the month, you expression would use getMonth :

date.getMonth()+1

Develop your expression in the Devtool or Node Console

You can test your expression quickly with:

Browser Devtool Console

Illustration of Devtool and a time expression to get the month

Javascript Expression Console Devtool

Node Console

Illustration of Node in the terminal and a time expression to get the month

Node Console Expression Generator Illustration

Snippet of expressions

This section is showing snippet of expression (Javascript).

Time

  • Month where x represents the column name value of the parent columns
x.getMonth()+1 // Month
  • Month names. The below snippet use a date column as parent and lookup an array to get the month name.
var monthNames = ['Jan', 'Feb', 'Maa', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'];
monthNames[x.getMonth()]
  • Year number
x.getFullYear()

Email

Email String concatenation where:

  • x would be a name from the first parent column
  • y would be a company name from the second parent column
  • z would be a TLD domain extension (.com, …)
x+'@'+y+'.'+z



Related Pages
Undraw Data Processing
Expression Generator

A expression data-supplier is a column data supplier that can generate data created from: other columns (known as parent columns) by giving an expression (or formula). The arguments of...
Undraw Data Processing
How to add information about the selected resources with the Enrich operation ?

enrich is an intermediate operation that will add virtual columns to its inputs thanks to data supplier. Enrich accepts only one argument data-def where you can define extra columns called virtual columns...
Undraw Data Processing
Tabulify - Date and Time data type

This page contains the documentation how Tabulify is managing time ansi data type (ie date, timestamp, time). Note that we follow the sql type specification, therefore, we use the following time data...
Undraw Data Processing
Tabulify - How to generate a sequence of date and timestamp

This how-to shows you how to generate a sequence of date and timestamp with the column sequence generator.

Task Runner