diff --git a/src/_h5ai/public/js/lib/core/event.js b/src/_h5ai/public/js/lib/core/event.js index 16f16e1f..7b7b0c3f 100644 --- a/src/_h5ai/public/js/lib/core/event.js +++ b/src/_h5ai/public/js/lib/core/event.js @@ -1,5 +1,4 @@ -const isStr = x => typeof x === 'string'; -const isFn = x => typeof x === 'function'; +const {isStr, isFn} = require('../lo'); const subscriptions = {}; diff --git a/src/_h5ai/public/js/lib/core/format.js b/src/_h5ai/public/js/lib/core/format.js index 85901f3a..74747322 100644 --- a/src/_h5ai/public/js/lib/core/format.js +++ b/src/_h5ai/public/js/lib/core/format.js @@ -1,4 +1,4 @@ -const isNum = x => typeof x === 'number'; +const {isNum} = require('../lo'); const decimalMetric = { t: 1000.0, diff --git a/src/_h5ai/public/js/lib/core/location.js b/src/_h5ai/public/js/lib/core/location.js index f7813cf1..4b8122e1 100644 --- a/src/_h5ai/public/js/lib/core/location.js +++ b/src/_h5ai/public/js/lib/core/location.js @@ -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({ diff --git a/src/_h5ai/public/js/lib/core/resource.js b/src/_h5ai/public/js/lib/core/resource.js index 85de53f3..64db4005 100644 --- a/src/_h5ai/public/js/lib/core/resource.js +++ b/src/_h5ai/public/js/lib/core/resource.js @@ -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/'; diff --git a/src/_h5ai/public/js/lib/core/types.js b/src/_h5ai/public/js/lib/core/types.js index 256324ee..c9798200 100644 --- a/src/_h5ai/public/js/lib/core/types.js +++ b/src/_h5ai/public/js/lib/core/types.js @@ -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 = {}; diff --git a/src/_h5ai/public/js/lib/lo.js b/src/_h5ai/public/js/lib/lo.js new file mode 100644 index 00000000..7d396723 --- /dev/null +++ b/src/_h5ai/public/js/lib/lo.js @@ -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 +}; diff --git a/src/_h5ai/public/js/lib/model/item.js b/src/_h5ai/public/js/lib/model/item.js index dfa4ee21..9468eaf4 100644 --- a/src/_h5ai/public/js/lib/model/item.js +++ b/src/_h5ai/public/js/lib/model/item.js @@ -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 { diff --git a/src/_h5ai/public/js/lib/server.js b/src/_h5ai/public/js/lib/server.js index a65c9e4e..13148c65 100644 --- a/src/_h5ai/public/js/lib/server.js +++ b/src/_h5ai/public/js/lib/server.js @@ -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 => { diff --git a/src/_h5ai/public/js/lib/view/view.js b/src/_h5ai/public/js/lib/view/view.js index 075c7633..a1688001 100644 --- a/src/_h5ai/public/js/lib/view/view.js +++ b/src/_h5ai/public/js/lib/view/view.js @@ -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 = `
@@ -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('