Object
import createObjectDict from '@bbc/front-end-kit/js/utils/object';
import { isChildOf } from '@bbc/front-end-kit/js/utils/object';
A collection of utility functions that help us to facilitate Object manipulations.
Warning
Make sure to not duplicate Lodash's methods. We import Lodash in every project, it's very stable and maintained by a large community. Let us not reinvent the wheel.
createObjectDict (default export)
Creates a flat object dictionary from a given object. Keys of the returned object are dot-notation path strings; values are the primitive values found at those paths. Recurses into nested objects until a primitive is found.
Warning
Large deeply-nested objects may cause performance issues.
Parameters
obj(Object): The object to create a dictionary from.path(string, default''): Internal — the current dot-notation prefix. Leave empty when calling externally.
Returns
Object — a flat dictionary where each key is a dot-notation path and each value is the primitive at that path.
import createObjectDict from '@bbc/front-end-kit/js/utils/object';
const result = createObjectDict({
prop: 'value',
prop2: {
value: 'yes',
very: {
deep: {
prop: true
}
}
}
});
console.log(result);
/*
{
'prop': 'value',
'prop2.value': 'yes',
'prop2.very.deep.prop': true
}
*/
isChildOf
Checks if a given class definition extends from another class by walking the prototype chain.
Parameters
child(Function): The class to check.parent(Function): The class to check against.
Returns
boolean — true if child extends parent (directly or transitively), false otherwise.
import { isChildOf } from '@bbc/front-end-kit/js/utils/object';
import EventDispatcher from '@bbc/front-end-kit/js/managers/EventDispatcher';
import InstanceMgr from '@bbc/front-end-kit/js/managers/InstanceMgr';
import Component from '@bbc/front-end-kit/js/components/Component';
console.log(isChildOf(InstanceMgr, EventDispatcher)); // false
console.log(isChildOf(Component, EventDispatcher)); // true