Section » list

list() → array
list(options) → array
list(loader) → array
list(loader, options) → array

Retrieve the items of a list, optionally supplying a loader and options.

enocolors:
- pink
- peach

textures:
-
javascriptconst document = eno.parse(input);

document.list('colors');  // returns [ 'pink', 'peach' ]
document.list('sounds');  // returns []

document.list('textures');                            // throws an error
document.list('textures', { enforceValues: false });  // returns [ null ]

document.list('sounds', { required: true });        // throws an error
document.list('sounds', { enforceElement: true });  // throws an error

document.list('colors', ({ value }) => value.toUpperCase());   // returns [ 'PINK', 'PEACH' ]

document.list('colors', ({ value }) => {   // throws an error
  if(value === 'peach') {
    throw 'Peach may not be on the list!';
  }
  return `I like ${value}`;
});

document.list('colors', { withElements: true });
  // returns [{ element: [object Field value="pink"], value: 'pink' },
  //          { element: [object Field value="pink"], value: 'peach' }]

document.list('colors', { minCount: 3 });  // throws an error

Parameters

name

The name of the list to return as a string.

loader

A function returning the transformed/validated value or throwing an error.(The function is applied to each list item on its own, being passed a singleobject parameter with the keys name and value)

options

enforceElement

Whether the list must be present in the document (defaults to false)

enforceValues

Whether empty list items (- in the document, mapping to null) are disallowed (defaults to true)

exactCount

Validate that there are exactly n items in the list (takes a number, defaults to null)

minCount

Validate that there are at least n items in the list (takes a number, defaults to null)

maxCount

Validate that there are at most n items in the list (takes a number, defaults to null)

required

Alias for enforceElement (this exists on many methods and depending on context refers to either element or value)

withElements

Whether to return an array of objects of the form { element: ..., value: ... } instead of just the values (defaults to false)

Return value

The (optionally transformed/validated) values of the items of this list as an array.With withElements set to true it returns an array of objects of the form { element: ..., value: ... } instead.