tia
Version:
Time is All (logs driven test engine with ExtJs support)
175 lines • 5.78 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const util_1 = require("util");
exports.debugLocale = false;
let locale = {};
let invertedLocaleFirstKey = {};
let invertedLocaleAllKeys = {};
/**
* Sets locale object. Locale object is key-value object for localization.
* Key is english text, and value is any utf8 text.
*
* @param objExpression - expression how to get locale object.
* @param {boolean} [enableLog=true] - is logging needed for this action.
* @returns a promise which will be resolved with script return value.
*/
function setLocaleObject(objExpression, enableLog) {
return gIn.wrap({
msg: 'setLocaleObject ... ',
enableLog,
act: () => {
const scriptStr = `return tiaEJ.setLocale(${objExpression});`;
return gT.s.browser.executeScriptWrapper(scriptStr).then(res => {
locale = res.locale;
invertedLocaleFirstKey = res.invertedLocaleFirstKey;
invertedLocaleAllKeys = res.invertedLocaleAllKeys;
});
},
});
}
exports.setLocaleObject = setLocaleObject;
// TODO:
// export setImagesMap
let extraLocale = {};
let invertedExtraLocaleFirstKey = {};
let invertedExtraLocaleAllKeys = {};
function setExtraLocale(newExtraLocale) {
extraLocale = newExtraLocale;
const invertedExtraObject = gT.commonMiscUtils.invertMapObj(newExtraLocale);
invertedExtraLocaleFirstKey = invertedExtraObject.invertedMapFirstKey;
invertedExtraLocaleAllKeys = invertedExtraObject.invertedMapAllKeys;
}
function setExtraLocaleObject(localeObj, enableLog) {
setExtraLocale(localeObj);
const objStr = util_1.inspect(localeObj, { compact: true, breakLength: 200 });
return gIn.wrap({
msg: 'setExtraLocaleObject ... ',
enableLog,
act: () => {
const scriptStr = `return tiaEJ.setExtraLocale(${objStr});`;
return gT.s.browser.executeScriptWrapper(scriptStr).then(res => {
if (res !== true) {
throw new Error('setExtraLocaleObject: Unexpected return value');
}
});
},
});
}
exports.setExtraLocaleObject = setExtraLocaleObject;
function getLocStr(key) {
const str = locale[key];
if (!str) {
throw new Error(`Key: "${key} is not found in locale`);
}
return str;
}
exports.getLocStr = getLocStr;
function getExtraLocStr(key) {
const str = extraLocale[key];
if (!str) {
throw new Error(`Key: "${key} is not found in extra locale`);
}
return str;
}
exports.getExtraLocStr = getExtraLocStr;
function locKeyToStr(str) {
const reExtra = /el"(.*?)"/g;
const result = str.replace(reExtra, (m, key) => getExtraLocStr(key));
const re = /l"(.*?)"/g;
return result.replace(re, (m, key) => getLocStr(key));
}
exports.locKeyToStr = locKeyToStr;
function locKeyToStrAndEscapeSlashes(str) {
let result = locKeyToStr(str);
result = result.replace(/\\/g, '\\\\');
return result;
}
exports.locKeyToStrAndEscapeSlashes = locKeyToStrAndEscapeSlashes;
function getFirstLocaleKey(value, extra) {
if (extra) {
return invertedExtraLocaleFirstKey[value];
}
return invertedLocaleFirstKey[value];
}
exports.getFirstLocaleKey = getFirstLocaleKey;
function getAllLocaleKeys(value, extra) {
if (extra) {
return invertedExtraLocaleAllKeys[value];
}
return invertedLocaleAllKeys[value];
}
exports.getAllLocaleKeys = getAllLocaleKeys;
function convertTextToFirstLocKey(text) {
let locKey = getFirstLocaleKey(text);
let result;
let locFound = false;
if (locKey) {
locFound = true;
result = `l"${locKey}"`;
}
else {
locKey = getFirstLocaleKey(text, true);
if (locKey) {
locFound = true;
result = `el"${locKey}"`;
}
else {
result = text;
}
}
if (locFound && exports.debugLocale) {
result += ` ("${text}")`;
}
return result;
}
exports.convertTextToFirstLocKey = convertTextToFirstLocKey;
// Component info string.
function getCIS(tEQ, compName, idForLog = '') {
return `${compName}${idForLog ? ` ${idForLog}` : ''} "${tEQ}":`;
}
exports.getCIS = getCIS;
// CIS + Raw val.
function getCISRVal(tEQ, compName, idForLog = '', val) {
return `${compName}${idForLog ? ` ${idForLog}` : ''} "${tEQ}": Raw val: '${val}'`;
}
exports.getCISRVal = getCISRVal;
// eslint-disable-next-line max-params
function getCISContent(prefix, tEQ, compName, idForLog = '', val, // eslint-disable-line @typescript-eslint/no-explicit-any
noWrap = false) {
const valArg = noWrap ? val : gT.cC.content.wrap(val);
return `${prefix}: ${compName}${idForLog ? ` ${idForLog}` : ''} "${tEQ}":\n${valArg}`;
}
exports.getCISContent = getCISContent;
/**
* Returns locale keys for which values are equal to given text.
* Requires gT.e.utils.setLocaleObject(expression) call before.
* @param text
* @returns {string}
*/
// export function getLocKeysByText(text) {
// const res = [];
// for (const key of locale) {
// if (locale.hasOwnProperty(key)) {
// if (text === locale[key]) {
// res.push(key);
// }
// }
// }
// return res.join(', ');
// };
/**
* The mode in which native language text is added after locale keys.
* @param newMode
* @param enableLog
* @return {*}
*/
function setDebugLocaleMode(newMode, enableLog) {
exports.debugLocale = newMode;
return gIn.wrap({
msg: `Set debugLocale mode to '${newMode} ... '`,
enableLog,
act: () => gT.s.browser.executeScript(`return tiaEJ.setDebugLocaleMode(${newMode});`, false),
});
}
exports.setDebugLocaleMode = setDebugLocaleMode;
//# sourceMappingURL=extjs-utils.js.map