Skip to content

Commit

Permalink
test: allCharactersSame, binary2Decimal, getVersion, and isObjectEmpty
Browse files Browse the repository at this point in the history
  • Loading branch information
201flaviosilva committed Oct 24, 2022
1 parent d15446d commit 54829f2
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/binary2Decimal.js
@@ -1,13 +1,12 @@
/**
* Convert a number/string to a decimal
* Convert a binary number to a decimal
*
* @example
* binary2Decimal(101010); // 42
* binary2Decimal("101010"); // 42
*
* @param {number|string} binary - the number/string to be converted
* @param {string} binary - the value to be converted
* @returns {number} - conversion decimal
*/
export function binary2Decimal(binary) {
if (typeof binary === "string") return parseInt(binary.split("").reverse().join(""), 2);
else if (typeof binary === "number") return parseInt(binary, 2);
if (typeof binary === "string") return parseInt(binary, 2);
}
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -18,6 +18,7 @@ import { FIFO } from "./FIFO.js";
import { getUrlParameter } from "./getUrlParameter.js";
import { getVersion } from "./getVersion.js";
import { invertSentence, invertWords } from "./invertText.js";
import { isObjectEmpty } from "./isObjectEmpty.js";
import { isFalsy, isTruthy } from "./isTruthyFalsy.js";
import { stringToKebabCase, kebabCaseToNormal } from "./kebabCase.js";
import { and, or, xor } from "./logicalOperators.js";
Expand Down Expand Up @@ -55,6 +56,7 @@ export {
getUrlParameter,
getVersion,
invertSentence, invertWords,
isObjectEmpty,
isFalsy, isTruthy,
stringToKebabCase, kebabCaseToNormal,
and, or, xor, // logicalOperators
Expand Down
17 changes: 17 additions & 0 deletions tests/allCharactersSame.test.js
@@ -0,0 +1,17 @@
import { allCharactersSame } from "../src/index";

describe("allCharactersSame.js", () => {
test("if all characters of a string are equal", () => {
expect(allCharactersSame("beep")).toBe(false);
expect(allCharactersSame("")).toBe(true);
expect(allCharactersSame("b")).toBe(true);
expect(allCharactersSame("aaaaaaaaaaaa")).toBe(true);
});

test("if all characters of a array are equal", () => {
expect(allCharactersSame(["a", "a"])).toBe(true);
expect(allCharactersSame(["beep", "beep"])).toBe(true);
expect(allCharactersSame(["a", "b"])).toBe(false);
expect(allCharactersSame([1, 2])).toBe(false);
});
});
7 changes: 7 additions & 0 deletions tests/binary2Decimal.test.js
@@ -0,0 +1,7 @@
import { binary2Decimal } from "../src/index";

describe("binary2Decimal.js", () => {
it("should return 42 for '101010'", () => {
expect(binary2Decimal("101010")).toBe(42);
});
});
8 changes: 8 additions & 0 deletions tests/getVersion.test.js
@@ -0,0 +1,8 @@
import { getVersion } from "../src/index";
import Package from "../package.json";

describe("getVersion.js", () => {
it("should return the current lib version", () => {
expect(getVersion()).toBe(Package.version);
});
});
13 changes: 13 additions & 0 deletions tests/isObjectEmpty.test.js
@@ -0,0 +1,13 @@
import { isObjectEmpty } from "../src/index";

describe("isObjectEmpty.js", () => {
it("should return true for the empties object", () => {
expect(isObjectEmpty([])).toBe(true);
expect(isObjectEmpty({})).toBe(true);
});

it("should return false for the non empties object", () => {
expect(isObjectEmpty([1, 2])).toBe(false);
expect(isObjectEmpty({ beep: "Boop" })).toBe(false);
});
});

0 comments on commit 54829f2

Please sign in to comment.