gfdu
Version:
Global Frontend Developer Utils - Created by Muthukumar D
231 lines • 8.84 kB
JavaScript
;
function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; }
var muthu_React = _interopDefault(require('react'));
var muthu_PropTypes = _interopDefault(require('prop-types'));
var muthu_axios = _interopDefault(require('axios'));
var muthu_moment = _interopDefault(require('moment'));
let muthu_headers, muthu_baseUrl, muthu_instance, muthu_with_credentials, muthu_api_status;
let muthu_windowObj = window;
let initializeApiCall = function initializeApiCall(a,b,c,d) {
muthu_headers = a;
muthu_baseUrl = b;
muthu_with_credentials = c;
muthu_api_status = d;
}
let api_status_func = function api_status_func(error) {
muthu_api_status.map((e)=>{
if(error.response.status == e.status_code){
e.func();
}
});
}
let getApiCall = function getApiCall(url, callback) {
createInstance(url, null, 0)
.then((result) => {
callback(result.data);
})
.catch((error) => {
callback(error);
api_status_func(error);
});
}
let postApiCall = function postApiCall(url, data, callback) {
createInstance(url, data, 1)
.then((result) => {
callback(result.data);
})
.catch((error) => {
callback(error);
api_status_func(error);
});
}
let putApiCall = function putApiCall(url, data, callback) {
createInstance(url, data, 2)
.then((result) => {
callback(result.data);
})
.catch((error) => {
callback(error);
api_status_func(error);
});
}
let patchApiCall = function patchApiCall(url, data, callback) {
createInstance(url, data, 3)
.then((result) => {
callback(result.data);
})
.catch((error) => {
callback(error);
api_status_func(error);
});
}
let deleteApiCall = function deleteApiCall(url, data, callback) {
createInstance(url, data, 4)
.then((result) => {
callback(result.data);
})
.catch((error) => {
callback(error);
api_status_func(error);
});
}
let createInstance = function createInstance(append, data, type) {
muthu_instance = muthu_axios.create({
baseURL: muthu_baseUrl,
headers: muthu_headers,
withCredentials: muthu_with_credentials
});
var url = muthu_baseUrl+append
switch (type) {
case 0:
return muthu_instance.get(url);
case 1:
return muthu_instance.post(url, data);
case 2:
return muthu_instance.put(url, data);
case 3:
return muthu_instance.patch(url, data);
case 4:
return muthu_instance.delete(url, data);
default:
return muthu_instance.get(url);
}
};
let setCookie = function setCookie(muthu_name, storevalue) {
document.cookie = muthu_name + "=" + storevalue;
}
let setCookieWithExpiry = function setCookieWithExpiry(muthu_name, storevalue, expirydays) {
const d = new Date();
d.setTime(d.getTime() + (expirydays * 24 * 60 * 60 * 1000));
let expires = "expires="+d.toUTCString();
document.cookie = muthu_name + "=" + storevalue + ";" + expires + ";path=/";
}
let getCookie = function getCookie(muthu_name) {
let muthu_get_name = muthu_name + "=";
let ca = document.cookie.split(';');
for(let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(muthu_get_name) == 0) {
return c.substring(muthu_get_name.length, c.length);
}
}
return "";
}
let removeCookie = function removeCookie(muthu_name) {
document.cookie = muthu_name + "=";
}
let deleteAllCookies = function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
let storageSetItem = function storageSetItem(key, value) {
return muthu_windowObj.localStorage.setItem(key, JSON.stringify(value));
}
let storageGetItem = function storageGetItem(key) {
return JSON.parse(muthu_windowObj.localStorage.getItem(key));
}
let storageRemoveItem = function storageRemoveItem(key) {
return muthu_windowObj.sessionStorage.removeItem(key);
}
let storageRemoveAll = function storageRemoveAll() {
return muthu_windowObj.sessionStorage.clear();
}
let sessionStorageSetItem = function sessionStorageSetItem(key, value) {
return muthu_windowObj.sessionStorage.setItem(key, JSON.stringify(value));
}
let sessionStorageGetItem = function sessionStorageGetItem(key) {
return JSON.parse(muthu_windowObj.sessionStorage.getItem(key));
}
let sessionStorageRemoveItem = function sessionStorageRemoveItem(key) {
return muthu_windowObj.sessionStorage.removeItem(key);
}
let sessionStorageRemoveAll = function sessionStorageRemoveAll() {
return muthu_windowObj.sessionStorage.clear();
}
let currencyFormatter = function currencyFormatter(amount,code) {
return new Intl.NumberFormat('en-IN', { style: 'currency', currency: code }).format(amount);
}
let showTimeZoneDate = function showTimeZoneDate(date) {
if (date === "" || date === undefined || date === null || date === "-") { return ""; }
var date = muthu_moment(date);
return muthu_moment.utc(date).local().format("DD-MM-YYYY h:mm a");
}
let showZoneDate = function showZoneDate(date) {
if (date === "" || date === undefined || date === null || date === "-") { return ""; }
var date = muthu_moment(date);
return muthu_moment.utc(date).local().format("DD-MM-YYYY");
}
let showZoneTime = function showZoneTime(time) {
if (time === "" || time === undefined || time === null || time === "-") { return ""; }
var time = muthu_moment(time);
return muthu_moment.utc(time).local().format("h:mm a");
}
let textCapitalize = function textCapitalize(data) {
if (data !== undefined && data !== null && data !== "") {
return data.charAt(0).toUpperCase() + data.slice(1);
} else {
return data;
}
}
let returnFirstDegreeObjValue = function returnFirstDegreeObjValue(obj, key) {
return obj && (obj[key] || obj[key] === 0) ? obj[key] : "N/A";
}
let returnSecondDegreeObjValue = function returnSecondDegreeObjValue(obj, key, postion) {
if (obj && obj[key] && (obj[key][postion] || obj[key][postion] === 0))
return obj[key][postion];
else return "N/A";
}
let returnThirdDegreeObjValue = function returnThirdDegreeObjValue(masterobj, obj, key, postion) {
if (
masterobj &&
masterobj[obj] &&
masterobj[obj][key] &&
(masterobj[obj][key][postion] || masterobj[obj][key][postion] === 0)
)
return masterobj[obj][key][postion];
else return "N/A";
}
let returnFourthDegreeObjValue = function returnFourthDegreeObjValue(masterobj, obj, key, postion, fourthObj) {
if (
masterobj &&
masterobj[obj] &&
masterobj[obj][key] &&
masterobj[obj][key][postion] &&
(masterobj[obj][key][postion][fourthObj] || masterobj[obj][key][postion][fourthObj] === 0)
)
return masterobj[obj][key][postion][fourthObj];
else return "N/A";
}
let returnFifthDegreeObjValue = function returnFifthDegreeObjValue(masterobj, obj, key, postion, fourthObj, fifthObj) {
if (
masterobj &&
masterobj[obj] &&
masterobj[obj][key] &&
masterobj[obj][key][postion] &&
masterobj[obj][key][postion][fourthObj] &&
(masterobj[obj][key][postion][fourthObj][fifthObj] || masterobj[obj][key][postion][fourthObj][fifthObj] === 0)
)
return masterobj[obj][key][postion][fourthObj][fifthObj];
else return "N/A";
}
let parseJson = function parseJson(jsondata){
return JSON.parse(jsondata);
}
let stringifyJson = function stringifyJson(jsondata){
return JSON.stringify(jsondata);
}
let reloadWindow = function reloadWindow(){
return muthu_windowObj.location.reload();
}
let reloadWindowToPath = function reloadWindowToPath(pathtoload){
return muthu_windowObj.location.href = pathtoload;
}
module.exports = {initializeApiCall, getApiCall, postApiCall, putApiCall, patchApiCall, deleteApiCall, setCookie, setCookieWithExpiry, getCookie, removeCookie, deleteAllCookies, storageSetItem, storageGetItem, storageRemoveItem, storageRemoveAll, sessionStorageSetItem, sessionStorageGetItem, sessionStorageRemoveItem, sessionStorageRemoveAll, currencyFormatter, showTimeZoneDate, showZoneDate, showZoneTime, textCapitalize, returnFirstDegreeObjValue, returnSecondDegreeObjValue, returnThirdDegreeObjValue, returnFourthDegreeObjValue, returnFifthDegreeObjValue, parseJson, stringifyJson, reloadWindow, reloadWindowToPath};