react-native-site24x7-rn
Version:
Monitor react native mobile applications with site24x7 Mobile RUM
238 lines (219 loc) • 5.5 kB
JavaScript
const MAX_USER_TRACE_LENGTH = 20;
const MAX_SCREEN_NAME_LENGTH = 150;
const MAX_USER_TRACE_MESSAGE_LENGTH = 200;
const SOURCE_MAP_PREFIX = 'file://reactnative.local/';//No I18N
const devicePathPattern = /^(.*@)?.*\/[^\.]+(\.app|CodePush)\/?(.*)/;
let self = module.exports = {
deviceId:undefined,
currentScreen:'',
userBreadCrumbs:[],
/**
* Simple is object check.
*
* @param value
* @returns {boolean}
*/
isObject: (value)=> {
return value ? typeof value === 'object' && !Array.isArray(value) && !self.isEmpty(value) : false;//No I18N
},
/**
* Simple is string check
* @param value
* @return {boolean}
*/
isString : (value)=> {
return typeof value === 'string';//No I18N
},
/**
* Simple is number check
* @param value
* @return {boolean}
*/
isNumber : (value)=> {
return typeof value === 'number';//No I18N
},
/**
* Simple finite check
* @param value
* @returns {boolean}
*/
isFinite : (value)=> {
return Number.isFinite(value);
},
/**
* Simple integer check
* @param value
* @returns {boolean}
*/
isInteger : (value)=> {
return Number.isInteger(value);
},
/**
* Simple is boolean check
*
* @param value
* @return {boolean}
*/
isBoolean : (value)=> {
return typeof value === 'boolean';//No I18N
},
/**
*
* @param value
* @returns {arg is Array<any>}
*/
isArray : (value)=> {
return Array.isArray(value);
},
/**
*
* @param value
* @returns {boolean}
*/
isEmpty : (item) =>{
if(item === null || item === void 0){
return true;
}else{
return false;
}
},
/**
*
* @param value
* @returns {boolean}
*/
isEmptyString : (item) =>{
if(!item || item===""){
return true;
}else{
return false;
}
},
/**
* setting current screen
*
* @param value
* @return
*/
setCurrentScreen : (screenName) =>{
self.addUserBreadCrumbs("Navigation", screenName);//No I18N
self.currentScreen = screenName;
},
/**
* getting current screen
*
* @return {string}
*/
getCurrentScreen : () =>{
self.currentScreen = self.currentScreen.length>MAX_SCREEN_NAME_LENGTH ? self.currentScreen.substr(0,MAX_SCREEN_NAME_LENGTH) : self.currentScreen;
return self.currentScreen;
},
/**
* getting device guid
*
* @return {string}
*/
getDeviceGuid : () =>{
if(!self.deviceId){
self.deviceId = self.generateUid();
}
return self.deviceId;
},
/**
* generating uid
*
* @return {string}
*/
generateUid : () =>{
function chr(){
return (Math.floor(Math.random() * 10000)).toString();
}
return chr() + chr() + chr() + chr();
},
/**
* sanitising url and removing url query params
*
* @param url
* @return {string}
*/
sanitiseURL : (url) => {
return url.split("?")[0];
},
/**
* sanitising url and removing url query params
*
* @param event
* @param msg
* @return {string}
*/
addUserBreadCrumbs: (event,msg) => {
msg = msg.length>MAX_USER_TRACE_MESSAGE_LENGTH ? msg.substring(0,MAX_USER_TRACE_MESSAGE_LENGTH) : msg;
self.userBreadCrumbs.push({action :event,name :msg,timestamp:(new Date()).getTime()});
self.userBreadCrumbs = self.userBreadCrumbs.slice(Math.max(self.userBreadCrumbs.length - MAX_USER_TRACE_LENGTH, 0));
},
/**
* Getting user breadcrumbs
*
* @return {Array}
*/
getBreadCrumbs:()=>{
return self.userBreadCrumbs;
},
/**
* Clearing user breadcrumbs
*/
clearBreadCrumbs:()=>{
self.userBreadCrumbs = [];
},
/**
* removing protocol from https
*
* @param url
* @return {string}
*/
removeProtocol : (url) => {
return url.replace(/^http(s)?:\/\//i, '');
},
/**
* check if item should be ignored from list
*
* @return {boolean}
*/
shouldIgnoreFromList : (item,list,isUrlList) =>{
var target = item;
if(isUrlList){
target = self.removeProtocol(item);
}
return list.some((ignored) => {return target.indexOf(ignored)>-1});
},
/**
* removing protocol from https
*
* @return {string}
*/
cleanFilePath : (frames) => frames.map((frame) => {
const result = devicePathPattern.exec(frame.file);
if (result) {
const [_, __, ___, fileName] = result;
return { ...frame, file: SOURCE_MAP_PREFIX + fileName };
}
return frame;
}),
/**
* Checks whether the passed array of strings is valid or empty.
*
* @param {string[]} arr - The array to check.
* @returns {boolean} True if the array is valid or empty, false otherwise.
*/
isValidStringArray : (arr) => {
if (!Array.isArray(arr)) {
return false;
}
for (let item of arr) {
if (typeof item !== 'string') {
return false;
}
}
return true;
}
}