Table of Contents

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:

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:

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

x.getMonth()+1 // Month
var monthNames = ['Jan', 'Feb', 'Maa', 'Apr', 'Mei', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dec'];
monthNames[x.getMonth()]
x.getFullYear()

Email

Email String concatenation where:

x+'@'+y+'.'+z