-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ardalan Amini
committed
Jan 28, 2018
1 parent
e09fce2
commit e9240ba
Showing
182 changed files
with
3,003 additions
and
3,159 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
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,23 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
append(value?: any): void | ||
} | ||
} | ||
|
||
/** | ||
* Same as push but uses concat | ||
* @memberof Array | ||
* @param {*} value | ||
* @example | ||
* var myArray = [1, 2, 3] | ||
* myArray.append(0); // myArray => [1, 2, 3, 0] | ||
*/ | ||
Array.prototype.append = function(value: any): void { | ||
let array = this.concat([value]) | ||
|
||
this.length = 0 | ||
|
||
this.push(...array) | ||
} |
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,11 @@ | ||
require('../../../es6/array/append') | ||
|
||
describe("Array.prototype.append", () => { | ||
test("myArray = [1, 2, 3] & myArray.append(0) results myArray to be [1, 2, 3, 0]", () => { | ||
expect((() => { | ||
let myArray = [1, 2, 3] | ||
myArray.append(0) | ||
return myArray | ||
})()).toEqual([1, 2, 3, 0]) | ||
}) | ||
}) |
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,50 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
average(key?: string): number | ||
avg(key?: string): number | ||
} | ||
} | ||
|
||
/** | ||
* Returns the average value of a given key | ||
* @memberof Array | ||
* @param {String} [key] | ||
* @returns {number} | ||
* @example | ||
* [1, 2, 3].average(); // 2 | ||
* [{a: 1}, {a: 2}, {a: 3}].average('a'); // 2 | ||
* [{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b'); // 2 | ||
*/ | ||
Array.prototype.average = function(key?: string): number { | ||
let sum = 0 | ||
|
||
if (key) { | ||
let keys = key.split('.') | ||
|
||
this.map((item) => { | ||
keys.map((k) => item = item && item[k] || 0) | ||
|
||
sum += item | ||
}) | ||
|
||
return sum / this.length | ||
} | ||
|
||
this.map((number) => sum += number) | ||
|
||
return sum / this.length | ||
} | ||
|
||
/** | ||
* An alias of Array.prototype.average | ||
* @memberof Array | ||
* @param {String} [key] | ||
* @returns {number} | ||
* @example | ||
* [1, 2, 3].avg(); // 2 | ||
* [{a: 1}, {a: 2}, {a: 3}].avg('a'); // 2 | ||
* [{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].avg('a.b'); // 2 | ||
*/ | ||
Array.prototype.avg = Array.prototype.average |
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,15 @@ | ||
require('../../../es6/array/average') | ||
|
||
describe("Array.prototype.average", () => { | ||
test("[1, 2, 3].average() returns 2", () => { | ||
expect([1, 2, 3].average()).toBe(2) | ||
}) | ||
|
||
test("[{a: 1}, {a: 2}, {a: 3}].average('a') returns 2", () => { | ||
expect([{a: 1}, {a: 2}, {a: 3}].average('a')).toBe(2) | ||
}) | ||
|
||
test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b') returns 2", () => { | ||
expect([{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].average('a.b')).toBe(2) | ||
}) | ||
}) |
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,19 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
chunk(size: number): Array<Array<T>> | ||
} | ||
} | ||
|
||
/** | ||
* Chunks the array into smaller arrays of a specified size | ||
* @memberof Array | ||
* @param {number} size | ||
* @returns {Array[]} | ||
* @example | ||
* [1, 2, 3, 4, 5].chunk(2); // [[1,2],[3,4],[5]] | ||
*/ | ||
Array.prototype.chunk = function(size: number): Array<Array<any>> { | ||
return Array.from({ length: Math.ceil(this.length / size) }, (value: any, index: number) => this.slice(index * size, index * size + size)) | ||
} |
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,7 @@ | ||
require('../../../es6/array/chunk') | ||
|
||
describe("Array.prototype.chunk", () => { | ||
test("[1, 2, 3, 4, 5].chunk(2) returns [[1,2],[3,4],[5]]", () => { | ||
expect([1,2,3,4,5].chunk(2)).toEqual([[1,2],[3,4],[5]]) | ||
}) | ||
}) |
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,18 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
clone(): Array<T> | ||
} | ||
} | ||
|
||
/** | ||
* Returns the cloned array | ||
* @memberof Array | ||
* @returns {Array} | ||
* @example | ||
* [1, 2, 3].clone(); // [1, 2, 3] | ||
*/ | ||
Array.prototype.clone = function(): Array<any> { | ||
return [...this] | ||
} |
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,7 @@ | ||
require('../../../es6/array/clone') | ||
|
||
describe("Array.prototype.clone", () => { | ||
test("[1, 2, 3].clone() returns [1, 2, 3]", () => { | ||
expect([1, 2, 3].clone()).toEqual([1, 2, 3]) | ||
}) | ||
}) |
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,18 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
compact(): Array<T> | ||
} | ||
} | ||
|
||
/** | ||
* Removes falsey values from the array | ||
* @memberof Array | ||
* @returns {Array} | ||
* @example | ||
* [0, 1, false, 2, '', 3, 'a', 'e' * 23, NaN, 's', 34].compact(); // [ 1, 2, 3, 'a', 's', 34 ] | ||
*/ | ||
Array.prototype.compact = function(): Array<any> { | ||
return this.filter(Boolean) | ||
} |
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,7 @@ | ||
require('../../../es6/array/compact') | ||
|
||
describe("Array.prototype.compact", () => { | ||
test("[0,1,false,2,'',3,'a','e' * 23,NaN,'s',34].compact() returns [1,2,3,'a','s',34]", () => { | ||
expect([0,1,false,2,'',3,'a','e' * 23,NaN,'s',34].compact()).toEqual([1,2,3,'a','s',34]) | ||
}) | ||
}) |
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,19 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
contains(value: any): boolean | ||
} | ||
} | ||
|
||
/** | ||
* Determines whether the collection contains a given item | ||
* @memberof Array | ||
* @param {*} value | ||
* @returns {boolean} | ||
* @example | ||
* [1, 2, 3].contains(2); // true | ||
*/ | ||
Array.prototype.contains = function(value: any): boolean { | ||
return this.indexOf(value) !== -1 | ||
} |
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,7 @@ | ||
require('../../../es6/array/contains') | ||
|
||
describe("Array.prototype.contains", () => { | ||
test("[1, 2, 3].contains(2) returns true", () => { | ||
expect([1, 2, 3].contains(2)).toBe(true) | ||
}) | ||
}) |
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,20 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
count(value?: any): number | ||
} | ||
} | ||
|
||
/** | ||
* Counts the occurrences of a value in an array | ||
* @memberof Array | ||
* @param {*} [value] | ||
* @returns {number} | ||
* @example | ||
* [1, 1, 2, 1, 2, 3].count(); // 6 | ||
* [1, 1, 2, 1, 2, 3].count(1); // 3 | ||
*/ | ||
Array.prototype.count = function(value: any): number { | ||
return value ? this.reduce((a, v) => (v === value ? a + 1 : a + 0), 0) : this.length | ||
} |
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,11 @@ | ||
require('../../../es6/array/count') | ||
|
||
describe("Array.prototype.count", () => { | ||
test("[1, 1, 2, 1, 2, 3].count() returns 6", () => { | ||
expect([1, 1, 2, 1, 2, 3].count()).toBe(6) | ||
}) | ||
|
||
test("[1, 1, 2, 1, 2, 3].count(1) returns 3", () => { | ||
expect([1, 1, 2, 1, 2, 3].count(1)).toBe(3) | ||
}) | ||
}) |
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,24 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
countBy(fn: string | (() => any)): { [key: string]: any } | ||
} | ||
} | ||
|
||
/** | ||
* Groups the elements of an array based on the given function and returns the count of elements in each group | ||
* @memberof Array | ||
* @param {String|Function} fn | ||
* @returns {Object} | ||
* @example | ||
* [6.1, 4.2, 6.3].countBy(Math.floor); // {4: 1, 6: 2} | ||
* ['one', 'two', 'three'].countBy('length'); // {3: 2, 5: 1} | ||
*/ | ||
Array.prototype.countBy = function(fn: string | (() => any)): { [key: string]: any } { | ||
return this.map(typeof fn === 'function' ? fn : (val) => val[fn]).reduce((acc: { [key: string]: any }, val: string, i) => { | ||
acc[val] = (acc[val] || 0) + 1 | ||
|
||
return acc | ||
}, {}) | ||
} |
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,11 @@ | ||
require('../../../es6/array/countBy') | ||
|
||
describe("Array.prototype.countBy", () => { | ||
test('[6.1, 4.2, 6.3].countBy(Math.floor) returns {4: 1, 6: 2}', () => { | ||
expect([6.1, 4.2, 6.3].countBy(Math.floor)).toEqual({4: 1, 6: 2}) | ||
}) | ||
|
||
test("['one', 'two', 'three'].countBy('length') returns {3: 2, 5: 1}", () => { | ||
expect(['one', 'two', 'three'].countBy('length')).toEqual({3: 2, 5: 1}) | ||
}) | ||
}) |
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,30 @@ | ||
export { } | ||
|
||
declare global { | ||
interface Array<T> { | ||
crossJoin(array: Array<any>): Array<[T, any]> | ||
} | ||
} | ||
|
||
/** | ||
* Cross joins the array's values among the given arrays, returning a Cartesian product with all possible permutations | ||
* @memberof Array | ||
* @param {Array} array | ||
* @returns {Array[]} | ||
* @example | ||
* [1, 2].crossJoin(['a', 'b']); // [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']] | ||
*/ | ||
Array.prototype.crossJoin = function(array: Array<any>): Array<[any, any]> { | ||
let joined: Array<[any, any]> = [] | ||
|
||
this.map((item) => { | ||
array.map((value) => { | ||
joined.push([ | ||
item, | ||
value | ||
]) | ||
}) | ||
}) | ||
|
||
return joined | ||
} |
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,7 @@ | ||
require('../../../es6/array/crossJoin') | ||
|
||
describe("Array.prototype.crossJoin", () => { | ||
test("[1, 2].crossJoin(['a', 'b']); returns [[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]", () => { | ||
expect([1, 2].crossJoin(['a', 'b'])).toEqual([[1, 'a'], [1, 'b'], [2, 'a'], [2, 'b']]) | ||
}) | ||
}) |
Oops, something went wrong.