Value library
Contents
Helper functions for creating the complex value types that JSON cannot express on its own — geo-points, instants, dates, node references, and binary attachments.
Install
Add the library to your build.gradle file:
dependencies {
include xplibs.value
}
TypeScript types come from the @enonic-types/lib-value package, installed at the version of XP your project builds against — see TypeScript.
Complex value types
Every property in the storage has a value type, and storage uses it to determine both validation and indexing automatically. JSON carries some value types on its own — strings, numbers, booleans, and nested objects (a Set). The rest have no JSON equivalent, and nothing declares a node property’s type up front, so '59.9139,10.7522' is just a string unless the value says otherwise.
These helpers supply that missing type. Pass the result straight into the property where the value belongs, and storage picks the index mappings from it: a GeoPoint gets the geoPoint mapping, an Instant, LocalDate, or LocalDateTime gets datetime, each alongside the default text mapping. Store the same value as a plain string and it is indexed as text only — geo and date-range queries will not match it.
The helpers are used almost exclusively with lib-node against custom repositories. lib-content does not need them, because a content type declares its field types and the engine converts plain JSON according to that schema.
import {connect} from '/lib/xp/node';
import {geoPoint, instant, localDate, reference} from '/lib/xp/value';
const repo = connect({
repoId: 'my-repo',
branch: 'master'
});
const office = repo.create({
_name: 'oslo-office',
displayName: 'Oslo office',
location: geoPoint(59.9139, 10.7522),
openedAt: instant('2016-08-01T11:22:00Z'),
leaseExpires: localDate('2027-06-30'),
manager: reference('1234-5678-91011')
});
{
_id: 'b186d24f-ac38-42ca-a6db-1c1bda6c6c26',
_name: 'oslo-office',
_path: '/oslo-office',
_nodeType: 'default',
displayName: 'Oslo office',
location: '59.9139,10.7522',
openedAt: '2016-08-01T11:22:00Z',
leaseExpires: '2027-06-30',
manager: '1234-5678-91011'
}
Complex values are read back as strings. create() and get() serialize every property to its string form, so a GeoPoint returns as '<lat>,<lon>' and a Reference as the node ID. Only the JavaScript view is flattened — the stored property keeps its value type and its index mappings.
Editors are the exception. Inside the editor callback of update, modify, or patch, properties arrive as the complex values themselves rather than strings, which is what lets an untouched property round-trip without degrading to text. |
Functions
binary
Creates a BinaryAttachment value from a name and a binary stream.
Parameters
binary() takes two positional arguments: name (string — the binary name) and stream (ByteSource — the binary stream).
Returns
object : (BinaryAttachment)
Example
import {binary} from '/lib/xp/value';
import {newStream} from '/lib/xp/io';
const attachment = binary('myFile.txt', newStream('Hello World'));
geoPoint
Creates a GeoPoint value from numeric coordinates.
Parameters
geoPoint() takes two positional arguments: lat (number — latitude) and lon (number — longitude).
Returns
object : (GeoPoint)
Example
import {geoPoint} from '/lib/xp/value';
const location = geoPoint(59.9139, 10.7522);
const lat = location.getLatitude();
Coordinates keep a decimal point in their string form, so geoPoint(80, -80) reads back as '80.0,-80.0'.
geoPointString
Creates a GeoPoint value from a comma-separated latitude and longitude string.
Parameters
geoPointString() takes one positional argument: value (string — comma-separated latitude and longitude, e.g. '59.9139,10.7522').
Returns
object : (GeoPoint)
Example
import {geoPointString} from '/lib/xp/value';
const location = geoPointString('59.9139,10.7522');
instant
Creates an Instant value from an ISO-8601 string or a Date object.
Parameters
instant() takes one positional argument: value (string | Date — an ISO-8601-formatted instant such as '2011-12-03T10:15:30Z', or a JavaScript Date).
Returns
object : (Instant)
Example
import {instant} from '/lib/xp/value';
const openedAt = instant('2016-08-01T11:22:00Z');
const millis = openedAt.toEpochMilli();
localDate
Creates a LocalDate value from an ISO local date string or a Date object.
Parameters
localDate() takes one positional argument: value (string | Date — an ISO local date such as '2011-12-03', or a JavaScript Date).
Returns
object : (LocalDate)
Example
import {localDate} from '/lib/xp/value';
const leaseExpires = localDate('2027-06-30');
const year = leaseExpires.getYear();
localDateTime
Creates a LocalDateTime value from a local date-time string or a Date object.
Parameters
localDateTime() takes one positional argument: value (string | Date — a local date-time string such as '2007-12-03T10:15:30', or a JavaScript Date).
Returns
object : (LocalDateTime)
Example
import {localDateTime} from '/lib/xp/value';
const doorsOpen = localDateTime('2016-01-08T10:00:00.000');
localTime
Creates a LocalTime value from an ISO local time string or a Date object.
Parameters
localTime() takes one positional argument: value (string | Date — an ISO local time such as '10:15:30', or a JavaScript Date).
Returns
object : (LocalTime)
Example
import {localTime} from '/lib/xp/value';
const doorsOpen = localTime('10:00:00.000');
reference
Creates a Reference value from a node ID string.
Parameters
reference() takes one positional argument: value (string — a node ID, e.g. '1234-5678-91011').
Returns
object : (Reference)
Example
import {reference} from '/lib/xp/value';
const manager = reference('1234-5678-91011');
const nodeId = manager.getNodeId();
Type Definitions
GeoPoint
A typed geo-point value. Returned by geoPoint() and geoPointString().
Methods
| Name | Type | Description |
|---|---|---|
|
|
number |
The latitude component. |
|
|
number |
The longitude component. |
|
|
string |
Serialized form — |
Instant
A typed instant (point in time) value. Returned by instant().
Methods
| Name | Type | Description |
|---|---|---|
|
|
number |
Seconds since the Unix epoch. |
|
|
number |
Nanoseconds adjustment within the current second. |
|
|
number |
Milliseconds since the Unix epoch. |
Reference
A typed node reference value. Returned by reference().
Methods
| Name | Type | Description |
|---|---|---|
|
|
string |
The referenced node ID. |
|
|
string |
String form of the node ID. |
LocalDateTime
A typed local date-time value (no time-zone). Returned by localDateTime().
Methods
| Name | Type | Description |
|---|---|---|
|
|
number |
The year. |
|
|
number |
Month as a number (1–12). |
|
|
Month as a string constant. |
|
|
|
number |
Day of the month (1–31). |
|
|
Day of the week as a string constant. |
|
|
|
number |
Hour of day (0–23). |
|
|
number |
Minute of hour (0–59). |
|
|
number |
Second of minute (0–59). |
|
|
number |
Nanosecond of second. |
LocalDate
A typed local date value (no time, no time-zone). Returned by localDate().
Methods
| Name | Type | Description |
|---|---|---|
|
|
number |
The year. |
|
|
number |
Month as a number (1–12). |
|
|
Month as a string constant. |
|
|
|
number |
Day of the month (1–31). |
|
|
number |
Day of the year (1–366). |
|
|
Day of the week as a string constant. |
|
|
|
boolean |
Whether the year is a leap year. |
LocalTime
A typed local time value (no date, no time-zone). Returned by localTime().
Methods
| Name | Type | Description |
|---|---|---|
|
|
number |
Hour of day (0–23). |
|
|
number |
Minute of hour (0–59). |
|
|
number |
Second of minute (0–59). |
|
|
number |
Nanosecond of second. |
BinaryAttachment
A typed binary attachment value wrapping a name reference and a stream. Returned by binary().
Methods
| Name | Type | Description |
|---|---|---|
|
|
Reference to the stored binary. |
|
|
|
The underlying binary stream. |
BinaryReference
A reference to a stored binary. Returned by BinaryAttachment.getReference().
Methods
| Name | Type | Description |
|---|---|---|
|
|
string |
The binary name. |
Month
Month name constant — one of 'JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'.
DayOfWeek
Day name constant — one of 'MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATURDAY', 'SUNDAY'.