Remove some more lodash refs.

This commit is contained in:
Lars Jung 2016-06-24 16:59:31 +02:00
parent 26c4d569aa
commit d8d7b997b5
10 changed files with 89 additions and 49 deletions

View file

@ -1,5 +1,4 @@
const isStr = x => typeof x === 'string';
const isFn = x => typeof x === 'function';
const {isStr, isFn} = require('../lo');
const subscriptions = {};

View file

@ -1,4 +1,4 @@
const isNum = x => typeof x === 'number';
const {isNum} = require('../lo');
const decimalMetric = {
t: 1000.0,

View file

@ -1,13 +1,10 @@
const {each, values, isFn, difference} = require('../lo');
const {win} = require('../globals');
const {request} = require('../server');
const allsettings = require('./settings');
const event = require('./event');
const notification = require('../view/notification');
const each = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key));
const values = obj => Object.keys(obj).map(key => obj[key]);
const isFn = x => typeof x === 'function';
const difference = (arr1, arr2) => arr1.filter(x => arr2.indexOf(x) < 0);
const doc = win.document;
const settings = Object.assign({

View file

@ -1,8 +1,7 @@
const {includes} = require('../lo');
const config = require('../config');
const settings = require('./settings');
const includes = (arr, x) => arr.indexOf(x) >= 0;
const imagesHref = settings.publicHref + 'images/';
const uiHref = imagesHref + 'ui/';
const themesHref = imagesHref + 'themes/';

View file

@ -1,8 +1,6 @@
const {each, map} = require('../lo');
const config = require('../config');
const each = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key));
const map = (arr, fn) => Array.from(arr, fn);
const reEndsWithSlash = /\/$/;
const regexps = {};

View file

@ -0,0 +1,50 @@
const tof = (x, str) => typeof x === str;
const isStr = x => tof(x, 'string');
const isFn = x => tof(x, 'function');
const isNum = x => tof(x, 'number');
const keys = obj => {
if (!obj || isStr(obj)) {
return [];
}
if (obj.hasOwnProperty('length')) {
obj = Array.from(obj);
}
return Object.keys(obj);
};
const values = obj => keys(obj).map(key => obj[key]);
const each = (obj, fn) => keys(obj).forEach(key => fn(obj[key], key));
const filter = (obj, fn) => values(obj).filter(fn);
const map = (obj, fn) => values(obj).map(fn);
const includes = (obj, x) => values(obj).indexOf(x) >= 0;
const difference = (obj1, obj2) => {
obj2 = values(obj2);
return filter(obj1, x => obj2.indexOf(x) < 0);
};
const intersection = (obj1, obj2) => {
obj2 = values(obj2);
return filter(obj1, x => obj2.indexOf(x) >= 0);
};
const sortBy = (obj, sel) => {
const selFn = isFn(sel) ? sel : x => x[sel];
const cmpFn = (x, y) => {
x = selFn(x);
y = selFn(y);
return x < y ? -1 : x > y ? 1 : 0;
};
return values(obj).sort(cmpFn);
};
module.exports = {
isStr,
isFn,
isNum,
keys,
values,
each,
filter,
map,
includes,
difference,
intersection,
sortBy
};

View file

