extra-array
Advanced tools
Comparing version 2.0.13 to 2.0.14
62
fresh.js
@@ -23,2 +23,11 @@ // PURE FUNCTIONS | ||
/** | ||
* Gets elements before last element. | ||
* @param {Array} x array | ||
* @returns {Array} elements before last | ||
*/ | ||
function init(x) { | ||
return x.slice(0, -1); | ||
} | ||
/** | ||
* Gets elements after head. | ||
@@ -33,11 +42,26 @@ * @param {Array} x array | ||
/** | ||
* Gets elements before last element. | ||
* Gets all initial segments. | ||
* @param {Array} x array | ||
* @returns {Array} elements before last | ||
* @returns {Array} [initial segment ...] | ||
*/ | ||
function init(x) { | ||
return x.slice(0, -1); | ||
function inits(x) { | ||
var a = []; | ||
for(var i=0, I=x.length; i<I; i++) | ||
a.push(x.slice(0, i)); | ||
return a; | ||
} | ||
/** | ||
* Gets all final segments. | ||
* @param {Array} x array | ||
* @returns {Array} [final segment ...] | ||
*/ | ||
function tails(x) { | ||
var a = []; | ||
for(var i=0, I=x.length; i<I; i++) | ||
a.push(x.slice(i)); | ||
return a; | ||
} | ||
/** | ||
* Gets largest element, as per compare function. | ||
@@ -98,11 +122,24 @@ * @param {Array} x array | ||
function takeWhile(x, fn, ths=null) { | ||
var a = [], i = -1; | ||
for(var e of x) { | ||
if(fn.call(ths, e, ++i, x)) a.push(e); | ||
else break; | ||
} | ||
return a; | ||
return x.slice(0, findFailIndex(x, fn, ths)); | ||
} | ||
/** | ||
* Get suffix remaining after takeWhile(). | ||
* @param {Array} x array | ||
* @param {function} fn filter function (elem, index, array) | ||
* @param {object?} ths this argument | ||
* @returns {Array} suffix | ||
*/ | ||
function dropWhile(x, fn, ths=null) { | ||
return x.slice(findFailIndex(x, fn, ths)); | ||
} | ||
function findFailIndex(x, fn, ths=null) { | ||
var i = -1; | ||
for(var e of x) | ||
if(!fn.call(ths, e, ++i, x)) break; | ||
return i; | ||
} | ||
/** | ||
* Splits array to elements which do, and dont satify the filter. | ||
@@ -165,4 +202,6 @@ * @param {Array} x array | ||
exports.last = last; | ||
exports.init = init; | ||
exports.tail = tail; | ||
exports.init = init; | ||
exports.inits = inits; | ||
exports.tails = tails; | ||
exports.max = max; | ||
@@ -173,4 +212,5 @@ exports.min = min; | ||
exports.takeWhile = takeWhile; | ||
exports.dropWhile = dropWhile; | ||
exports.partition = partition; | ||
exports.zip = zip; | ||
exports.append = append; |
{ | ||
"name": "extra-array", | ||
"version": "2.0.13", | ||
"version": "2.0.14", | ||
"description": "Standard utility methods for Array.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,2 +5,3 @@ cons | ||
intercalate | ||
dropWhileEnd | ||
@@ -7,0 +8,0 @@ remove |
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
23608
625
103
0