{"id":20471386,"url":"https://github.com/statewalker/statewalker-adapters","last_synced_at":"2025-03-05T13:42:29.125Z","repository":{"id":63384553,"uuid":"567497568","full_name":"statewalker/statewalker-adapters","owner":"statewalker","description":null,"archived":false,"fork":false,"pushed_at":"2022-11-17T23:22:28.000Z","size":6,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-19T06:19:45.563Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/statewalker.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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":"2022-11-17T23:13:46.000Z","updated_at":"2022-11-17T23:14:25.000Z","dependencies_parsed_at":"2022-11-18T00:00:40.165Z","dependency_job_id":null,"html_url":"https://github.com/statewalker/statewalker-adapters","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-adapters","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-adapters/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-adapters/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/statewalker%2Fstatewalker-adapters/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/statewalker","download_url":"https://codeload.github.com/statewalker/statewalker-adapters/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242039035,"owners_count":20061922,"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-15T14:15:58.176Z","updated_at":"2025-03-05T13:42:29.103Z","avatar_url":"https://github.com/statewalker.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# @statewalker/adapters\n\nThis library implements the [Adapter pattern](https://en.wikipedia.org/wiki/Adapter_pattern) in JavaScript.\n\n\u003e Adapters allow dynamically adjust JavaScript objects to various contexts without changing objects nor contexts. Adapters implement \"late binding\" between objects and allows to create applications with high cohesion and low coupling between individual parts.\n\nExamples of usage of this pattern:\n\nExample 1: Show in a context menu file-specific operations - resize images, share files or apply spellchecker for texts. Completely different files - image, audio files or texts - can be adapted to add file-specific operations in the menu. \n\nExample 2: The same image can have different adapters in a file manager menu and in an image-editing interface (ex: update curves or luminosity).\n\nAll these requirements can be covered by using this simple adapter class.\n\n## Usage\n\n```js\n\n// Create an adapter registry\nconst registry = new Adapters();\n\n// Register adapters from one type to another:\nregistry.set(\"menu\", \"file\", { ... }); // An adapter for files\nregistry.set(\"menu\", \"configuration\", { ... }) // Configuration menu \nregistry.set(\"menu.context\", \"audio\", { ... }) // Register a audio-player specific operations in the menu\n... \n\n// Use the registered adapters:\nconst obj = ...; // An object for which we want to find an adapter for a specific context.\n                 // In this case the context is the menu.\nconst operations = registry.getAll(\"menu\", obj.type);\n// Do something for \n\n```\n\n## Example\n\n```js\n// Create a new adapters registry\nconst registry = new Adapters();\n\n// Register an adapter providing new menu operations for files: copy, delete and move:\nregistry.set(\"menu\", \"file\", new MenuItems([\n  { label : \"Copy\", action : copyFileAction },\n  { label : \"Delete\", action : deleteFileAction },\n  { label : \"Move\", action : moveFileAction },\n]))\n\n// Register image-specific operations - resize and share: \nregistry.set(\"menu\", \"file.image\", new MenuItems([\n  { label : \"Resize Image\", action : resizeImageAction },\n  { label : \"Share Image\", action : shareImageAction },\n]))\n\n// Add image-specific operations for a context menu - update image liminosity:\nregistry.set(\"menu.images\", \"file.image\", new MenuItems([\n  { label : \"Update Luminosity\", action : updateImageLuminosityAction },\n]))\n\n// Now we can use these adapters to show menus reflecting operations specific for files:\nconst file1 = ...; // Get file from somewhere with the type \"file.binary\"\nconst file1MenuItems = registry.getAll(\"menu\", file.type);\n// It will contains 3 operations: \"Copy\", \"Delete\", \"Move\"\n\nconst imageFile = ...;\nconst imageMenuItems = registry.getAll(\"menu\", imageFile.type);\n// The returned list of operations will contain the \"Copy\", \"Delete\", \"Move\" items\n// (like for all other files), but also the \"Resize Image\" and \"Share Image\" options.\n\n```\n\n## `Adapters` Class Methods\n\n* `constructor(separator = '.')` - constructor defines the separator character used to transform type strings to hierarchies\n\n* `set(from, to, adapter)` - registers an adapter from the type `from` to the type `to`. The `adapter` is the adapter instance. `from` and `to` parameters are strings representing hierarchy of the adaptable and target types.\n\n* `get(from, to, fromExact = false, toExact = false)` - returns an adapter from one class to another;\n  - if the `fromExact` parameter is `true` then this method return an adapter (if any) for the exact type defined by the first (`from` parameter)\n  - if the `toExact` parameter is `true` then this method returns an adapter for the exact target type (`to` type) and it do not try to expand the resulting type\n\n* `getAll(from, to, fromExact = false, toExact = false)` - returns all adapters from the initial to the target types; the meaning of the `fromExact` and `toExact` parameters is the same as for the `get(...)`method - they enable/disable types expansion\n\n* `remove(from, to)` - removes the registered adapter from the initial type to the target type\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-adapters","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstatewalker%2Fstatewalker-adapters","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstatewalker%2Fstatewalker-adapters/lists"}