Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- "use strict";
- Object.prototype.getInformations = function(onlyInstance = false) {
- if (typeof onlyInstance !== "boolean") {
- throw new Error("getInformations() needs no argument or a boolean (onlyInstance).");
- }
- let a = [
- String(this).match(/^function\s*\(((?:.|\n)*)\)\s*{(?:.|\n)*};?/),
- String(this).match(/^\(((?:.|\n)*)\)\s*=>\s*{(?:.|\n)*};?/)
- ];
- if (a[0]) {
- if (!onlyInstance) {
- let args = a[0][1].split(",");
- if (objToReturn)
- if (args.length === 1) {
- args = null;
- } else {
- args = args.map(el => {
- return el.replace(/\s/g, "");
- }).map(el => {
- if (~el.search(/\.{3}.*/)) {
- return [
- el.substr(3),
- "..."
- ];
- } else if (el.split("=").length === 1) {
- return [
- el,
- null
- ];
- } else {
- return [
- el.split("=")[0],
- el.substr(el.split("=")[0].length + 1)
- ];
- }
- });
- }
- return {
- "instance": "AnonymousFunction",
- "details": {
- "arguments": args
- }
- };
- } else {
- return {
- "instance": "AnonymousFunction",
- "details": {}
- };
- }
- } else if (a[1]) {
- if (!onlyInstance) {
- let args = a[1][1].split(",");
- if (args.length === 1) {
- args = null;
- } else {
- args = args.map(el => {
- return el.replace(/\s/g, "");
- }).map(el => {
- if (~el.search(/\.{3}.*/)) {
- return [
- el.substr(3),
- "..."
- ];
- } else if (el.split("=").length === 1) {
- return [
- el,
- null
- ];
- } else {
- return [
- el.split("=")[0],
- el.substr(el.split("=")[0].length + 1)
- ];
- }
- });
- }
- return {
- "instance": "ArrowFunction",
- "details": {
- "arguments": args
- }
- };
- } else {
- return {
- "instance": "ArrowFunction",
- "details": {}
- };
- }
- } else if (this.constructor.name === "Array") {
- let arrContent = "";
- let arrContentArray = [];
- this.forEach((el, i) => {
- let elInfo;
- let extType;
- if (el === null) {
- elInfo = {
- "instance": "null"
- };
- } else if (el === undefined) {
- elInfo = {
- "instance": "undefined"
- };
- } else {
- elInfo = el.getInformations();
- }
- extType = ["Object", "Array"].includes(elInfo.instance);
- if (i !== this.length - 1) {
- if (extType) {
- arrContent += (elInfo.details.instance + ", ");
- } else {
- arrContent += (elInfo.instance + ", ");
- }
- } else {
- if (extType) {
- arrContent += elInfo.details.instance;
- } else {
- arrContent += elInfo.instance;
- }
- }
- if (!onlyInstance) {
- if (extType) {
- arrContentArray.push(elInfo.details.content);
- } else {
- arrContentArray.push(elInfo.instance);
- }
- }
- }, this);
- if (!onlyInstance) {
- return {
- "instance": "Array",
- "details": {
- "instance": `Array[${arrContent}]`,
- "content": arrContentArray
- }
- };
- } else {
- return {
- "instance": "Array",
- "details": {
- "instance": `Array[${arrContent}]`
- }
- };
- }
- } else if (this.constructor.name === "Object") {
- let objContent = "";
- let objContentArray = [];
- Object.entries(this).forEach((el, i) => {
- let elInfo = [el[0]];
- let extType;
- if (el[1] === null) {
- elInfo[1] = {
- "instance": "null"
- };
- } else if (el[1] === undefined) {
- elInfo[1] = {
- "instance": "undefined"
- };
- } else {
- elInfo[1] = el[1].getInformations();
- }
- extType = ["Object", "Array"].includes(elInfo[1].instance);
- if (i !== Object.entries(this).length - 1) {
- if (extType) {
- objContent += `"${elInfo[0]}": ${elInfo[1].details.instance}, `;
- } else {
- objContent += `"${elInfo[0]}": ${elInfo[1].instance}`;
- }
- } else {
- if (extType) {
- objContent += `"${elInfo[0]}": ${elInfo[1].details.instance}`;
- } else {
- objContent += `"${elInfo[0]}": ${elInfo[1].instance}`;
- }
- }
- if (!onlyInstance) {
- if (extType) {
- objContentArray.push([
- elInfo[0],
- elInfo[1].details.instance
- ]);
- } else {
- objContentArray.push([
- elInfo[0],
- elInfo[1].instance
- ]);
- }
- }
- }, this);
- if (!onlyInstance) {
- return {
- "instance": "Object",
- "details": {
- "instance": `Object{${objContent}}`,
- "content": objContentArray
- }
- };
- } else {
- return {
- "instance": "Object",
- "details": {
- "instance": `Object{${objContent}}`
- }
- };
- }
- } else {
- return {
- "instance": this.constructor.name
- };
- }
- }
- let ObjectType = {
- "createFunction": function(name, fn, argsTypes) {
- let fnInfo = fn.getInformations();
- if (
- (typeof name === "string") &&
- (!!name) &&
- (["AnonymousFunction", "ArrowFunction"].includes(fnInfo.instance)) &&
- (argsTypes instanceof Array) &&
- (argsTypes.length == fnInfo.details.arguments.length)
- ) {
- if (ObjectType.functions[name] === undefined) {
- ObjectType.functions[name] = [];
- }
- if (!ObjectType.functions[name].some(el => el.argsTypes.every((elem, index) => {
- if (elem instanceof Array) {
- return elem.every((element, i) => element === argsTypes[index][i]);
- } else {
- return elem === argsTypes[index];
- }
- }))) {
- ObjectType.functions[name].push({
- "fn": fn,
- "argsTypes": argsTypes
- });
- } else {
- 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)).");
- }
- } else {
- 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)).");
- }
- },
- "callFunction": function(name, ...args) {
- if (
- (typeof name === "string") &&
- (!!name) &&
- (ObjectType.functions.hasOwnProperty(name))
- ) {
- let argsTypes = args.map(el => {
- let info = el.getInformations(true);
- if (["Object", "Array"].includes(info.instance)) {
- return info.details.instance;
- } else {
- return info.instance;
- }
- });
- let fn;
- try {
- fn = ObjectType.functions[name][
- ObjectType.functions[name].findIndex(infos => infos.argsTypes.every((el, index) => {
- if (el instanceof Array) {
- return el.includes(argsTypes[index]);
- } else {
- return el === argsTypes[index];
- }
- }))
- ].fn;
- return fn(...args)
- } catch (e) {
- console.log(e);
- throw new Error("Object types of arguments are invalid.");
- }
- } else {
- throw new Error("callFunction() needs a string to know which function to call.");
- }
- },
- "removeFunction": function(name, argsTypes) {
- if (
- (typeof name === "string") &&
- (!!name) &&
- (ObjectType.functions.hasOwnProperty(name)) &&
- (argsTypes instanceof Array)
- ) {
- let index = ObjectType.functions[name].findIndex(el => el.argsTypes.every((elem, ind) => {
- if (elem instanceof Array) {
- return elem.every((element, i) => element === argsTypes[ind][i]);
- } else {
- return elem === argsTypes[ind];
- }
- }));
- if (index === -1) {
- throw new Error("The function you searched for seems to not exist.");
- } else {
- return ObjectType.functions[name].splice(index, 1);
- }
- } else {
- throw new Error("removeFunction() needs a string (name) and an array (argsTypes) to remove a function.");
- }
- },
- "functions": {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement