
Security News
Meet Socket at Black Hat and DEF CON 2025 in Las Vegas
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
javascript-object-templates
Advanced tools
Templates for Javascript Objects
Create a template using a layout object describing the type and default value. If the default value is null, then the field can be of any type.
var JOT = require('JavascriptObjectTemplates');
// Create new template
var template = new JOT({
myString: 'myStringValue',
myNumber: 1234
multipleValues: null,
subObject: {
subNumber: 5678
}
});
Note: This will be foundation for all following examples.
Get a value from the template.
template.get('myString'); // 'myStringValue'
template.get('myNumber'); // 1234
template.get('subObject.subNumber'); // 5678
Set a value to the template.
Note: Only accepts same type unless it's default value was null.
template.set('myString', 'myNewStringValue'); // true
template.get('myString'); // 'myNewStringValue'
template.set('myString', 1234); // false (because the type is different)
template.get('myString'); // 'myNewStringValue'
template.set('myBoolean', true); // false (because the field isn't defined in layout)
Set multiple values from an object.
template.get('myString'); // 'myStringValue'
template.get('myNumber'); // 1234
template.merge({
myString: 'myObjectStringValue',
myNumber: 'example',
myBoolean: false,
subObject: {
subNumber: 0
}
});
template.get('myString'); // 'myObjectStringValue'
template.get('myNumber'); // 1234 (because the type is different)
template.get('myBoolean'); // undefined (because the field isn't defined in layout)
template.get('subObject.subNumber'); // 0
Get the template as object.
Can be used together with reset()
to allow the template to be used multiple times.
template.set('myString', 'myNewStringValue');
template.set('myNumber', 5678); // false (because the type is different)
template.getObject();
/*
{
myString: 'myNewStringValue',
myNumber: 5678
multipleValues: null,
subObject: {
subNumber: 5678
}
}
*/
Resets the template back to default values.
Can be used together with getObject()
to allow the template to be used multiple times.
template.set('myString', 'myNewStringValue');
template.set('myNumber', 5678); // false (because the type is different)
template.getObject();
/*
{
myString: 'myNewStringValue',
myNumber: 5678
multipleValues: null,
subObject: {
subNumber: 5678
}
}
*/
template.reset();
template.getObject();
/*
{
myString: 'myStringValue',
myNumber: 1234
multipleValues: null,
subObject: {
subNumber: 5678
}
}
*/
FAQs
Templates for Javascript Objects
We found that javascript-object-templates demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Meet Socket at Black Hat & DEF CON 2025 for 1:1s, insider security talks at Allegiant Stadium, and a private dinner with top minds in software supply chain security.
Security News
CAI is a new open source AI framework that automates penetration testing tasks like scanning and exploitation up to 3,600× faster than humans.
Security News
Deno 2.4 brings back bundling, improves dependency updates and telemetry, and makes the runtime more practical for real-world JavaScript projects.