ObjectHelper
Constructors
Constructor
new ObjectHelper():
ObjectHelper
Returns
ObjectHelper
Methods
assign()
staticassign<T>(init, ...sources):T
This method copies all properties from initial and further sources objects into a new object. If the same property exists in multiple objects, the value from the last object will be used.
const init = { a: 1, b: 2 };
const source1 = { b: 3, c: 4 };
const source2 = { c: 5, d: 6 };
const result = ObjectHelper.assign(init, source1, source2);
util.log(result); // { a: 1, b: 3, c: 5, d: 6 }
Type Parameters
T
T
Parameters
init
T
the initial object
sources
...any[]
further source objects
Returns
T
the new object
assignFromJson()
staticassignFromJson<T>(init,json):T
This method parses the provided JSON string and copies all the properties from the initial and the parsed JSON into a new object.
const init = { a: 1, b: 2 };
const json = '{"b":3,"c":4}';
const result = ObjectHelper.assignFromJson(init, json);
util.log(result); // { a: 1, b: 3, c: 4 }
Type Parameters
T
T
Parameters
init
T
the initial object
json
string
the json string
Returns
T
Static
functionToString()
staticfunctionToString(func):string
This method converts a function into a string
const func = function () {
return "test";
};
const result = ObjectHelper.functionToString(func);
util.log(result); // "function () {\n return "test";\n }"
Parameters
func
() => unknown
your function
Returns
string
the function as a string
Static
stringifyJSONWithFunctions()
staticstringifyJSONWithFunctions(source):string
Convert an object to a json string with functions in it.
const source = {
prop1: 1,
prop2: function () {
return "test";
},
};
const result = ObjectHelper.stringifyJSONWithFunctions(source);
util.log(result); // '{"prop1":1,"prop2":"function () {\n return "test";\n }"}'
Parameters
source
unknown
the source object
Returns
string
the function as a string
Static
parseJSONWithFunctions()
staticparseJSONWithFunctions(json):unknown
This method parse an json string with string functions
const json = '{"prop1":1,"prop2":"function () {\n return "test";\n }"}';
const result = ObjectHelper.parseJSONWithFunctions(json);
util.log(result.prop2()); // "test"
Parameters
json
string
the json string
Returns
unknown
the parsed object
Static
renderTemplateString()
staticrenderTemplateString(template,data):string
This method renders semantic templates (Mustache templates) with the provided data.
const template = `Hello my name is {{name}}.`;
const data = { name: "Chuck Norris" };
const result = ObjectHelper.renderTemplateString(template, data);
util.log(result); // Hello my name is Chuck Norris.
Parameters
template
string
the templated string like: Hello my name is {{name}}.
data
object
the data object to use, like: { name: "Chuck Norris" } or
Returns
string
returns the rendered template string, like: Hello my name is Chuck Norris.
Static
renderTemplateStringCustom()
staticrenderTemplateStringCustom(template,replace):string
This method renders semantic templates (Mustache templates) with a custom replace function that is called when a key is found, that should be replaced.
const docFile = context.file;
util.log(docFile.name); // Chuck Norris
const template = `Hello my name is {{name}}.`;
const result = ObjectHelper.renderTemplateStringCustom(template, (placeholderName) => docFile.getFieldAutoText(placeholderName));
// or shorter: const result = ObjectHelper.renderTemplateStringCustom(template, docFile.getFieldAutoText);
util.log(result); // Hello my name is Chuck Norris.
Parameters
template
string
the templated string like: Hello my name is {{name}}.
replace
(key) => string
the replace function that will be called when a key is found, that should be replaced, like: (key) => data[key] //data["name"] -> Chuck Norris
Returns
string
returns the rendered template string, like: Hello my name is Chuck Norris.