-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(add-object-property): new function with unit tests for adding a …
…new property to an object
- Loading branch information
1 parent
eab2f23
commit 5f1eb22
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/** | ||
* | ||
* @name addObjectProperty | ||
* | ||
* @summary How do you add a key-value pair or new property to an object? | ||
* | ||
* @since 1.3.0 | ||
* | ||
* @category Object | ||
* | ||
* @param {Object} x - Object to add new property to | ||
* @param {string} y - Property name | ||
* @param {any} z - Property value | ||
* | ||
* @return {Object} Provided object with new property added | ||
* | ||
* @example | ||
* => polyfig.addObjectProperty({}, "poly", ["fig"]); | ||
* => {"poly": ["fig"]} | ||
*/ | ||
|
||
function addObjectProperty(x, y, z) { | ||
return typeof x === "object" && x !== null && typeof y === "string" | ||
? (x[y] = z) | ||
: new Error("x is not of type object or y is not of type string"); | ||
} | ||
|
||
module.exports = addObjectProperty; |