AbsolutePaths: written utility function popIfEndsWith()
It will be necessary in the next commit to evaluate the Etherpad base install path.
This commit is contained in:
parent
dbf7eff1fc
commit
cbce3c1b08
1 changed files with 28 additions and 0 deletions
|
@ -19,5 +19,33 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var log4js = require('log4js');
|
var log4js = require('log4js');
|
||||||
|
var _ = require('underscore');
|
||||||
|
|
||||||
var absPathLogger = log4js.getLogger('AbsolutePaths');
|
var absPathLogger = log4js.getLogger('AbsolutePaths');
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If stringArray's last elements are exactly equal to lastDesiredElements,
|
||||||
|
* returns a copy in which those last elements are popped, or false otherwise.
|
||||||
|
*
|
||||||
|
* @param {string[]} stringArray - The input array.
|
||||||
|
* @param {string[]} lastDesiredElements - The elements to remove from the end
|
||||||
|
* of the input array.
|
||||||
|
* @return {string[]|boolean} The shortened array, or false if there was no
|
||||||
|
* overlap.
|
||||||
|
*/
|
||||||
|
var popIfEndsWith = function(stringArray, lastDesiredElements) {
|
||||||
|
if (stringArray.length <= lastDesiredElements.length) {
|
||||||
|
absPathLogger.debug(`In order to pop "${lastDesiredElements.join(path.sep)}" from "${stringArray.join(path.sep)}", it should contain at least ${lastDesiredElements.length + 1 } elements`);
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
const lastElementsFound = _.last(stringArray, lastDesiredElements.length);
|
||||||
|
|
||||||
|
if (_.isEqual(lastElementsFound, lastDesiredElements)) {
|
||||||
|
return _.initial(stringArray, lastDesiredElements.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
absPathLogger.debug(`${stringArray.join(path.sep)} does not end with "${lastDesiredElements.join(path.sep)}"`);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
Loading…
Reference in a new issue