Skip to content

Commit

Permalink
feat(add-object-property): new function with unit tests for adding a …
Browse files Browse the repository at this point in the history
…new property to an object
  • Loading branch information
JamieSlome committed Dec 28, 2020
1 parent eab2f23 commit 5f1eb22
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const sortArrayByKey = require("./util/sortArrayByKey");
const mergeObjects = require("./util/mergeObjects");
const sizeOfObject = require("./util/sizeOfObject");
const onlyUniqueValues = require("./util/onlyUniqueValues");
const addObjectProperty = require("./util/addObjectProperty");

module.exports = {
removeFromString,
Expand All @@ -21,5 +22,6 @@ module.exports = {
sortArrayByKey,
mergeObjects,
sizeOfObject,
onlyUniqueValues
onlyUniqueValues,
addObjectProperty
};
15 changes: 15 additions & 0 deletions test/polyfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,19 @@ describe("polyfig", function () {
outcome.should.eqls([1, 2, 3, "1", "3", "2"]);
});
});

describe("addObjectProperty", function () {
it("should exist as a method", function () {
expect(polyfig.addObjectProperty).to.exist;
});

it("should add a new property to an object", function () {
let x = {};
let y = "poly";
let z = ["fig"];
polyfig.addObjectProperty(x, y, z);
let outcome = x;
outcome.should.eqls({ poly: ["fig"] });
});
});
});
28 changes: 28 additions & 0 deletions util/addObjectProperty.js
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;

0 comments on commit 5f1eb22

Please sign in to comment.