Basics #
Comments #
Sift supports both single line comments and multi line comments.
Example:
/*
this
is a
multiline comment
*/
// this is a single line comment
Semicolons #
All expressions should end with a semicolon ;.
Example:
let name = "John";
sello "name" = "Peter";
sello "age" = 29;
Variables #
Variables are defined using the let keyword.
let a = 3;
let b = 1.2;
let c = "hello";
Variables are updated without the let, for example this works as you would expect:
let world = "Earth";
world = "world";
Arithmetic operations #
Sift supports all the basic arithmetic operation of int and float types.
let a = 3;
let b = 1.2;
let numA = a + b; // 4.2
let numB = a - b; // 1.8
let numC = a * b; // 3.6
let numD = a / b; // 2.5
OR strings #
You can specify multiple items and the first non-empty will be used.
Example:
sello "private name" = src("name") || src("title") || src("product");
Given that the fields in your source is:
- name: (empty)
- title: “My product”
- product: “My cool product”
This means that “private name” would be set to “My product” in the above example.
Functions #
You can create a function with the fn keyword. You can include any number of function arguments.
Example:
fn getType(size, color) {
return size + "-" + color;
}
sello "model" = getType("large", "red"); // large-red
ON statements #
By using on you can specify if a mapping should only be used on a specific event. For example, you might want to only set the product images when the product is created and you don’t want your source to change them after that.
on statements can also include an array with multiple events to match.
Example:
sello "private name" = src("name");
on "create" {
sello "images" = src("images");
sello "price" = src("my-price");
}
In the example above, the private name will be set both when the product is created and during any updates. However, images and price will only be set at the time of product creation and will not be updated thereafter.
Example with array in on:
sello "private name" = src("name");
on ["create", "quantity"] {
sello "images" = src("images");
sello "price" = src("my-price");
}
IF statements #
You can use if, else if and else to create if statements.
Example:
let category = "";
let age = 20;
if (age == 0) {
category = "newborn";
} else if (age < 19) {
category = "child";
} else {
category = "adult";
}
The above example will set category to adult
Arrays #
An array is a list which organizes items by linear sequence. Arrays can hold multiple types.
let a = [1, 2.3, "array"];
let b = [false, true, "Hello World", 3, 3.13];
Adding to an array is done via the push function:
let a = push(a, "another");
You can access a specific item in array by it’s index (starting at 0)
let a = [1, 2.3, "array"];
let n = a[1]; // 2.3
Multi value properties #
Some properties accept multiple values, for example ZalandoGender. To specify multiple values, prepend the value string with multi: and separate values with pipe |.
Example:
sello "property ZalandoGender" = "multi:Man|Woman";
For loops #
There are two types of for loops in Sift: looping over a range of numbers and looping over items in an array.
Looping over a range of numbers #
You can loop over a range of numbers using the following syntax:
for (let i = start; i < end; i = i + 1) {
// code to be executed
}
Example:
for (let i = 1; i <= 5; i = i + 1) {
sum = sum + i;
}
// sum will be 15 (1 + 2 + 3 + 4 + 5)
Looping over items in an array #
You can loop over items in an array using the following syntax:
for (let item in array) {
// code to be executed
}
Example:
let fruits = ["apple", "banana", "cherry"];
let result = "";
for (let fruit in fruits) {
result = result + fruit + " ";
}
// result will be "apple banana cherry "
If you change the value of the variable in the array, so will it also change in the original array.
Example:
let numbers = [1, 2, 3];
for (let num in numbers) {
num = num * 2;
}
// numbers will be [2, 4, 6]
Breaking out of loops #
You can use the break statement to exit a loop prematurely.
Example:
let sum = 0;
for (let i = 1; i <= 10; i = i + 1) {
if (i > 5) {
break;
}
sum = sum + i;
}
// sum will be 15 (1 + 2 + 3 + 4 + 5)
Continue to next iteration #
You can use the continue statement to skip the current iteration and move to the next one.
Example:
let sum = 0;
for (let i = 1; i <= 5; i = i + 1) {
if (i == 3) {
continue;
}
sum = sum + i;
}
// sum will be 12 (1 + 2 + 4 + 5)
IN operator #
You can use the in operator to check if a scalar (string or number) value exists in an array.
Example:
let fruits = ["apple", "banana", "cherry"];
if ("banana" in fruits) {
// Do something
}