Advertisement
Beafantles

Untitled

Jul 17th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. "use strict";
  2.  
  3. Object.prototype.getInformations = function(onlyInstance = false) {
  4.     if (typeof onlyInstance !== "boolean") {
  5.         throw new Error("getInformations() needs no argument or a boolean (onlyInstance).");
  6.     }
  7.     let a = [
  8.         String(this).match(/^function\s*\(((?:.|\n)*)\)\s*{(?:.|\n)*};?/),
  9.         String(this).match(/^\(((?:.|\n)*)\)\s*=>\s*{(?:.|\n)*};?/)
  10.     ];
  11.     if (a[0]) {
  12.         if (!onlyInstance) {
  13.             let args = a[0][1].split(",");
  14.             if (objToReturn)
  15.             if (args.length === 1) {
  16.                 args = null;
  17.             } else {
  18.                 args = args.map(el => {
  19.                     return el.replace(/\s/g, "");
  20.                 }).map(el => {
  21.                     if (~el.search(/\.{3}.*/)) {
  22.                         return [
  23.                             el.substr(3),
  24.                             "..."
  25.                         ];
  26.                     } else if (el.split("=").length === 1) {
  27.                         return [
  28.                             el,
  29.                             null
  30.                         ];
  31.                     } else {
  32.                         return [
  33.                             el.split("=")[0],
  34.                             el.substr(el.split("=")[0].length + 1)
  35.                         ];
  36.                     }
  37.                 });
  38.             }
  39.  
  40.             return {
  41.                 "instance": "AnonymousFunction",
  42.                 "details": {
  43.                     "arguments": args
  44.                 }
  45.             };
  46.         } else {
  47.             return {
  48.                 "instance": "AnonymousFunction",
  49.                 "details": {}
  50.             };
  51.         }
  52.     } else if (a[1]) {
  53.         if (!onlyInstance) {
  54.             let args = a[1][1].split(",");
  55.             if (args.length === 1) {
  56.                 args = null;
  57.             } else {
  58.                 args = args.map(el => {
  59.                     return el.replace(/\s/g, "");
  60.                 }).map(el => {
  61.                     if (~el.search(/\.{3}.*/)) {
  62.                         return [
  63.                             el.substr(3),
  64.                             "..."
  65.                         ];
  66.                     } else if (el.split("=").length === 1) {
  67.                         return [
  68.                             el,
  69.                             null
  70.                         ];
  71.                     } else {
  72.                         return [
  73.                             el.split("=")[0],
  74.                             el.substr(el.split("=")[0].length + 1)
  75.                         ];
  76.                     }
  77.                 });
  78.             }
  79.  
  80.             return {
  81.                 "instance": "ArrowFunction",
  82.                 "details": {
  83.                     "arguments": args
  84.                 }
  85.             };
  86.         } else {
  87.             return {
  88.                 "instance": "ArrowFunction",
  89.                 "details": {}
  90.             };
  91.         }
  92.     } else if (this.constructor.name === "Array") {
  93.         let arrContent = "";
  94.         let arrContentArray = [];
  95.         this.forEach((el, i) => {
  96.             let elInfo;
  97.             let extType;
  98.  
  99.             if (el === null) {
  100.                 elInfo = {
  101.                     "instance": "null"
  102.                 };
  103.             } else if (el === undefined) {
  104.                 elInfo = {
  105.                     "instance": "undefined"
  106.                 };
  107.             } else {
  108.                 elInfo = el.getInformations();
  109.             }
  110.  
  111.             extType = ["Object", "Array"].includes(elInfo.instance);
  112.  
  113.             if (i !== this.length - 1) {
  114.                 if (extType) {
  115.                     arrContent += (elInfo.details.instance + ", ");
  116.                 } else {
  117.                     arrContent += (elInfo.instance + ", ");
  118.                 }
  119.             } else {
  120.                 if (extType) {
  121.                     arrContent += elInfo.details.instance;
  122.                 } else {
  123.                     arrContent += elInfo.instance;
  124.                 }
  125.             }
  126.             if (!onlyInstance) {
  127.                 if (extType) {
  128.                     arrContentArray.push(elInfo.details.content);
  129.                 } else {
  130.                     arrContentArray.push(elInfo.instance);
  131.                 }
  132.             }
  133.         }, this);
  134.  
  135.         if (!onlyInstance) {
  136.             return {
  137.                 "instance": "Array",
  138.                 "details": {
  139.                     "instance": `Array[${arrContent}]`,
  140.                     "content": arrContentArray
  141.                 }
  142.             };
  143.         } else {
  144.             return {
  145.                 "instance": "Array",
  146.                 "details": {
  147.                     "instance": `Array[${arrContent}]`
  148.                 }
  149.             };
  150.         }
  151.     } else if (this.constructor.name === "Object") {
  152.         let objContent = "";
  153.         let objContentArray = [];
  154.         Object.entries(this).forEach((el, i) => {
  155.             let elInfo = [el[0]];
  156.             let extType;
  157.  
  158.             if (el[1] === null) {
  159.                 elInfo[1] = {
  160.                     "instance": "null"
  161.                 };
  162.             } else if (el[1] === undefined) {
  163.                 elInfo[1] = {
  164.                     "instance": "undefined"
  165.                 };
  166.             } else {
  167.                 elInfo[1] = el[1].getInformations();
  168.             }
  169.  
  170.             extType = ["Object", "Array"].includes(elInfo[1].instance);
  171.  
  172.             if (i !== Object.entries(this).length - 1) {
  173.                 if (extType) {
  174.                     objContent += `"${elInfo[0]}": ${elInfo[1].details.instance}, `;
  175.                 } else {
  176.                     objContent += `"${elInfo[0]}": ${elInfo[1].instance}`;
  177.                 }
  178.             } else {
  179.                 if (extType) {
  180.                     objContent += `"${elInfo[0]}": ${elInfo[1].details.instance}`;
  181.                 } else {
  182.                     objContent += `"${elInfo[0]}": ${elInfo[1].instance}`;
  183.                 }
  184.             }
  185.             if (!onlyInstance) {
  186.                 if (extType) {
  187.                     objContentArray.push([
  188.                         elInfo[0],
  189.                         elInfo[1].details.instance
  190.                     ]);
  191.                 } else {
  192.                     objContentArray.push([
  193.                         elInfo[0],
  194.                         elInfo[1].instance
  195.                     ]);
  196.                 }
  197.             }
  198.         }, this);
  199.  
  200.         if (!onlyInstance) {
  201.             return {
  202.                 "instance": "Object",
  203.                 "details": {
  204.                     "instance": `Object{${objContent}}`,
  205.                     "content": objContentArray
  206.                 }
  207.             };
  208.         } else {
  209.             return {
  210.                 "instance": "Object",
  211.                 "details": {
  212.                     "instance": `Object{${objContent}}`
  213.                 }
  214.             };
  215.         }
  216.     } else {
  217.         return {
  218.             "instance": this.constructor.name
  219.         };
  220.     }
  221. }
  222.  
  223. let ObjectType = {
  224.     "createFunction": function(name, fn, argsTypes) {
  225.         let fnInfo = fn.getInformations();
  226.         if (
  227.             (typeof name === "string") &&
  228.             (!!name) &&
  229.             (["AnonymousFunction", "ArrowFunction"].includes(fnInfo.instance)) &&
  230.             (argsTypes instanceof Array) &&
  231.             (argsTypes.length == fnInfo.details.arguments.length)
  232.         ) {
  233.             if (ObjectType.functions[name] === undefined) {
  234.                 ObjectType.functions[name] = [];
  235.             }
  236.             if (!ObjectType.functions[name].some(el => el.argsTypes.every((elem, index) => {
  237.                 if (elem instanceof Array) {
  238.                     return elem.every((element, i) => element === argsTypes[index][i]);
  239.                 } else {
  240.                     return elem === argsTypes[index];
  241.                 }
  242.             }))) {
  243.                 ObjectType.functions[name].push({
  244.                     "fn": fn,
  245.                     "argsTypes": argsTypes
  246.                 });
  247.             } else {
  248.                 throw new Error("A function have already the same name and the same object types of arguments or createFunction() got wrong inputs (name, fn, argsType). name must be a string. fn must be an AnonymousFunction or an ArrowFunction with the same number of parameters in argsTypes. argsTypes must be an array (the array must have strings or arrays (with strings inside)).");
  249.             }
  250.         } else {
  251.             throw new Error("A function have already the same name and the same object types of arguments or createFunction() got wrong inputs (name, fn, argsType). name must be a string. fn must be an AnonymousFunction or an ArrowFunction with the same number of parameters in argsTypes. argsTypes must be an array (the array must have strings or arrays (with strings inside)).");
  252.         }
  253.     },
  254.     "callFunction": function(name, ...args) {
  255.         if (
  256.             (typeof name === "string") &&
  257.             (!!name) &&
  258.             (ObjectType.functions.hasOwnProperty(name))
  259.         ) {
  260.             let argsTypes = args.map(el => {
  261.                 let info = el.getInformations(true);
  262.                 if (["Object", "Array"].includes(info.instance)) {
  263.                     return info.details.instance;
  264.                 } else {
  265.                     return info.instance;
  266.                 }
  267.             });
  268.             let fn;
  269.             try {
  270.                 fn = ObjectType.functions[name][
  271.                     ObjectType.functions[name].findIndex(infos => infos.argsTypes.every((el, index) => {
  272.                         if (el instanceof Array) {
  273.                             return el.includes(argsTypes[index]);
  274.                         } else {
  275.                             return el === argsTypes[index];
  276.                         }
  277.                     }))
  278.                 ].fn;
  279.                 return fn(...args)
  280.             } catch (e) {
  281.                 console.log(e);
  282.                 throw new Error("Object types of arguments are invalid.");
  283.             }
  284.         } else {
  285.             throw new Error("callFunction() needs a string to know which function to call.");
  286.         }
  287.     },
  288.     "removeFunction": function(name, argsTypes) {
  289.         if (
  290.             (typeof name === "string") &&
  291.             (!!name) &&
  292.             (ObjectType.functions.hasOwnProperty(name)) &&
  293.             (argsTypes instanceof Array)
  294.         ) {
  295.             let index = ObjectType.functions[name].findIndex(el => el.argsTypes.every((elem, ind) => {
  296.                 if (elem instanceof Array) {
  297.                     return elem.every((element, i) => element === argsTypes[ind][i]);
  298.                 } else {
  299.                     return elem === argsTypes[ind];
  300.                 }
  301.             }));
  302.             if (index === -1) {
  303.                 throw new Error("The function you searched for seems to not exist.");
  304.             } else {
  305.                 return ObjectType.functions[name].splice(index, 1);
  306.             }
  307.         } else {
  308.             throw new Error("removeFunction() needs a string (name) and an array (argsTypes) to remove a function.");
  309.         }
  310.     },
  311.     "functions": {}
  312. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement