gpii-universal
Version:
Cross platform, core components of the GPII personalization infrastructure.
89 lines (77 loc) • 3.23 kB
JavaScript
/*
* GPII Utilities
*
* Copyright 2018 OCAD University
*
* Licensed under the New BSD license. You may not use this file except in
* compliance with this License.
*
* You may obtain a copy of the License at
* https://github.com/GPII/universal/blob/master/LICENSE.txt
*/
;
var fluid = fluid || require("infusion"),
util = util || require("util"),
gpii = fluid.registerNamespace("gpii");
gpii.reservedGpiiKeys = ["noUser", "restore", "reset", "readSetting"];
/**
* Check if the given GPII key is a system reserved key.
* @param {String} gpiiKey - The GPII key.
* @return {Boolean} Return true if the given GPII key is a system reserved key. Otherwise, return false.
*/
gpii.isReservedGpiiKey = function (gpiiKey) {
return gpii.reservedGpiiKeys.indexOf(gpiiKey) !== -1;
};
/**
* Modify an array not to include string(s) in the other given array (or a string).
*
* @param {Array} array - An array to have given values removed - MODIFIED IN PLACE.
* @param {Primitive|Array<Primitive>} valuesToRemove - A primitive value, or an array of primitive values, to be removed.
*/
gpii.arrayDifference = function (array, valuesToRemove) {
if (!array || !valuesToRemove) {
return;
}
var arrayToRemove = fluid.makeArray(valuesToRemove);
fluid.remove_if(array, function (i) {
return arrayToRemove.includes(i);
});
};
/**
* Get inverse capability tranformation rule. If the settings handler has "inverseCapabilitiesTransformations"
* block defined, return it. Otherwise, attempt to calculate the inverse rule based on "capabilitiesTransformations"
* definition.
*
* @param {Object} settingsHandler - A settings handler object from the solution registry.
* @return {Object} - The inverse capability transformation rule.
*/
gpii.getInverseRules = function (settingsHandler) {
return settingsHandler.inverseCapabilitiesTransformations ? settingsHandler.inverseCapabilitiesTransformations :
settingsHandler.capabilitiesTransformations ? fluid.model.transform.invertConfiguration(settingsHandler.capabilitiesTransformations) : {};
};
// Solve GPII-3310 by ensuring fluid.log output in the cloud lies on a single line
gpii.renderCloudLoggingArgs = function (args) {
fluid.each(args, function (arg, i) {
var togo = fluid.isPrimitive(arg) ? arg : util.inspect(arg, {
breakLength: Infinity,
depth: 3,
compact: true
});
if (typeof(togo) === "string" && togo.length > fluid.logObjectRenderChars) {
togo = togo.substring(0, fluid.logObjectRenderChars) + " .... [output suppressed at " + fluid.logObjectRenderChars + " chars - for more output, increase fluid.logObjectRenderChars]";
}
args[i] = togo;
});
};
gpii.applyCloudLogging = function () {
fluid.loggingEvent.addListener(gpii.renderCloudLoggingArgs, "renderNodeLoggingArgs", "before:log");
};
gpii.removeCloudLogging = function () {
fluid.loggingEvent.removeListener("renderNodeLoggingArgs");
};
fluid.defaults("gpii.withCloudLogging", {
listeners: {
"onCreate.applyCloudLogging": "gpii.applyCloudLogging",
"onDestroy.removeCloudLogging": "gpii.removeCloudLogging"
}
});