@appvise/template-object
v1.2.0
Published
Resolve an object with data later to be inserted
Downloads
8
Readme
@appvise/template-object
Resolve an object with data later to be inserted
Usage
Example code
import {
resolveObject,
ref,
bound
} from '@appvise/template-object';
// The template we'll resolve later
const template = {
foo : 'bar',
subject : ref('subject'),
deep_stringed : ref('deep.stringed'),
deep_art : ref(['deep','art']),
called_plain : bound(() => 'dinges'),
called_args : bound((...args: any[]) => args, null, ['hello','world']),
called_reffed : bound((...args: any[]) => args, null, ['hello',ref('subject')]),
};
// Resolve the template with multiple vectors
const mapped = resolveObject(template, [
// Single vector
{
subject : 'world',
deep : {
stringed : 'works',
art : 'pretty',
},
},
// Single vector
{
subject : 'calzone',
deep : {
stringed : 'stringed',
art : 'art',
},
},
]);
// The data is NOT directly inserted into the object
console.log(mapped);
// JSON.stringify fully resolves the whole object
console.log(JSON.stringify(mapped, null, 2));
// Or directly fetch values
console.log(mapped[0].subject);
console.log(mapped[1].subject);
Output
[
{
foo: 'bar',
subject: Ref { path: [Array], fallback: undefined },
deep_stringed: Ref { path: [Array], fallback: undefined },
deep_art: Ref { path: [Array], fallback: undefined },
called_plain: BoundFunction {
subject: [Function (anonymous)],
self: undefined,
args: []
},
called_args: BoundFunction {
subject: [Function (anonymous)],
self: null,
args: [Array]
},
called_reffed: BoundFunction {
subject: [Function (anonymous)],
self: null,
args: [Array]
}
},
{
foo: 'bar',
subject: Ref { path: [Array], fallback: undefined },
deep_stringed: Ref { path: [Array], fallback: undefined },
deep_art: Ref { path: [Array], fallback: undefined },
called_plain: BoundFunction {
subject: [Function (anonymous)],
self: undefined,
args: []
},
called_args: BoundFunction {
subject: [Function (anonymous)],
self: null,
args: [Array]
},
called_reffed: BoundFunction {
subject: [Function (anonymous)],
self: null,
args: [Array]
}
}
]
[
{
"foo": "bar",
"subject": "world",
"deep_stringed": "works",
"deep_art": "pretty",
"called_plain": "dinges",
"called_args": [
"hello",
"world"
],
"called_reffed": [
"hello",
"world"
]
},
{
"foo": "bar",
"subject": "calzone",
"deep_stringed": "stringed",
"deep_art": "art",
"called_plain": "dinges",
"called_args": [
"hello",
"world"
],
"called_reffed": [
"hello",
"calzone"
]
}
]
world
calzone
License
Copyright © 2022 App-vise B.V.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.