Comparing version 2.8.3 to 2.9.0
{ | ||
"name": "rjs", | ||
"version": "2.8.3", | ||
"version": "2.9.0", | ||
"description": "A library of Javascript utility functions with an emphasis on Functional Programming.", | ||
@@ -5,0 +5,0 @@ "main": "rjs.js", |
@@ -83,2 +83,7 @@ A library of Javascript utility functions with an emphasis on Functional Programming. | ||
/** Returns a new function that inserts the given curried arguments to the inner function at the specified index of its runtime arguments. | ||
i.e. A normal curry is equivalent to f.curryAt(0, args) and an rcurry is equivalent to f.curryAt(n, args) where n is the arrity of the function. | ||
*/ | ||
curryAt(f, index, curriedArgs) | ||
// Returns a new function that always passes the given curried arguments to the inner function before normal arguments. | ||
@@ -259,4 +264,1 @@ curry(f, curriedArgs) | ||
createNew(f) | ||
// Invokes a callback after all the given asynchronous functions have completed. All asynchronous functions must accept a single callback argument. | ||
callAfterDone(requests, callback) |
114
rjs.js
/** | ||
* Raine's Javascript Extensions | ||
* v2.8.3 | ||
* v2.9.0 | ||
* A library of Javascript utility functions with an emphasis on Functional Programming. | ||
@@ -43,17 +43,26 @@ */ | ||
/** Returns a new function that always passes the given curried arguments to the inner function before normal arguments. */ | ||
var curry = function(f, curriedArgs) { | ||
/** Returns a new function that inserts the given curried arguments to the inner function at the specified index of its runtime arguments. | ||
i.e. A normal curry is equivalent to f.curryAt(0, args) and an rcurry is equivalent to f.curryAt(n, args) where n is the arrity of the function. | ||
*/ | ||
var curryAt = function(f, index, curriedArgs) { | ||
return function() { | ||
var givenArgs = Array.prototype.slice.call(arguments, 0); | ||
return f.apply(this, curriedArgs.concat(givenArgs)); | ||
// handle negative indices | ||
// Note that we add 1 so that -1 points to the slot AFTER the last element, not before the last element (which is what the last index points to). | ||
if(index < 0) { | ||
index = circ(givenArgs, index) + 1; | ||
} | ||
var spliceArgs = [givenArgs, index, 0].concat(curriedArgs); | ||
var newArgs = spliced.apply(this, spliceArgs); | ||
return f.apply(this, newArgs); | ||
}; | ||
}; | ||
/** Returns a new function that always passes the given curried arguments to the inner function before normal arguments. */ | ||
var curry = curryAt(curryAt, 1, 0); | ||
/** Returns a new function that always passes the given curried arguments to the inner function after normal arguments. */ | ||
var rcurry = function(f, curriedArgs) { | ||
return function() { | ||
var givenArgs = Array.prototype.slice.call(arguments, 0); | ||
return f.apply(this, givenArgs.concat(curriedArgs)); | ||
}; | ||
}; | ||
var rcurry = curryAt(curryAt, 1, -1); | ||
@@ -341,4 +350,6 @@ /** Returns a new function that calls the given function with a limit on the number of arguments. */ | ||
/** Indexes into an array, supports negative indices. */ | ||
var index = function(arr, i) { | ||
/** Returns the in-bounds index of the given index for the array, supports negative and out-of-bounds indices. | ||
@private | ||
*/ | ||
var circ = function(arr, i) { | ||
@@ -351,5 +362,10 @@ // return first index if i is null or undefined | ||
// one modulus to get in range, another to eliminate negative | ||
return arr[(i % arr.length + arr.length) % arr.length]; | ||
return (i % arr.length + arr.length) % arr.length; | ||
}; | ||
/** Indexes into an array, supports negative indices. */ | ||
var index = function(arr, i) { | ||
return arr[circ(arr, i)]; | ||
}; | ||
/** Returns a new array containing the elements of the given array shifted n spaces to the left, wrapping around the end. */ | ||
@@ -394,3 +410,3 @@ var rotate = function(arr, n) { | ||
/** Functional, nondestructive version of Array.prototype.splice. */ | ||
var splice = function(arr, index, howMany/*, elements*/) { | ||
var spliced = function(arr, index, howMany/*, elements*/) { | ||
var elements = Array.prototype.slice.apply(arguments, [3]); | ||
@@ -753,65 +769,3 @@ var results = []; | ||
/** Invokes a callback after all the given asynchronous functions have completed. All asynchronous functions must accept a single callback argument. */ | ||
var callAfterDone = function(requests, callback) { | ||
// defaults | ||
requests = requests || []; | ||
callback = callback || function() {}; | ||
// an array of objects which contain a key and a response | ||
var responses = []; | ||
var isArray = typeOf(requests) == 'array'; | ||
var len = (isArray ? requests : keys(requests)).length; | ||
var completed = 0; | ||
// if the requests is empty, call the callback immediately | ||
if(len === 0) { | ||
callback(); | ||
} | ||
// if all requests have returned, calls the final callback with an array of responses in the order of the original requests | ||
var checkAllDone = function() { | ||
if(completed === len) { | ||
if(isArray) { | ||
responses.sort(compareProperty('key')); | ||
callback(pluck(responses, 'result')); | ||
} | ||
else { | ||
callback(toObject(responses, function(responsePair) { | ||
return keyValue(responsePair.key, responsePair.result); | ||
})); | ||
} | ||
} | ||
}; | ||
// stores the given result in the responses object and checks if we're all done | ||
var oneDone = function(key, result) { | ||
completed++; | ||
responses.push({ key: key, result: result }); | ||
checkAllDone(); | ||
}; | ||
// calls the given function for each item in the request queue. Passes the index or key to the callback function, depending if the requests queue is an array or object. | ||
var eachKey = function(f) { | ||
if(isArray) { | ||
for(var i=0; i<len; i++) { | ||
f(i); | ||
} | ||
} | ||
else { | ||
for(var key in requests) { | ||
f(key); | ||
} | ||
} | ||
}; | ||
eachKey(function(key) { | ||
var req = requests[key]; | ||
// RECURSION | ||
(typeOf(req) == 'function' ? req : curry(callAfterDone, [req]))(curry(oneDone, [key])); | ||
}); | ||
}; | ||
/*********************************** | ||
@@ -825,4 +779,4 @@ * Prototype Installation | ||
install(Number, rjs, ['ordinal', { mapNumber: 'map' }]); | ||
install(Array, rjs, ['map', 'each', 'pluck', 'group', 'orderedGroup', 'tally', 'contains', 'strictContains', 'unique', 'reversed', 'index', 'rotate', 'toObject', 'find', 'findByProperty', 'filterBy', 'any', 'all' ]); | ||
install(Function, rjs, ['curry', 'rcurry', 'arritize', 'currify', 'toInstance', 'new']); | ||
install(Array, rjs, ['map', 'each', 'pluck', 'group', 'orderedGroup', 'tally', 'contains', 'strictContains', 'unique', 'reversed', 'index', 'rotate', 'toObject', 'find', 'findByProperty', 'filterBy', 'any', 'all', 'spliced' ]); | ||
install(Function, rjs, ['curryAt', 'curry', 'rcurry', 'arritize', 'currify', 'toInstance', 'new']); | ||
return rjs; | ||
@@ -843,2 +797,3 @@ }; | ||
all : all, | ||
curryAt : curryAt, | ||
curry : curry, | ||
@@ -883,3 +838,3 @@ rcurry : rcurry, | ||
findByProperty : findByProperty, | ||
splice : splice, | ||
spliced : spliced, | ||
range : range, | ||
@@ -912,3 +867,2 @@ filter : filter, | ||
'new' : createNew, | ||
callAfterDone : callAfterDone, | ||
@@ -915,0 +869,0 @@ // prototype installation |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
554723
33
13719
263