You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.0 KiB
127 lines
4.0 KiB
const sleep = (ms)=>{ |
|
return new Promise(resolve=>{ |
|
console.log(`${(ms/1000).toFixed(2)}s SHOULD WAIT`); |
|
setTimeout(resolve,ms); |
|
}) |
|
} |
|
function convertStringToJson(string) { |
|
try { |
|
return JSON.parse(string); |
|
} catch (error) { |
|
console.log(error); |
|
return {}; |
|
} |
|
} |
|
|
|
function checkingOptions({ question, answer }) { |
|
let options = convertStringToJson(question?.options) || [], |
|
inputType = +question?.inputType || -1; |
|
|
|
// console.log("options", options) |
|
if (!Array.isArray(options)) options = [options]; |
|
const OptionsValidValue = options.map((option) => { |
|
return parseValue(option?.value); |
|
}) |
|
|
|
const questionAnswer = answer[question.id]; |
|
if (questionAnswer === null || questionAnswer === undefined) return { |
|
ok: false |
|
} |
|
|
|
switch (inputType) { |
|
case 1: //TEXT |
|
case 3: |
|
if (typeof questionAnswer !== "string" || questionAnswer.length == 0) { |
|
return { |
|
ok: false |
|
} |
|
} |
|
break; |
|
case 2: //NUMBER |
|
if (!isNumber(questionAnswer) || !OptionsValidValue.includes(questionAnswer?.toString())) { |
|
return { |
|
ok: false |
|
} |
|
} |
|
break; |
|
case 4: // tak entekhab |
|
case 6: |
|
console.log("OptionsValidValue",OptionsValidValue); |
|
console.log("questionAnswer",questionAnswer) |
|
if (!OptionsValidValue.includes(questionAnswer)) { |
|
return { |
|
ok: false |
|
} |
|
} |
|
break; |
|
case 5: // chand entekhab |
|
case 7: |
|
if (Array.isArray(questionAnswer)) { |
|
for (let ans of questionAnswer) { |
|
if (!OptionsValidValue.includes(ans)) return { ok: false } |
|
} |
|
} else if (typeof questionAnswer === "string") { |
|
for (let ans of questionAnswer.split(",")) { |
|
if (!OptionsValidValue.includes(ans)) return { ok: false } |
|
} |
|
} else { |
|
return { ok: false }; |
|
} |
|
break; |
|
case 8: |
|
console.log("oomadinja"); |
|
console.log("javabesh", questionAnswer, isValidDateStrict(questionAnswer)) |
|
if (!isValidDateStrict(questionAnswer)) return { ok: false }; |
|
break; |
|
|
|
case 10: |
|
if (!isBoolean(questionAnswer)) return { ok: false } |
|
break; |
|
default: |
|
return { |
|
ok: false |
|
} |
|
} |
|
return { |
|
ok: true |
|
} |
|
} |
|
|
|
const checkCondition = ({ data, condition, inputType = 2, options = null }, conditionArray = new Set()) => { |
|
let ands = true; |
|
for (const key in condition) { |
|
const conditionValue = condition[key]; |
|
if (key == 'or') { |
|
let ors = false; |
|
for (const cond of conditionValue) { |
|
const orConditionChecking = checkCondition({ data, condition: cond }, conditionArray); |
|
ors = ors || orConditionChecking.ok; |
|
conditionArray = new Set([...conditionArray, ...orConditionChecking.conditionArray]) |
|
if (ors) break; |
|
} |
|
ands = ands && ors; |
|
} else { |
|
if (data[key] == 'true') data[key] = true; |
|
if (data[key] == 'false') data[key] = false; |
|
|
|
switch (key) { |
|
case [Op.gt]: |
|
ands = ands && data[key] > conditionValue; |
|
if (!conditionArray.has(key)) conditionArray.add(key); |
|
break; |
|
default: |
|
ands = ands && data[key] == conditionValue; |
|
if (!conditionArray.has(key)) conditionArray.add(key); |
|
} |
|
} |
|
if (!ands) { |
|
return { ok: false, conditionArray }; |
|
} |
|
} |
|
|
|
return { ok: true, conditionArray }; |
|
}; |
|
module.exports={ |
|
sleep, |
|
convertStringToJson |
|
} |