An attempt to recursively define most built-in js functions
npm install recursify``bash`
$ npm install recursify
`javascript
var R = require('recursify');
R.map(function(x) { return x * 3}, [1,2,3]);
// return [3,6,9];
`
---R
Recursify
* R
* ~isString(input) ⇒ boolean
* ~isArray(input) ⇒ boolean
* ~isObject(input) ⇒ boolean
* ~isNumber(input) ⇒ boolean
* ~isFunction(input) ⇒ boolean
* ~isSimilar(input1, input2) ⇒ boolean
* ~isEmpty(input) ⇒ boolean
* ~exists(value, list) ⇒ boolean
* ~map(func, list) ⇒ array
* ~each(func, list) ⇒ array
* ~filter(func, list) ⇒ array
* ~reverse(list) ⇒ array
* ~insert(value, list) ⇒ array
* ~insertDesc(value, list) ⇒ array
* ~sort(list) ⇒ array
* ~sortDesc(list) ⇒ array
* ~qsort(list) ⇒ array
* ~qsortDesc(list) ⇒ array
~foldr(func, value, list) ⇒ \
~foldl(func, value, list) ⇒ \
* ~addr(list1, list2) ⇒ array
* ~addl(list1, list2) ⇒ array
* ~rgive(value, list, mutate) ⇒ array
* ~lgive(value, list, mutate) ⇒ array
* ~cplist(list) ⇒ array
* ~indexOf(match, list, index) ⇒ number
* ~genList(size, function) ⇒ array
* ~keys(obj) ⇒ array
* ~lenObj(obj) ⇒ number
* ~cpobj(obj) ⇒ object
* ~lmerge(obj1, obj2) ⇒ object
* ~rmerge(obj1, obj2) ⇒ object
* ~eachObj(func, obj) ⇒ object
* ~mapObj(func, obj) ⇒ object
~foldObj(func, start, obj) ⇒ \
* ~times(number, function) ⇒ undefined
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isString('test');
// return true;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isArray([1,2,3]);
// return true;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isObject({});
// return true;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isNumber(1);
// return true;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isFunction(function(){});
// return true;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input1 | * |
| input2 | * |
Example
`js`
isSimilar({}, []);
// return false;
*
Kind: inner method of R
| Param | Type |
|---|---|
| input | * |
Example
`js`
isEmpty([]);
// return true;
*
Kind: inner method of R
| Param | Type | Description |
|---|---|---|
| value | * | Value to see if it exists |
| list | array | List to search |
Example
`js`
exists(1, [1,2,3]);
// return true;`
Example js`
exists(1, [2,3]);
// return false;
*
Kind: inner method of R
Returns: array - New array
| Param | Type | Description |
|---|---|---|
| func | function | Function to execute on each element of list |
| list | array | Input array |
Example
`js`
map(function(x) { return x * 2; }, [1,2,3]);
// return [2,4,6];
*
Kind: inner method of R
Returns: array - Input array
| Param | Type | Description |
|---|---|---|
| func | function | Function to execute on each element of list |
| list | array | Input array |
Example
`js`
var count = 0;
each(function(x) { return count += x; }, [1,2,3]);
// return [1,2,3]
// count => 6;
*
Kind: inner method of R
Returns: array - New filtered array
| Param | Type | Description |
|---|---|---|
| func | function | Function to execute on each element of list (must return boolean) |
| list | array | Input array |
Example
`js`
filter(function(x) { return x < 3; }, [1,2,3]);
// return [1,2];
*
Kind: inner method of R
Returns: array - New reversed array
| Param | Type | Description |
|---|---|---|
| list | array | Input array |
Example
`js`
reverse([1,2,3]);
// return [3,2,1];
*
Kind: inner method of R
Returns: array - New array with inserted value
| Param | Type | Description |
|---|---|---|
| value | * | Value to insert into list |
| list | array | Input array (presorted ascending) |
Example
`js`
insert(2, [1,4,5]);
// return [1,2,4,5];
*
Kind: inner method of R
Returns: array - New array with inserted value
| Param | Type | Description |
|---|---|---|
| value | * | Value to insert into list |
| list | array | Input array (presorted descending) |
Example
`js`
insertDesc(3, [4,2,1]);
// return [4,3,2,1];
*
Kind: inner method of R
Returns: array - New sorted array-ascending
| Param | Type | Description |
|---|---|---|
| list | array | Input array |
Example
`js`
sort([1,5,3,3,1,3,5,6]);
// return [1,1,3,3,3,5,5,6];
*
Kind: inner method of R
Returns: array - New sorted array-descending
| Param | Type | Description |
|---|---|---|
| list | array | Input array |
Example
`js`
sortDesc([1,4,2,4,1,3]);
// return [4,4,3,2,1,1];
*
Kind: inner method of R
Returns: array - New sorted-unique-ascending
| Param | Type | Description |
|---|---|---|
| list | array | Input array |
Example
`js`
qsort([5,4,1,2,4,1,3,4,6]);
// return [1,2,3,4,5,6];
*
Kind: inner method of R
Returns: array - New sorted-unique-descending
| Param | Type | Description |
|---|---|---|
| list | array | Input array |
Example
`js`
qsortDesc([1,5,2,1,3,5,6,7,3]);
// return [7,6,5,3,2,1];
*
Kind: inner method of R
Returns: \* - Result after collecting
| Param | Type | Description |
|---|---|---|
| func | function | Function which takes the following arguments (currentValue, elementValue, index) |
| value | * | Starting value |
| list | array | Input array |
Example
`js`
foldr(function(x, y) { return x + y; }, 10, [1,2,3]);
// return 16;
*
Kind: inner method of R
Returns: \* - Result after collecting
| Param | Type | Description |
|---|---|---|
| func | function | Function which takes the following arguments (currentValue, elementValue, index) |
| value | * | Starting value |
| list | array | Input array |
Example
`js`
foldl(function(x, y) { return x + y; }, 10, [1,2,3]);
// return 16;
*
Kind: inner method of R
Returns: array - New array of list1 + list2
| Param | Type |
|---|---|
| list1 | array |
| list2 | array |
Example
`js`
addr([1,2,3], [4,5,6]);
// return [1,2,3,4,5,6];
*
Kind: inner method of R
Returns: array - New array of list2 + list1;
| Param | Type |
|---|---|
| list1 | array |
| list2 | array |
Example
`js`
addl([1,2,3], [4,5,6]);
// return [4,5,6,1,2,3];
*
Kind: inner method of R
Returns: array - New array of appended value
| Param | Type | Description |
|---|---|---|
| value | * | |
| list | array | |
| mutate | boolean | if true, mutate input list. Default: false |
Example
`js`
rgive(1, [4,3,2]);
// return [4,3,2,1];
*
Kind: inner method of R
Returns: array - New array of prepended value
| Param | Type | Description |
|---|---|---|
| value | * | |
| list | array | |
| mutate | boolean | if true, mutate input list: Default false |
Example
`js`
lgive(1, [2,3,4]);
// return [1,2,3,4];
*
Kind: inner method of R
| Param | Type |
|---|---|
| list | array |
Example
`js`
var a = [1,2,3];
var b = cplist(a);
a == b
// return false;
*
Kind: inner method of R
Returns: number - index value if match found. Returns -1 if no match.
| Param | Type | Description |
|---|---|---|
| match | * | Value to search for |
| list | array | Array to search through |
| index | number | Starting index to search from: Default 0 |
Example
`js`
indexOf('l', 'hello');
// return 2;`
Example js`
indexOf('a', 'hello');
// return -1;
*
Kind: inner method of R
Returns: array - New array of generated values
| Param | Type | Description |
|---|---|---|
| size | number | indicate size of array desired |
| function | function | to execute for each element of array. Takes argument (currentIndex) |
Example
`js`
genList(5, function(x) { return x * 3; });
// return [0,3,6,9,12];
*
Kind: inner method of R
| Param | Type |
|---|---|
| obj | object |
Example
`js`
keys({a: 1, b: 2});
// return ['a', 'b'];
*
Kind: inner method of R
| Param | Type |
|---|---|
| obj | object |
Example
`js`
lenObj({a: 1, b: 2});
// return 2;
*
Kind: inner method of R
Returns: object - Copy of object
| Param | Type |
|---|---|
| obj | object |
Example
`js`
var a = {hello: 'world'};
var b = cpboj(a);
b === a;
// return false
*
Kind: inner method of R
Returns: object - obj1
| Param | Type |
|---|---|
| obj1 | object |
| obj2 | object |
Example
`js`
var a = {a: 1};
var b = {b: 2};
lmerge(a, b);
// return {a: 1, b:2};
// a => {a: 1,b: 2};
// b => {b: 2};
*
Kind: inner method of R
Returns: object - obj2
| Param | Type |
|---|---|
| obj1 | object |
| obj2 | object |
Example
`js`
var a = {a: 1};
var b = {b: 2};
rmerge(a,b);
// return {a: 1,b: 2};
// a => {a: 1};
// b => {a: 1,b: 2};
*
Kind: inner method of R
Returns: object - - input object
| Param | Type | Description |
|---|---|---|
| func | function | function that executes with (value, key) as arguments |
| obj | object |
Example
`js`
var a = {a: 1, b: 2};
var count = 0;
eachObj(function(value, key) { count += value; }, a);
// return {a: 1, b: 2}
// count => 3;
*
Kind: inner method of R
Returns: object - - new object
| Param | Type | Description |
|---|---|---|
| func | function | function that executes with (value, key, {}) as arguments |
| obj | object |
Example
`js`
var a = {a: 1, b: 2}
mapObj(function(value, key, obj) {
obj[key + value] = value * 3;
}, a);
// return { a1: 3, b2: 6};
*
Kind: inner method of R
| Param | Type | Description |
|---|---|---|
| func | function | function to execute upon iteration with arguments (result, value, key) |
| start | * | starting value when executing foldObj |
| obj | object |
Example
`js`
var a = {a: 1, b: 2}
foldObj(function(result, val, key) { return result + val }, 0, a);
// return 3;
*
Kind: inner method of R
| Param | Type | Description |
|---|---|---|
| number | number | of times to execute function |
| function | function | to execute: takes 1 argument (currentNumber) |
Example
`js``
times(3, function(x) { console.log('Number: ', x); });
// return Number: 3
// return Number: 2
// return Number: 1
// return undefined
*
New functions, issues, and pull requests welcome
1. Fork it
2. Create your feature branch (git checkout -b feature/my-new-feature)
3. Commit your changes (git commit -am 'Add some feature')
4. Push to the branch (git push origin feature/my-new-feature)
5. Create a new Pull Request