react-native-surveys
Version:
Build your own forms, surveys and polls for your React Native apps.
166 lines (143 loc) • 4.54 kB
JavaScript
import { decodeData } from "./base64";
export function predefineValuesFromState(state = {}, form) {
if (!form.pages) return;
let key,
keys = Object.keys(state);
let n = keys.length;
const newState = {};
while (n--) {
key = keys[n];
newState[key.toLowerCase()] = state[key];
}
form.pages.forEach(page => {
if (page.blocks) {
page.blocks.forEach(block => {
if (block.type === "contact") {
if (block.value) {
block.value.forEach(field => {
const fieldLabel = field.question
? field.question.toLowerCase()
: "";
if (fieldLabel && newState[fieldLabel]) {
field.value = newState[fieldLabel];
}
});
}
}
const blockLabel = block.question ? block.question.toLowerCase() : "";
if (blockLabel && newState[blockLabel]) {
block.value = newState[blockLabel];
}
});
}
});
return form;
}
export function parseUrlData(url = "") {
if (!url) return {};
let search = url.split("/")[3];
if (!search) {
search = url.split("/")[1];
}
if (!search) {
return {};
}
if (search.indexOf("?") !== -1) {
search = search.substring(0, search.indexOf("?"));
}
return decodeData(search);
}
export function getRandomInt(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
}
export function protectWithKey(license, key) {
const rand = getRandomInt(0, license.length - 1);
return [license.slice(0, rand), key, license.slice(rand)].join("");
}
export function queryStringToJSON() {
if (!window || !window.location) return {};
let pairs = window.location.search.slice(1).split("&");
let result = {};
pairs.forEach(function(pair) {
pair = pair.split("=");
result[pair[0]] = decodeURIComponent(pair[1] || "");
});
return JSON.parse(JSON.stringify(result));
}
export function returnStateQueryParameter() {
const queryParameters = queryStringToJSON();
const queryState = queryParameters.state;
let state;
if (queryState) {
try {
state = JSON.parse(decodeURIComponent(queryState));
} catch (e) {
console.error("Invalid format of state query parameter.");
}
}
return state;
}
export function getBrowser() {
// Not in browser
if (!window || !window.location) return ""; // Opera 8.0+
const isOpera =
(!!window.opr && !!window.opr.addons) ||
!!window.opera ||
navigator.userAgent.indexOf(" OPR/") >= 0; // Firefox 1.0+
const isFirefox = typeof InstallTrigger !== "undefined"; // Safari 3.0+ "[object HTMLElementConstructor]"
const isSafari =
/constructor/i.test(window.HTMLElement) ||
(function(p) {
return p.toString() === "[object SafariRemoteNotification]";
})(!window["safari"] || window.safari.pushNotification); // Internet Explorer 6-11
const isIE =
/*@cc_on!@*/
false || !!document.documentMode; // Edge 20+
const isEdge = !isIE && !!window.StyleMedia; // Chrome 1+
const isChrome = !!window.chrome; // Blink engine detection
const isBlink = (isChrome || isOpera) && !!window.CSS;
return isOpera
? "Opera"
: isFirefox
? "Firefox"
: isSafari
? "Safari"
: isChrome
? "Chrome"
: isIE
? "IE"
: isEdge
? "Edge"
: isBlink
? "Blink"
: "";
}
export function getOperatingSystem() {
if (!window || !window.location) return "";
const userAgent = navigator.userAgent || navigator.vendor || window.opera;
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
if (window.navigator.userAgent.indexOf("Windows NT 10.0") !== -1)
return "Windows 10";
if (window.navigator.userAgent.indexOf("Windows NT 6.2") !== -1)
return "Windows 8";
if (window.navigator.userAgent.indexOf("Windows NT 6.1") !== -1)
return "Windows 7";
if (window.navigator.userAgent.indexOf("Windows NT 6.0") !== -1)
return "Windows Vista";
if (window.navigator.userAgent.indexOf("Windows NT 5.1") !== -1)
return "Windows XP";
if (window.navigator.userAgent.indexOf("Windows NT 5.0") !== -1)
return "Windows 2000";
if (window.navigator.userAgent.indexOf("Mac") !== -1) return "Mac";
if (window.navigator.userAgent.indexOf("X11") !== -1) return "UNIX";
if (window.navigator.userAgent.indexOf("Linux") !== -1) return "Linux";
return "";
}