mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-01-23 10:45:47 +00:00
59 lines
1.3 KiB
JavaScript
59 lines
1.3 KiB
JavaScript
/* global DOM */
|
|
|
|
'use strict';
|
|
|
|
const Info = DOM.CurrentInfo;
|
|
const {escapeRegExp} = require('../../common/util');
|
|
|
|
module.exports = function setCurrentByChar(char, charStore) {
|
|
let firstByName;
|
|
let skipCount = 0;
|
|
let setted = false;
|
|
let i = 0;
|
|
|
|
const escapeChar = escapeRegExp(char);
|
|
const regExp = new RegExp('^' + escapeChar + '.*$', 'i');
|
|
const {files} = Info;
|
|
const chars = charStore();
|
|
const n = chars.length;
|
|
|
|
while(i < n && char === chars[i])
|
|
i++;
|
|
|
|
if (!i)
|
|
charStore([]);
|
|
|
|
const skipN = skipCount = i;
|
|
|
|
charStore(charStore().concat(char));
|
|
|
|
const names = DOM.getFilenames(files);
|
|
const isTest = (a) => regExp.test(a);
|
|
const isRoot = (a) => a === '..';
|
|
const not = (f) => (a) => !f(a);
|
|
const setCurrent = (name) => {
|
|
const byName = DOM.getCurrentByName(name);
|
|
|
|
if (!skipCount) {
|
|
setted = true;
|
|
DOM.setCurrentFile(byName);
|
|
return true;
|
|
} else {
|
|
if (skipN === skipCount)
|
|
firstByName = byName;
|
|
|
|
--skipCount;
|
|
}
|
|
};
|
|
|
|
names
|
|
.filter(isTest)
|
|
.filter(not(isRoot))
|
|
.some(setCurrent);
|
|
|
|
if (!setted) {
|
|
DOM.setCurrentFile(firstByName);
|
|
charStore([char]);
|
|
}
|
|
};
|
|
|