Skip to content

Commit

Permalink
Some improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Ardalan Amini committed May 25, 2018
1 parent 95a20f7 commit 54fbf5d
Show file tree
Hide file tree
Showing 34 changed files with 178 additions and 70 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Changelog


## [next](https://github.com/ardalanamini/prototyped.js/releases/tag/next) *(2018-__-__)*
**Implemented enhancements:**
- `Number`
- function `digitize` added


## [v0.8.0](https://github.com/ardalanamini/prototyped.js/releases/tag/v0.8.0) *(2018-05-25)*
**Implemented enhancements:**
- `Array.prototype`
Expand Down
6 changes: 5 additions & 1 deletion lib/array/get/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ describe("Array.prototype.get", () => {
expect([1, 2, 3].get(0, "default value")).toBe(1);
});

test("[1, 2, 3].get(4, -10) returns 0", () => {
test("[1, 2, 3].get(4, -10) returns -10", () => {
expect([1, 2, 3].get(4, -10)).toBe(-10);
});

test("[1, 2, 3].get(4) returns null", () => {
expect([1, 2, 3].get(4)).toBe(null);
});
});
8 changes: 8 additions & 0 deletions lib/array/implode/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ describe("Array.prototype.implode", () => {
expect([{ a: { b: "first" } }, { a: { b: "second" } }, { a: { b: "third" } }].implode("a.b", ", "))
.toBe("first, second, third");
});

test(
"[{a: {b: \"first\"}}, {a: {b: \"second\"}}, {a: {b: \"third\"}}].implode(\"a.c\")" +
" returns \"\"",
() => {
expect([{ a: { b: "first" } }, { a: { b: "second" } }, { a: { b: "third" } }].implode("a.c"))
.toBe("");
});
});
14 changes: 3 additions & 11 deletions lib/array/max/method.ts
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;
4 changes: 4 additions & 0 deletions lib/array/max/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe("Array.prototype.max", () => {
expect([{ a: 1 }, { a: 2 }, { a: 3 }].max("a")).toBe(3);
});

test("[{a: 1}, {a: 2}, {a: 3}].max(\"b\") returns -Infinity", () => {
expect([{ a: 1 }, { a: 2 }, { a: 3 }].max("b")).toBe(-Infinity);
});

test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].max(\"a.b\") returns 3", () => {
expect([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }].max("a.b")).toBe(3);
});
Expand Down
25 changes: 6 additions & 19 deletions lib/array/median/method.ts
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;
4 changes: 4 additions & 0 deletions lib/array/median/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ describe("Array.prototype.median", () => {
expect([{ foo: 10 }, { foo: 10 }, { foo: 20 }, { foo: 40 }].median("foo")).toBe(15);
});

test("[{foo: 10}, {foo: 10}, {foo: 20}, {foo: 40}].median(\"bar\") returns NaN", () => {
expect([{ foo: 10 }, { foo: 10 }, { foo: 20 }, { foo: 40 }].median("bar")).toBe(NaN);
});

test("[{foo: 10}, {foo: 10}, {foo: 20}].median(\"foo\") returns 10", () => {
expect([{ foo: 10 }, { foo: 10 }, { foo: 20 }].median("foo")).toBe(10);
});
Expand Down
14 changes: 3 additions & 11 deletions lib/array/min/method.ts
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;
4 changes: 4 additions & 0 deletions lib/array/min/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ describe("Array.prototype.min", () => {
expect([{ a: 1 }, { a: 2 }, { a: 3 }].min("a")).toBe(1);
});

test("[{a: 1}, {a: 2}, {a: 3}].min(\"b\") returns +Infinity", () => {
expect([{ a: 1 }, { a: 2 }, { a: 3 }].min("b")).toBe(+Infinity);
});

test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].min(\"a.b\") returns 1", () => {
expect([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }].min("a.b")).toBe(1);
});
Expand Down
12 changes: 6 additions & 6 deletions lib/array/pad/test.ts
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]);
});
});
5 changes: 5 additions & 0 deletions lib/array/pluck/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ describe("Array.prototype.pluck", () => {
expect([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }].pluck("a.b"))
.toEqual([1, 2, 3]);
});

