Skip to content

Commit

Permalink
feature(cloudcmd) show size as <link> for links
Browse files Browse the repository at this point in the history
  • Loading branch information
coderaiser committed Oct 11, 2018
1 parent e18ddb2 commit 846f4aa
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 5 deletions.
12 changes: 9 additions & 3 deletions client/dom/dom-tree.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const currify = require('currify/legacy');

const DOM = module.exports;

/**
Expand All @@ -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!');

if (Array.isArray(className))
return className.some(currify(isContainClass, element));

const classList = element.classList;
const ret = classList.contains(className);

return ret;
return classList.contains(className);
};

module.exports.isContainClass = isContainClass;

/**
* Function search element by tag
* @param tag - className
Expand Down
57 changes: 57 additions & 0 deletions client/dom/dom-tree.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});

6 changes: 4 additions & 2 deletions client/dom/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
]);
};

/**
Expand Down
4 changes: 4 additions & 0 deletions common/cloudfunc.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function getAttribute(type) {
return 'target="_blank" ';
}

module.exports._getSize = getSize;
function getSize(file) {
const {
size,
Expand All @@ -239,6 +240,9 @@ function getSize(file) {
if (type === 'directory')
return '&lt;dir&gt;';

if (/link/.test(type))
return '&lt;link&gt;';

return size;
}

Expand Down
63 changes: 63 additions & 0 deletions common/cloudfunc.spec.js
Original file line number Diff line number Diff line change
@@ -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();
});

0 comments on commit 846f4aa

Please sign in to comment.