Simple Luxon object bindings for JSONata
npm install luxon-jsonataJSONata bindings for the Luxon date library.
DateTime, Duration and Interval objects so they can be
ts
import addLuxon from "luxon-jsonata";
import jsonata from "jsonata";
const expr = jsonata(
'$Duration.fromISO("P2M").plus({"months":3, "days":10}).toISO()'
);
addLuxon(expr);
console.log(expr.evaluate({}));
// Result is "P5M10D"
`
Caveats
Not all methods are going to work. In particular, methods that themselves take functions, or require a standard Date
object are a bit awkward to use in JSONata. Perhaps in future we can dynamically remove these from the bound objects.
For Luxon member functions that themselves take functions, like Duration.mapUnits, you can assign a Javascript
function to the expression that takes the object and performs the operation you need:
`ts
const double = (x: Duration) => x.mapUnits((u) => u * 2);
const expr = jsonata(
"$double($Duration.fromObject({ 'hours': 1, 'minutes': 30 })).toObject()"
);
addLuxon(expr);
expr.assign("double", double);
console.log(expr.evaluate({}));
// Result is { hours: 2, minutes: 60 }
``