-
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
May 25, 2018
1 parent
95a20f7
commit 54fbf5d
Showing
34 changed files
with
178 additions
and
70 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
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,21 +1,13 @@ | ||
const method = (arr: any[], key?: string): number => { | ||
let max: number = -Infinity; | ||
let reducer = (item: any) => item; | ||
|
||
if (key) { | ||
const keys = key.split("."); | ||
|
||
arr.map((item) => { | ||
keys.map((k) => item = (item && item[k]) || 0); | ||
|
||
max = Math.max(item, max); | ||
}); | ||
|
||
return max; | ||
reducer = (item: any) => keys.reduce((prev, curr) => (prev && prev[curr]) || -Infinity, item); | ||
} | ||
|
||
arr.map((num) => max = Math.max(num, max)); | ||
|
||
return max; | ||
return arr.reduce((prev, cur) => Math.max(prev, reducer(cur)), -Infinity); | ||
}; | ||
|
||
export = method; |
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,31 +1,18 @@ | ||
const method = (arr: any[], key?: string): number => { | ||
arr.sort((a, b) => a - b); | ||
const items = [...arr].sort((a, b) => a - b); | ||
const half = Math.floor(items.length / 2); | ||
|
||
const half = Math.floor(arr.length / 2); | ||
let reducer = (item: any) => item; | ||
|
||
if (key) { | ||
const keys = key.split("."); | ||
|
||
if (arr.length % 2) { | ||
let value = arr[half]; | ||
|
||
keys.map((k) => value = (value && value[k]) || value); | ||
|
||
return value; | ||
} | ||
|
||
let value1 = arr[half - 1]; | ||
let value2 = arr[half]; | ||
|
||
keys.map((k) => value1 = (value1 && value1[k]) || value1); | ||
keys.map((k) => value2 = (value2 && value2[k]) || value2); | ||
|
||
return (value1 + value2) / 2; | ||
reducer = (item: any) => keys.reduce((prev, curr) => (prev && prev[curr]) || prev, item); | ||
} | ||
|
||
if (arr.length % 2) return arr[half]; | ||
if (items.length % 2) return reducer(items[half]); | ||
|
||
return (arr[half - 1] + arr[half]) / 2; | ||
return (reducer(items[half - 1]) + reducer(items[half])) / 2; | ||
}; | ||
|
||
export = method; |
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,21 +1,13 @@ | ||
const method = (arr: any[], key?: string): number => { | ||
let min: number = +Infinity; | ||
let reducer = (item: any) => item; | ||
|
||
if (key) { | ||
const keys = key.split("."); | ||
|
||
arr.map((item) => { | ||
keys.map((k) => item = item && item[k] || 0); | ||
|
||
min = Math.min(item, min); | ||
}); | ||
|
||
return min; | ||
reducer = (item: any) => keys.reduce((prev, curr) => (prev && prev[curr]) || +Infinity, item); | ||
} | ||
|
||
arr.map((num) => min = Math.min(num, min)); | ||
|
||
return min; | ||
return arr.reduce((prev, cur) => Math.min(prev, reducer(cur)), +Infinity); | ||
}; | ||
|
||
export = method; |
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,15 +1,15 @@ | ||
import "./index"; | ||
|
||
describe("Array.prototype.pad", () => { | ||
test("[1, 2, 3].pad(5, 0) returns [1, 2, 3]", () => { | ||
expect([1, 2, 3].pad(2, 0)).toEqual([1, 2, 3]); | ||
test("[1, 2, 3].pad(2) returns [1, 2, 3]", () => { | ||
expect([1, 2, 3].pad(2)).toEqual([1, 2, 3]); | ||
}); | ||
|
||
test("[1, 2, 3].pad(5, 0) returns [1, 2, 3, 0, 0]", () => { | ||
expect([1, 2, 3].pad(5, 0)).toEqual([1, 2, 3, 0, 0]); | ||
test("[1, 2, 3].pad(5) returns [1, 2, 3, 0, 0]", () => { | ||
expect([1, 2, 3].pad(5)).toEqual([1, 2, 3, 0, 0]); | ||
}); | ||
|
||
test("[1, 2, 3].pad(-5, 0) returns [0, 0, 1, 2, 3]", () => { | ||
expect([1, 2, 3].pad(-5, 0)).toEqual([0, 0, 1, 2, 3]); | ||
test("[1, 2, 3].pad(-5, 1) returns [1, 1, 1, 2, 3]", () => { | ||
expect([1, 2, 3].pad(-5, 1)).toEqual([1, 1, 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
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,18 +1,13 @@ | ||
const method = (arr: any[], key?: string): number => { | ||
if (key) { | ||
const keys = key.split("."); | ||
let sum = 0; | ||
|
||
arr.map((item) => { | ||
keys.map((k) => item = (item && item[k]) || 0); | ||
const reducer = (item: any) => keys.reduce((prev, curr) => (prev && prev[curr]) || 0, item); | ||
|
||
sum += item; | ||
}); | ||
|
||
return sum; | ||
return arr.reduce((prev, cur) => prev + reducer(cur), 0); | ||
} | ||
|
||
return arr.reduce((acc, val) => acc + val, 0); | ||
return arr.reduce((prev, cur) => prev + cur, 0); | ||
}; | ||
|
||
export = method; |
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import * as method from "./method"; | ||
|
||
declare global { | ||
interface NumberConstructor { | ||
digitize(num: number): number[]; | ||
} | ||
} | ||
|
||
/** | ||
* Converts the number to an array of digits | ||
* @memberof Number | ||
* @param {number} num | ||
* @returns {number[]} | ||
* @example | ||
* Number.digitize(123); // [1, 2, 3] | ||
*/ | ||
Number.digitize = method; |
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,3 @@ | ||
const method = (num: number): number[] => [...`${num}`].map((i) => +i); | ||
|
||
export = method; |
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 @@ | ||
import "./index"; | ||
|
||
describe("Number.prototype.digitize", () => { | ||
test("Number.digitize(123) returns [1, 2, 3]", () => { | ||
expect(Number.digitize(123)).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 |
---|---|---|
@@ -1,3 +1,4 @@ | ||
/** @namespace Number */ | ||
|
||
import "./digitize"; | ||
import "./isInstance"; |
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,5 +1,7 @@ | ||
import * as digitize from "./digitize/method"; | ||
import * as isInstance from "./isInstance/method"; | ||
|
||
export { | ||
digitize, | ||
isInstance, | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import * as utils from "./utils"; | ||
|
||
describe("Object utils", () => { | ||
test("addPrototype", () => { | ||
utils.addPrototype("a", () => 1); | ||
|
||
utils.addPrototype("a", () => 2); | ||
|
||
expect((Object.prototype as any).a()).toBe(1); | ||
}); | ||
}); |
Oops, something went wrong.