an npm package for easing the iterations
over objs , strings , ...
- single entry point
- one syntax
- cover all types
using npm
$ npm i --save for-ease
const For = require('for-ease');
import For = require('for-ease');
function job(index){console.log(index);}
For(5).do(job);// output : 0,1,2,3,4
For([2, 3]).do((index : number, currentElement: number, copyOfArr: number[]) => {
// Your Code
});
For('godSake').do((index: number, char: string, str: string) => {
// Your Code
});
let obj = {
name: 'god',
lastName: 'sake'
};
For(obj).do((key: string, value: string) => {
// Your Code
});
function reverser(array: string[]) {
return function () {
return array.pop();
}
}
let func = reverser(['god', 'sake']);
let result: string = '';
For(func).do((returns: string) => {
result += returns;
});
// result will be : 'sakegod'
//or get the return array
let result = For(func).do(x => x).returns;
// result is ['sake','god']
// dont forget that the callback function must return its argument
// otherwise the .returns give empty array
const result = For([1, 2, 3]).returnSum();
//result is 6
const result = For([1,2,3,1]).returnUniq();
// result is [1,2,3];
const originalArr = [1,2,-1];
const result = For(originalArr).returnSorted();
// result is [-1,1,2];
// originalArr still is [1,2,-1]
let sum = 0;
For([1, 2, 3, 4]).tillKey('<= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is 6
let sum = 0;
For([1, 2, 3, 4]).tillKey('>= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is 7
let sum = 0;
For([1, 2, 3, 4]).tillKey('< 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
// sum is 3
let sum = 0;
For([1, 2, 3, 4]).tillKey('> 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is 4
let sum = 0;
For([1, 2, 3, 4]).tillKey('= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
// sum is 3
let sum = '';
For(['1', '2', '3', '5']).tillKey('>= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
// sum is '35'
let sum = '';
let arr = [{ name: 'batman' }, { name: 'superman' }];
For(arr).tillKey('= 1').do((index: number, val: { name: string }, arr: number[]) => {
sum += val.name;
});
//sum is 'superman'
let result = For([0,1,2,3]).tillKey('<= 2').returns;
//result is [0,1,2]
let sum = 0;
For([1, 2, 3, 4]).tillValue('<= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is 3
let sum = 0;
For([1, 2, 3, 4]).tillValue('= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is 2
let sum = '';
For(['1', '2', '3', '5']).tillValue('>= 2').do((index: number, val: number, arr: number[]) => {
sum += val;
});
//sum is '235'
let sum = 0;
let arr = [{ name: 'batman', age: 34 }, { name: 'superman', age: 22 }, { name: 'flash', age: 18 }];
For(arr).tillValue('age > 19').do((index: number, val: { name: string, age: number }, arr: number[]) => {
sum += val.age;
});
//sum is 56
let result = For([1,2,3,4,5]).tillValue('<= 2').returns;
//result is [1,2]
let result = For(3).do().returns;
//or do this
let sameResult = For(3).returns;
//result is [0,1,2] like sameResult
let originalArray = [2,3,4];
let copyOfArray = For(originalArray).do().returns;
// or do this
let copyOfArray = For(originalArray).returns;
let originalArr = [1, 2, 3];
let result = For(originalArr).do(() => 2).returns;
// originalArr is [1,2,3]
//result is [2,2,2];
let obj = { name: 'superman', age: 34, phone: 34234 };
let result = For(obj).do().returns;
// or just do this
let result = For(obj).returns;
// either way result will be [['name', 'superman'], ['age', 34], ['phone', 34234]]
let result = For(4).do().returns;
// or just this
let result = For(4).returns;
//either way result will be [0,1,2,3]
let result = For(3).do(() => 0).returns;
//result is [0,0,0]
function reverser(array: string[]) {
return function () {
return array.pop();
}
}
let f = reverser(['god', 'sake']);
let result:any[] = [];
result = For(f).do((ret => ret)).returns;
//result is ['sake','god']
let originalArray = [1, 2, 3, 4];
let resultArray: any[] = [];
For(originalArray).do(() => 2).assign(resultArray);
//resultArr is [2 , 2 ,2 , 2] cause in this example the callback function always returns 2
const originalArray = [1, 2, 3, 4];
let resultArray: any[] = [56, 87];
For(originalArray).do((index,value) => value + 1).append(resultArray);
//resultArray is [56, 87, 2, 3, 4, 5]
let originalArray = [1, 2, 3, 4];
let resultArray: any[] = [56, 87];
For(originalArray).do((index , value) => value * value).prepend(resultArray);
//resultArray is [ 16, 9, 4, 1, 56, 87 ]
static methods are only available from version 1.1.0 and later
For
function by defualt
makes a copy
of given array but you can disable
this behavior for better performance by calling this method
For.disableCopy();
- Note :
returnSorted
method always change the copy of given array not the original
- Note : if you disable the
copy
thereturn
ofcallback
function will change the original array (if callback does not return anything original array remainsintact
) for example :
let originalArr = [1, 2, 3];
let result = For(originalArr).do(() => 2).returns;
// now result and originalArr are [2,2,2];
// defualt behavior(if copy is enable) originalArr remains [1,2,3]
// but result will be [2,2,2]
this is the defualt behavior of
For
function
For.enableCopy();//only call this if you have already disabled copying
note : you can check if copying is on or off by logging the For.__copy property (true means it copies given arrays)
I ran this test several times in different machines
For.disableCopy();//disabling the copy for better performance
const number = 6000000
const arr = For(number).returns; // this command just make us the array
//from 0 to 5999999
function a(){ } // dummy function
console.time('For');
For(arr).do(a);
console.timeEnd('For')
console.time('default for');
for (let i = 0; i < arr.length; i++) {
a();
}
console.timeEnd('default for');
console.time('forEach');
arr.forEach(a);
console.timeEnd('forEach');
For: 36.849ms
default for: 25.236ms
forEach: 294.389ms