feature(listeners) add get-index, get-range

This commit is contained in:
coderaiser 2017-05-15 15:43:05 +03:00
parent 7ed91a6084
commit 999c79559f
5 changed files with 83 additions and 21 deletions

View file

@ -0,0 +1,11 @@
'use strict';
module.exports = (array, item) => {
const index = array.indexOf(item);
if (!~index)
return 0;
return index;
};

View file

@ -0,0 +1,12 @@
'use strict';
module.exports = (indexFrom, indexTo, files) => {
if (indexFrom < indexTo)
return files.slice(indexFrom, indexTo + 1);
if (indexFrom > indexTo)
return files.slice(indexTo, indexFrom + 1);
return [files[indexFrom]];
};

View file

@ -5,13 +5,16 @@
const exec = require('execon');
const itype = require('itype/legacy');
const currify = require('currify/legacy');
const getRange = require('./get-range');
const uploadFiles = require('./dom/upload-files');
const getIndex = currify(require('./get-index'));
const uploadFiles = require('../dom/upload-files');
const {
FS,
apiURL
} = require('../common/cloudfunc');
} = require('../../common/cloudfunc');
module.exports.init = () => {
contextMenu();
@ -25,15 +28,6 @@ module.exports.init = () => {
CloudCmd.Listeners = module.exports;
const getIndex = currify((array, item) =>{
const index = array.indexOf(item);
if (!~index)
return 0;
return index;
});
const unselect = (event) => {
const isMac = /Mac/.test(window.navigator.platform);
const {
@ -380,16 +374,6 @@ function getFilesRange(from, to) {
return getRange(indexFrom, indexTo, files);
}
function getRange(indexFrom, indexTo, files) {
if (indexFrom < indexTo)
return files.slice(indexFrom, indexTo + 1);
if (indexFrom > indexTo)
return files.slice(indexTo, indexFrom + 1);
return files[indexFrom];
}
function contextMenu() {
const fm = DOM.getFM();

View file

@ -0,0 +1,21 @@
'use strict';
const test = require('tape');
const dir = '../../../client/listeners';
const getIndex = require(`${dir}/get-index`);
test('cloudcmd: client: listeners: getIndex: not found', (t) => {
const array = ['hello'];
t.equal(getIndex(array, 'world'), 0, 'should return index');
t.end();
});
test('cloudcmd: client: listeners: getIndex: found', (t) => {
const array = ['hello', 'world'];
t.equal(getIndex(array, 'world'), 1, 'should return index');
t.end();
});

View file

@ -0,0 +1,34 @@
'use strict';
const test = require('tape');
const dir = '../../../client/listeners';
const getRange = require(`${dir}/get-range`);
test('cloudcmd: client: listeners: getRange: direct', (t) => {
const expected = ['hello', 'world'];
const files = [...expected, 'how', 'come'];
const result = getRange(0, 1, files);
t.deepEqual(expected, result, 'should return range');
t.end();
});
test('cloudcmd: client: listeners: getRange: reverse', (t) => {
const expected = ['hello', 'world'];
const files = [...expected, 'how', 'come'];
const result = getRange(1, 0, files);
t.deepEqual(expected, result, 'should return range');
t.end();
});
test('cloudcmd: client: listeners: getRange: one', (t) => {
const expected = ['hello'];
const files = [...expected, 'how', 'come'];
const result = getRange(0, 0, files);
t.deepEqual(expected, result, 'should return range');
t.end();
});