diff --git a/test/clone-object.js b/test/clone-object.js index 86ec647..0a8a03e 100644 --- a/test/clone-object.js +++ b/test/clone-object.js @@ -2,6 +2,8 @@ describe('clone object', function () { it('should clone an object', function () { var expected = {name: 'Ahmed', age: 27, skills: ['cycling', 'walking', 'eating']}, obj = {}; + // simple one liner to deep clone an object, (regarded to be the fastest method for deep cloning objects?) + obj = JSON.parse(JSON.stringify(expected)); expect(obj).toEqual(expected); expect(obj).not.toBe(expected); diff --git a/test/flatten-array.js b/test/flatten-array.js index c7f0632..cf1fdc1 100644 --- a/test/flatten-array.js +++ b/test/flatten-array.js @@ -1,8 +1,14 @@ describe('flatten array', function () { it('should flatten an array', function () { - var arr = [1, 2, [1, 2, [3, 4, 5, [1]]], 2, [2]], - expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; + var arr = [1, 2, [1, 2, [3, 4, 5, [1]]], 2, [2]]; + var expected = [1, 1, 1, 2, 2, 2, 2, 3, 4, 5]; + //enabling deep level flatten use recursion using reduce and concat methods + function flatten(arr) { + return arr.reduce(function (flat, val) => Array.isArray(val) ? flat.concat(flatten(val)) : flat.concat(val), []); + } + //sorting out an array in ascending order + arr = flatten(arr).sort(); expect(arr).toEqual(expected); }); -}); \ No newline at end of file +}); diff --git a/test/scoping.js b/test/scoping.js index 557c54a..832ae80 100644 --- a/test/scoping.js +++ b/test/scoping.js @@ -15,10 +15,11 @@ describe('scoping', function () { return this.foo; }; +//using the bind method to call a function with the 'this' value set explicitly Module.prototype.req = function() { - return request(this.method); + return request(this.method.bind(this)); }; expect(mod.req()).toBe('bar'); }); -}); \ No newline at end of file +});