Compute
Overview
The compute option can be used to extend, modify, and remove properties from the basket.
This is often desirable to model the basket in a way that is more convenient for your application, remove sensitive data, or to compute derived properties.
The compute function
The output of the compute function is merged with the basket object with the following rules:
- Non-existing properties are added.
- Existing properties are overwritten.
- Properties set to
undefinedare removed.
important
The id property can NOT be removed or modified.
Deriving properties
declo.config.ts
.compute((basket) => {
const products = basket.objects.filter((obj) => obj.type === 'product')
const delivery = basket.objects.find((obj) => obj.type === 'delivery')
const totalPrice = basket.objects.reduce((acc, obj) => acc + obj.price, 0)
const totalVat = totalPrice * 0.25
return {
products,
delivery,
totalVat,
totalPrice,
}
})
Modifying properties
declo.config.ts
.compute((basket) => {
return {
data: { ...basket.data, discount: 0.1, },
objects: basket.objects.filter((obj) => obj.type !== 'delivery'),
}
})
Removing properties
declo.config.ts
.compute((basket) => {
return {
data: undefined,
}
})
Combining all
declo.config.ts
.compute((basket) => {
const products = basket.objects.filter((obj) => obj.type === 'product')
const delivery = basket.objects.find((obj) => obj.type === 'delivery')
const totalPrice = basket.objects.reduce((acc, obj) => acc + obj.price, 0)
const totalVat = totalPrice * 0.25
return {
data: undefined,
objects: undefined,
products,
delivery,
totalVat,
totalPrice,
}
})