{"id":13624366,"url":"https://github.com/jonrandy/metho","last_synced_at":"2026-01-21T20:44:41.297Z","repository":{"id":44654617,"uuid":"414929842","full_name":"jonrandy/metho","owner":"jonrandy","description":"A new method for methods","archived":false,"fork":false,"pushed_at":"2022-08-21T10:45:02.000Z","size":48,"stargazers_count":223,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-08-09T10:50:03.550Z","etag":null,"topics":["extend","javascript","js","monkey-patching","patching","prototype","symbols","syntax"],"latest_commit_sha":null,"homepage":"https://metho.js.org","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jonrandy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-10-08T09:45:00.000Z","updated_at":"2025-03-26T12:45:55.000Z","dependencies_parsed_at":"2022-09-26T21:50:52.898Z","dependency_job_id":null,"html_url":"https://github.com/jonrandy/metho","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/jonrandy/metho","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonrandy%2Fmetho","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonrandy%2Fmetho/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonrandy%2Fmetho/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonrandy%2Fmetho/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jonrandy","download_url":"https://codeload.github.com/jonrandy/metho/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jonrandy%2Fmetho/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28642280,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-21T18:04:35.752Z","status":"ssl_error","status_checked_at":"2026-01-21T18:03:55.054Z","response_time":86,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["extend","javascript","js","monkey-patching","patching","prototype","symbols","syntax"],"created_at":"2024-08-01T21:01:41.780Z","updated_at":"2026-01-21T20:44:41.280Z","avatar_url":"https://github.com/jonrandy.png","language":"JavaScript","readme":"\n# Metho - A new method for methods\n\n\u003cimg align=\"right\" height=100 src=\"https://user-images.githubusercontent.com/1510194/173243187-08fdcc0f-204d-43c0-b07e-5b7bcb2713a4.png\"\u003eA small library to allow you to safely add 'dynamic properties' to objects, with the help of Symbols. This is useful (among other things) for 'monkey patching' native JavaScript types to give them new capabilities (see [metho-number](https://github.com/jonrandy/metho-number), [metho-set](https://github.com/jonrandy/metho-set), [metho-function](https://github.com/jonrandy/metho-function), [metho-array](https://github.com/jonrandy/metho-array), and [metho-string](https://github.com/jonrandy/metho-string)).\n\nSome examples of what is possible:\n\n```js\nimport * as Metho from 'metho'\n\nconst asHex = Metho.add(\n  Number.prototype,\n  function() { return this.toString(16) }\n)\n\nconsole.log(65534[asHex])  // fffe\n\nconst upper = Metho.add(\n  String.prototype,\n  function() { return this.toUpperCase() }\n)\nconst chunk = Metho.add(\n  String.prototype,\n  function(length) {\n    return this.match(new RegExp('.{1,' + length + '}', 'g'))\n  }\n)\n\nconsole.log(\"Hello World!\"[upper][chunk(2)])  // ['HE', 'LL', 'O ', 'WO', 'RL', 'D!']\n```\n\n## How to use\n\nMetho is fairly simple, and offers 4 basic functions for adding these 'dynamic properties' to your target object(s). All functions will return either a Symbol, or a function that returns a Symbol. These Symbols are the property 'names'.\n\n### `add(targetOrTargets, function, [options={}])`\nThis is probably the function you'll need most often. It will use either `addWithParams` or `addSimple` based on the number of arguments the passed function expects - 0 will cause `addSimple` to be used, anything else will cause `addWithParams` or `addProperty` to be used - based upon the state of the `outerSyntax` option. When added with `outerSyntax` set to `true` - the syntax for your property will be that of a more regular function call:\n```js\n// options.outerSyntax = false\nobject[property(x)]\n\n// options.outerSyntax = true\nobject[property](x)\n```\nThere is a slight performance hit when not using `outerSyntax` - hence the reason for the switch. To specify more than one target for the function, you should pass an array of targets.\n\n**Important note** - it has been pointed out that functions with a default argument(s) that start from the first argument do not seem to work correctly with the `add` method. Whilst they do *seem* to behave oddly, they are actually behaving correctly as they do not actually *expect* any arguments (for a clearer explanation, see the information about [`function.length`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/length) on MDN). If you want to add such methods you should use the `addWithParams` method directly.\n\n### `addWithParams(targetOrTargets, function, [options={}])`\nAdds a 'dynamic property` that can accept parameters. If you wish to pass **no** parameters when calling it, you can simply omit the parentheses - this is particularly useful in the case of functions where all parameters have defaults or are entirely optional.\n```js\nconsole.log(object[property(param1, param2)]  // call the dynamic property and pass parameters\nconsole.log(object[propertyWithDefaultParams])  // equivalent to object[propertyWithDefaultParams()]\n\n```\n\n### `addSimple(targetOrTargets, function, [options={}])`\nAdds a 'dynamic property` that has no parameters\n```js\nconsole.log(object[property])\n```\n\n### `addProperty(targetOrTargets, propertyValue, [options={}])`\nAdds a regular property to the target(s) (will not be automatically called if it is a function)\n```js\nconsole.log(object[property])\n```\n\n## Advanced usage and `options`\n\nMost, if not all of the below were added to facilitate the ability to have Metho 'methods' that can be shared between different targets in different libraries (e.g. the 'method' would acquire more capabilities when a second library is imported that uses it). For an example of this in action, please refer to the [metho-string](https://github.com/jonrandy/metho-string) and [metho-array](https://github.com/jonrandy/metho-array) libraries - where this functionality is used to create shared 'methods' such as `reverse` and `chunk`.\n\n### Option `symbolName`\nThis is used to give a name to the generated Symbol (i.e. when it is created with `new Symbol(symbolName`)\n\n### Option `register`\nUsed to internally register the created/used `Symbol` (or function) in an internal registry within Metho - used in conjunction `symbolName` which will become the 'key' in the registry\n\n### Option `useSymbol`\nThis allows an existing Symbol to be used instead of a new one being created. This is available only for `addProperty` and `addSimple` - meaning that it can also be passed to `add`\n\n### `data`\nThis is a symbol created by Metho for the intended purpose of being a key to store arbitary 'data' on a target object\n```js\nmyTarget[Metho.data] = \"Arbitrary value\"\n```\n\n### `getRegistered(name)`\nThis will return the Symbol or function stored in the registry with the given name as key\n\n### `addWithSharedSymbolName(target, function, symbolName)`\nA convenience function to assist when adding new 'methods' to new targets, where the 'method' may already be in existence. This is best understood in conjunction with `metho-string` and `metho-array` mentioned above\n","funding_links":[],"categories":["js","javascript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonrandy%2Fmetho","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjonrandy%2Fmetho","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjonrandy%2Fmetho/lists"}