feature(cloudcmd) show size as <link> for links

This commit is contained in:
coderaiser 2018-10-11 13:53:22 +03:00
parent e18ddb2e18
commit 846f4aa78f
5 changed files with 138 additions and 6 deletions

View file

@ -1,5 +1,7 @@
'use strict';
const currify = require('currify/legacy');
const DOM = module.exports;
/**
@ -8,19 +10,23 @@ const DOM = module.exports;
* @param element
* @param className
*/
module.exports.isContainClass = (element, className) => {
const isContainClass = (element, className) => {
if (!element)
throw Error('element could not be empty!');
if (!className)
throw Error('className could not be empty!');
const classList = element.classList;
const ret = classList.contains(className);
if (Array.isArray(className))
return className.some(currify(isContainClass, element));
return ret;
const classList = element.classList;
return classList.contains(className);
};
module.exports.isContainClass = isContainClass;
/**
* Function search element by tag
* @param tag - className

View file

@ -0,0 +1,57 @@
'use strict';
const test = require('tape');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const tryCatch = require('try-catch');
const {
isContainClass,
} = require('./dom-tree');
test('dom: isContainClass: no element', (t) => {
const [e] = tryCatch(isContainClass);
t.equal(e.message, 'element could not be empty!', 'should throw when no element');
t.end();
});
test('dom: isContainClass: no className', (t) => {
const [e] = tryCatch(isContainClass, {});
t.equal(e.message, 'className could not be empty!', 'should throw when no element');
t.end();
});
test('dom: isContainClass: contains', (t) => {
const contains = sinon.stub();
const el = {
classList: {
contains,
}
};
const className = 'hello';
isContainClass(el, className);
t.ok(contains.calledWith(className), 'should call contains');
t.end();
});
test('dom: isContainClass: contains: array', (t) => {
const contains = sinon.stub();
const el = {
classList: {
contains,
}
};
const className = 'hello';
isContainClass(el, [
'world',
className,
'hello',
]);
t.ok(contains.calledWith(className), 'should call contains');
t.end();
});

View file

@ -497,9 +497,11 @@ function CmdProto() {
this.isCurrentIsDir = (currentFile) => {
const current = currentFile || DOM.getCurrentFile();
const fileType = DOM.getByDataName('js-type', current);
const ret = DOM.isContainClass(fileType, 'directory');
return ret;
return DOM.isContainClass(fileType, [
'directory',
'directory-link',
]);
};
/**

View file

@ -230,6 +230,7 @@ function getAttribute(type) {
return 'target="_blank" ';
}
module.exports._getSize = getSize;
function getSize(file) {
const {
size,
@ -239,6 +240,9 @@ function getSize(file) {
if (type === 'directory')
return '&lt;dir&gt;';
if (/link/.test(type))
return '&lt;link&gt;';
return size;
}

63
common/cloudfunc.spec.js Normal file
View file

@ -0,0 +1,63 @@
'use strict';
const test = require('tape');
const cloudfunc = require('./cloudfunc');
const {
_getSize,
} = cloudfunc;
test('cloudfunc: getSize: dir', (t) => {
const type = 'directory';
const size = 0;
const result = _getSize({
type,
size,
});
const expected = '&lt;dir&gt;';
t.equal(result, expected, 'should equal');
t.end();
});
test('cloudfunc: getSize: link: dir', (t) => {
const type = 'directory-link';
const size = 0;
const result = _getSize({
type,
size,
});
const expected = '&lt;link&gt;';
t.equal(result, expected, 'should equal');
t.end();
});
test('cloudfunc: getSize: link: file', (t) => {
const type = 'file-link';
const size = 0;
const result = _getSize({
type,
size,
});
const expected = '&lt;link&gt;';
t.equal(result, expected, 'should equal');
t.end();
});
test('cloudfunc: getSize: file', (t) => {
const type = 'file';
const size = '100.00kb';
const result = _getSize({
type,
size,
});
const expected = '100.00kb';
t.equal(result, expected, 'should equal');
t.end();
});