test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].pluck(\"a.c\") returns [undefined, undefined, undefined]", () => {
expect([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }].pluck("a.c"))
.toEqual([undefined, undefined, undefined]);
});
});
2 changes: 1 addition & 1 deletion lib/array/pull/method.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const method = (arr: any[], ...args: any[]): void => {
const pulled = arr.filter((value: any) => !(Array.isArray(args[0]) ? args[0] : args).includes(value));
const pulled = arr.filter((value: any) => !(args as any).includes(value));

arr.length = 0;

Expand Down
8 changes: 3 additions & 5 deletions lib/array/pull/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ describe("Array.prototype.pull", () => {
"myArray = [\"a\", \"b\", \"c\", \"a\", \"b\", \"c\"] & myArray.pull(\"a\", \"c\") results " +
"myArray to be [ \"b\", \"b\" ]",
() => {
expect((() => {
const myArray = ["a", "b", "c", "a", "b", "c"];
const myArray = ["a", "b", "c", "a", "b", "c"];

myArray.pull("a", "c");
myArray.pull("a", "c");

return myArray;
})()).toEqual(["b", "b"]);
expect(myArray).toEqual(["b", "b"]);
});
});
4 changes: 4 additions & 0 deletions lib/array/repeat/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ describe("Array.repeat", () => {
test("Array.repeat(5, 2) returns [2,2,2,2,2]", () => {
expect(Array.repeat(5, 2)).toEqual([2, 2, 2, 2, 2]);
});

test("Array.repeat(2) returns [0,0]", () => {
expect(Array.repeat(2)).toEqual([0, 0]);
});
});
11 changes: 3 additions & 8 deletions lib/array/sum/method.ts
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;
4 changes: 4 additions & 0 deletions lib/array/sum/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ describe("Array.prototype.sum", () => {
test("[{a: {b: 1}}, {a: {b: 2}}, {a: {b: 3}}].sum(\"a.b\") returns 6", () => {
expect([{ a: { b: 1 } }, { a: { b: 2 } }, { a: { b: 3 } }].sum("a.b")).toBe(6);
});

test("[{a: 1}, {a: 2}, {a: 3}].sum(\"b\") returns 0", () => {
expect([{ a: 1 }, { a: 2 }, { a: 3 }].sum("b")).toBe(0);
});
});
4 changes: 4 additions & 0 deletions lib/array/tail/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ describe("Array.prototype.tail", () => {
test("[1, 2, 3].tail() returns [2, 3]", () => {
expect([1, 2, 3].tail()).toEqual([2, 3]);
});

test("[1].tail() returns []", () => {
expect([1].tail()).toEqual([]);
});
});
4 changes: 2 additions & 2 deletions lib/function/cache/method.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// tslint:disable-next-line:ban-types
const method = (func: Function): any => {
const cache = new Map();
const cache = func.cached || new Map();

const cached = (...args: any[]) => {
const key = JSON.stringify(args);

return cache.has(key) ? cache.get(key) : cache.set(key, func.call(func, ...args)) && cache.get(key);
};

cached.cached = cache;
func.cached = cache;

return cached;
};
Expand Down
5 changes: 5 additions & 0 deletions lib/function/cache/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,9 @@ describe("Function.prototype.cache", () => {
expect(Math.max.cache(1, 22, 15))
.toBe(22);
});

test("Math.max.cache(1, 22, 15) returns 22", () => {
expect(Math.max.cache(1, 22, 15))
.toBe(22);
});
});
17 changes: 17 additions & 0 deletions lib/number/digitize/index.ts
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;
3 changes: 3 additions & 0 deletions lib/number/digitize/method.ts
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;
7 changes: 7 additions & 0 deletions lib/number/digitize/test.ts
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]);
});
});
1 change: 1 addition & 0 deletions lib/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/** @namespace Number */

import "./digitize";
import "./isInstance";
2 changes: 2 additions & 0 deletions lib/number/methods.ts
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,
};
8 changes: 8 additions & 0 deletions lib/object/clone/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,12 @@ describe("Object.prototype.$clone", () => {
expect(a).not.toBe(b);
expect(a.obj).not.toBe((b as any).obj);
});

test("{ foo: 'bar', arr: [ 1, 2 ] }.$clone(true)", () => {
const a = { foo: "bar", arr: [1, 2] };
const b = a.$clone(true); // a !== b, a.obj !== b.obj

expect(a).not.toBe(b);
expect(a.arr).not.toBe((b as any).arr);
});
});
8 changes: 3 additions & 5 deletions lib/object/equals/method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@ const method = (obj: { [key: string]: any }, obj2: any): boolean => {

if (obj instanceof Date && obj2 instanceof Date) return obj.getTime() === obj2.getTime();

if (!obj || !obj2 || (!isObject(obj) && !isObject(obj2))) return obj === obj2;
if (!obj || !obj2 || !isObject(obj2)) return obj === obj2;

if (obj === null || obj === undefined || obj2 === null || obj2 === undefined) return false;

if ((obj as any).prototype !== obj2.prototype) return false;
if ((obj as any).prototype !== (obj2 as any).prototype) return false;

const keys = Object.keys(obj);

if (keys.length !== Object.keys(obj2).length) return false;

return keys.every((k) => method((obj as any)[k], obj2[k]));
return keys.every((k) => method((obj as any)[k], (obj2 as any)[k]));
};

export = method;
15 changes: 15 additions & 0 deletions lib/object/equals/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,19 @@ describe("Object.prototype.$equals", () => {
expect(date.$equals({ a: 1 }))
.toBe(false);
});

test("date.$equals(date) returns true", () => {
const date = new Date();

expect(date.$equals(new Date()))
.toBe(true);
});

test("false.$equals(null) returns false", () => {
expect(false.$equals(null)).toBe(false);
});

test("Date.$equals(Object) returns false", () => {
expect(Date.$equals(Object)).toBe(false);
});
});
11 changes: 11 additions & 0 deletions lib/object/test.ts
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);
});
});
Loading

0 comments on commit 54fbf5d

Please sign in to comment.