This repository has been archived by the owner on Feb 20, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 606
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #237 There seem to be lint fixes for other files as well. Hope won't be a problem.
- Loading branch information
Showing
38 changed files
with
355 additions
and
235 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,11 @@ | ||
import test from 'ava' | ||
import { | ||
arrayAverage | ||
arrayAverage, | ||
} from '../src' | ||
|
||
test('Calculates the average of an array', t => { | ||
const array = [1, 2, 3, 4] | ||
const expected = 2.5 | ||
const actual = arrayAverage(array) | ||
t.deepEqual(actual, expected) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
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) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.