{"id":22222008,"url":"https://github.com/fabiospampinato/context-keys","last_synced_at":"2025-07-27T16:32:12.405Z","repository":{"id":57206504,"uuid":"203024270","full_name":"fabiospampinato/context-keys","owner":"fabiospampinato","description":"Performant and feature rich library for managing context keys.","archived":false,"fork":false,"pushed_at":"2024-04-05T00:10:37.000Z","size":101,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-11-11T05:59:12.332Z","etag":null,"topics":["context","context-keys","keys"],"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/fabiospampinato.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,"governance":null,"roadmap":null,"authors":null,"dei":null},"funding":{"github":"fabiospampinato","custom":"https://www.paypal.me/fabiospampinato"}},"created_at":"2019-08-18T15:38:02.000Z","updated_at":"2023-12-11T16:11:38.000Z","dependencies_parsed_at":"2024-04-05T01:33:24.940Z","dependency_job_id":"6457aaa9-f3ed-48c7-98ce-a7c07e3e736b","html_url":"https://github.com/fabiospampinato/context-keys","commit_stats":{"total_commits":49,"total_committers":2,"mean_commits":24.5,"dds":"0.020408163265306145","last_synced_commit":"e4b1d238bff7e6d44d53a49e6ca0a8ef591ce47d"},"previous_names":[],"tags_count":19,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcontext-keys","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcontext-keys/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcontext-keys/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fabiospampinato%2Fcontext-keys/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fabiospampinato","download_url":"https://codeload.github.com/fabiospampinato/context-keys/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":227817017,"owners_count":17824200,"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":["context","context-keys","keys"],"created_at":"2024-12-02T23:16:31.758Z","updated_at":"2024-12-02T23:16:32.323Z","avatar_url":"https://github.com/fabiospampinato.png","language":"JavaScript","funding_links":["https://github.com/sponsors/fabiospampinato","https://www.paypal.me/fabiospampinato"],"categories":[],"sub_categories":[],"readme":"# Context Keys\n\nPerformant and feature rich library for managing context keys.\n\n## Features\n\n- **Universal**: this library works both in the browser and in Node.\n- **Performant**: this library is about as fast as it gets, and it has just 2 tiny first-party dependencies.\n- **Flexible**: context keys can be primitives, arrays or plain objects.\n- **Expressive**: expressions are written in a full-fledged subset of JavaScript, this allows you to write complex expressions like `isFoo \u0026\u0026 ( !isBar || settings.foo[3] === \"foo\" )`.\n- **Safe**: expressions are executed through [`safex`](https://github.com/fabiospampinato/safex), to ensure they are executed safely.\n- **Batching**: changes happening within a microtask are batched and coalesced together for performance automatically.\n\n## Install\n\n```sh\nnpm install --save context-keys\n```\n\n## Usage\n\nThe following interface is provided:\n\n```ts\ntype ChangeHandler = ( value: boolean ) =\u003e void;\ntype ChangeAllHandler = () =\u003e void;\ntype Disposer = () =\u003e void;\ntype Expression = string;\ntype Key = string;\ntype Value = unknown;\n\nclass ContextKeys {\n\n  // Create a new instance, optionally with some context keys\n  constructor ( keys?: Record\u003cKey, Value\u003e );\n\n  // Evaluate an expression to a boolean\n  eval ( expression: Expression ): boolean;\n\n  // Get the value of a context key\n  get ( key: Key ): Value | undefined;\n\n  // Get the values of all context keys\n  getAll (): Record\u003cKey, Value | undefined\u003e;\n\n  // Checks if a context key is defined\n  has ( key: Key ) : boolean;\n\n  // Remove single context key\n  remove ( key: Key ): void;\n\n  // Remove all context keys and change handlers\n  reset (): void;\n\n  // Add or update a context key\n  set ( key: Key, value: Value ): void;\n\n  // Register a callback which will be called whenever any context key changes\n  onChange ( handler: ChangeAllHandler ): Disposer;\n  // Register a callback which will be called whenever the value of the expression changes. Call the disposer to unregister the callback\n  onChange ( expression: Expr, handler: ChangeHandler ): Disposer;\n\n}\n```\n\nYou can use it like this:\n\n```ts\nimport ContextKeys from 'context-keys';\n\n// Create a new instance, with some context keys already\n\nconst ck = new ContextKeys ({\n  isFoo: true,\n  isBar: false\n});\n\n// Evaluate an expression\n\nck.eval ( 'isFoo' ); // =\u003e true\nck.eval ( 'isBar' ); // =\u003e false\nck.eval ( 'isBaz' ); // =\u003e undefined\nck.eval ( 'isFoo \u0026\u0026 isBar || 123' ); // =\u003e 123\n\n// Get the value of a context key\n\nck.get ( 'isFoo' ); // =\u003e true\nck.get ( 'isBar' ); // =\u003e false\nck.get ( 'isBaz' ); // =\u003e undefined\n\n// Get the values of all context keys\n\nck.getAll (); // =\u003e { isFoo: true, isBar: false }\n\n// Check if a context key is defined\n\nck.has ( 'isFoo' ); // =\u003e true\nck.has ( 'isBar' ); // =\u003e true\nck.has ( 'isBaz' ); // =\u003e undefined\n\n// Set the value of a context key\n\nck.set ( isBar, true );\nck.set ( isBaz, 123 );\n\n// Remove a context key\n\nck.remove ( 'isBaz' );\n\n// Remove all context keys and change handlers\n\nck.reset ();\n\n// Register a global onChange handler\n\nck.onChange ( () =\u003e {\n  console.log ( 'Some context key changed!' );\n});\n\n// Register an expression-specific onChange handler\n\nconst dispose = ck.onChange ( 'isFoo', value =\u003e {\n  console.log ( 'The result of the expression changed!' );\n});\n\ndispose (); // Dispose of that listener\n```\n\n## License\n\nMIT © Fabio Spampinato\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Fcontext-keys","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffabiospampinato%2Fcontext-keys","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffabiospampinato%2Fcontext-keys/lists"}