@byte-this/funscript
Advanced tools
Comparing version
{ | ||
"name": "@byte-this/funscript", | ||
"version": "1.0.11", | ||
"version": "1.0.12", | ||
"description": "A typescript API which facilitates functional programming.", | ||
@@ -5,0 +5,0 @@ "main": "public-api.js", |
import { iFunAr } from "../models/fun-ar"; | ||
/** | ||
* Object which will hold async versions of array methods | ||
* (Yes, I do see the irony in using some imperative programming to implement this) | ||
*/ | ||
export declare const FunAr: iFunAr; |
@@ -38,7 +38,43 @@ "use strict"; | ||
}; | ||
var __spreadArray = (this && this.__spreadArray) || function (to, from) { | ||
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++) | ||
to[j] = from[i]; | ||
return to; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.FunAr = void 0; | ||
var findIndexSeq = function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var elementIndex, found, i; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
elementIndex = -1; | ||
found = false; | ||
i = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(elementIndex === -1 && i < input.length)) return [3 /*break*/, 7]; | ||
if (!(typeof thisArg !== 'undefined')) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, callback.bind(thisArg, input[i], i, input)()]; | ||
case 2: | ||
found = _a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, callback(input[i], i, input)]; | ||
case 4: | ||
found = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
if (found) { | ||
elementIndex = i; | ||
} | ||
_a.label = 6; | ||
case 6: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 7: return [2 /*return*/, elementIndex]; | ||
} | ||
}); | ||
}); }; | ||
/** | ||
* Object which will hold async versions of array methods | ||
* (Yes, I do see the irony in using some imperative programming to implement this) | ||
*/ | ||
@@ -56,16 +92,16 @@ exports.FunAr = { | ||
forEach: function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var promise; | ||
return __generator(this, function (_a) { | ||
promise = Promise.resolve(); | ||
input.forEach(function (item, index) { | ||
if (typeof thisArg !== 'undefined') { | ||
promise.then(callback.bind(thisArg, item, index, input)); | ||
} | ||
else { | ||
promise.then(function () { return callback(item, index, input); }); | ||
} | ||
}); | ||
return [2 /*return*/, promise.then(function () { return void 0; })]; | ||
return [2 /*return*/, input.reduce(function (promise, item, index) { | ||
return promise.then(function () { | ||
if (typeof thisArg !== 'undefined') { | ||
return promise.then(callback.bind(thisArg, item, index, input)); | ||
} | ||
else { | ||
return promise.then(function () { return callback(item, index, input); }); | ||
} | ||
}); | ||
}, Promise.resolve())]; | ||
}); | ||
}); }, | ||
findIndex: findIndexSeq, | ||
/** | ||
@@ -79,30 +115,9 @@ * Find an item using an asynchronous callback, returns a promise of the first found item or undefined | ||
find: function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var element, found, i; | ||
var index; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
element = void 0; | ||
found = false; | ||
i = 0; | ||
_a.label = 1; | ||
case 0: return [4 /*yield*/, findIndexSeq(input, callback, thisArg)]; | ||
case 1: | ||
if (!(!found && i < input.length)) return [3 /*break*/, 7]; | ||
if (!(typeof thisArg !== 'undefined')) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, callback.bind(thisArg, input[i], i, input)()]; | ||
case 2: | ||
found = _a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, callback(input[i], i, input)]; | ||
case 4: | ||
found = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
if (found) { | ||
element = input[i]; | ||
} | ||
_a.label = 6; | ||
case 6: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 7: return [2 /*return*/, element]; | ||
index = _a.sent(); | ||
return [2 /*return*/, index === -1 ? undefined : input[index]]; | ||
} | ||
@@ -119,30 +134,15 @@ }); | ||
map: function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var newAr, i, item, mappedItem; | ||
var onResponse; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
newAr = []; | ||
i = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(i < input.length)) return [3 /*break*/, 7]; | ||
item = input[i]; | ||
mappedItem = void 0; | ||
if (!(typeof thisArg !== 'undefined')) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, callback.bind(thisArg, item, i, input)()]; | ||
case 2: | ||
mappedItem = _a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, callback(item, i, input)]; | ||
case 4: | ||
mappedItem = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
newAr.push(mappedItem); | ||
_a.label = 6; | ||
case 6: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 7: return [2 /*return*/, newAr]; | ||
} | ||
onResponse = function (partialList, itemResult) { return __spreadArray(__spreadArray([], partialList), [itemResult]); }; | ||
return [2 /*return*/, input.reduce(function (promise, item, index) { | ||
return promise.then(function (partialList) { | ||
if (typeof thisArg !== 'undefined') { | ||
return promise.then(callback.bind(thisArg, item, index, input)).then(function (itemResult) { return onResponse(partialList, itemResult); }); | ||
} | ||
else { | ||
return promise.then(function () { return callback(item, index, input); }).then(function (itemResult) { return onResponse(partialList, itemResult); }); | ||
} | ||
}); | ||
}, Promise.resolve([]))]; | ||
}); | ||
@@ -158,32 +158,15 @@ }); }, | ||
filter: function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var newAr, i, item, doInclude; | ||
var onResponse; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
newAr = []; | ||
i = 0; | ||
_a.label = 1; | ||
case 1: | ||
if (!(i < input.length)) return [3 /*break*/, 7]; | ||
item = input[i]; | ||
doInclude = void 0; | ||
if (!thisArg) return [3 /*break*/, 3]; | ||
return [4 /*yield*/, callback.bind(thisArg, item, i, input)()]; | ||
case 2: | ||
doInclude = _a.sent(); | ||
return [3 /*break*/, 5]; | ||
case 3: return [4 /*yield*/, callback(item, i, input)]; | ||
case 4: | ||
doInclude = _a.sent(); | ||
_a.label = 5; | ||
case 5: | ||
if (doInclude) { | ||
newAr.push(item); | ||
} | ||
_a.label = 6; | ||
case 6: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 7: return [2 /*return*/, newAr]; | ||
} | ||
onResponse = function (partialList, item, doInclude) { return !doInclude ? partialList : __spreadArray(__spreadArray([], partialList), [item]); }; | ||
return [2 /*return*/, input.reduce(function (promise, item, index) { | ||
return promise.then(function (partialList) { | ||
if (thisArg) { | ||
return promise.then(callback.bind(thisArg, item, index, input)).then(function (doInclude) { return onResponse(partialList, item, doInclude); }); | ||
} | ||
else { | ||
return promise.then(function () { return callback(item, index, input); }).then(function (doInclude) { return onResponse(partialList, item, doInclude); }); | ||
} | ||
}); | ||
}, Promise.resolve([]))]; | ||
}); | ||
@@ -199,26 +182,10 @@ }); }, | ||
reduce: function (input, callback, initialValue) { return __awaiter(void 0, void 0, void 0, function () { | ||
var value, startIndex, i, item; | ||
var fullInput, firstValue, indexOffset; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
value = input[0]; | ||
startIndex = 1; | ||
if (typeof initialValue !== 'undefined') { | ||
value = initialValue; | ||
startIndex = 0; | ||
} | ||
i = startIndex; | ||
_a.label = 1; | ||
case 1: | ||
if (!(i < input.length)) return [3 /*break*/, 4]; | ||
item = input[i]; | ||
return [4 /*yield*/, callback(value, item, i, input)]; | ||
case 2: | ||
value = _a.sent(); | ||
_a.label = 3; | ||
case 3: | ||
i++; | ||
return [3 /*break*/, 1]; | ||
case 4: return [2 /*return*/, value]; | ||
} | ||
fullInput = typeof initialValue === 'undefined' ? input.slice(1) : input; | ||
firstValue = typeof initialValue === 'undefined' ? input[0] : initialValue; | ||
indexOffset = typeof initialValue === 'undefined' ? 1 : 0; | ||
return [2 /*return*/, fullInput.reduce(function (promise, item, index) { | ||
return promise.then(function (acc) { return callback(acc, item, index + indexOffset); }); | ||
}, Promise.resolve(firstValue))]; | ||
}); | ||
@@ -236,22 +203,11 @@ }); } | ||
forEach: function (input, callback, thisArg) { return __awaiter(void 0, void 0, void 0, function () { | ||
var promises; | ||
return __generator(this, function (_a) { | ||
promises = []; | ||
input.forEach(function (item, index) { | ||
if (typeof thisArg !== 'undefined') { | ||
promises.push((function () { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, callback.bind(thisArg, item, index, input)()]; | ||
}); | ||
}); })()); | ||
} | ||
else { | ||
promises.push((function () { return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, callback(item, index, input)]; | ||
}); | ||
}); })()); | ||
} | ||
}); | ||
return [2 /*return*/, Promise.all(promises).then(function () { return void 0; })]; | ||
return [2 /*return*/, Promise.all(input.reduce(function (promises, item, index) { | ||
if (typeof thisArg !== 'undefined') { | ||
return __spreadArray(__spreadArray([], promises), [callback.bind(thisArg)(item, index, input)]); | ||
} | ||
else { | ||
return __spreadArray(__spreadArray([], promises), [callback(item, index, input)]); | ||
} | ||
}, [])).then(function () { return void 0; })]; | ||
}); | ||
@@ -258,0 +214,0 @@ }); }, |
@@ -47,3 +47,3 @@ "use strict"; | ||
counter = 0; | ||
input = [1, 2, 3, 4, 5]; | ||
input = new Array(100000).fill(0).map(function (_, index) { return index; }); | ||
expectedOutput = input.reduce(function (acc, item) { return acc + item; }); | ||
@@ -169,2 +169,64 @@ return [4 /*yield*/, fun_ar_1.FunAr.async.seq.forEach(input, function (item) { return counter += item; })]; | ||
}); }, 100); | ||
it("should propogate errors thrown", function () { return __awaiter(void 0, void 0, void 0, function () { | ||
var message, input, map, err_1, newMessage, newInput, asyncMap, err_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
message = "Test throw!"; | ||
input = [1, 2, 3]; | ||
map = function (item) { | ||
if (item === 2) { | ||
throw new Error(message); | ||
} | ||
return item; | ||
}; | ||
_a.label = 1; | ||
case 1: | ||
_a.trys.push([1, 3, , 4]); | ||
return [4 /*yield*/, fun_ar_1.FunAr.async.seq.map(input, map)]; | ||
case 2: | ||
_a.sent(); | ||
fail("Error should have been thrown and propogated!"); | ||
return [3 /*break*/, 4]; | ||
case 3: | ||
err_1 = _a.sent(); | ||
expect(err_1.toString().indexOf(message)).toBeGreaterThan(-1); | ||
return [3 /*break*/, 4]; | ||
case 4: | ||
newMessage = "Test in test throw!"; | ||
newInput = [4, 5, 6]; | ||
asyncMap = function (item) { return __awaiter(void 0, void 0, void 0, function () { | ||
var result; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, new Promise(function (resolve, reject) { | ||
if (item === 5) { | ||
reject(newMessage); | ||
} | ||
else { | ||
resolve(item); | ||
} | ||
})]; | ||
case 1: | ||
result = _a.sent(); | ||
return [2 /*return*/, result * 2]; | ||
} | ||
}); | ||
}); }; | ||
_a.label = 5; | ||
case 5: | ||
_a.trys.push([5, 7, , 8]); | ||
return [4 /*yield*/, fun_ar_1.FunAr.async.seq.map(newInput, asyncMap)]; | ||
case 6: | ||
_a.sent(); | ||
fail("Error should have been thrown and propogated!"); | ||
return [3 /*break*/, 8]; | ||
case 7: | ||
err_2 = _a.sent(); | ||
expect(err_2.toString().indexOf(newMessage)).toBeGreaterThan(-1); | ||
return [3 /*break*/, 8]; | ||
case 8: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }); | ||
}); |
@@ -11,2 +11,4 @@ export declare type ArMapAsyncCallback<T> = (element: T, index?: number, array?: T[]) => T | Promise<T>; | ||
export declare type ArFindAsync = <T>(input: T[], callback: ArFindAsyncCallback<T>, thisArg?: any) => Promise<T | undefined>; | ||
export declare type ArFindIndexAsyncCallback<T> = (element: T, index?: number, array?: T[]) => boolean | Promise<boolean>; | ||
export declare type ArFindIndexAsync = <T>(input: T[], callback: ArFindIndexAsyncCallback<T>, thisArg?: any) => Promise<number>; | ||
export interface iFunAr { | ||
@@ -20,2 +22,3 @@ async: { | ||
find: ArFindAsync; | ||
findIndex: ArFindIndexAsync; | ||
}; | ||
@@ -22,0 +25,0 @@ parallel: { |
245506
0.76%2010
1.01%