Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const usedVariables = {
- functions: {
- fun1: [],
- fun2: ["context.res.fun1"],
- fun3: ["context.res.fun1", "context.res.fun2"],
- fun4: ["context.vals.var2"],
- var1: [],
- var2: ["context"],
- funIf1: ["context.vals.var1"],
- funIf2: ["context.res.funIf1", "context.vals.var1"],
- response: ["context.res.fun2"],
- cmtnh8O9w: [],
- ifcdQKlmk: ["context.vals.var1"],
- },
- variables: {
- context: {
- type: "string",
- usedInFunctions: ["var2"],
- },
- "context.res.fun1": {
- type: "string",
- usedInFunctions: ["fun2", "fun3"],
- },
- "context.res.fun2": {
- type: "string",
- usedInFunctions: ["fun3", "response"],
- },
- "context.vals.var1": {
- type: "string",
- usedInFunctions: ["ifcdQKlmk", "funIf1", "funIf2"],
- },
- "context.vals.var2": {
- type: "string",
- usedInFunctions: ["fun4"],
- },
- "context.res.funIf1": {
- type: "string",
- usedInFunctions: ["funIf2"],
- },
- },
- payloadData: {
- context: {
- res: {
- fun1: "your_value_here",
- fun2: "your_value_here",
- funIf1: "your_value_here",
- },
- vals: {
- var1: "your_value_here",
- var2: "your_value_here",
- },
- },
- },
- };
- const json_script = {
- order: {
- root: [
- "fun1",
- "fun2",
- "var1",
- "funIMcyTJ",
- "ifcdQKlmk",
- "cmtnh8O9w",
- "fun3",
- "var2",
- "fun4",
- "var07zmwP",
- ],
- ifcdQKlmk: ["funIf1", "funIf2"],
- },
- title: "untitled1",
- blocks: {
- fun1: {
- data: "funcjkJqeufS",
- name: "fun1",
- type: "function-true",
- active: true,
- },
- fun2: {
- data: "funcz2HdqwVZ",
- name: "fun2",
- type: "function-true",
- active: true,
- },
- fun3: {
- data: "funcy271Bf9P",
- name: "fun3",
- type: "function-true",
- active: false,
- },
- fun4: {
- data: "functTGPav6f",
- name: "fun4",
- type: "function-true",
- active: true,
- },
- var1: {
- data: "2",
- html: "2",
- name: "var1",
- type: "variable",
- },
- var2: {
- data: "context",
- html: "context",
- name: "var2",
- type: "variable",
- },
- funIf1: {
- data: "funcyumy9O8K",
- name: "funIf1",
- type: "function-true",
- active: true,
- },
- funIf2: {
- data: "funcgrdMhoNH",
- name: "funIf2",
- type: "function-true",
- active: true,
- },
- response: {
- data: "context?.res?.fun2",
- type: "response",
- responseType: "custom",
- },
- cmtnh8O9w: {
- data: "this is comment 1",
- type: "comment",
- isGenerated: true,
- },
- funIMcyTJ: {
- url: "",
- data: "funIMcyTJ",
- name: "untitled1",
- type: "function",
- metaData: {},
- isDrafted: true,
- },
- ifcdQKlmk: {
- html: "${context?.vals?.var1} === 2 && (5 + 1 > 5)",
- type: "ifBlock",
- valid: true,
- condition: "context?.vals?.var1 === 2 && (5 + 1 > 5)",
- },
- var07zmwP: {
- type: "variable",
- name: "untitled2",
- data: "",
- isDrafted: true,
- },
- },
- version: "1.1.7",
- identifier: "scriFj56pbNT",
- webhook_type: "add",
- };
- let _dependencyObj = {};
- const getType = (id) => {
- return json_script["blocks"][id]?.type;
- };
- const isSlugActive = (id) => {
- if (json_script["blocks"][id]?.active === undefined) return true;
- else return json_script["blocks"][id]?.active;
- };
- const addContextDependencies = (id, resultSet) => {
- const index = json_script.order.root.indexOf(id);
- if (index !== -1) {
- for (let i = index - 1; i >= 0; i--) {
- const blockId = json_script.order.root[i];
- if (isSlugActive(blockId)) {
- let dependencyKey;
- const blockType = getType(blockId);
- switch (blockType) {
- case "ifBlock":
- dependencyKey = `context.if.${blockId}`;
- break;
- case "variable":
- dependencyKey = `context.vals.${blockId}`;
- break;
- case "comment":
- // Ignore comment blocks
- continue;
- default:
- dependencyKey = `context.res.${blockId}`;
- }
- resultSet.add(dependencyKey);
- }
- }
- }
- };
- const createDependencyObjForResponse = (id) => {
- const key_name = `_response`;
- const resultSet = new Set();
- const val = usedVariables["functions"][id];
- if (val && Array.isArray(val)) {
- for (const v of val) {
- if (v == "context") {
- addContextDependencies(id, resultSet);
- continue;
- }
- const k = v.slice(v.lastIndexOf("."));
- if (getType(k) === "variable") {
- resultSet.add(`context.vals.${k}`);
- } else {
- resultSet.add(`context.res.${k}`);
- }
- }
- }
- return { [key_name]: resultSet };
- };
- const createDependencyObjForVariable = (id) => {
- const key_name = `context.vals.${id}`;
- const resultSet = new Set();
- const val = usedVariables["functions"][id];
- if (val && Array.isArray(val)) {
- for (const v of val) {
- if (v == "context") {
- addContextDependencies(id, resultSet);
- continue;
- }
- const k = v.slice(v.lastIndexOf("."));
- if (getType(k) === "variable") {
- resultSet.add(`context.vals.${k}`);
- } else {
- resultSet.add(`context.res.${k}`);
- }
- }
- }
- return { [key_name]: resultSet };
- };
- const createDependencyObjForElse = (id) => {
- const key_name = `context.res.${id}`;
- const resultSet = new Set();
- const val = usedVariables["functions"][id];
- if (val && Array.isArray(val)) {
- for (const v of val) {
- if (v == "context") {
- addContextDependencies(id, resultSet);
- continue;
- }
- const k = v.slice(v.lastIndexOf("."));
- if (getType(k) === "variable") {
- resultSet.add(`context.vals.${k}`);
- } else {
- resultSet.add(`context.res.${k}`);
- }
- }
- }
- return { [key_name]: resultSet };
- };
- const createDependencyObjForIfBlock = (id) => {
- const key_name = `context.if.${id}`;
- const resultSet = new Set();
- const val = usedVariables["functions"][id];
- if (val && Array.isArray(val)) {
- for (const v of val) {
- if (v == "context") {
- addContextDependencies(id, resultSet);
- continue;
- }
- const k = v.slice(v.lastIndexOf("."));
- if (getType(k) === "variable") {
- if (!_dependencyObj[v]) {
- createDependencyObjForVariable(k);
- }
- resultSet.add(`context.vals.${k}`);
- } else {
- resultSet.add(`context.res.${k}`);
- }
- }
- }
- return { [key_name]: resultSet };
- };
- const createDependencyObj = () => {
- for (const key of Object.keys(usedVariables["functions"])) {
- if (isSlugActive(key)) {
- if (getType(key) === "ifBlock") {
- _dependencyObj = {
- ..._dependencyObj,
- ...createDependencyObjForIfBlock(key),
- };
- } else if (getType(key) === "comment") {
- continue;
- } else if (getType(key) === "response") {
- _dependencyObj = {
- ..._dependencyObj,
- ...createDependencyObjForResponse(key),
- };
- } else if (getType(key) === "variable") {
- _dependencyObj = {
- ..._dependencyObj,
- ...createDependencyObjForVariable(key),
- };
- } else {
- _dependencyObj = {
- ..._dependencyObj,
- ...createDependencyObjForElse(key),
- };
- }
- }
- }
- return _dependencyObj;
- };
- const x = createDependencyObj();
- console.log(x);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement