@interaktiv/mibuilder-core
Version:
Core libraries to interact with MiBuilder projects.
93 lines (77 loc) • 2.77 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.sanitizeAppName = sanitizeAppName;
exports.generateActivityName = generateActivityName;
exports.replaceRawValuesInTiApp = replaceRawValuesInTiApp;
exports.VALID_APP_NAME_PARTS_REGEXP = void 0;
const xpath = require('xpath');
const {
capitalize,
trim
} = require('@interaktiv/dxl');
const {
has,
isString
} = require('@interaktiv/types');
const VALID_APP_NAME_PARTS_REGEXP = /[^A-Za-zöäüß0-9_-]/;
/**
* Sanitizes app name for special characters
*
* @public
* @param {String} appName The app name to sanitize
* @return {String} The sanitized app name
*/
exports.VALID_APP_NAME_PARTS_REGEXP = VALID_APP_NAME_PARTS_REGEXP;
function sanitizeAppName(appName) {
if (isString(appName) === false || trim(appName).length === 0) {
return appName;
}
return appName.split(VALID_APP_NAME_PARTS_REGEXP).filter(Boolean).join(' ');
}
/**
* Generates Android Activity name from given app name
*
* @public
* @param {string} appName The app name to generate the activity name for
* @return {string} The activity name
*/
function generateActivityName(appName) {
if (isString(appName) === false || trim(appName).length === 0) return '';
let activityName = appName.split(/[^A-Za-z0-9_]/).map(word => capitalize(word.toLowerCase())).join('');
if (/^[0-9]/.test(activityName)) activityName = `_${activityName}`;
return activityName;
}
function replaceRawValuesInTiApp(tiapp, raws = {}) {
const {
doc
} = tiapp;
const selectPath = xpath.useNamespaces({
ti: 'http://ti.appcelerator.org',
android: 'http://schemas.android.com/apk/res/android'
});
Object.keys(raws).filter(xPath => Object.prototype.hasOwnProperty.call(raws, xPath)).forEach(xPath => {
const node = selectPath(xPath, doc, true);
if (node == null) return;
let replaceWith = raws[xPath];
if (typeof replaceWith === 'string') {
const now = new Date();
replaceWith = replaceWith.replace('$DATE$', now.toLocaleDateString()).replace('$TIME$', now.toLocaleTimeString()).replace('$DATETIME$', now.toLocaleString()).replace('$TIME_EPOCH$', now.getTime().toString());
}
let matchResult;
while (matchResult = /\{tiapp\.([^{}]*)\}/gm.exec(replaceWith)) {
replaceWith = replaceWith.replace(matchResult[0], tiapp[matchResult[1]]);
}
while (matchResult = /\{tiappProperty\.([^{}]*)\}/gm.exec(replaceWith)) {
replaceWith = replaceWith.replace(matchResult[0], tiapp.getProperty(matchResult[1]));
}
if (has(node, 'value')) {
node.value = replaceWith;
} else if (has(node, 'data')) {
node.data = replaceWith;
} else if (has(node, 'firstChild')) {
node.firstChild.data = replaceWith;
}
});
}