@oat-sa/tao-core-sdk
Version:
Core libraries of TAO
95 lines (89 loc) • 2.98 kB
JavaScript
define(['lodash'], function (_) { 'use strict';
_ = _ && Object.prototype.hasOwnProperty.call(_, 'default') ? _['default'] : _;
/**
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; under version 2
* of the License (non-upgradable).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Copyright (c) 2017-2019 (original work) Open Assessment Technologies SA ;
*/
/**
* Makes the target a states handler by delegating calls to the states API.
* @param {Object} [target = {}] - the target object, a new plain object is created when omitted.
* @returns {Object} the target for convenience
*/
function statifierFactory(target) {
let states = {};
const statesApi = {
/**
* Tells if the state is set
* @param {String} name
* @returns {Boolean}
*/
getState(name) {
return !!states[name];
},
/**
* Sets a state.
* Without value, the state is always set.
* @example
* statesHandler.setState("ready");
*
* // return `true`
* statesHandler.getState("ready");
*
* @param {String} name
* @param {Boolean} [value]
* @returns {statesApi}
*/
setState(name, value) {
if (typeof value === 'undefined') {
value = true;
}
states[name] = !!value;
return this;
},
/**
* Cleans up all states
* @returns {statesApi}
*/
clearStates() {
states = {};
return this;
},
/**
* Returns all current states set
* @returns {Array}
*/
getStates() {
return _.reduce(states, function (result, state, key) {
if (state) {
result.push(key);
}
return result;
}, []);
}
};
target = target || {};
_(statesApi).functions().forEach(function (method) {
target[method] = function delegate() {
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
args[_key] = arguments[_key];
}
return statesApi[method].apply(target, args);
};
});
return target;
}
return statifierFactory;
});