refactor(cloudfunc) buildFromJSON: getDotDot

This commit is contained in:
coderaiser 2017-08-02 12:35:49 +03:00
parent 7cb681f293
commit 9dd993fe3f
2 changed files with 30 additions and 6 deletions

View file

@ -23,6 +23,7 @@ module.exports.MAX_FILE_SIZE = 500 * 1024;
module.exports.Entity = Entity;
module.exports.getHeaderField = getHeaderField;
module.exports.getPathLink = getPathLink;
module.exports.getDotDot = getDotDot;
module.exports.formatMsg = (msg, name, status) => {
status = status || 'ok';
@ -152,12 +153,9 @@ module.exports.buildFromJSON = (params) => {
fileTable += header + '<ul data-name="js-files" class="files">';
/* Если мы не в корне */
if (path !== '/') {
/* убираем последний слеш и каталог в котором мы сейчас находимся*/
const lastSlash = path.substr(path, path.lastIndexOf('/'));
const dotDot = lastSlash.substr(lastSlash, lastSlash.lastIndexOf('/'));
const link = prefix + FS + (dotDot || '/');
if (path !== '/') {
const dotDot = getDotDot(path);
const link = prefix + FS + dotDot;
const linkResult = rendy(template.link, {
link,
@ -253,3 +251,14 @@ function _getHeaderField(sort, order, name) {
return `${name}${arrow}`;
}
function getDotDot(path) {
// убираем последний слеш и каталог в котором мы сейчас находимся
const lastSlash = path.substr(path, path.lastIndexOf('/'));
const dotDot = lastSlash.substr(lastSlash, lastSlash.lastIndexOf('/'));
if (!dotDot)
return '/';
return dotDot;
}

View file

@ -213,3 +213,18 @@ test('cloudfunc: getPathLink: no template', (t) => {
t.end();
});
test('cloudfunc: getDotDot', (t) => {
const dotDot = CloudFunc.getDotDot('/home');
t.equal(dotDot, '/', 'should return root');
t.end();
});
test('cloudfunc: getDotDot: two levels deep', (t) => {
const dotDot = CloudFunc.getDotDot('/home/coderaiser/');
console.log(dotDot);
t.equal(dotDot, '/home', 'should return up level');
t.end();
});