ObjectManager
Utility-class with several static functions to simplify validation, merging and other standard operations on javascript-Objects.
Static Method Summary
Static Public Methods | ||
public static |
clone(originalObject: *): {} Copies all values from given originalObject into a new object, which is returned to caller. |
|
public static |
mergeAndClone(originalObject: *, overrideObject: *): {} Merges all values from originalObject and overrideObject into a new object that is returned. |
|
public static |
mergeInPlace(originalObject: *, overrideObject: *) Merges all values from overrideObject into originalObject. |
|
public static |
Validate that an object and nested keys are not null, undefined or empty string "" or empty object {}. |
|
public static |
validateAllowEmptyObject(givenObject: *, args: *): boolean Validate that an object and nested keys are not null, undefined or empty string "". |
|
public static |
validateAllowEmptyString(givenObject: *, args: *): boolean Validate that an object and nested keys are not null, undefined or empty object {}. |
|
public static |
validateAllowEmptyStringAndEmptyObject(givenObject: *, args: *): boolean Validate that an object and nested keys are not null or undefined. |
|
public static |
validateOrCallback(callback: *, objectToBeValidated: *, args: *): * Utilityfunction to simplify validation! uses validateOrFallback for validation, and executes given callback (and returns returnvalue from it) if validation fails. |
|
public static |
validateOrError(errorMessage: *, objectToBeValidated: *, args: *): * Utilityfunction to simplify validation! uses validateOrCallback for validation, and passes a callback that simply thrown an Error if validation fails. |
|
public static |
validateOrFallback(fallbackValue: *, objectToBeValidated: *, args: *): * uses validate to lookup given args in given objectToBeValidated. |
Static Public Methods
public static clone(originalObject: *): {} source
Copies all values from given originalObject into a new object, which is returned to caller.
uses ObjectManager#mergeAndClone, but passes an empty object as one of the two it desires for merging..
Params:
Name | Type | Attribute | Description |
originalObject | * |
Return:
{} |
public static mergeAndClone(originalObject: *, overrideObject: *): {} source
Merges all values from originalObject and overrideObject into a new object that is returned.
This is a deep-merge (unlike Object.assign).
First, all values from originalObject are merged into a new object. Then all values from overrideObject are merged into the same object, overriding any corresponding keys from originalObject.
Params:
Name | Type | Attribute | Description |
originalObject | * | initial values for new object |
|
overrideObject | * | object to override values from original object with |
Return:
{} | new object containing values from originalObject overridden by overrideObject (see example) |
Example:
let originalObject = {
foo: "bar",
person: {
name: "Sandy claws",
age: 42
}
}
let overrideObject = {
foo: "baz",
person: {
age: 23,
phone: 12345678
}
}
let mergedObject = ObjectManager.mergeAndCopy(originalObject, overrideObject);
// mergedObject will now be:
mergedObject == {
foo: "baz",
person: {
age: 23,
phone: 12345678,
name: "Sandy claws"
}
}
public static mergeInPlace(originalObject: *, overrideObject: *) source
Merges all values from overrideObject into originalObject. This happens in place (as objects are passed-by-reference), so originalObject is modified.
This is a deep-merge (unlike Object.assign).
Params:
Name | Type | Attribute | Description |
originalObject | * | the object to modify |
|
overrideObject | * | the object to copy values from |
Example:
let originalObject = {
foo: "bar",
person: {
name: "Sandy claws",
age: 42
}
}
let overrideObject = {
foo: "baz",
person: {
age: 23,
phone: 12345678
}
}
ObjectManager.mergeInPlace(originalObject, overrideObject);
// originalObject will now be:
originalObject == {
foo: "baz",
person: {
age: 23,
phone: 12345678,
name: "Sandy claws"
}
}
public static validate(givenObject: *, args: *): boolean source
Validate that an object and nested keys are not null, undefined or empty string "" or empty object {}.
Params:
Name | Type | Attribute | Description |
givenObject | * | the object to validate |
|
args | * | nested keys to check |
Example:
// check that myObject.foo.bar exists:
validateAllowEmptyObject(myObject, "foo", "bar")
public static validateAllowEmptyObject(givenObject: *, args: *): boolean source
Validate that an object and nested keys are not null, undefined or empty string "".
Params:
Name | Type | Attribute | Description |
givenObject | * | the object to validate |
|
args | * | nested keys to check |
Example:
// check that myObject.foo.bar exists:
validateAllowEmptyObject(myObject, "foo", "bar")
public static validateAllowEmptyString(givenObject: *, args: *): boolean source
Validate that an object and nested keys are not null, undefined or empty object {}.
Params:
Name | Type | Attribute | Description |
givenObject | * | the object to validate |
|
args | * | nested keys to check |
Example:
// check that myObject.foo.bar exists:
validateAllowEmptyObject(myObject, "foo", "bar")
public static validateAllowEmptyStringAndEmptyObject(givenObject: *, args: *): boolean source
Validate that an object and nested keys are not null or undefined.
Params:
Name | Type | Attribute | Description |
givenObject | * | the object to validate |
|
args | * | nested keys to check |
Example:
// check that myObject.foo.bar exists:
validateAllowEmptyObject(myObject, "foo", "bar")
public static validateOrCallback(callback: *, objectToBeValidated: *, args: *): * source
Utilityfunction to simplify validation! uses validateOrFallback for validation, and executes given callback (and returns returnvalue from it) if validation fails.
Params:
Name | Type | Attribute | Description |
callback | * | Function to be executed if validation fails |
|
objectToBeValidated | * | The object to do validation-lookup in |
|
args | * | indices used for lookup in objectToBeValidated |
Return:
* | lookup in objectToBeValidated if validation succeeded, returnvalue from callback if not. |
public static validateOrError(errorMessage: *, objectToBeValidated: *, args: *): * source
Utilityfunction to simplify validation! uses validateOrCallback for validation, and passes a callback that simply thrown an Error if validation fails.
Params:
Name | Type | Attribute | Description |
errorMessage | * | the message to use in new Error(errorMessage) |
|
objectToBeValidated | * | the object to validate args in |
|
args | * | args for lookup. see validateOrFallback |
Return:
* | the looked-up value from objectToBeValidated if it exists |
public static validateOrFallback(fallbackValue: *, objectToBeValidated: *, args: *): * source
uses validate to lookup given args in given objectToBeValidated. This ensures the lookup is not null, undefined, empty object, or empty string. If this test fails, given fallbackValue is returned.
Params:
Name | Type | Attribute | Description |
fallbackValue | * | what to return if empty |
|
objectToBeValidated | * | object to do lookup in |
|
args | * | indices used for lookup in object |
Return:
* | lookup in objectToBeValidated if validation succeeded, fallbackValue if not. |
Example:
// to validate myObject.foo.bar, and get "helloworld" back as default if it is empty:
validateOrFallback("helloworld", myObject, "foo", "bar")