-
Notifications
You must be signed in to change notification settings - Fork 1
Feature/refactor #1
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| { | ||
| "esversion": 6 | ||
| } |
| 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; |
| 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; |
| 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; |
| 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; |
| 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; |
| 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) => { | ||
| if (valueFn) { | ||
| valueFn(data, arr[i], i, arr); | ||
| } | ||
| if (err) { | ||
| return onComplete(err); | ||
| } | ||
| i++; | ||
| return next(); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| next(); | ||
| } | ||
|
|
||
| module.exports = iterate; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,45 +1,23 @@ | ||
| const iterate = require('./iterate-promise'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,46 +1,24 @@ | ||
| const iterate = require('./iterate'); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = []; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) => { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
There was a problem hiding this comment.
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').