Skip to content
This repository was archived by the owner on Mar 14, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 6
}
52 changes: 13 additions & 39 deletions filter-promise.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,23 @@
const iterate = require('./iterate-promise');

/**
* Array filter to remove any falsy values from array
* @param {array} to be operated on
* @param {function} handle the operations, receiveds current element, index, main array and next callback
* @param {function} callback function, called when operations are completed
*/
function arrFilter(arr, func) {
return new Promise((resolve, reject) => {
if (!Array.isArray(arr)) {
return reject(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return reject(new Error('Second argument must be a valid function.'));
}

// Initialize
const len = arr.length;
let i = 0;
let _arr = [];

if (len > 0) {
func(arr[i], i, arr, next);
} else {
return resolve(_arr);
}

function next(err, isTrue) {
setImmediate(() => {
if (err) {
return reject(err);
}

if (isTrue) {
_arr = _arr.concat([arr[i]]);
}

if (++i < len) {
func(arr[i], i, arr, next);
return;
}

resolve(_arr);
});
}
});
const result = [];
return iterate(
arr,
func,
(value, item) => {
if (value) {
result.push(item);
}
},
() => result
);
}

module.exports = arrFilter;
51 changes: 13 additions & 38 deletions filter.js
Original file line number Diff line number Diff line change
@@ -1,49 +1,24 @@
const iterate = require('./iterate');

/**
* Array filter to remove any falsy values from array
* @param {array} to be operated on
* @param {function} handle the operations, receiveds current element, index, main array and next callback
* @param {function} callback function, called when operations are completed
*/
function arrFilter(arr, func, cb) {
const onComplete = (typeof cb === 'function') ? cb : function () { };

if (!Array.isArray(arr)) {
return onComplete(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return onComplete(new Error('Second argument must be a valid function.'));
}

// Initialize
let i = 0;
let _arr = [];
const len = arr.length;

if (len > 0) {
func(arr[i], i, arr, next);
} else {
return onComplete(null, _arr);
}

function next(err, isTrue) {
setImmediate(() => {
if (err) {
return onComplete(err);
const result = [];
return iterate(
arr,
func,
cb,
(value, item) => {
if (value) {
result.push(item);
}

if (isTrue) {
_arr = _arr.concat([arr[i]]);
}

if (++i < len) {
func(arr[i], i, arr, next);
return;
}

onComplete(null, _arr);
});
}
},
() => result
);
}

module.exports = arrFilter;
40 changes: 6 additions & 34 deletions foreach-promise.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,13 @@
const iterate = require('./iterate-promise');

/**
* An array foreach operation
* @param {array} to iterate over
* @param {function} to receive each element
* @returns {promise}
*/
module.exports = function (arr, func) {
return new Promise((resolve, reject) => {
if (!Array.isArray(arr)) {
return reject(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return reject(new Error('Second argument must be a valid function.'));
}

// Initialize
let i = 0;
const len = arr.length;

if (len > 0) {
func(arr[i], i, arr, next);
} else {
resolve();
}

function next(err) {
setImmediate(() => {
if (err) {
return reject(err);
}

if (++i < len) {
return func(arr[i], i, arr, next);
}
function arrForeach(arr, func) {
return iterate(arr, func);
}

return resolve();
});
}
});
};
module.exports = arrForeach;
38 changes: 3 additions & 35 deletions foreach.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,13 @@
const iterate = require('./iterate');

/**
* An array foreach operation
* @param {array} to iterate over
* @param {function} to receive each element
* @param {function} callback called when iteration completes
*/
function arrForeach(arr, func, cb) {
const onComplete = (typeof cb === 'function') ? cb : function () { };

if (!Array.isArray(arr)) {
return onComplete(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return onComplete(new Error('Second argument must be a valid function.'));
}

// Initialize
let i = 0;
const len = arr.length;
const _arr = arr.slice();

if (len > 0) {
func(_arr[i], i, _arr, next);
} else {
return onComplete();
}

function next(err) {
setImmediate(() => {
if (err) {
return onComplete(err);
}

if (++i < len) {
func(_arr[i], i, _arr, next);
return;
}

return onComplete();
});
}
return iterate(arr, func, cb);
}

module.exports = arrForeach;
9 changes: 9 additions & 0 deletions iterate-promise.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const iterate = require('./iterate');

function iteratePromise(arr, func, valueFn, resultFn) {
return new Promise((resolve, reject) => {
iterate(arr, func, (err, data) => err ? reject(err) : resolve(data), valueFn, resultFn);
});
}

module.exports = iteratePromise;
39 changes: 39 additions & 0 deletions iterate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function iterate(arr, func, cb, valueFn, resultFn) {
const onComplete = typeof cb === 'function' ? cb : () => {};

if (!Array.isArray(arr)) {
return onComplete(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return onComplete(new Error('Second argument must be a valid function.'));
}

let i = 0;
const len = arr.length;

function next() {
setImmediate(() => {
if (i >= len) {
if (resultFn) {
return onComplete(undefined, resultFn());
}
return onComplete();
}
func(arr[i], i, arr, (err, data) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

if (valueFn) {
valueFn(data, arr[i], i, arr);
}
if (err) {
return onComplete(err);
}
i++;
return next();
});
});
}

next();
}

module.exports = iterate;
48 changes: 13 additions & 35 deletions map-promise.js
Original file line number Diff line number Diff line change
@@ -1,45 +1,23 @@
const iterate = require('./iterate-promise');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


/**
* Map operation for array
* @param {array} to iterated over
* @param {function} map operation function
* @returns {promise}
*/
function arrMap(arr, func) {
return new Promise((resolve, reject) => {
if (!Array.isArray(arr)) {
return reject(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return reject(new Error('Second argument must be a valid function.'));
}

// Initialize
let i = 0;
const len = arr.length;
const _arr = new Array(len);

if (len > 0) {
func(arr[i], i, arr, next);
} else {
resolve(_arr);
}

function next(err, newValue) {
setImmediate(() => {
if (err) {
return reject(err);
}

if (i < len) {
_arr[i++] = newValue;
return func(arr[i], i, arr, next);
}

return resolve(_arr);
});
}
});
const result = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

return iterate(
arr,
func,
(value) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

if (value) {
result.push(value);
}
},
() => result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

);
}

module.exports = arrMap;
48 changes: 13 additions & 35 deletions map.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,24 @@
const iterate = require('./iterate');

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).


/**
* Map operation for array
* @param {array} to iterate over
* @param {function} to perform map operation
* @param {function} callback to indicate loop completion
*/
function arrMap(arr, func, cb) {
const onComplete = (typeof cb === 'function') ? cb : function () { };

if (!Array.isArray(arr)) {
return onComplete(new Error('First argument must be a valid array.'));
}

if (typeof func !== 'function') {
return onComplete(new Error('Second argument must be a valid function.'));
}

// Initialize
let i = 0;
const len = arr.length;
const _arr = new Array(len);

if (len > 0) {
func(arr[i], i, arr, next);
} else {
onComplete(null, _arr);
}

function next(err, newValue) {
setImmediate(() => {
if (err) {
return onComplete(err);
}

if (i < len) {
_arr[i++] = newValue;
func(arr[i], i, arr, next);
return;
const result = [];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'const' is available in ES6 (use 'esversion: 6') or Mozilla JS extensions (use moz).

return iterate(
arr,
func,
cb,
(value) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

if (value) {
result.push(value);
}

onComplete(null, _arr);
});
}
},
() => result

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'arrow function syntax (=>)' is only available in ES6 (use 'esversion: 6').

);
}

module.exports = arrMap;
Loading