{"id":21509299,"url":"https://github.com/mmis1000/js-function-overload","last_synced_at":"2025-06-14T18:37:11.758Z","repository":{"id":57243029,"uuid":"85378703","full_name":"mmis1000/js-function-overload","owner":"mmis1000","description":"function overload in javascript","archived":false,"fork":false,"pushed_at":"2018-11-27T04:40:29.000Z","size":9,"stargazers_count":3,"open_issues_count":1,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-31T19:46:07.319Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mmis1000.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-03-18T05:42:51.000Z","updated_at":"2024-10-06T17:26:17.000Z","dependencies_parsed_at":"2022-09-15T00:50:44.860Z","dependency_job_id":null,"html_url":"https://github.com/mmis1000/js-function-overload","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmis1000%2Fjs-function-overload","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmis1000%2Fjs-function-overload/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmis1000%2Fjs-function-overload/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mmis1000%2Fjs-function-overload/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mmis1000","download_url":"https://codeload.github.com/mmis1000/js-function-overload/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226098712,"owners_count":17573291,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-23T21:23:22.643Z","updated_at":"2024-11-23T21:23:23.144Z","avatar_url":"https://github.com/mmis1000.png","language":"JavaScript","readme":"Function overload\n===================\n\noverload the function with flexible way\n\nUsage\n===================\n\nload this library\n```javascript\nvar overload = require(\"function-overload\").overload;\n```\n\ncreate templete method\n```javascript\nvar newFunc = overload();\n```\n\nor init method with default handle\n```javascript\nvar newFunc = overload(function (...args) {\n    // code goes here\n});\n```\n\nset the default handle\n```javascript\nvar newFunc = overload();\nnewFunc[\"default\"](function (...args) {\n    // code goes here\n});\n```\n\noverload with given signature\n```javascript\nvar newFunc = overload();\n\n// method 1\nnewFunc.overload([Number], function (arr) {\n    // handle the array of number\n});\n\n// method 2\nnewFunc.overload([Boolean], function (arr) {\n    // handle the array of boolean\n});\n\n// method 3\nnewFunc.overload(Number, function (num) {\n    // handle the number\n});\n\nnewFunc([1]); // trigger method 1\nnewFunc([true]); // trigger method 2\nnewFunc(1); // trigger method 3\nnewFunc(); // throws, because no one can handle it and default handler does no exist\n```\n\nnested signature\n```javascript\nvar newFunc = overload();\n\n// method 1\nnewFunc.overload([{id: Number, name: String}], function (arrOfUser) {\n    // handle these users\n});\n\nnewFunc([{id: 0, name: 'root'}]); // trigger method 1\nnewFunc([{id: '0', name: 'root'}]]); //throws, because no one can handle it\n```\n\nspecial types\n\nrequires these types first\n```javascript\nvar types = require(\"function-overload\").types;\n```\n\nequal\n\u003cbr\u003e\nvirtual type matches only when the content is exactly equal to target.\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.equal(0), function (zero) {\n    // handle it\n});\n\nnewFunc(0); // pass\nnewFunc(1); // throws\nnewFunc(\"0\"); // throws\n```\n\nbiggerThan\n\u003cbr\u003e\nvirtual type matches only when the content is a number bigger than passed number.\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.biggerThan(0), function (positiveNumber) {\n    // handle it\n});\n\nnewFunc(1); // pass\nnewFunc(0); // throws\nnewFunc(-1); // throws\nnewFunc(\"1\"); // throws\n```\n\nsmallerThan\n\u003cbr\u003e\nvirtual type matches only when the content is a number smaller than passed number.\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.smallerThan(0), function (negtiveNumber) {\n    // handle it\n});\n\nnewFunc(1); // throws\nnewFunc(0); // throws\nnewFunc(-1); // pass\nnewFunc(\"1\"); // throws\n```\n\nbiggerEqualThan\n\u003cbr\u003e\nvirtual type matches only when the content is a number bigger than or equal to passed number.\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.biggerEqualThan(0), function (positiveNumber) {\n    // handle it\n});\n\nnewFunc(1); // pass\nnewFunc(0); // pass\nnewFunc(-1); // throws\nnewFunc(\"1\"); // throws\n```\n\nsmallerEqualThan\n\u003cbr\u003e\nvirtual type matches only when the content is a number smaller than or equal to passed number.\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.smallerEqualThan(0), function (negtiveNumber) {\n    // handle it\n});\n\nnewFunc(1); // throws\nnewFunc(0); // pass\nnewFunc(-1); // pass\nnewFunc(\"1\"); // throws\n```\n\nor\n\u003cbr\u003e\nvirtual type matches only when one of the two methods matched\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(\ntypes.or(String, Number), function (stringOrNumber) {\n    // handle it\n});\n\nnewFunc(1); // pass\nnewFunc(\"1\"); // pass\nnewFunc(true); // throws\n```\n\nand\n\u003cbr\u003e\nvirtual type matches only when both methods matched\n```javascript\nvar newFunc = overload();\n\nnewFunc.overload(types.and(\n    types.biggerEqualThan(0),\n    types.smallerThan(10)\n), function (numberBwetweenOneAndTen) {\n    // handle it\n});\n\nnewFunc(-1); // throws\nnewFunc(0); // pass\nnewFunc(9); // pass\nnewFunc(10); // throws\nnewFunc(true); // throws\n```\n\ncustom types\n```javascript\nt.unshift(\"odd\", function (makeValidator) {\n    return {\n        $typeCheck: function (val) {\n            return \"number\" === typeof val \u0026\u0026 ((val % 2) + 2) % 2 === 1;\n        },\n        $typeDescription: \"odd number\",\n        $typeSignature: \"{odd number}\"\n    }\n})\n\nvar newFunc2 = overload();\n\nnewFunc.overload(types.odd(), function (oddNumber) {\n    // handle it\n});\n\nnewFunc(0); // throws\nnewFunc(0.5); // throws\nnewFunc(1); // pass\n\n```\n\ncustom type 2\n```javascript\n\nt.unshift(\"not\", function (makeValidator, input) {\n    // create the validator from input\n    var originalValidator = makeValidator(input);\n    \n    return {\n        $typeCheck: function (val) {\n             return !originalValidator.$typeCheck(val)\n        },\n        $typeDescription: \"not \" + originalValidator.$typeDescription,\n        $typeSignature: \"{not\" + originalValidator.$typeSignature + \"}\"\n    }\n})\n\nvar newFunc2 = overload();\n\nnewFunc2.overload(types.not(Number), function (notNumber) {\n    // handle it\n});\n\nnewFunc(0); // throws\nnewFunc(\"0\"); // pass\nnewFunc(true); // pass\n```\n\ncleaned method\n```javascript\nvar newFunc = overload(function () {\n    return 1;\n});\n// create cleaned method with the original one\nvar newFunc2 = newFunc.clean();\n\nnewFunc(); // -\u003e 1\nnewFunc2(); // -\u003e 1\n\nnewFunc2.overload(someFunc) // throws\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmis1000%2Fjs-function-overload","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmmis1000%2Fjs-function-overload","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmmis1000%2Fjs-function-overload/lists"}