From 9234e3f287268f1f3f02cc97fca87e31421cbc95 Mon Sep 17 00:00:00 2001 From: Lars Jung Date: Sat, 25 Jun 2016 00:39:57 +0200 Subject: [PATCH] Clean code. --- src/_h5ai/public/js/lib/core/location.js | 15 +++-------- src/_h5ai/public/js/lib/core/resource.js | 4 +-- src/_h5ai/public/js/lib/core/store.js | 4 +-- src/_h5ai/public/js/lib/core/util.js | 10 +------ src/_h5ai/public/js/lib/lo.js | 7 ++--- src/_h5ai/public/js/lib/model/item.js | 33 ++++++++++-------------- 6 files changed, 22 insertions(+), 51 deletions(-) diff --git a/src/_h5ai/public/js/lib/core/location.js b/src/_h5ai/public/js/lib/core/location.js index 4b8122e1..6ee48ac9 100644 --- a/src/_h5ai/public/js/lib/core/location.js +++ b/src/_h5ai/public/js/lib/core/location.js @@ -71,18 +71,9 @@ const encodedHref = href => { return forceEncoding(location); }; -const getDomain = () => { - return doc.domain; -}; - -const getAbsHref = () => { - return absHref; -}; - -const getItem = () => { - const Item = require('../model/item'); - return Item.get(absHref); -}; +const getDomain = () => doc.domain; +const getAbsHref = () => absHref; +const getItem = () => require('../model/item').get(absHref); const load = callback => { request({action: 'get', items: {href: absHref, what: 1}}).then(json => { diff --git a/src/_h5ai/public/js/lib/core/resource.js b/src/_h5ai/public/js/lib/core/resource.js index 0ae30374..27806144 100644 --- a/src/_h5ai/public/js/lib/core/resource.js +++ b/src/_h5ai/public/js/lib/core/resource.js @@ -9,9 +9,7 @@ const defaultThemeHref = themesHref + 'default/'; const defaultIcons = ['file', 'folder', 'folder-page', 'folder-parent', 'ar', 'aud', 'bin', 'img', 'txt', 'vid', 'x']; -const image = id => { - return uiHref + id + '.svg'; -}; +const image = id => uiHref + id + '.svg'; const icon = id => { const baseId = (id || '').split('-')[0]; diff --git a/src/_h5ai/public/js/lib/core/store.js b/src/_h5ai/public/js/lib/core/store.js index 59e414af..6cfbf52b 100644 --- a/src/_h5ai/public/js/lib/core/store.js +++ b/src/_h5ai/public/js/lib/core/store.js @@ -21,9 +21,7 @@ const put = (key, value) => { save(obj); }; -const get = key => { - return load()[key]; -}; +const get = key => load()[key]; module.exports = { diff --git a/src/_h5ai/public/js/lib/core/util.js b/src/_h5ai/public/js/lib/core/util.js index 56ef64c2..60c2f7c8 100644 --- a/src/_h5ai/public/js/lib/core/util.js +++ b/src/_h5ai/public/js/lib/core/util.js @@ -1,12 +1,4 @@ -const regularCmpFn = (val1, val2) => { - if (val1 < val2) { - return -1; - } - if (val1 > val2) { - return 1; - } - return 0; -}; +const regularCmpFn = (x, y) => x < y ? -1 : x > y ? 1 : 0; // Natural Sort algorithm for Javascript - Version 0.7 - Released under MIT license // Author: Jim Palmer (based on chunking idea from Dave Koelle) diff --git a/src/_h5ai/public/js/lib/lo.js b/src/_h5ai/public/js/lib/lo.js index efd1bf23..090ca3de 100644 --- a/src/_h5ai/public/js/lib/lo.js +++ b/src/_h5ai/public/js/lib/lo.js @@ -25,13 +25,10 @@ const intersection = (obj1, obj2) => { obj2 = values(obj2); return filter(obj1, x => obj2.indexOf(x) >= 0); }; +const cmp = (x, y) => x < y ? -1 : x > y ? 1 : 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; - }; + const cmpFn = (x, y) => cmp(selFn(x), selFn(y)); return values(obj).sort(cmpFn); }; const debounce = (fn, delay) => { diff --git a/src/_h5ai/public/js/lib/model/item.js b/src/_h5ai/public/js/lib/model/item.js index f6174377..544ac693 100644 --- a/src/_h5ai/public/js/lib/model/item.js +++ b/src/_h5ai/public/js/lib/model/item.js @@ -4,19 +4,12 @@ const location = require('../core/location'); const settings = require('../core/settings'); const types = require('../core/types'); - const reEndsWithSlash = /\/$/; const reSplitPath = /^(.*\/)([^\/]+\/?)$/; const cache = {}; -const startsWith = (sequence, part) => { - if (!sequence || !sequence.indexOf) { - return false; - } - - return sequence.indexOf(part) === 0; -}; +const startsWith = (sequence, part) => isStr(sequence) && sequence.startsWith(part); const createLabel = sequence => { sequence = sequence.replace(reEndsWithSlash, ''); @@ -26,7 +19,7 @@ const createLabel = sequence => { return sequence; }; -const splitPath = sequence => { // eslint-disable-line consistent-return +const splitPath = sequence => { if (sequence === '/') { return { parent: null, @@ -35,17 +28,19 @@ const splitPath = sequence => { // eslint-disable-line consistent-return } const match = reSplitPath.exec(sequence); - if (match) { - const split = { - parent: match[1], - name: match[2] - }; - - if (split.parent && !startsWith(split.parent, settings.rootHref)) { - split.parent = null; - } - return split; + if (!match) { + return null; } + + const split = { + parent: match[1], + name: match[2] + }; + + if (split.parent && !startsWith(split.parent, settings.rootHref)) { + split.parent = null; + } + return split; }; const getItem = options => {