@plurid/loque
v0.0.0-0
Published
Object Data Locator, Extractor, Updater
Downloads
8
Readme
loque
provides utility for locating, extracting, and updating data from object-like structures (arbitrarily nested maps and lists) based on a querying syntax.
Contents
About
Given a collection of data, loque
selects, retrieves, and modifies values.
Example:
import loque from '@plurid/loque';
const data = {
records: [
{
ownedBy: 'A',
id: '1',
value: 'one',
},
{
ownedBy: 'B',
id: '2',
value: 'two',
},
{
ownedBy: 'A',
id: '3',
value: 'three',
},
{
ownedBy: 'A',
id: '4',
value: 'four',
},
],
};
const main = () => {
// LocatorStatements
const locatorRecordOne = loque.locate(
'records.id:1',
);
// Extracted data obtained with locator statements.
// {
// ownedBy: 'A',
// id: '1',
// value: 'one',
// };
const recordOne = loque.extract(
locatorRecordOne,
data,
).data;
// Extracted data obtained with locator string.
// [
// {
// ownedBy: 'A',
// id: '1',
// value: 'one',
// },
// {
// ownedBy: 'B',
// id: '2',
// value: 'two',
// },
// ];
const recordsOneTwo = loque.extract(
'records . id:1 & id:2',
data,
).data;
// Updated data with locator string.
// {
// records: [
// ...
// {
// ownedBy: 'A',
// id: '3',
// value: 'three-modified',
// },
// ...
// ],
// };
const newData = loque.update(
'records.id:3',
data,
{
value: 'three-modified',
},
);
// Extraction with cursor.
const lastTwo = loque.extract(
'records.ownedBy:A |last 2|',
newData,
).data;
const firstTwo = loque.extract(
'records.ownedBy:A |first 2|',
newData,
);
// Cursor index value of the last document.
const firstTwoCursor = firstTwo.cursor;
const nextTwo = loque.extract(
`records.ownedBy:A |first 2 above ${firstTwoCursor}|`,
newData,
).data;
}
main();
Data
A collection of data is an arbitrary structure composed of collections
of documents
and values
.
const data = {
// collections
aCollection: [
],
// or values
aValue: 'value',
};
A collection
is a specialized list
.
A value
can be a number
, a string
, a boolean
, a map
.
A document
can contain arbitrary values
and collections
.
Packages
@plurid/loque-javascript • the JavaScript
/TypeScript
implementation