assign-default-values-to-object
v1.0.2
Published
A function to assign default values to undefined values in an object
Downloads
10
Maintainers
Readme
Assign default values to object
An npm module exporting a function to assign default values to an object.
Installing
npm i assign-default-values-to-object
Basic Usage
import assignDefaultValuesToObject, { RecursiveRequired } from "assign-default-values-to-object";
interface IObject {
requiredProp: string;
optionalProp1?: string;
optionalProp2?: string;
}
const object: IObject = {
requiredProp: "objectRequiredProp",
optionalProp1: "objectOptionalProp1"
};
const defaultObject: IObject = {
requiredProp: "defaultObjectRequiredProp",
optionalProp1: "defaultObjectOptionalProp1",
optionalProp2: "defaultObjectOptionalProp2"
};
const parsedObject = assignDefaultValuesToObject(defaultObject, object) as RecursiveRequired<
IObject
>;
console.log(parsedObject.requiredProp)
// objectRequiredProp
console.log(parsedObject.optionalProp1)
// objectOptionalProp1
console.log(parsedObject.optionalProp2)
// defaultObjectOptionalProp2
Contributing
Please read CONTRIBUTING.md.