From 9683267f566ace5b27cc7c9a9d5280ddcf2c2f3d Mon Sep 17 00:00:00 2001 From: TheSoniix <70700254+TheSoniix@users.noreply.github.com> Date: Thu, 16 Dec 2021 20:15:53 +0100 Subject: [PATCH 1/2] Add duplicateObject, deepDuplicateObject, deepDuplicateArray to general methods --- README.md | 33 +++++++++++++++++++++++++++++++++ helpful.js | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/tester.js | 21 +++++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/README.md b/README.md index eb5e4f7..cc21b57 100644 --- a/README.md +++ b/README.md @@ -466,6 +466,39 @@ let merged2 = helpful.mergeObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); // {"a": **Return Type:** object (`{"a": 1, "b": 2}`) +### duplicateObject +Duplicate an object +``` javascript +let duplicate = helpful.duplicateObject({a: 'a', b: 2, c: true, d: null}) // {a: 'a', b: 2, c: true, d: null} +``` + +**Parameters:** +- object: object (`{a: 'a', b: 2, c: true, d: null}`) + +**Return Type:** object (`{a: 'a', b: 2, c: true, d: null}`) + +### deepDuplicateObject +Duplicate an object and all inner objects and arrays +``` javascript +let deepDuplicate = helpful.deepDuplicateObject({a: 1, f: {g: ['h']}}) // {a: 1, f: {g: ['h']}} +``` + +**Parameters:** +- object: object (`{a: 1, f: {g: ['h']}}`) + +**Return Type:** object (`{a: 1, f: {g: ['h']}}`) + +### deepDuplicateArray +Duplicate an array and all inner objects and arrays +``` javascript +let deepDuplicate = helpful.deepDuplicateArray([1, {b: [2]}]) // [1, {b: [2]}] +``` + +**Parameters:** +- array: array (`[1, {b: [2]}]`) + +**Return Type:** array (`[1, {b: [2]}]`) + ## Hex ### hex.convertFromString diff --git a/helpful.js b/helpful.js index f55f7df..f110159 100644 --- a/helpful.js +++ b/helpful.js @@ -326,6 +326,51 @@ return returnObject; } + helpful.duplicateObject = function (object) { + return {...object}; + } + + const typeCheck = function(oldEle, newEle) { + if (oldEle === null) { + newEle= null; + } else if (oldEle === undefined) { + newEle = undefined; + } else if (Array.isArray(oldEle)) { + newEle = helpful.deepFlattenArray(oldEle); + } else if (typeof oldEle === 'object') { + if (oldEle.constructor.name === 'Date') { + newEle = new Date(oldEle.getTime()); + } else { + newEle = helpful.deepDuplicateObject(oldEle); + } + } else { + newEle = oldEle + } + + return newEle; + } + + helpful.deepDuplicateObject = function (object) { + let keys = Object.keys(object); + let newObject = {}; + + for (const key of keys) { + newObject[key] = typeCheck(object[key], newObject[key]); + } + + return {...newObject}; + } + + helpful.deepDuplicateArray = function (array) { + let newArray = []; + + for (let i = 0; i < array.length; i++) { + newArray[i] = typeCheck(array[i], newArray[i]); + } + + return [...newArray]; + } + helpful.hex = {}; /* Modified from https://github.com/TogaTech/tEnvoy */ diff --git a/test/tester.js b/test/tester.js index 1df872e..bd9abaa 100644 --- a/test/tester.js +++ b/test/tester.js @@ -297,6 +297,27 @@ describe("Tests", function() { assert.deepEqual(expected, actual); }); + i++; + it(`${i}: duplicateObject - Should duplicate an object`, function () { + let expected = {a: '1', b: 2, c: false, d: undefined, e: null}; + let actual = helpful.duplicateObject(expected); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: deepDuplicateObject - Should duplicate an object and all inner objects and arrays`, function () { + let expected = {a: 1, b: 'b', c: undefined, d: null, e: true, f: {g: ['h', 'i', {j: 'k'}]}}; + let actual = helpful.deepDuplicateObject(expected); + assert.deepEqual(expected, actual) + }); + + i++; + it(`${i}: deepDuplicateArray - Should duplicate an array and all inner objects and arrays`, function () { + let expected = [1, 'a', undefined, null, true, {b: ['c', 'd', {e: 'f', g: 2}]}]; + let actual = helpful.deepDuplicateArray(expected); + assert.deepEqual(expected, actual); + }); + }); describe("Hex", function() { let i = 0; From f53fa05d57909d57a6185549730bcb23e6721ec2 Mon Sep 17 00:00:00 2001 From: TheSoniix <70700254+TheSoniix@users.noreply.github.com> Date: Thu, 30 Dec 2021 14:21:47 +0100 Subject: [PATCH 2/2] fixed merge issues --- README.md | 27 +- helpful.js | 5 - test/tester.js | 711 +++++++++++++++++++++++++------------------------ 3 files changed, 374 insertions(+), 369 deletions(-) diff --git a/README.md b/README.md index f4321e0..c821a40 100644 --- a/README.md +++ b/README.md @@ -466,6 +466,20 @@ let merged2 = helpful.mergeObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); // {"a": **Return Type:** object (`{"a": 1, "b": 2}`) +### partitionArray +Partitions an array +```javascript +let partitionArray1 = helpful.partitionArray([1, 2, 3, 4], n => n > 2); // Array(2) [[3, 4], [1, 2]] +let partitionArray2 = helpful.partitionArray([1, 2, 3, 4, 5], n => true); // Array(2) [[1, 2, 3, 4, 5], []] +let partitionArray3 = helpful.partitionArray([1, 2, 3, 4, 5], n => false); // Array(2) [[], [1, 2, 3, 4, 5]] +``` +**Parameters:** +- array: Array (`[1, 2, 3, 4, 5]`) +- func: function(`n => n > 2`) + +**Return Type:** Array (`Array(2) [[3, 4, 5], [1, 2]]`) + + ### duplicateObject Duplicate an object ``` javascript @@ -499,19 +513,6 @@ let deepDuplicate = helpful.deepDuplicateArray([1, {b: [2]}]) // [1, {b: [2]}] **Return Type:** array (`[1, {b: [2]}]`) -### partitionArray - Partitions an array -```javascript -let partitionArray1 = helpful.partitionArray([1, 2, 3, 4], n => n > 2); // Array(2) [[3, 4], [1, 2]] -let partitionArray2 = helpful.partitionArray([1, 2, 3, 4, 5], n => true); // Array(2) [[1, 2, 3, 4, 5], []] -let partitionArray3 = helpful.partitionArray([1, 2, 3, 4, 5], n => false); // Array(2) [[], [1, 2, 3, 4, 5]] -``` -**Parameters:** -- array: Array (`[1, 2, 3, 4, 5]`) -- func: function(`n => n > 2`) - -**Return Type:** Array (`Array(2) [[3, 4, 5], [1, 2]]`) - ## Hex diff --git a/helpful.js b/helpful.js index 70d1e3d..e404578 100644 --- a/helpful.js +++ b/helpful.js @@ -358,28 +358,23 @@ } else { newEle = oldEle } - return newEle; } helpful.deepDuplicateObject = function (object) { let keys = Object.keys(object); let newObject = {}; - for (const key of keys) { newObject[key] = typeCheck(object[key], newObject[key]); } - return {...newObject}; } helpful.deepDuplicateArray = function (array) { let newArray = []; - for (let i = 0; i < array.length; i++) { newArray[i] = typeCheck(array[i], newArray[i]); } - return [...newArray]; } diff --git a/test/tester.js b/test/tester.js index cef7ce3..ccc3ffd 100644 --- a/test/tester.js +++ b/test/tester.js @@ -1,374 +1,383 @@ const helpful = require("../helpful.js") const assert = require("assert"); -describe("Tests", function() { - describe("General", function() { - let i = 0; - - i++; - it(`${i}: stringToArray - should convert string to array`, function() { - let expected = ["t", "e", "s", "t"]; - let actual = helpful.stringToArray("test"); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: duplicateArray - Should duplicate array`, function() { - let expected = ["t", "e", "s", "t"]; - let actual = helpful.duplicateArray(expected); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function() { - let expected = [1]; - let actual = helpful.differenceOfArrays([2, 1], [2, 3]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function() { - let expected = []; - let actual = helpful.differenceOfArrays([], [2, 3]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function() { - let expected = [10, 20]; - let actual = helpful.differenceOfArrays([10, 20], [2, 1]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: sumOfArrays - Should find the sum of two arrays`, function() { - let expected = [1, 2, 3, 4]; - let actual = helpful.sumOfArrays([1, 2], [3, 4]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: capitalize - Should render capitalized word (single word)`, function() { - let expected = "Hello"; - let actual = helpful.capitalize('heLLo'); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: capitalize - Should render capitalized word (multiple words)`, function() { - let expected = "Hello Javascript World"; - let actual = helpful.capitalize('hello javaScript world'); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: mergeArrays - Should merge the elements of two arrays`, function() { - let expected = [2, 4, 6, 8, 10, 1]; - let actual = helpful.mergeArrays([2, 4, 6], [6, 8, 10, 4, 1, 2]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: shuffleArray - Should shuffle array`, function() { - let unshuffled = [1, 2, 3, 4, 5, 6, 7]; - let shuffled = helpful.shuffleArray(unshuffled); - assert.equal(7, shuffled.length); - assert.equal(7, unshuffled.length); - }); - - i++; - it(`${i}: reverseArray - Should reverse array`, function() { - let expected = [7, 6, 5, 4, 3, 2, 1]; - let actual = helpful.reverseArray([1, 2, 3, 4, 5, 6, 7]); - assert.deepEqual(expected, actual); - assert.equal(7, expected.length); - assert.equal(7, actual.length); - }); - - i++; - it(`${i}: pad - Should default pad with spaces`, function() { - let expected = " test "; - let actual = helpful.pad("test", 8); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: pad - Should pad the end if extra room`, function() { - let expected = "**test***"; - let actual = helpful.pad("test", 9, "*"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: pad - Should cut the end of the padded characters if too long`, function() { - let expected = "_a_test_a_-"; - let actual = helpful.pad("test", 11, "_a_-"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: pad - Should cut the end of both sides if too long`, function() { - let expected = "_a_test_a_"; - let actual = helpful.pad("test", 10, "_a_-"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: padStart - Should pad only the beginning of the string`, function() { - let expected = "**test"; - let actual = helpful.padStart("test", 6, "*"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: padStart - Should cut the end of the padded characters if too long`, function() { - let expected = "_atest"; - let actual = helpful.padStart("test", 6, "_a_-"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: padEnd - Should pad only the end of the string`, function() { - let expected = "test**"; - let actual = helpful.padEnd("test", 6, "*"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: padEnd - Should cut the end of the padded characters if too long`, function() { - let expected = "test_a"; - let actual = helpful.padEnd("test", 6, "_a_-"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: chunkArray - Should split array into n-sized chunks`, function() { - let expected = [[1, 2, 3], [4, 5, 6]]; - let actual = helpful.chunkArray([1, 2, 3, 4, 5, 6], 3); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: chunkArray - Should split array into n-sized chunks (with one incomplete chunk)`, function() { - let expected = [[1, 2, 3], [4, 5]]; - let actual = helpful.chunkArray([1, 2, 3, 4, 5], 3); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: average - Should calculate the average of an array`, function() { - let expected = 2.75; - let actual = helpful.average([1, 2, 4, 4]); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: fillArray - Should return array filled with the given value`, function() { - let expected = [1, 1, 1, 1, 1, 1, 1]; - let actual = helpful.fillArray([1, 2, 3, 4, 5, 6, 7], 1); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: fillArray - Should fill array with the given value`, function() { - let expected = ["*", "*", "*", "*"]; - let actual1 = [1, 2, 3, 4]; - let actual2 = helpful.fillArray(actual1, "*"); - assert.deepEqual(expected, actual1); - assert.deepEqual(expected, actual2); - }); - - i++; - it(`${i}: flatten - Should flatten a multidimensional array`, function() { - let expected = [0, 1, 2, 3]; - let actual = helpful.flattenArray([[0, 1], [2, 3]]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: flatten - Should flatten a multidimensional array containing a multidimensional array`, function() { - let expected = [0, 1, 2, [3, 4]]; - let actual = helpful.flattenArray([[0, 1], [2, [3, 4]]]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: deepFlatten - Should deep flatten a multidimensional array`, function() { - let expected = [0, 1, 2, 3]; - let actual = helpful.deepFlattenArray([[0, 1], [2, 3]]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: deepFlatten - Should deep flatten a multidimensional array containing a multidimensional array`, function() { - let expected = [0, 1, 2, 3, 4]; - let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4]]]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: deepFlatten - Should deep flatten a multidimensional array containing multiple inner multidimensional arrays`, function() { - let expected = [0, 1, 2, 3, 4, 5, 6]; - let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]]]]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: padArray - Should default pad an array with undefined`, function () { - let expected = [undefined, undefined, "t", "e", "s", "t", undefined, undefined]; - let actual = helpful.padArray(["t", "e", "s", "t"], 8); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: padArray - Should pad an array end if extra room`, function () { - let expected = ["*", "*", "t", "e", "s", "t", "*", "*", "*"]; - let actual = helpful.padArray(["t", "e", "s", "t"], 9, ["*"]); - assert.deepEqual(expected, actual); - }); +describe("Tests", function () { + describe("General", function () { + let i = 0; + + i++; + it(`${i}: stringToArray - should convert string to array`, function () { + let expected = ["t", "e", "s", "t"]; + let actual = helpful.stringToArray("test"); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: duplicateArray - Should duplicate array`, function () { + let expected = ["t", "e", "s", "t"]; + let actual = helpful.duplicateArray(expected); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: duplicateArray - Should duplicate array`, function () { + let expected = ["t", "e", "s", "t"]; + let actual = helpful.duplicateArray(expected); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function () { + let expected = [1]; + let actual = helpful.differenceOfArrays([2, 1], [2, 3]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function () { + let expected = []; + let actual = helpful.differenceOfArrays([], [2, 3]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: differenceOfArrays - Should find the difference of two arrays`, function () { + let expected = [10, 20]; + let actual = helpful.differenceOfArrays([10, 20], [2, 1]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: sumOfArrays - Should find the sum of two arrays`, function () { + let expected = [1, 2, 3, 4]; + let actual = helpful.sumOfArrays([1, 2], [3, 4]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: capitalize - Should render capitalized word (single word)`, function () { + let expected = "Hello"; + let actual = helpful.capitalize('heLLo'); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: capitalize - Should render capitalized word (multiple words)`, function () { + let expected = "Hello Javascript World"; + let actual = helpful.capitalize('hello javaScript world'); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: mergeArrays - Should merge the elements of two arrays`, function () { + let expected = [2, 4, 6, 8, 10, 1]; + let actual = helpful.mergeArrays([2, 4, 6], [6, 8, 10, 4, 1, 2]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: shuffleArray - Should shuffle array`, function () { + let unshuffled = [1, 2, 3, 4, 5, 6, 7]; + let shuffled = helpful.shuffleArray(unshuffled); + assert.equal(7, shuffled.length); + assert.equal(7, unshuffled.length); + }); + + i++; + it(`${i}: reverseArray - Should reverse array`, function () { + let expected = [7, 6, 5, 4, 3, 2, 1]; + let actual = helpful.reverseArray([1, 2, 3, 4, 5, 6, 7]); + assert.deepEqual(expected, actual); + assert.equal(7, expected.length); + assert.equal(7, actual.length); + }); + + i++; + it(`${i}: pad - Should default pad with spaces`, function () { + let expected = " test "; + let actual = helpful.pad("test", 8); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: pad - Should pad the end if extra room`, function () { + let expected = "**test***"; + let actual = helpful.pad("test", 9, "*"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: pad - Should cut the end of the padded characters if too long`, function () { + let expected = "_a_test_a_-"; + let actual = helpful.pad("test", 11, "_a_-"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: pad - Should cut the end of both sides if too long`, function () { + let expected = "_a_test_a_"; + let actual = helpful.pad("test", 10, "_a_-"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: padStart - Should pad only the beginning of the string`, function () { + let expected = "**test"; + let actual = helpful.padStart("test", 6, "*"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: padStart - Should cut the end of the padded characters if too long`, function () { + let expected = "_atest"; + let actual = helpful.padStart("test", 6, "_a_-"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: padEnd - Should pad only the end of the string`, function () { + let expected = "test**"; + let actual = helpful.padEnd("test", 6, "*"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: padEnd - Should cut the end of the padded characters if too long`, function () { + let expected = "test_a"; + let actual = helpful.padEnd("test", 6, "_a_-"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: chunkArray - Should split array into n-sized chunks`, function () { + let expected = [[1, 2, 3], [4, 5, 6]]; + let actual = helpful.chunkArray([1, 2, 3, 4, 5, 6], 3); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: chunkArray - Should split array into n-sized chunks (with one incomplete chunk)`, function () { + let expected = [[1, 2, 3], [4, 5]]; + let actual = helpful.chunkArray([1, 2, 3, 4, 5], 3); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: average - Should calculate the average of an array`, function () { + let expected = 2.75; + let actual = helpful.average([1, 2, 4, 4]); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: fillArray - Should return array filled with the given value`, function () { + let expected = [1, 1, 1, 1, 1, 1, 1]; + let actual = helpful.fillArray([1, 2, 3, 4, 5, 6, 7], 1); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: fillArray - Should fill array with the given value`, function () { + let expected = ["*", "*", "*", "*"]; + let actual1 = [1, 2, 3, 4]; + let actual2 = helpful.fillArray(actual1, "*"); + assert.deepEqual(expected, actual1); + assert.deepEqual(expected, actual2); + }); + + i++; + it(`${i}: flatten - Should flatten a multidimensional array`, function () { + let expected = [0, 1, 2, 3]; + let actual = helpful.flattenArray([[0, 1], [2, 3]]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: flatten - Should flatten a multidimensional array containing a multidimensional array`, function () { + let expected = [0, 1, 2, [3, 4]]; + let actual = helpful.flattenArray([[0, 1], [2, [3, 4]]]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: deepFlatten - Should deep flatten a multidimensional array`, function () { + let expected = [0, 1, 2, 3]; + let actual = helpful.deepFlattenArray([[0, 1], [2, 3]]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: deepFlatten - Should deep flatten a multidimensional array containing a multidimensional array`, function () { + let expected = [0, 1, 2, 3, 4]; + let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4]]]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: deepFlatten - Should deep flatten a multidimensional array containing multiple inner multidimensional arrays`, function () { + let expected = [0, 1, 2, 3, 4, 5, 6]; + let actual = helpful.deepFlattenArray([[0, 1], [2, [3, 4, [5, [6]]]]]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: padArray - Should default pad an array with undefined`, function () { + let expected = [undefined, undefined, "t", "e", "s", "t", undefined, undefined]; + let actual = helpful.padArray(["t", "e", "s", "t"], 8); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: padArray - Should pad an array end if extra room`, function () { + let expected = ["*", "*", "t", "e", "s", "t", "*", "*", "*"]; + let actual = helpful.padArray(["t", "e", "s", "t"], 9, ["*"]); + assert.deepEqual(expected, actual); + }); + + i++ + it(`${i}: padArray - Should cut the end of the padding array on one side if too long`, function () { + let expected = ["_", "a", "_", "t", "e", "s", "t", "_", "a", "_", "-"]; + let actual = helpful.padArray(["t", "e", "s", "t"], 11, ["_", "a", "_", "-"]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: padArray - Should cut the end of the padding array on both sides if too long`, function () { + let expected = ["_", "a", "_", "t", "e", "s", "t", "_", "a", "_"]; + let actual = helpful.padArray(["t", "e", "s", "t"], 10, ["_", "a", "_", "-"]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: padArrayStart - Should pad only the beginning of the array`, function () { + let expected = ["*", "*", "t", "e", "s", "t"]; + let actual = helpful.padArrayStart(["t", "e", "s", "t"], 6, ["*"]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: padArrayEnd - Should pad only the end of the array`, function () { + let expected = ["t", "e", "s", "t", "*", "*"]; + let actual = helpful.padArrayEnd(["t", "e", "s", "t"], 6, ["*"]); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: escape - Should escape HTML characters`, function () { + let expected = "<p>"; + let actual = helpful.escape("
"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: escape - Should escape HTML entities`, function () { + let expected = "<p>"; + let actual = helpful.escape("<p>"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: unescape - Should reverse escape HTML characters`, function () { + let expected = "
"; + let actual = helpful.unescape("<p>"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: unescape - Should reverse escape HTML entities`, function () { + let expected = "<p>"; + let actual = helpful.unescape("<p>"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: mergeObjects - Should merge two objects`, function () { + let expected = {"a": 1, "b": 2}; + let actual = helpful.mergeObjects({"a": 1}, {"b": 2}); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: mergeObjects - Should merge two objects, giving the first object's keys precedence`, function () { + let expected = {"a": 1, "b": 2, "c": 4}; + let actual = helpful.mergeObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); + assert.deepEqual(expected, actual); + }); i++ - it(`${i}: padArray - Should cut the end of the padding array on one side if too long`, function () { - let expected = ["_", "a", "_", "t", "e", "s", "t", "_", "a", "_", "-"]; - let actual = helpful.padArray(["t", "e", "s", "t"], 11, ["_", "a", "_", "-"]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: padArray - Should cut the end of the padding array on both sides if too long`, function () { - let expected = ["_", "a", "_", "t", "e", "s", "t", "_", "a", "_"]; - let actual = helpful.padArray(["t", "e", "s", "t"], 10, ["_", "a", "_", "-"]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: padArrayStart - Should pad only the beginning of the array`, function () { - let expected = ["*", "*", "t", "e", "s", "t"]; - let actual = helpful.padArrayStart(["t", "e", "s", "t"], 6, ["*"]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: padArrayEnd - Should pad only the end of the array`, function () { - let expected = ["t", "e", "s", "t", "*", "*"]; - let actual = helpful.padArrayEnd(["t", "e", "s", "t"], 6, ["*"]); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: escape - Should escape HTML characters`, function () { - let expected = "<p>"; - let actual = helpful.escape("
"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: escape - Should escape HTML entities`, function () { - let expected = "<p>"; - let actual = helpful.escape("<p>"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: unescape - Should reverse escape HTML characters`, function () { - let expected = "
"; - let actual = helpful.unescape("<p>"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: unescape - Should reverse escape HTML entities`, function () { - let expected = "<p>"; - let actual = helpful.unescape("<p>"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: mergeObjects - Should merge two objects`, function () { - let expected = {"a": 1, "b": 2}; - let actual = helpful.mergeObjects({"a": 1}, {"b": 2}); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: mergeObjects - Should merge two objects, giving the first object's keys precedence`, function () { - let expected = {"a": 1, "b": 2, "c": 4}; - let actual = helpful.mergeObjects({"a": 1, "b": 2}, {"b": 3, "c": 4}); - assert.deepEqual(expected, actual); - }); - - i++; - - it(`${i}: duplicateObject - Should duplicate an object`, function () { - let expected = {a: '1', b: 2, c: false, d: undefined, e: null}; - let actual = helpful.duplicateObject(expected); - assert.deepEqual(expected, actual); - }); - - i++; - it(`${i}: deepDuplicateObject - Should duplicate an object and all inner objects and arrays`, function () { - let expected = {a: 1, b: 'b', c: undefined, d: null, e: true, f: {g: ['h', 'i', {j: 'k'}]}}; - let actual = helpful.deepDuplicateObject(expected); - assert.deepEqual(expected, actual) - }); - - i++; - it(`${i}: deepDuplicateArray - Should duplicate an array and all inner objects and arrays`, function () { - let expected = [1, 'a', undefined, null, true, {b: ['c', 'd', {e: 'f', g: 2}]}]; - let actual = helpful.deepDuplicateArray(expected); - - it(`${i}: partitionArray - Should return array partitioned based on condition (item > 2)`, function() { + it(`${i}: partitionArray - Should return array partitioned based on condition (item > 2)`, function () { let expected = [[3, 4], [1, 2]]; let actual = helpful.partitionArray([1, 2, 3, 4], n => n > 2); assert.deepEqual(expected, actual); }); - + i++; - it(`${i}: partitionArray - Should return array partitioned based on condition (item = true)`, function() { + it(`${i}: partitionArray - Should return array partitioned based on condition (item = true)`, function () { let expected = [[1, 2, 3, 4], []]; let actual = helpful.partitionArray([1, 2, 3, 4], n => true); assert.deepEqual(expected, actual); }); i++; - it(`${i}: partitionArray - Should return array partitioned based on condition (item = false)`, function() { + it(`${i}: partitionArray - Should return array partitioned based on condition (item = false)`, function () { let expected = [[], [1, 2, 3, 4]]; let actual = helpful.partitionArray([1, 2, 3, 4], n => false); assert.deepEqual(expected, actual); }); - }); - describe("Hex", function() { - let i = 0; - - i++; - it(`${i}: hex.convertFromString - Should convert string to hex`, function() { - let expected = "74657374"; - let actual = helpful.hex.convertFromString("test"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: hex.convertToString - Should convert hex to string`, function() { - let expected = "test"; - let actual = helpful.hex.convertToString("74657374"); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: hex.convertFromBytes - Should convert bytes to hex`, function() { - let expected = "74657374"; - let actual = helpful.hex.convertFromBytes(new Uint8Array([116, 101, 115, 116])); - assert.equal(expected, actual); - }); - - i++; - it(`${i}: hex.convertToBytes - Should convert hex to bytes`, function() { - let expected = new Uint8Array([116, 101, 115, 116]); - let actual = helpful.hex.convertToBytes("74657374"); - assert.deepEqual(expected, actual); - }); - - }); + i++; + it(`${i}: duplicateObject - Should duplicate an object`, function () { + let expected = {a: '1', b: 2, c: false, d: undefined, e: null}; + let actual = helpful.duplicateObject(expected); + assert.deepEqual(expected, actual); + }); + + i++; + it(`${i}: deepDuplicateObject - Should duplicate an object and all inner objects and arrays`, function () { + let expected = {a: 1, b: 'b', c: undefined, d: null, e: true, f: {g: ['h', 'i', {j: 'k'}]}}; + let actual = helpful.deepDuplicateObject(expected); + assert.deepEqual(expected, actual) + }); + + i++; + it(`${i}: deepDuplicateArray - Should duplicate an array and all inner objects and arrays`, function () { + let expected = [1, 'a', undefined, null, true, {b: ['c', 'd', {e: 'f', g: 2}]}]; + let actual = helpful.deepDuplicateArray(expected); + assert.deepEqual(expected, actual) + }); + + }); + describe("Hex", function () { + let i = 0; + + i++; + it(`${i}: hex.convertFromString - Should convert string to hex`, function () { + let expected = "74657374"; + let actual = helpful.hex.convertFromString("test"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: hex.convertToString - Should convert hex to string`, function () { + let expected = "test"; + let actual = helpful.hex.convertToString("74657374"); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: hex.convertFromBytes - Should convert bytes to hex`, function () { + let expected = "74657374"; + let actual = helpful.hex.convertFromBytes(new Uint8Array([116, 101, 115, 116])); + assert.equal(expected, actual); + }); + + i++; + it(`${i}: hex.convertToBytes - Should convert hex to bytes`, function () { + let expected = new Uint8Array([116, 101, 115, 116]); + let actual = helpful.hex.convertToBytes("74657374"); + assert.deepEqual(expected, actual); + }); + + }); });