react-native-blitzllama
Version:
Blitzllama React Native Library
190 lines (189 loc) • 7.13 kB
JavaScript
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateDataField = exports.formatResponses = exports.isRtl = exports.deleteData = exports.getData = exports.storeData = exports.questRules = void 0;
const react_native_1 = require("react-native");
const async_storage_1 = __importDefault(require("@react-native-async-storage/async-storage"));
react_native_1.LogBox.ignoreAllLogs();
const checkRange = (question_type, value) => {
if (value === null) {
return false;
}
if (question_type === 'nps_feedback' && value >= 0 && value <= 10) {
return true;
}
if ((question_type === 'scale_feedback' ||
question_type === 'star_feedback' ||
question_type === 'emoji_feedback') &&
value >= 1 &&
value <= 5) {
return true;
}
return false;
};
const questRules = (question_type, rule_sets, question_order, scale_value, responses, survey_input) => {
for (let i = 0; i < rule_sets.length; i++) {
const { rule_type, option, options, skip_to } = rule_sets[i];
if (question_type === 'nps_feedback' ||
question_type === 'scale_feedback' ||
question_type === 'star_feedback' ||
question_type === 'emoji_feedback') {
if (scale_value !== undefined &&
option !== undefined &&
checkRange(question_type, scale_value) &&
checkRange(question_type, option) &&
((rule_type === 'eq' && scale_value === option) ||
(rule_type === 'neq' && scale_value !== option) ||
(rule_type === 'lt' && scale_value < option) ||
(rule_type === 'lte' && scale_value <= option) ||
(rule_type === 'gt' && scale_value > option) ||
(rule_type === 'gte' && scale_value >= option))) {
return skip_to;
}
}
if (question_type === 'single_select_feedback') {
if (options && options[0] && responses && rule_type === 'in_list_exact') {
if (responses.find((o) => o.option_text === options[0])) {
return skip_to;
}
}
}
if (options && responses && question_type === 'multi_select_feedback') {
const response_arr = responses.map((r) => r.option_text);
if (rule_type === 'in_list_exact') {
let check_equal = true;
if (options.length !== response_arr.length) {
check_equal = false;
}
for (let res = 0; res < response_arr.length; res++) {
if (!options.includes(response_arr[res])) {
check_equal = false;
}
}
if (check_equal) {
return skip_to;
}
}
if (rule_type === 'in_list_once') {
for (let res = 0; res < response_arr.length; res++) {
if (options.includes(response_arr[res])) {
return skip_to;
}
}
}
}
if (options &&
survey_input &&
question_type === 'data_collection' &&
rule_type === 'in_list_once') {
if (options.includes(survey_input)) {
return skip_to;
}
}
if (rule_type === 'submit') {
return skip_to;
}
if (i + 1 === rule_sets.length) {
return question_order + 1;
}
}
};
exports.questRules = questRules;
const storeData = async (key, value) => {
try {
await async_storage_1.default.setItem(key, value);
}
catch (e) {
console.log('e', e);
}
};
exports.storeData = storeData;
const getData = async (key) => {
let jsonValue = null;
try {
jsonValue = await async_storage_1.default.getItem(key);
}
catch (e) {
console.log('e', e);
}
return jsonValue ? jsonValue : null;
};
exports.getData = getData;
const deleteData = async (key) => {
try {
await async_storage_1.default.removeItem(key);
}
catch (e) {
console.log('e', e);
}
};
exports.deleteData = deleteData;
const isRtl = (lang) => {
return ['ar', 'ur', 'fa', 'he'].includes(lang);
};
exports.isRtl = isRtl;
const formatResponses = (question_type, response) => {
if (question_type === 'single_select_feedback' ||
question_type === 'multi_select_feedback') {
return response.map((r) => r.option_text);
}
return response;
};
exports.formatResponses = formatResponses;
const validateDataField = (str, validations, is_mandatory) => {
if (str.trim().length === 0) {
return {
goto_next: !is_mandatory,
message: 'Enter valid input',
};
}
const str_length = str.trim().length;
if (validations && validations.field_type) {
if (validations.field_type === 'number') {
const regrexForNumber = /^\d+$/;
if (!regrexForNumber.test(str.trim())) {
return {
goto_next: false,
message: `Please enter a ${validations.min_length}-digit number`,
};
}
if (validations.min_length > 0 && validations.min_length > str_length) {
return {
goto_next: false,
message: `Please enter a ${validations.min_length}-digit number`,
};
}
if (validations.max_length && validations.max_length < str_length) {
return {
goto_next: false,
message: `Please enter a number with ${validations.min_length}-${validations.max_length} digits`,
};
}
}
if (validations.field_type === 'email') {
const regexForEmail = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
if (!regexForEmail.test(str)) {
return {
goto_next: false,
message: 'Please enter a valid email Id',
};
}
}
if (validations.min_length > 0 && validations.min_length > str_length) {
return {
goto_next: false,
message: `Please enter a ${validations.min_length}-character text`,
};
}
if (validations.max_length && validations.max_length < str_length) {
return {
goto_next: false,
message: `Please enter a text with ${validations.min_length}-${validations.max_length} characters`,
};
}
}
return { goto_next: true, message: '' };
};
exports.validateDataField = validateDataField;
;