import { One } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
// set the same value on all objects
o1.set({ p1: 78 });
Calls corresponding methods in a similar way to set. In this case, call arguments are interpreted instead of property values.
Multiple arguments should be provided as an object with the [arg]
property whose value
should be an array containing the arguments. A regular array is considered a single argument.
Nested one objects will have their call method invoked correspondingly.
import { One, map, args } from "deleight/onetomany";
const arr1 = [], arr2 = [], arr3 = [];
const o4 = new One({ arr1, arr2, arr3 });
o4.call({ push: 78 });
o4.call({ push: { [args]: [56, 57] } });
// arr1 = arr2 = arr3 = [78, 56, 57]
The opposite of extend. Removes the specified keys from this.many
and
returns this
typed differently.
Rest
...keys: U[]import { One } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
const o2 = o1.contract('first', 'third');
// One({ second })
Performs delete
on the objects in this One
. The argument is
treated the same way as in get
.
import { One, map } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
o1.set({ p1: 78 });
const complex = { first: 56, second: 12, third: 12 };
o1.set({ p2: { [map]: complex }, p3: complex });
o1.delete('p1', 'p2');
// One({ first: { p3: complex }, second: { p3: complex }, third: { p3: complex } })
Joins many
with this.many
and returns this
. The benefit of
using this function instead of something like Object.assign
is to handle some
special cases and ensure that the updated One
is (almost) correctly typed during
development.
If the same key exists in the joined objects, the new one will overwrite the
current one, except if the current value is a One
instance when extend
will
be called on it instead.
import { One } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
const o2 = o1.extend({ fourth });
// One({ first, second, third, fourth })
Gets the property (or properties) with the specified name(s) from all the
objects in this.many
.
what
is a string, returns only the property specified (as an object mapping
object key to property value).what
is an array of strings, returns 'sub-objects' of all the objects
consisting of the specified property names (also mapped like 1).this.many
is a One
instance, its get
method is
called for its property value(s).Optional
valueKey: IKeyimport { One, map } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
o1.set({ p1: 78 });
const complex = { first: 56, second: 12, third: 12 };
o1.set({ p2: { [map]: complex } });
o1.get('p1');
// { first: 78, second: 78, third: 78 }
o1.get('p1', 'p2');
// { first: { p1: 78, p2: 56 }, second: { p1: 78, p2: 12 }, third: { p1: 78, p2: 12 } }
o1.get({ first: 'p2', second: 'p1' });
// { first: 56, second: 78 }
Sets one or more properties on all the objects in this instance.
what
is an object mapping property names to property values.
Each value in what
is set on all the objects in this One
.
The same value will be set unless the value is an object containing the [map]
property.
In such a case, the value of the property is treated as a map of object key (in this.many
)
to object property value. That is, the objects with corresponding
keys will have their property values set to the corresponding values.
This will call set
on any nested One
objects accordingly.
import { One, map } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
o1.set({ p1: 78 });
const complex = { first: 56, second: 12, third: 12 };
o1.set({ p2: { [map]: complex }, p3: complex });
o1.get('p2', 'p3');
// { first: { p2: 56, p3: complex }, second: { p2: 12, p3: complex }, third: { p2: 12, p3: complex } }
Creates and returns another instance of One
containing only the
objects with these names. If a name is not present in this.many
,
a new object is created for it in the returned One
.
Rest
...names: (keyof T)[]import { One } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
const o2 = o1.slice('first', 'second')
// One({ first, second })
Creates and returns an instance of View
for only the
specified properties. what
is an object mapping object keys (in this.many
)
to the property names.
import { One, map } from "deleight/onetomany";
const first = {}, second = {}, third = {}, fourth = {};
const many = { first, second, third };
const o1 = new One(many);
o1.set({ p1: 78 });
const complex = { first: 56, second: 12, third: 12 };
o1.set({ p2: { [map]: complex } });
o1.view({first: 'p1', second: 'p2'});
view.get(); // { first: 78, second: 12 }
Creates a single object that propagates actions on it to multiple objects.