{"id":16666691,"url":"https://github.com/nerdo/implements-interface","last_synced_at":"2025-10-07T05:29:49.946Z","repository":{"id":57272959,"uuid":"158134624","full_name":"nerdo/implements-interface","owner":"nerdo","description":"Tests whether an object has the same methods and signatures defined by a prototype.","archived":false,"fork":false,"pushed_at":"2019-05-14T23:01:16.000Z","size":99,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-19T14:43:02.076Z","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/nerdo.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":"2018-11-18T22:49:06.000Z","updated_at":"2019-05-14T23:01:17.000Z","dependencies_parsed_at":"2022-09-15T10:53:10.658Z","dependency_job_id":null,"html_url":"https://github.com/nerdo/implements-interface","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/nerdo%2Fimplements-interface","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fimplements-interface/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fimplements-interface/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nerdo%2Fimplements-interface/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nerdo","download_url":"https://codeload.github.com/nerdo/implements-interface/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243310685,"owners_count":20270828,"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-10-12T11:11:48.580Z","updated_at":"2025-10-07T05:29:44.912Z","avatar_url":"https://github.com/nerdo.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# implements-interface\n\nA function that tests whether or not an object implements an interface defined by a prototype.\n\nThere are some important caveats. It does _not_ work reliably when testing against functions that evaluate to:\n\n* Native functions: As far as I know, it isn't possible to get the arguments from a native function so always return an empty set. Depending on whether your interface method has arguments or not, you will either get a false positive or a false negative.\n* Bound functions: If you bind a function, e.g. `const bar = foo.bind(this)`, the returned function (in this case, `bar`) turns out to be native and has the same drawbacks.\n\n## Installing\n\n```\nnpm install --save-dev implements-interface\n```\n\n## Getting Started\n\nImport the library, define your interface as a regular JavaScript class, and simply call implementsInterface with the object to test and the interface (class), e.g.:\n\n```\nimport { implementsInterface } from 'implements-interface'\n\nclass FooBarInterface {\n  foo (x, y) { }\n  bar (z, ...rest) { }\n}\n\nconst passingObj = {\n  foo (a, b) {\n    // arbitrary implementation\n  },\n  bar (a, ...more) {\n    // arbitrary implementation\n  }\n}\n\nconst failingObj = {\n  foo (a, b, c) {\n    // arbitrary implementation\n  },\n  bar (a, ...more) {\n    // arbitrary implementation\n  }\n}\n\ntry {\n  console.log(implementsInterface(passingObj, FooBarInterface)) // returns true\n  console.log(implementsInterface(failingObj, FooBarInterface)) // throws Error\n} catch (e) {\n  console.log(e)\n}\n```\n\nYou can use this as a basis for a matcher in a testing framework, for example, with jest, a matcher might be defined and used like this:\n\n```\n/* global test, expect */\nimport { implementsInterface } from 'implements-interface'\n\nexpect.extend({\n  toImplement (object, Interface) {\n    try {\n      implementsInterface(object, Interface)\n      return {\n        message: () =\u003e `expected object not to implement ${Interface.name}.`,\n        pass: true\n      }\n    } catch (e) {\n      return {\n        message: () =\u003e e.message,\n        pass: false\n      }\n    }\n  }\n})\n\nclass FooBarInterface {\n  foo (x, y) { }\n  bar (z, ...rest) { }\n}\n\ntest('successfully implementing the FooBarInterface', () =\u003e {\n  const object = {\n    foo (a, b) {\n      // arbitrary implementation\n    },\n    bar (a, ...more) {\n      // arbitrary implementation\n    }\n  }\n\n  expect(new FooBarInterface()).toImplement(FooBarInterface)\n  expect(object).toImplement(FooBarInterface)\n})\n\ntest('improperly implementing the FooBarInterface', () =\u003e {\n  const object = {\n    foo (a, b) {\n      // arbitrary implementation\n    },\n    bar (a, more) {\n      // arbitrary implementation\n    }\n  }\n\n  expect(object).not.toImplement(FooBarInterface)\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnerdo%2Fimplements-interface","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnerdo%2Fimplements-interface","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnerdo%2Fimplements-interface/lists"}