@ -1,4 +1,4 @@
const {lo} = require('../globals');
const {keys, each, filter, sortBy, isFn, isStr, isNum} = require('../lo');
const server = require('../server');
const location = require('../core/location');
const settings = require('../core/settings');
@ -49,9 +49,9 @@ function splitPath(sequence) { // eslint-disable-line consistent-return
}
function getItem(options) {
if (lo.isString(options)) {
if (isStr(options)) {
options = {href: options};
} else if (!options || !lo.isString(options.href)) {
} else if (!options || !isStr(options.href)) {
return null;
}
@ -63,10 +63,10 @@ function getItem(options) {
const item = cache[href] || new Item(href); // eslint-disable-line no-use-before-define
if (lo.isNumber(options.time)) {
if (isNum(options.time)) {
item.time = options.time;
}
if (lo.isNumber(options.size)) {
if (isNum(options.size)) {
item.size = options.size;
}
if (options.managed) {
@ -89,7 +89,7 @@ function removeItem(absHref) {
if (item.parent) {
delete item.parent.content[item.absHref];
}
lo.each(item.content, child => {
each(item.content, child => {
removeItem(child.absHref);
});
}
@ -98,7 +98,7 @@ function removeItem(absHref) {
function fetchContent(absHref, callback) {
const item = getItem(absHref);
if (!lo.isFunction(callback)) {
if (!isFn(callback)) {
callback = () => undefined;
}
@ -107,7 +107,7 @@ function fetchContent(absHref, callback) {
} else {
server.request({action: 'get', items: {href: item.absHref, what: 1}}).then(response => {
if (response.items) {
lo.each(response.items, jsonItem => {
each(response.items, jsonItem => {
getItem(jsonItem);
});
}
@ -135,13 +135,13 @@ function Item(absHref) {
if (split.parent) {
this.parent = getItem(split.parent);
this.parent.content[this.absHref] = this;
if (lo.keys(this.parent.content).length > 1) {
if (keys(this.parent.content).length > 1) {
this.parent.isContentFetched = true;
}
}
}
lo.extend(Item.prototype, {
Object.assign(Item.prototype, {
isFolder() {
return reEndsWithSlash.test(this.absHref);
},
@ -168,7 +168,7 @@ lo.extend(Item.prototype, {
},
isEmpty() {
return lo.keys(this.content).length === 0;
return keys(this.content).length === 0;
},
fetchContent(callback) {
@ -188,7 +188,7 @@ lo.extend(Item.prototype, {
},
getSubfolders() {
return lo.sortBy(lo.filter(this.content, item => {
return sortBy(filter(this.content, item => {
return item.isFolder();
}), item => {
return item.label.toLowerCase();
@ -199,7 +199,7 @@ lo.extend(Item.prototype, {
let folders = 0;
let files = 0;
lo.each(this.content, item => {
each(this.content, item => {
if (item.isFolder()) {
folders += 1;
} else {

View file

@ -1,6 +1,5 @@
const {jq} = require('./globals');
const each = (obj, fn) => Object.keys(obj).forEach(key => fn(obj[key], key));
const {each} = require('./lo');
const request = data => {
return new Promise(resolve => {

View file

@ -1,4 +1,5 @@
const {jq, lo} = require('../globals');
const {each, map, includes, intersection} = require('../lo');
const {jq} = require('../globals');
const event = require('../core/event');
const format = require('../core/format');
const location = require('../core/location');
@ -9,7 +10,7 @@ const base = require('./base');
const modes = ['details', 'grid', 'icons'];
const sizes = [20, 40, 60, 80, 100, 150, 200, 250, 300, 350, 400];
const settings = lo.extend({
const settings = Object.assign({
binaryPrefix: false,
hideFolders: false,
hideParentFolder: false,
@ -18,7 +19,7 @@ const settings = lo.extend({
sizes
}, allsettings.view);
const sortedSizes = settings.sizes.sort((a, b) => a - b);
const checkedModes = lo.intersection(settings.modes, modes);
const checkedModes = intersection(settings.modes, modes);
const storekey = 'view';
const tplView =
`<div id="view">
@ -78,7 +79,7 @@ function createStyles(size) {
}
function addCssStyles() {
const styles = lo.map(sortedSizes, size => createStyles(size));
const styles = map(sortedSizes, size => createStyles(size));
styles.push(`#view .icon img {max-width: ${settings.maxIconSize}px; max-height: ${settings.maxIconSize}px;}`);
jq('<style/>').text(styles.join('\n')).appendTo('head');
}
@ -88,11 +89,11 @@ function set(mode, size) {
mode = mode || stored && stored.mode;
size = size || stored && stored.size;
mode = lo.includes(settings.modes, mode) ? mode : settings.modes[0];
size = lo.includes(settings.sizes, size) ? size : settings.sizes[0];
mode = includes(settings.modes, mode) ? mode : settings.modes[0];
size = includes(settings.sizes, size) ? size : settings.sizes[0];
store.put(storekey, {mode, size});
lo.each(checkedModes, m => {
each(checkedModes, m => {
if (m === mode) {
$view.addClass('view-' + m);
} else {
@ -100,7 +101,7 @@ function set(mode, size) {
}
});
lo.each(sortedSizes, s => {
each(sortedSizes, s => {
if (s === size) {
$view.addClass('view-size-' + s);
} else {
@ -194,15 +195,11 @@ function checkHint() {
}
function setItems(items) {
const removed = lo.map($items.find('.item'), item => {
return jq(item).data('item');
});
const removed = map($items.find('.item'), el => jq(el).data('item'));
$items.find('.item').remove();
lo.each(items, e => {
$items.append(createHtml(e));
});
each(items, item => $items.append(createHtml(item)));
base.$content.scrollLeft(0).scrollTop(0);
checkHint();
@ -210,11 +207,11 @@ function setItems(items) {
}
function changeItems(add, remove) {
lo.each(add, item => {
each(add, item => {
createHtml(item).hide().appendTo($items).fadeIn(400);
});
lo.each(remove, item => {
each(remove, item => {
item.$view.fadeOut(400, () => {
item.$view.remove();
});
@ -240,7 +237,7 @@ function onLocationChanged(item) {
items.push(item.parent);
}
lo.each(item.content, child => {
each(item.content, child => {
if (!(child.isFolder() && settings.hideFolders)) {
items.push(child);
}
@ -253,7 +250,7 @@ function onLocationChanged(item) {
function onLocationRefreshed(item, added, removed) {
const add = [];
lo.each(added, child => {
each(added, child => {
if (!(child.isFolder() && settings.hideFolders)) {
add.push(child);
}

View file

@ -1,4 +1,5 @@
const {jq, lo} = require('../globals');
const {each} = require('../lo');
const {jq} = require('../globals');
const event = require('../core/event');
const resource = require('../core/resource');
const allsettings = require('../core/settings');
@ -7,7 +8,7 @@ const base = require('./base');
const view = require('./view');
const settings = lo.extend({
const settings = Object.assign({
modeToggle: false
}, allsettings.view);
const tplSettings =
@ -29,7 +30,7 @@ let sizes;
function onChanged(mode, size) {
jq('#viewmode-settings .mode').removeClass('active');
jq('#viewmode-' + mode).addClass('active');
jq('#viewmode-size').val(lo.indexOf(sizes, size));
jq('#viewmode-size').val(sizes.indexOf(size));
if (settings.modeToggle === 'next') {
mode = modes[(modes.indexOf(mode) + 1) % modes.length];
@ -45,7 +46,7 @@ function addSettings() {
const $viewBlock = jq(tplSettings);
if (modes.length > 1) {
lo.each(modes, mode => {
each(modes, mode => {
jq(tplMode.replace(/\[MODE\]/g, mode))
.on('click', () => {
view.setMode(mode);