{"id":17048696,"url":"https://github.com/neocotic/pollock","last_synced_at":"2026-04-05T17:38:48.948Z","repository":{"id":57326966,"uuid":"96160581","full_name":"neocotic/pollock","owner":"neocotic","description":"A simple lightweight JavaScript library for adding abstract methods to types","archived":false,"fork":false,"pushed_at":"2018-11-23T17:02:28.000Z","size":113,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"master","last_synced_at":"2025-04-11T05:44:10.466Z","etag":null,"topics":["abstract","browser","javascript","method","nodejs"],"latest_commit_sha":null,"homepage":"","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/neocotic.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-07-04T00:39:02.000Z","updated_at":"2024-07-25T05:23:05.000Z","dependencies_parsed_at":"2022-09-13T18:53:48.559Z","dependency_job_id":null,"html_url":"https://github.com/neocotic/pollock","commit_stats":null,"previous_names":["notninja/pollock"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocotic%2Fpollock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocotic%2Fpollock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocotic%2Fpollock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/neocotic%2Fpollock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/neocotic","download_url":"https://codeload.github.com/neocotic/pollock/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248594142,"owners_count":21130313,"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":["abstract","browser","javascript","method","nodejs"],"created_at":"2024-10-14T09:52:47.686Z","updated_at":"2025-12-12T04:39:25.530Z","avatar_url":"https://github.com/neocotic.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![Pollock](https://raw.githack.com/neocotic/pollock/master/images/pollock.png)](https://github.com/neocotic/pollock)\n\nA simple lightweight JavaScript library for adding abstract methods to types which, when called, report a useful error\nindicating that they have not been implemented/overridden on the child type.\n\n[![Build Status](https://img.shields.io/travis/neocotic/pollock/develop.svg?style=flat-square)](https://travis-ci.org/neocotic/pollock)\n[![Coverage](https://img.shields.io/codecov/c/github/neocotic/pollock/develop.svg?style=flat-square)](https://codecov.io/gh/neocotic/pollock)\n[![Dev Dependency Status](https://img.shields.io/david/dev/neocotic/pollock.svg?style=flat-square)](https://david-dm.org/neocotic/pollock?type=dev)\n[![License](https://img.shields.io/npm/l/pollock.svg?style=flat-square)](https://github.com/neocotic/pollock/blob/master/LICENSE.md)\n[![Release](https://img.shields.io/npm/v/pollock.svg?style=flat-square)](https://www.npmjs.com/package/pollock)\n\n* [Install](#install)\n* [API](#api)\n* [Bugs](#bugs)\n* [Contributors](#contributors)\n* [License](#license)\n\n## Install\n\nInstall using the package manager for your desired environment(s):\n\n``` bash\n$ npm install --save pollock\n# OR:\n$ bower install --save pollock\n```\n\nIf you want to simply download the file to be used in the browser you can find them below:\n\n* [Development Version](https://unpkg.com/pollock/dist/pollock.js) (4.6kb - [Source Map](https://unpkg.com/pollock/dist/pollock.js.map))\n* [Production Version](https://unpkg.com/pollock/dist/pollock.min.js) (723b - [Source Map](https://unpkg.com/pollock/dist/pollock.min.js.map))\n\n## API\n\nThe API couldn't be simpler and consists of a single function, `pollock`:\n\n``` javascript\npollock(type, methodName[, options])\n```\n\nThe most common use case is to add an abstract instance method to a type (i.e. it's prototype) which, when called,\nthrows an error:\n\n``` javascript\nclass GraphicObject {\n  constructor(x, y) {\n    this.x = x;\n    this.y = y;\n  }\n\n  moveTo(newX, newY) {\n    // ...\n  }\n}\n\npollock(GraphicObject, 'draw');\npollock(GraphicObject, 'resize');\n\nclass Circle extends GraphicObject {\n  draw() {\n    // ...\n  }\n\n  resize() {\n    // ...\n  }\n}\n\nclass Rectangle extends GraphicObject {\n  draw() {\n    // ...\n  }\n}\n```\n\nBy declaring the abstract methods `draw` and `resize`, it will make it much easier/quicker to discover cases where\nchildren of that type have not implemented those methods, but it's important to note it's only reported if/when the\nmethod is called.\n\n``` javascript\nconst circle = new Circle(0, 0);\ncircle.draw();\ncircle.resize();\n\nconst rect = new Rectangle(50, 50);\nrect.draw();\nrect.resize();\n//=\u003e Error(GraphicObject#resize abstract method is not implemented)\n```\n\nThe examples in this document are all using ECMAScript 2015 classes, which your code may not be. Don't worry though,\npollock works with ECMAScript Version 5 as well. ECMAScript 2015 classes were used mainly because they better\ndemonstrate the inheritance without the noise of different type extension mechanisms.\n\n### Static Methods\n\nWhile pollock creates instance methods by default, enabling the `static` option will result in the abstract method being\nassigned directly to the type instead, effectively making it static.\n\n``` javascript\nclass GraphicObject {\n  // ...\n}\n\npollock(GraphicObject, 'getEdges', { static: true });\n\nclass Circle extends GraphicObject {\n  static getEdges() {\n    return 1;\n  }\n\n  // ...\n}\n\nclass Rectangle extends GraphicObject {\n  // ...\n}\n```\n\nThis behaves exactly as you'd expect it to and the only difference is in the error message; the character separating the\ntype and method names is different. This is to help differentiate such cases while debugging as, in theory, a single\ntype could have two abstract methods with the same name; one instance and one static.\n\n``` javascript\nCircle.getEdges();\n//=\u003e 1\n\nRectangle.getEdges();\n//=\u003e Error(GraphicObject.getEdges abstract method is not implemented)\n```\n\n### Asynchronous Methods\n\nIn most cases, throwing an error as soon as the abstract method is called is best, regardless of whether the method is\nintended to be synchronous or asynchronous in nature. However, pollock is flexible and allows you to easily support two\nof the most common asynchronous patterns should you wish: callbacks and promises.\n\n#### Callback\n\nIn order to have the abstract method invoke a callback function with the error instead of it being thrown, you just need\nto specify the index of the callback argument via the `callback` option.\n\n``` javascript\nclass UserService {\n  getUserCount(callback) {\n    this.getUsers((error, users) =\u003e {\n      if (error) {\n        callback(error);\n      } else {\n        callback(null, users.length);\n      }\n    });\n  }\n}\n\npollock(UserService, 'getUser', { callback: 1 });\npollock(UserService, 'getUsers', { callback: 0 });\n\nclass UserServiceImpl extends UserService {\n  getUsers(callback) {\n    // ...\n  }\n}\n```\n\nNow the error will be passed to the specified callback function argument when invoked.\n\n``` javascript\nconst userService = new UserServiceImpl();\nuserService.getUser(123, (error) =\u003e {\n  //=\u003e Error(UserService#getUser abstract method is not implemented)\n});\n```\n\nIf the value of the `callback` option is negative (i.e. less than zero), then it will be applied to the end of argument\nlist passed to the abstract method. For example; to always treat the last argument as the callback function argument,\npass `-1`.\n\nIf the specified index is invalid or does not match an argument that is a function, then the abstract method will fall\nback on throwing the error instead.\n\n#### Promise\n\nFor the abstract method to return a ECMAScript 2015 `Promise` that has been rejected with the error instead of it being\nthrown, simply enable the `promise` option.\n\n``` javascript\nclass UserService {\n  getUserCount() {\n    return this.getUsers()\n      .then((users) =\u003e users.length);\n  }\n}\n\npollock(UserService, 'getUser', { promise: true });\npollock(UserService, 'getUsers', { promise: true });\n\nclass UserServiceImpl extends UserService {\n  getUser(id) {\n    // ...\n  }\n}\n```\n\nDone!\n\n``` javascript\nconst userService = new UserServiceImpl();\nuserService.getUsers()\n  .catch((error) =\u003e {\n    //=\u003e Error(UserService#getUsers abstract method is not implemented)\n  });\n```\n\nIf the current environment does not support ECMAScript 2015's `Promise`, which is tested by detecting whether it's in\nthe global scope, then the abstract method will fall back on throwing the error instead.\n\n### Custom Type Name\n\nThe type name that is reported in the error message can be controlled using the `typeName` option. The type name\nresolution occurs in the following order:\n\n1. `typeName` option, if specified\n2. `type.name` property value, if available\n3. `\"\u003canonymous\u003e\"` otherwise\n\nThis can be useful for cases where your code is minified and you don't want errors like \"p#lock abstract method is not\nimplemented\" or you're using a library/framework that creates the constructor function for you and results in the\nassigned name being lost/distorted.\n\n``` javascript\nconst GraphicObject = Nevis.extend({\n  // ...\n});\n\npollock(GraphicObject, 'draw', { typeName: 'GraphicObject' });\npollock(GraphicObject, 'resize', { typeName: 'GraphicObject' });\n```\n\n## Bugs\n\nIf you have any problems with pollock or would like to see changes currently in development you can do so\n[here](https://github.com/neocotic/pollock/issues).\n\n## Contributors\n\nIf you want to contribute, you're a legend! Information on how you can do so can be found in\n[CONTRIBUTING.md](https://github.com/neocotic/pollock/blob/master/CONTRIBUTING.md). We want your suggestions and pull\nrequests!\n\nA list of pollock contributors can be found in [AUTHORS.md](https://github.com/neocotic/pollock/blob/master/AUTHORS.md).\n\n## License\n\nCopyright © 2018 Alasdair Mercer\n\nSee [LICENSE.md](https://github.com/neocotic/pollock/raw/master/LICENSE.md) for more information on our MIT license.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneocotic%2Fpollock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fneocotic%2Fpollock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fneocotic%2Fpollock/lists"}