From 463c3ced541506521ff6204c06f3f13e388f4bb1 Mon Sep 17 00:00:00 2001 From: Kirill-Hatalski Date: Tue, 15 Oct 2024 18:12:52 +0300 Subject: [PATCH] chore: [wip] additional es6 code refactoring --- src/core/asyncProcess.js | 8 ++++---- src/core/connectivity.js | 12 ++++++------ src/core/databinder.js | 18 +++++++++--------- src/core/polling.js | 4 ++-- src/core/timer.js | 18 +++++++++--------- src/core/tokenHandler.js | 3 ++- src/util/capitalize.js | 2 +- src/util/httpErrorParser.js | 8 ++++---- src/util/namespace.js | 8 ++++---- src/util/position.js | 4 ++-- src/util/typeCaster.js | 2 +- 11 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/core/asyncProcess.js b/src/core/asyncProcess.js index 42a25fe..663cf85 100644 --- a/src/core/asyncProcess.js +++ b/src/core/asyncProcess.js @@ -41,7 +41,7 @@ function asyncProcessFactory() { * Tells if a process is running * @returns {Boolean} */ - isRunning: function isRunning() { + isRunning() { return running; }, @@ -50,7 +50,7 @@ function asyncProcessFactory() { * @param {Function} [cb] - The process to start * @returns {boolean} - Returns true if the process can be started */ - start: function start(cb) { + start(cb) { let started = false; if (!running) { steps = []; @@ -74,7 +74,7 @@ function asyncProcessFactory() { * @param {Promise} step * @returns {asyncProcess} */ - addStep: function addStep(step) { + addStep(step) { steps.push(step); /** @@ -92,7 +92,7 @@ function asyncProcessFactory() { * @param {Function} [cb] - A nodeback like function which will be called when all the deferred steps have finished or an error occurs * @returns {Promise} - Returns the finish promise */ - done: function done(cb) { + done(cb) { const self = this; const finish = Promise.all(steps); diff --git a/src/core/connectivity.js b/src/core/connectivity.js index d9de65a..98ec317 100644 --- a/src/core/connectivity.js +++ b/src/core/connectivity.js @@ -27,20 +27,20 @@ import eventifier from './eventifier.js'; /** * @type {Boolean} the current status, true means online */ -var status = navigator.onLine; +let status = navigator.onLine; /** * The connectivity module * @typedef {connectivity} */ -var connectivity = eventifier({ +const connectivity = eventifier({ /** * Set manually as online * @returns {connectivity} chains * @fires {connectivity#online} * @fires {connectivity#change} */ - setOnline: function setOnline() { + setOnline() { if (this.isOffline()) { status = true; @@ -55,7 +55,7 @@ var connectivity = eventifier({ * @fires {connectivity#offline} * @fires {connectivity#change} */ - setOffline: function setOffline() { + setOffline() { if (this.isOnline()) { status = false; @@ -68,7 +68,7 @@ var connectivity = eventifier({ * Are we online ? * @returns {Boolean} */ - isOnline: function isOnline() { + isOnline() { return status; }, @@ -76,7 +76,7 @@ var connectivity = eventifier({ * Are we offline * @returns {Boolean} */ - isOffline: function isOffline() { + isOffline() { return !status; } }); diff --git a/src/core/databinder.js b/src/core/databinder.js index aa65082..a723c0d 100644 --- a/src/core/databinder.js +++ b/src/core/databinder.js @@ -34,7 +34,7 @@ import Filters from './filter/filters.js'; * @param {string} path - the property path * @returns {*} */ -const locate = function locate(obj, path) { +function locate(obj, path) { const nodes = path.split('.'); const size = nodes.length; let i = 1; @@ -60,7 +60,7 @@ const locate = function locate(obj, path) { * @param {string} path - the property path * @param {string|boolean|number} value - the value to assign */ -const update = function update(obj, path, value) { +function update(obj, path, value) { const nodes = path.split('.'); const size = nodes.length; let i; @@ -86,7 +86,7 @@ const update = function update(obj, path, value) { * @param {Object} obj - the object to locate property into * @param {string} path - the property path */ -const remove = function remove(obj, path) { +function remove(obj, path) { const nodes = path.split('.'); const size = nodes.length; let i; @@ -112,7 +112,7 @@ const remove = function remove(obj, path) { * @param {jQueryElement} $node - the element that contains the items * @param {Boolean} [retry=false] - if we are in fault tolerancy context, to prevent deep recursivity */ -const order = function order(obj, path, $node, retry) { +function order(obj, path, $node, retry) { const values = locate(obj, path); let changed = false; if (_.isArray(values)) { @@ -148,7 +148,7 @@ const order = function order(obj, path, $node, retry) { * @param {string} path - the property path * @param {jQueryElement} $node - the element that contains the items */ -const resyncIndexes = function resyncIndexes(obj, path, $node) { +function resyncIndexes(obj, path, $node) { const values = locate(obj, path); if (_.isArray(values)) { _.forEach(values, function (value, position) { @@ -172,7 +172,7 @@ const resyncIndexes = function resyncIndexes(obj, path, $node) { * @param {jQueryElement} $container * @returns {jQueryElement} */ -const toBind = function toBind($node, $container) { +function toBind($node, $container) { if ($node[0].type && $node[0].name) { if ($node[0].type === 'radio' || $node[0].type === 'checkbox') { return $(`[name='${$node[0].name}']`, $container); @@ -188,7 +188,7 @@ const toBind = function toBind($node, $container) { * @param {String} eventName - the name of the event to bind * @private */ -const _unbind = function _unbind($node, $container, eventName) { +function _unbind($node, $container, eventName) { if ($node.length > 0) { const bounds = $._data($node[0], 'events'); if ( @@ -209,7 +209,7 @@ const _unbind = function _unbind($node, $container, eventName) { * @param {String} eventName - the name of the event to bind * @param {Function} cb - a jQuery event handler */ -const _bindOnce = function _bindOnce($node, $container, eventName, cb) { +function _bindOnce($node, $container, eventName, cb) { _unbind($node, $container, eventName); if ($node.length > 0) { const bounds = $._data($node[0], 'events'); @@ -236,7 +236,7 @@ const _bindOnce = function _bindOnce($node, $container, eventName, cb) { * @param {Object} model * @param {Object} options - to be documented */ -const DataBinder = function DataBinder($container, model, options) { +function DataBinder($container, model, options) { const self = this; this.$container = $container; this.model = model || {}; diff --git a/src/core/polling.js b/src/core/polling.js index 8fa81a6..e09e0b1 100644 --- a/src/core/polling.js +++ b/src/core/polling.js @@ -147,7 +147,7 @@ function pollingFactory(config, pollingInterval = _defaultInterval) { /** * Gets the current action into asynchronous mode. * The next iteration won't be executed until the resolve method has been called. - * However if the reject method is called, the polling is then stopped! + * However, if the reject method is called, the polling is then stopped! * @returns {Object} Returns a promise resolver that provides resolve() and reject() methods */ async() { @@ -223,7 +223,7 @@ function pollingFactory(config, pollingInterval = _defaultInterval) { return this; } - // the next() method can be called either to force a next iteration or to start immediately the action + // the next() method can be called either to force a next iteration or to start immediately the action, // so we need to ensure the schedule is not blocked state.stopped = false; diff --git a/src/core/timer.js b/src/core/timer.js index fd97a74..b1220a7 100644 --- a/src/core/timer.js +++ b/src/core/timer.js @@ -44,7 +44,7 @@ function timerFactory(config) { * @param {Number} [startDuration] - Initial duration (default: 0) * @returns {timer} */ - start: function start(startDuration) { + start(startDuration) { begin = now(); last = begin; duration = startDuration || 0; @@ -58,7 +58,7 @@ function timerFactory(config) { * Gets the time elapsed since the last tick * @returns {number} */ - tick: function tick() { + tick() { var timestamp = now(); var elapsed; if (state.running) { @@ -72,7 +72,7 @@ function timerFactory(config) { * Pause the timer * @returns {timer} */ - pause: function pause() { + pause() { if (state.running) { duration += now() - begin; state.running = false; @@ -85,7 +85,7 @@ function timerFactory(config) { * Resume the timer * @returns {timer} */ - resume: function resume() { + resume() { if (!state.running) { begin = now(); last = begin; @@ -100,7 +100,7 @@ function timerFactory(config) { * Stops the timer * @returns {timer} */ - stop: function stop() { + stop() { if (state.running) { duration += now() - begin; } @@ -116,7 +116,7 @@ function timerFactory(config) { * If the timer is stopped, gets the total duration between start and stop. * @returns {number} */ - getDuration: function getDuration() { + getDuration() { if (state.running) { return duration + (now() - begin); } @@ -128,7 +128,7 @@ function timerFactory(config) { * @param {String} stateName * @returns {Boolean} */ - is: function is(stateName) { + is(stateName) { return !!state[stateName]; }, @@ -137,7 +137,7 @@ function timerFactory(config) { * @param {Number} time * @returns {timer} */ - add: function add(time) { + add(time) { time = parseFloat(time); duration += time; last -= time; @@ -149,7 +149,7 @@ function timerFactory(config) { * @param {Number} time * @returns {timer} */ - sub: function sub(time) { + sub(time) { time = parseFloat(time); duration -= time; last += time; diff --git a/src/core/tokenHandler.js b/src/core/tokenHandler.js index c634570..da9be96 100644 --- a/src/core/tokenHandler.js +++ b/src/core/tokenHandler.js @@ -23,6 +23,7 @@ import _ from 'lodash'; import module from 'module'; import tokenStoreFactory from './tokenStore.js'; import promiseQueue from './promiseQueue.js'; +import store from "./store.js"; let validateTokensOpt = true; let clientConfigFetched = false; @@ -85,7 +86,7 @@ export default function tokenHandlerFactory(options) { // Some async checks before we go for the token: return tokenStore .expireOldTokens() - .then(() => { + .then(async () => { if (!clientConfigFetched) { // Client Config allowed! (first and only time) return this.getClientConfigTokens() diff --git a/src/util/capitalize.js b/src/util/capitalize.js index 348d206..1a9fc85 100644 --- a/src/util/capitalize.js +++ b/src/util/capitalize.js @@ -30,7 +30,7 @@ import _ from 'lodash'; * @param {Boolean} allWords capitalize all words, similar to PHP's ucWords() * @returns {*} */ -const capitalize = function capitalize(input, allWords) { +function capitalize(input, allWords) { const ucFirst = function ucFirst(str) { return str.charAt(0).toUpperCase() + str.substr(1); }; diff --git a/src/util/httpErrorParser.js b/src/util/httpErrorParser.js index ed6ab3e..8b3c5ae 100644 --- a/src/util/httpErrorParser.js +++ b/src/util/httpErrorParser.js @@ -43,10 +43,10 @@ export default { * @param {String} errorThrown - textual portion of the HTTP status, such as "Not Found" or "Internal Server Error." * @returns {Error} the new error */ - parse: function parse(xhr, options, errorThrown) { - var msg; - var json; - var error; + parse(xhr, options, errorThrown) { + let msg; + let json; + let error; try { json = JSON.parse(xhr.responseText); msg = json.message ? json.message : errorThrown; diff --git a/src/util/namespace.js b/src/util/namespace.js index 9b76bb4..4e248f6 100644 --- a/src/util/namespace.js +++ b/src/util/namespace.js @@ -49,7 +49,7 @@ const namespaceHelper = { * @param {Boolean} [normalize] - lower case the string to normalize all the names * @returns {String[]} the list of names (no empty, no duplicate) */ - split: function split(names, normalize) { + split(names, normalize) { if (!_.isString(names) || _.isEmpty(names)) { return []; } @@ -67,7 +67,7 @@ const namespaceHelper = { * @param {String} namespaced - the namespaced name * @returns {String} the name part */ - getName: function getName(namespaced) { + getName(namespaced) { if (!_.isString(namespaced) || _.isEmpty(namespaced)) { return ''; } @@ -83,7 +83,7 @@ const namespaceHelper = { * @param {String} [defaultNs] - the default namespace * @returns {String} the namespace, that defaults to defaultNs */ - getNamespace: function getNamespace(namespaced, defaultNs) { + getNamespace(namespaced, defaultNs) { if (!_.isString(namespaced) || _.isEmpty(namespaced)) { return ''; } @@ -100,7 +100,7 @@ const namespaceHelper = { * @param {Boolean} [normalize] - lower case the string to normalize all the names * @returns {String} - The list of namespaced names */ - namespaceAll: function namespaceAll(names, namespace, normalize) { + namespaceAll(names, namespace, normalize) { let suffix; if (!_.isArray(names)) { names = namespaceHelper.split(names, normalize); diff --git a/src/util/position.js b/src/util/position.js index 964583d..3d2a5bf 100644 --- a/src/util/position.js +++ b/src/util/position.js @@ -33,7 +33,7 @@ export default { * @param {HTMLElement} element - the element to check against the container * @returns {Boolean|undefined} or undefined if the parameters are incorrect, so check your return value type. */ - isInside: function isInside(container, element) { + isInside(container, element) { let containerCoords; let elementCoords; if (container instanceof HTMLElement && element instanceof HTMLElement) { @@ -66,7 +66,7 @@ export default { * @param {HTMLElement} element - the element to check against the container * @returns {Boolean|undefined} or undefined if the parameters are incorrect, so check your return value type. */ - isOver: function isInside(container, element) { + isOver(container, element) { let containerCoords; let elementCoords; if (container instanceof HTMLElement && element instanceof HTMLElement) { diff --git a/src/util/typeCaster.js b/src/util/typeCaster.js index 7c54bcd..edbcc6b 100644 --- a/src/util/typeCaster.js +++ b/src/util/typeCaster.js @@ -25,7 +25,7 @@ export default { * @param {Boolean} defaultValue * @returns {Boolean} true if value === "true", defaultValue if set, false if defaultValue not set */ - strToBool: function strToBool(value, defaultValue) { + strToBool(value, defaultValue) { if (_.isBoolean(value)) { return value; } else if (_.isString(value)) {