From 38869bda8f3133d9299c1746258bccad87f57545 Mon Sep 17 00:00:00 2001 From: Tulsi Sapkota Date: Sat, 9 Feb 2019 12:04:04 +0545 Subject: [PATCH] feat: add clone method (#238) Closes #237 There seem to be lint fixes for other files as well. Hope won't be a problem. --- README.md | 5 ++- src/clone.js | 60 ++++++++++++++++++++++++++++++ src/index.js | 2 + test/BitwiseAnd.test.js | 6 +-- test/array-average.test.js | 4 +- test/array-median.test.js | 34 ++++++++--------- test/clone.test.js | 55 +++++++++++++++++++++++++++ test/convertToRoman.test.js | 2 +- test/cube.test.js | 2 +- test/dec2bin.test.js | 2 +- test/divide-test.js | 2 +- test/find.test.js | 10 ++--- test/gcd.test.js | 14 +++---- test/get-object-size.test.js | 52 +++++++++++++------------- test/getMiddle.test.js | 2 +- test/is-array.js | 8 ++-- test/is-function.test.js | 6 +-- test/is-numeric.test.js | 20 +++++----- test/is-prime.test.js | 48 ++++++++++++------------ test/is-today.test.js | 40 ++++++++++---------- test/largest.test.js | 2 +- test/less-than.test.js | 12 +++--- test/max.test.js | 2 +- test/numberToString.test.js | 2 +- test/occurrences.test.js | 16 ++++---- test/range.test.js | 12 +++--- test/remove-duplicates.test.js | 26 ++++++------- test/revstring.test.js | 4 +- test/rollDice.test.js | 2 +- test/search-and-replace.test.js | 12 +++--- test/shallow-equal.test.js | 26 ++++++------- test/sort-objects-array.test.js | 62 +++++++++++++++---------------- test/sum.test.js | 2 +- test/swapElements.test.js | 12 +++--- test/temperatureConverter.test.js | 4 +- test/textJustification.test.js | 6 +-- test/to-power.test.js | 8 ++-- test/validateEmail.js | 6 +-- 38 files changed, 355 insertions(+), 235 deletions(-) create mode 100644 src/clone.js create mode 100644 test/clone.test.js diff --git a/README.md b/README.md index 6e47e449..49318efb 100644 --- a/README.md +++ b/README.md @@ -19,10 +19,13 @@ This repository exists as a resource for people to learn how to contribute to op ## Usage ```javascript -import {flatten, snakeToCamel} from 'stack-overflow-copy-paste' +import {flatten, snakeToCamel, clone} from 'stack-overflow-copy-paste' flatten([[1, 2,], 3]) // [1, 2, 3] snakeToCamel('snake-case-string') // 'snakeCaseString' + +const testObj = {a: 1, b: 2} +const copyObj = clone(testObj) ``` ## LICENSE diff --git a/src/clone.js b/src/clone.js new file mode 100644 index 00000000..975380c5 --- /dev/null +++ b/src/clone.js @@ -0,0 +1,60 @@ +/** + * Handler functions + */ +const handlers = { + handleDate: date => { + // Handle Date + const copy = new Date() + copy.setTime(date.getTime()) + return copy + }, + handleArray: array => { + const {length} = array + + // Init array + const copy = array.constructor(length) + + for (let i = 0; i < length; i++) { + copy[i] = clone(array[i]) + } + + return copy + }, + handleObject: object => { + // Also copy prototypes + const copy = Object.create(Object.getPrototypeOf(object)) + + for (const attr in object) { + if (object.hasOwnProperty(attr)) { + copy[attr] = clone(object[attr]) + } + } + return copy + }, + handle(object) { + if (object instanceof Date) { + return this.handleDate(object) + } else if (object instanceof Array) { + return this.handleArray(object) + } else { + return this.handleObject(object) + } + }, +} + +/** + * Original StackOverflow answer https://stackoverflow.com/a/728694/6880789 + * This function will return cloned value + * It can handle primitive data-type, Date, Array and Object + * @param {null|undefined|number|string|function|object} object - any type + * @return {null|undefined|number|string|function|object} - any type + */ +function clone(object) { + // Handle primitive data-types, null and undefined + if (typeof object !== 'object' || object === null || typeof object === 'function') { + return object + } + return handlers.handle(object) +} + +export default clone diff --git a/src/index.js b/src/index.js index 0937f19a..072f17fe 100644 --- a/src/index.js +++ b/src/index.js @@ -89,6 +89,7 @@ import toPower from './to-power' import truncate from './truncate' import validateEmail from './validateEmail' import removeElementByIndex from './removeElementByIndex' +import clone from './clone' export { reverseArrayInPlace, @@ -182,4 +183,5 @@ export { largest, hex2hsl, removeElementByIndex, + clone, } diff --git a/test/BitwiseAnd.test.js b/test/BitwiseAnd.test.js index d9b45c72..7389fa6c 100644 --- a/test/BitwiseAnd.test.js +++ b/test/BitwiseAnd.test.js @@ -2,9 +2,9 @@ import test from 'ava' import {BitwiseAnd} from '../src' test('Returns the Bitwise And of all the Array Elements', t => { - const name1=[1,3,5] - const actual = BitwiseAnd(name1) - const expected=1 + const name1 = [1, 3, 5] + const actual = BitwiseAnd(name1) + const expected = 1 t.deepEqual(actual, expected) }) diff --git a/test/array-average.test.js b/test/array-average.test.js index 05821b13..815a3359 100644 --- a/test/array-average.test.js +++ b/test/array-average.test.js @@ -1,6 +1,6 @@ import test from 'ava' import { - arrayAverage + arrayAverage, } from '../src' test('Calculates the average of an array', t => { @@ -8,4 +8,4 @@ test('Calculates the average of an array', t => { const expected = 2.5 const actual = arrayAverage(array) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/array-median.test.js b/test/array-median.test.js index ade81af3..1fc5506d 100644 --- a/test/array-median.test.js +++ b/test/array-median.test.js @@ -2,31 +2,31 @@ import test from 'ava' import {median} from '../src' test('accepts only typeof array param', t => { - const array = {} - const expected = `${array} is not an array.` - const actual = median(array) - t.deepEqual(actual, expected) + const array = {} + const expected = `${array} is not an array.` + const actual = median(array) + t.deepEqual(actual, expected) }) test('accepts only arrays with numeric items', t => { - const array = ['hello', 'world', 5, false] - const expected = `${array} contains non-numeric items.` - const actual = median(array) - t.deepEqual(actual, expected) + const array = ['hello', 'world', 5, false] + const expected = `${array} contains non-numeric items.` + const actual = median(array) + t.deepEqual(actual, expected) }) test('accepts only nonempty arrays', t => { - const array = [] - const expected = `${array} has no items.` - const actual = median(array) - t.deepEqual(actual, expected) + const array = [] + const expected = `${array} has no items.` + const actual = median(array) + t.deepEqual(actual, expected) }) test('finds the median of an array with odd number of items', t => { - const array = [13, 2, 5] - const expected = 5 - const actual = median(array) - t.deepEqual(actual, expected) + const array = [13, 2, 5] + const expected = 5 + const actual = median(array) + t.deepEqual(actual, expected) }) test('finds the median of an array with even number of items', t => { @@ -34,4 +34,4 @@ test('finds the median of an array with even number of items', t => { const expected = 6.5 const actual = median(array) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/clone.test.js b/test/clone.test.js new file mode 100644 index 00000000..085e94eb --- /dev/null +++ b/test/clone.test.js @@ -0,0 +1,55 @@ +import test from 'ava' +import {clone} from '../src' + +test('Clone number', t => { + const original = 1 + const cloned = clone(original) + t.deepEqual(cloned, original) +}) + +test('Clone string', t => { + const original = 'test string' + const cloned = clone(original) + t.deepEqual(cloned, original) +}) + +test('Clone array', t => { + const original = [1, 2, 3] + const cloned = clone(original) + t.deepEqual(cloned, original) +}) + +test('Date', t => { + const original = new Date() + const cloned = clone(original) + t.deepEqual(cloned.getTime(), original.getTime()) +}) + +test('Clone object', t => { + const original = {a: 'test', b: 'test'} + const cloned = clone(original) + t.deepEqual(cloned, original) +}) + +test('Clone object with prototype', t => { + const original = {a: 'test'} + Object.setPrototypeOf(original, {testprop: 4}) + const cloned = clone(original) + t.deepEqual(cloned.testprop, original.testprop) +}) + +test('Clone function', t => { + function original() { + return 1 + } + const cloned = clone(original) + t.deepEqual(cloned(), original()) +}) + +test('Clone Map', t => { + const original = new Map([['key', 'test'], ['a', 'b']]) + const cloned = clone(original) + t.deepEqual(cloned, original) +}) + + diff --git a/test/convertToRoman.test.js b/test/convertToRoman.test.js index fbec0e42..0c6a4560 100644 --- a/test/convertToRoman.test.js +++ b/test/convertToRoman.test.js @@ -6,4 +6,4 @@ test('convert to roman numeral', t => { const expected = 'XXIV' const actual = convertToRoman(object) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/cube.test.js b/test/cube.test.js index 4792e444..4b1c358b 100644 --- a/test/cube.test.js +++ b/test/cube.test.js @@ -6,4 +6,4 @@ test('Cube a number ', t => { const expected = 5 * 5 * 5 const actual = cube(number) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/dec2bin.test.js b/test/dec2bin.test.js index 1c237508..d59fdf00 100644 --- a/test/dec2bin.test.js +++ b/test/dec2bin.test.js @@ -13,7 +13,7 @@ test('throws error if number is less than zero', t => { //const actual = dec2bin(original) const error = t.throws(() => { dec2bin(original) - },RangeError) + }, RangeError) t.is(error.message, 'Input must be a positive integer') }) diff --git a/test/divide-test.js b/test/divide-test.js index 501d9d9d..046b4191 100644 --- a/test/divide-test.js +++ b/test/divide-test.js @@ -4,7 +4,7 @@ import {divide} from '../src' test('divide two numbers ', t => { const number1 = 7 const number2 = 2 - const expected = 7/2 + const expected = 7 / 2 const actual = divide(number1, number2) t.deepEqual(actual, expected) }) diff --git a/test/find.test.js b/test/find.test.js index 2d1a4487..12afd6bc 100644 --- a/test/find.test.js +++ b/test/find.test.js @@ -1,8 +1,8 @@ import test from 'ava' -import { find } from '../src' +import {find} from '../src' test('find returns undefined if the input is not an instance of Array', t => { - const original = { prop: 'test' } + const original = {prop: 'test'} const predicate = element => 'does nothing' const expected = undefined const actual = find(original, element => predicate(element)) @@ -10,15 +10,15 @@ test('find returns undefined if the input is not an instance of Array', t => { }) test('find returns the first element that matches the given predicate', t => { - const original = [{ a: 1, b: 2 }, { c: 3 }, { a: 1, c: 4 }] + const original = [{a: 1, b: 2}, {c: 3}, {a: 1, c: 4}] const predicate = element => element.a === 1 - const expected = { a: 1, b: 2 } + const expected = {a: 1, b: 2} const actual = find(original, element => predicate(element)) t.deepEqual(actual, expected) }) test('find returns undefined if nothing matches the given predicate', t => { - const original = [{ a: 1, b: 2 }, { c: 3 }, { a: 1, c: 4 }] + const original = [{a: 1, b: 2}, {c: 3}, {a: 1, c: 4}] const predicate = element => element.d === 1 const expected = undefined const actual = find(original, element => predicate(element)) diff --git a/test/gcd.test.js b/test/gcd.test.js index 73d06a76..1c6c324e 100644 --- a/test/gcd.test.js +++ b/test/gcd.test.js @@ -1,10 +1,10 @@ import test from 'ava' import {gcd} from '../src' -test("Get gcd for two integers", t => { - const a = 4; - const b = 64; - const expected = 4; - const actual = gcd(a, b); - t.deepEqual(actual, expected) -}) \ No newline at end of file +test('Get gcd for two integers', t => { + const a = 4 + const b = 64 + const expected = 4 + const actual = gcd(a, b) + t.deepEqual(actual, expected) +}) diff --git a/test/get-object-size.test.js b/test/get-object-size.test.js index 960ef4ec..4de08486 100644 --- a/test/get-object-size.test.js +++ b/test/get-object-size.test.js @@ -1,34 +1,34 @@ -import test from 'ava'; -import { getObjectSize } from '../src' +import test from 'ava' +import {getObjectSize} from '../src' -test('returns zero for empty object', t=> { - const testObject = {} - const expected = 0 - const actual = getObjectSize(testObject) - t.deepEqual(actual, expected) +test('returns zero for empty object', t => { + const testObject = {} + const expected = 0 + const actual = getObjectSize(testObject) + t.deepEqual(actual, expected) }) -test('returns 5 for object with 5 keys', t=> { - const testObject = { - prop1: 'prop1', - prop2: 'prop2', - prop3: 'prop3', - prop4: 'prop4', - prop5: 'prop5', - } - const expected = 5 - const actual = getObjectSize(testObject) - t.deepEqual(actual, expected) +test('returns 5 for object with 5 keys', t => { + const testObject = { + prop1: 'prop1', + prop2: 'prop2', + prop3: 'prop3', + prop4: 'prop4', + prop5: 'prop5', + } + const expected = 5 + const actual = getObjectSize(testObject) + t.deepEqual(actual, expected) }) test('returns size of empty array', t => { - const testArray = [] - const expected = 0 - const actual = getObjectSize(testArray) - t.deepEqual(actual, expected) + const testArray = [] + const expected = 0 + const actual = getObjectSize(testArray) + t.deepEqual(actual, expected) }) test('returns size of array with items', t => { - const testArray = ['test', 'test2'] - const expected = 2 - const actual = getObjectSize(testArray) - t.deepEqual(actual, expected) + const testArray = ['test', 'test2'] + const expected = 2 + const actual = getObjectSize(testArray) + t.deepEqual(actual, expected) }) diff --git a/test/getMiddle.test.js b/test/getMiddle.test.js index e0dfec72..226c649a 100644 --- a/test/getMiddle.test.js +++ b/test/getMiddle.test.js @@ -1,5 +1,5 @@ import test from 'ava' -import { getMiddle } from '../src' +import {getMiddle} from '../src' test('Gets middle character of given string with a length of uneven number of characters ', t => { const string = 'rumpelstiltskin' diff --git a/test/is-array.js b/test/is-array.js index 77df954d..6d5a4d1b 100644 --- a/test/is-array.js +++ b/test/is-array.js @@ -2,14 +2,14 @@ import test from 'ava' import {isArray} from '../src' test('check an actual array of strings', t => { - const object = ['one','two','three'] + const object = ['one', 'two', 'three'] const expected = true const actual = isArray(object) t.deepEqual(actual, expected) }) test('check a nested array', t => { - const object = ['one',[2,3,4],'three'] + const object = ['one', [2, 3, 4], 'three'] const expected = true const actual = isArray(object) t.deepEqual(actual, expected) @@ -23,7 +23,7 @@ test('check a strings', t => { }) test('check a JSON object', t => { - const object = { keyOne: 'value1', keyTwo: 'value2'} + const object = {keyOne: 'value1', keyTwo: 'value2'} const expected = false const actual = isArray(object) t.deepEqual(actual, expected) @@ -41,4 +41,4 @@ test('check undefined', t => { const expected = false const actual = isArray(object) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/is-function.test.js b/test/is-function.test.js index 99894e8c..64222d4a 100644 --- a/test/is-function.test.js +++ b/test/is-function.test.js @@ -4,7 +4,7 @@ import {isFunction} from '../src' test('check a function', t => { const func = () => {} const expected = true - const actual = isFunction(func) + const actual = isFunction(func) t.deepEqual(actual, expected) }) @@ -22,7 +22,7 @@ test('check a string', t => { t.deepEqual(actual, expected) }) -test('check a boolean', t=> { +test('check a boolean', t => { const boolean = true const expected = false const actual = isFunction(boolean) @@ -34,4 +34,4 @@ test('check a number', t => { const expected = false const actual = isFunction(number) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/is-numeric.test.js b/test/is-numeric.test.js index a8bc5bb6..35068066 100644 --- a/test/is-numeric.test.js +++ b/test/is-numeric.test.js @@ -5,15 +5,15 @@ import test from 'ava' import {isNumeric} from '../src' test('checks if expression is number and returns true', t => { - const n = "5"; - const expected = true; - const actual = isNumeric(n); - t.deepEqual(actual, expected) -}); + const n = '5' + const expected = true + const actual = isNumeric(n) + t.deepEqual(actual, expected) +}) test('checks if expression is number and returns false', t => { - const n = "hello"; - const expected = false; - const actual = isNumeric(n); - t.deepEqual(actual, expected) -}); + const n = 'hello' + const expected = false + const actual = isNumeric(n) + t.deepEqual(actual, expected) +}) diff --git a/test/is-prime.test.js b/test/is-prime.test.js index 202539cc..7c9c0fe8 100644 --- a/test/is-prime.test.js +++ b/test/is-prime.test.js @@ -2,43 +2,43 @@ import test from 'ava' import {isPrime} from '../src' test('check negative numbers -> -3', t => { - const expected = false; - const actual = isPrime(-3); + const expected = false + const actual = isPrime(-3) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check negative numbers -> -1', t => { - const expected = false; - const actual = isPrime(-1); + const expected = false + const actual = isPrime(-1) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check 0', t => { - const expected = false; - const actual = isPrime(0); + const expected = false + const actual = isPrime(0) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check odd number greater than 2 -> 4', t => { - const expected = false; - const actual = isPrime(4); + const expected = false + const actual = isPrime(4) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check prime numbers -> 1', t => { - const expected = true; - const actual = isPrime(1); + const expected = true + const actual = isPrime(1) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check prime numbers -> 3', t => { - const expected = true; - const actual = isPrime(3); + const expected = true + const actual = isPrime(3) - t.is(actual, expected); -}); \ No newline at end of file + t.is(actual, expected) +}) diff --git a/test/is-today.test.js b/test/is-today.test.js index 8c187533..9f4885e1 100644 --- a/test/is-today.test.js +++ b/test/is-today.test.js @@ -2,35 +2,35 @@ import test from 'ava' import {isToday} from '../src' test('check today', t => { - const expected = true; - const actual = isToday(new Date()); + const expected = true + const actual = isToday(new Date()) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check previous date', t => { - const today = new Date(); - const yesterday = new Date(today.setDate(today.getDate() - 1)); + const today = new Date() + const yesterday = new Date(today.setDate(today.getDate() - 1)) - const expected = false; - const actual = isToday(yesterday); + const expected = false + const actual = isToday(yesterday) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check future date', t => { - const today = new Date(); - const tomorrow = new Date(today.setDate(today.getDate() + 1)); + const today = new Date() + const tomorrow = new Date(today.setDate(today.getDate() + 1)) - const expected = false; - const actual = isToday(tomorrow); + const expected = false + const actual = isToday(tomorrow) - t.is(actual, expected); -}); + t.is(actual, expected) +}) test('check wrong parameter', t => { - const expected = false; - const actual = isToday('dummy'); + const expected = false + const actual = isToday('dummy') - t.is(actual, expected); -}) \ No newline at end of file + t.is(actual, expected) +}) diff --git a/test/largest.test.js b/test/largest.test.js index a011bd98..1c6c6a1e 100644 --- a/test/largest.test.js +++ b/test/largest.test.js @@ -1,5 +1,5 @@ import test from 'ava' -import { largest } from '../src' +import {largest} from '../src' test('gets largest number from array', t => { const array = [23, 45, 25, 67, 24, 86, 12, 94, 37] diff --git a/test/less-than.test.js b/test/less-than.test.js index 677c3c60..2218b999 100644 --- a/test/less-than.test.js +++ b/test/less-than.test.js @@ -2,9 +2,9 @@ import test from 'ava' import {lessThan} from '../src' test('checks if num1 is less than num2', t => { - const num1 = 1 - const num2 = 2 - const expected = true - const actual = lessThan(num1, num2) - t.deepEqual(actual, expected) -}) \ No newline at end of file + const num1 = 1 + const num2 = 2 + const expected = true + const actual = lessThan(num1, num2) + t.deepEqual(actual, expected) +}) diff --git a/test/max.test.js b/test/max.test.js index 522989d6..ceaf3264 100644 --- a/test/max.test.js +++ b/test/max.test.js @@ -2,7 +2,7 @@ import test from 'ava' import {max} from '../src' test('find the maximum value of list', t => { - const list = [ 22, 1, 99, 45 ] + const list = [22, 1, 99, 45] const result = max(list) t.deepEqual(99, result) }) diff --git a/test/numberToString.test.js b/test/numberToString.test.js index 2c3787a6..613d3fae 100644 --- a/test/numberToString.test.js +++ b/test/numberToString.test.js @@ -32,4 +32,4 @@ test('Numbers greater than Number.MAX_SAFE_INTEGER should return error message', const actual = numberToString(Number.MAX_SAFE_INTEGER + 1) const errorMessage = 'Number needs to be grater than 0 or less than 2^53-1.' t.deepEqual(actual, errorMessage) -}) \ No newline at end of file +}) diff --git a/test/occurrences.test.js b/test/occurrences.test.js index bab92049..ced13b69 100644 --- a/test/occurrences.test.js +++ b/test/occurrences.test.js @@ -1,5 +1,5 @@ import test from 'ava' -import { occurrences } from '../src' +import {occurrences} from '../src' test('empty substring', t => { t.deepEqual(occurrences('', ''), 1) @@ -34,10 +34,10 @@ test('overlap', t => { }) test('overlap no occurrences', t => { - t.deepEqual(occurrences('', 'foo', true), 0); - t.deepEqual(occurrences('abc', 'foo', true), 0); - t.deepEqual(occurrences('boo', 'foo', true), 0); - t.deepEqual(occurrences('fooofooofooofoo', 'foofoo', true), 0); - t.deepEqual(occurrences('blafobooblahfoboblah', 'foo', true), 0); - t.deepEqual(occurrences('fofofofaooooofo', 'foo', true), 0); -}) \ No newline at end of file + t.deepEqual(occurrences('', 'foo', true), 0) + t.deepEqual(occurrences('abc', 'foo', true), 0) + t.deepEqual(occurrences('boo', 'foo', true), 0) + t.deepEqual(occurrences('fooofooofooofoo', 'foofoo', true), 0) + t.deepEqual(occurrences('blafobooblahfoboblah', 'foo', true), 0) + t.deepEqual(occurrences('fofofofaooooofo', 'foo', true), 0) +}) diff --git a/test/range.test.js b/test/range.test.js index a52585be..1bb825ab 100644 --- a/test/range.test.js +++ b/test/range.test.js @@ -1,32 +1,32 @@ import test from 'ava' -import { range } from '../src' +import {range} from '../src' test('creates an array of numbers from start up to end', t => { const expected = [1, 2, 3, 4] - const actual = range(1, 5); + const actual = range(1, 5) t.deepEqual(actual, expected) }) test('creates an array of numbers starting from zero given an implicit end', t => { const expected = [0, 1, 2, 3] - const actual = range(4); + const actual = range(4) t.deepEqual(actual, expected) }) test('creates an array of numbers in given steps', t => { const expected = [0, 5, 10, 15] - const actual = range(0, 20, 5); + const actual = range(0, 20, 5) t.deepEqual(actual, expected) }) test('creates an array of negative numbers given a negative step', t => { const expected = [0, -1, -2, -3] - const actual = range(0, -4, -1); + const actual = range(0, -4, -1) t.deepEqual(actual, expected) }) test('creates an empty array with no parameters given', t => { const expected = [] - const actual = range(); + const actual = range() t.deepEqual(actual, expected) }) diff --git a/test/remove-duplicates.test.js b/test/remove-duplicates.test.js index 2c7b4c7d..42b1f84b 100644 --- a/test/remove-duplicates.test.js +++ b/test/remove-duplicates.test.js @@ -2,22 +2,22 @@ import test from 'ava' import {removeDuplicates} from '../src' test('check array with duplicate strings', t => { - const array = ['hello', 'world', 'world', 'array']; - const expected = ['hello', 'world', 'array']; - const actual = removeDuplicates(array); - t.deepEqual(actual, expected); + const array = ['hello', 'world', 'world', 'array'] + const expected = ['hello', 'world', 'array'] + const actual = removeDuplicates(array) + t.deepEqual(actual, expected) }) test('check array with duplicate ints', t => { - const array = [1, 2, 3, 4, 4, 5, 5, 4, 1, 6]; - const expected = [1, 2, 3, 4, 5, 6]; - const actual = removeDuplicates(array); - t.deepEqual(actual, expected); + const array = [1, 2, 3, 4, 4, 5, 5, 4, 1, 6] + const expected = [1, 2, 3, 4, 5, 6] + const actual = removeDuplicates(array) + t.deepEqual(actual, expected) }) test('check array with no duplicates', t => { - const array = [1, 3, 5, 7, 9]; - const expected = [1, 3, 5, 7, 9]; - const actual = removeDuplicates(array); - t.deepEqual(actual, expected); -}) \ No newline at end of file + const array = [1, 3, 5, 7, 9] + const expected = [1, 3, 5, 7, 9] + const actual = removeDuplicates(array) + t.deepEqual(actual, expected) +}) diff --git a/test/revstring.test.js b/test/revstring.test.js index f846d99b..88286852 100644 --- a/test/revstring.test.js +++ b/test/revstring.test.js @@ -2,8 +2,8 @@ import test from 'ava' import {revstring} from '../src' test('string with only letters', t => { - const str = "abcdefG" - const expected = "Gfedcba" + const str = 'abcdefG' + const expected = 'Gfedcba' const result = revstring(str) t.is(expected, result) }) diff --git a/test/rollDice.test.js b/test/rollDice.test.js index dbd022e0..aa265e6c 100644 --- a/test/rollDice.test.js +++ b/test/rollDice.test.js @@ -1,5 +1,5 @@ import test from 'ava' -import { rollDice } from '../src' +import {rollDice} from '../src' test('Dice value should not be greater than 6', t => { const max = 6 diff --git a/test/search-and-replace.test.js b/test/search-and-replace.test.js index dfa307a8..883f5d01 100644 --- a/test/search-and-replace.test.js +++ b/test/search-and-replace.test.js @@ -2,10 +2,10 @@ import test from 'ava' import {searchAndReplace} from '../src' test('check the resultant string', t => { - const str = 'the quick brown fox jumps over the lazy dog'; - const find = 'lazy'; - const replace = 'active'; - const expected = 'the quick brown fox jumps over the active dog'; - const actual = searchAndReplace(str, find, replace); - t.deepEqual(actual, expected); + const str = 'the quick brown fox jumps over the lazy dog' + const find = 'lazy' + const replace = 'active' + const expected = 'the quick brown fox jumps over the active dog' + const actual = searchAndReplace(str, find, replace) + t.deepEqual(actual, expected) }) diff --git a/test/shallow-equal.test.js b/test/shallow-equal.test.js index 845db54c..360a5964 100644 --- a/test/shallow-equal.test.js +++ b/test/shallow-equal.test.js @@ -5,13 +5,13 @@ test('Compares two equal objects', t => { const a = { a: 1, b: 2, - c: 'test' + c: 'test', } const b = { a: 1, b: 2, - c: 'test' + c: 'test', } const actual = shallowEqual(a, b) @@ -23,13 +23,13 @@ test('Compares two equal objects symmetrically', t => { const a = { a: 1, b: 2, - c: 'test' + c: 'test', } const b = { a: 1, b: 2, - c: 'test' + c: 'test', } t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) @@ -38,13 +38,13 @@ test('Compares two equal objects symmetrically', t => { test('Compares two inequal objects', t => { const a = { a: 1, - b: 2 + b: 2, } const b = { a: 1, b: 2, - c: 'test' + c: 'test', } const actual = shallowEqual(a, b) @@ -55,12 +55,12 @@ test('Compares two inequal objects', t => { test('Compares two inequal objects symmetrically', t => { const b = { a: 1, - b: 2 + b: 2, } const a = { a: 1, - b: 3 + b: 3, } t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) @@ -79,7 +79,7 @@ test('Compares only own properties of given objects', t => { const b = { a: 1, b: 2, - c: 'test' + c: 'test', } const actual = shallowEqual(a, b) @@ -100,7 +100,7 @@ test('Compares only own properties of given objects symmetrically', t => { const b = { a: 1, b: 2, - c: 'test' + c: 'test', } t.deepEqual(shallowEqual(a, b), shallowEqual(b, a)) @@ -109,7 +109,7 @@ test('Compares only own properties of given objects symmetrically', t => { test('Returns true when the same object is compared to itself', t => { const a = { a: 1, - b: 'test' + b: 'test', } const actual = shallowEqual(a, a) @@ -121,10 +121,10 @@ test('Returns true when the same object is compared to itself', t => { test('Returns false when one of the objects is null', t => { const a = { a: 1, - b: 'test' + b: 'test', } const actual = shallowEqual(a, null) const expected = false t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/sort-objects-array.test.js b/test/sort-objects-array.test.js index c12238e1..f1dfa955 100644 --- a/test/sort-objects-array.test.js +++ b/test/sort-objects-array.test.js @@ -2,42 +2,42 @@ import test from 'ava' import {sortObjectsArray} from '../src' test('sort an array of objects by ascending order', t => { - const original = [ - {Name: "Name", Surname: "Surname"}, - {Name:"AAA", Surname:"ZZZ"}, - {Name: "Name", Surname: "AAA"} - ] - const expected = [ - {Name: "Name", Surname: "AAA"}, - {Name: "Name", Surname: "Surname"}, - {Name:"AAA", Surname:"ZZZ"} - ] - const actual = original.slice(0).sort(sortObjectsArray('Surname')) - t.deepEqual(actual, expected) + const original = [ + {Name: 'Name', Surname: 'Surname'}, + {Name: 'AAA', Surname: 'ZZZ'}, + {Name: 'Name', Surname: 'AAA'}, + ] + const expected = [ + {Name: 'Name', Surname: 'AAA'}, + {Name: 'Name', Surname: 'Surname'}, + {Name: 'AAA', Surname: 'ZZZ'}, + ] + const actual = original.slice(0).sort(sortObjectsArray('Surname')) + t.deepEqual(actual, expected) }) test('sort an array of objects by descending order', t => { - const original = [ - {Name: "Name", Surname: "Surname"}, - {Name:"AAA", Surname:"ZZZ"}, - {Name: "Name", Surname: "AAA"} - ] - const expected = [ - {Name:"AAA", Surname:"ZZZ"}, - {Name: "Name", Surname: "Surname"}, - {Name: "Name", Surname: "AAA"} - ] - const actual = original.slice(0).sort(sortObjectsArray('-Surname')) - t.deepEqual(actual, expected) + const original = [ + {Name: 'Name', Surname: 'Surname'}, + {Name: 'AAA', Surname: 'ZZZ'}, + {Name: 'Name', Surname: 'AAA'}, + ] + const expected = [ + {Name: 'AAA', Surname: 'ZZZ'}, + {Name: 'Name', Surname: 'Surname'}, + {Name: 'Name', Surname: 'AAA'}, + ] + const actual = original.slice(0).sort(sortObjectsArray('-Surname')) + t.deepEqual(actual, expected) }) test('will not sort the an array if the property doesn\'t exist', t => { - const original = [ - {Name: "Name", Surname: "Surname"}, - {Name:"AAA", Surname:"ZZZ"}, - {Name: "Name", Surname: "AAA"} - ] - const actual = original.slice(0).sort(sortObjectsArray('Middle')) - t.deepEqual(actual, original) + const original = [ + {Name: 'Name', Surname: 'Surname'}, + {Name: 'AAA', Surname: 'ZZZ'}, + {Name: 'Name', Surname: 'AAA'}, + ] + const actual = original.slice(0).sort(sortObjectsArray('Middle')) + t.deepEqual(actual, original) }) diff --git a/test/sum.test.js b/test/sum.test.js index 4288de01..b9694f5b 100644 --- a/test/sum.test.js +++ b/test/sum.test.js @@ -2,7 +2,7 @@ import test from 'ava' import {sum} from '../src' test('sums all array elements', t => { - const array = [1,2,3,4,5] + const array = [1, 2, 3, 4, 5] const expected = 1 + 2 + 3 + 4 + 5 const actual = sum(array) t.deepEqual(actual, expected) diff --git a/test/swapElements.test.js b/test/swapElements.test.js index 4ec9e0ad..d6fe0f71 100644 --- a/test/swapElements.test.js +++ b/test/swapElements.test.js @@ -1,11 +1,11 @@ /** * Created by HP on 3/1/2018. */ -import test from 'ava'; -import {swapElements} from '../src'; +import test from 'ava' +import {swapElements} from '../src' test('should swap two elements at given indexes in the targeted array', t => { - const target = [1,2,3]; - swapElements(1,2,target); - t.deepEqual(target, [1,3,2]); -}); \ No newline at end of file + const target = [1, 2, 3] + swapElements(1, 2, target) + t.deepEqual(target, [1, 3, 2]) +}) diff --git a/test/temperatureConverter.test.js b/test/temperatureConverter.test.js index 1bfda6ab..79e8b169 100644 --- a/test/temperatureConverter.test.js +++ b/test/temperatureConverter.test.js @@ -3,8 +3,8 @@ import {temperatureConverter} from '../src' test('converts temperature', t => { const number = 78 - const expected = (number - 32) * 5/9 + const expected = (number - 32) * 5 / 9 const actual = temperatureConverter(number) t.is(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/textJustification.test.js b/test/textJustification.test.js index 9602e926..88f79503 100644 --- a/test/textJustification.test.js +++ b/test/textJustification.test.js @@ -2,9 +2,9 @@ import test from 'ava' import {textJustification} from '../src' test('Text justification function', t => { - const object = ['This','is','an','example','of','text','justification.'] + const object = ['This', 'is', 'an', 'example', 'of', 'text', 'justification.'] const len = 16 - const expected = ['This is an','example of text','justification. '] + const expected = ['This is an', 'example of text', 'justification. '] const actual = textJustification(object, len) t.deepEqual(actual, expected) -}) \ No newline at end of file +}) diff --git a/test/to-power.test.js b/test/to-power.test.js index 053d4783..b3c0e1d3 100644 --- a/test/to-power.test.js +++ b/test/to-power.test.js @@ -4,7 +4,7 @@ import {toPower} from '../src' test('calculate base number to exponent ', t => { const number1 = 3 const number2 = 4 - const expected = 3**4 + const expected = 3 ** 4 const actual = toPower(number1, number2) t.deepEqual(actual, expected) }) @@ -12,7 +12,7 @@ test('calculate base number to exponent ', t => { test('works for negative exponents ', t => { const number1 = 8 const number2 = -2 - const expected = 8**-2 + const expected = 8 ** -2 const actual = toPower(number1, number2) t.deepEqual(actual, expected) }) @@ -20,7 +20,7 @@ test('works for negative exponents ', t => { test('works for base 0 ', t => { const number1 = 0 const number2 = 4 - const expected = 0**4 + const expected = 0 ** 4 const actual = toPower(number1, number2) t.deepEqual(actual, expected) }) @@ -28,7 +28,7 @@ test('works for base 0 ', t => { test('works for exponent 0 ', t => { const number1 = 8 const number2 = 0 - const expected = 8**0 + const expected = 8 ** 0 const actual = toPower(number1, number2) t.deepEqual(actual, expected) }) diff --git a/test/validateEmail.js b/test/validateEmail.js index 97e2d7b0..c65ca97b 100644 --- a/test/validateEmail.js +++ b/test/validateEmail.js @@ -6,19 +6,19 @@ test('validate correct email', t => { const original = 'validate@email.com' const expected = true const actual = validateEmail(original) - t.deepEqual(actual,expected) + t.deepEqual(actual, expected) }) test('validate wrong format', t => { const original = 'validateemail' const expected = false const actual = validateEmail(original) - t.deepEqual(actual,expected) + t.deepEqual(actual, expected) }) test('validate wrong format with @', t => { const original = 'validate@' const expected = false const actual = validateEmail(original) - t.deepEqual(actual,expected) + t.deepEqual(actual, expected) })