{"id":20839071,"url":"https://github.com/maxhallinan/kontext","last_synced_at":"2025-05-08T21:35:09.646Z","repository":{"id":94143714,"uuid":"102652290","full_name":"maxhallinan/kontext","owner":"maxhallinan","description":"A higher-order function that proxies context to context-free functions.","archived":false,"fork":false,"pushed_at":"2017-10-04T18:58:14.000Z","size":78,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-11-13T14:51:27.082Z","etag":null,"topics":["higher-order-function"],"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/maxhallinan.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,"publiccode":null,"codemeta":null}},"created_at":"2017-09-06T19:56:29.000Z","updated_at":"2022-08-04T04:40:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"b7a903be-454c-4c34-935e-f081513277a7","html_url":"https://github.com/maxhallinan/kontext","commit_stats":{"total_commits":46,"total_committers":1,"mean_commits":46.0,"dds":0.0,"last_synced_commit":"a292fbb4fffe08c35e1a1c9697e3781e86659040"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxhallinan%2Fkontext","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxhallinan%2Fkontext/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxhallinan%2Fkontext/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/maxhallinan%2Fkontext/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/maxhallinan","download_url":"https://codeload.github.com/maxhallinan/kontext/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225110536,"owners_count":17422411,"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":["higher-order-function"],"created_at":"2024-11-18T01:12:16.868Z","updated_at":"2024-11-18T01:12:17.361Z","avatar_url":"https://github.com/maxhallinan.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# kontext\n[![Build Status](https://travis-ci.org/maxhallinan/kontext.svg?branch=master)](https://travis-ci.org/maxhallinan/kontext)\n[![Coverage Status](https://coveralls.io/repos/github/maxhallinan/kontext/badge.svg?branch=master)](https://coveralls.io/github/maxhallinan/kontext?branch=master)\n\nA higher-order function that proxies context to context free functions.\n\n\n## Install\n\n```\n$ yarn install kontext\n```\n\n\n## Usage\n\nProvide context values to context-free functions.\n\n```javascript\nimport kontext from 'kontext';\nimport { compose, log, prop, } from './util';\n\n// define a generic function\nconst greet = compose(\n  log,\n  prop(`greeting`),\n);\n\n// define a context\nfunction Greeter(opts = {}) {\n  this.greeting = opts.greeting;\n}\n// lift the generic function into the context\nGreeter.prototype.greet = kontext([ `greeting`, ])(greet);\n\nconst dog = new Greeter({\n  greeting: `Hello. This is Dog.`,\n});\ndog.greet(); // 'Hello. This is Dog.'\n```\n\nCompose context-free functions with a context setter to mutate the context.\n\n```javascript\nimport kontext from 'kontext';\nimport { add, compose, } from './util';\n\nconst count = prop(`count`);\nconst setCount = (count) =\u003e ({ count, });\nconst withCount = kontext([ `count`, ]);\n\nfunction Counter(opts = {}) {\n  this.count = opts.init || 0;\n}\n\n// compose generic functions with the context setter to mutate the context\nconst inc = (ctx, setCtx) =\u003e compose(\n  setCtx,\n  setCount,\n  add(1),\n  count,\n)(ctx);\nCounter.prototype.inc = withCount(inc);\n\n// create reusable logic that isn't coupled to `this`.\nconst skip = (n, ctx, setCtx) =\u003e compose(\n  setCtx,\n  setCount,\n  add(n),\n  count,\n)(ctx);\nCounter.prototype.skip = withCount(skip);\n\nconst counter = new Counter({ init: 1, });\ncounter.count; // 1\ncounter.inc();\ncounter.count; // 2\ncounter.skip(10);\ncounter.count; // 12\n```\n\n\n## API\n\n### kontext(ctxKeys)(baseFunction)\n\nTakes an array of keys and returns a higher-order function. Picks each key from\nthe context and provides the result to the base function.\n\n#### ctxKeys\n\nType: `Array k`\n\nAn array of keys to pick from the function context.\n\n\n#### baseFunction(...*, ctx, setCtx)\n\nThe function to lift into the context. The higher-order function appends `ctx`\nand `setCtx` to the arguments list of `baseFunction`.\n\n##### ctx\n\nType: `{k: *}`\n\nThe result of picking `ctxKeys` from the function context. The `ctx` object is a\ncollection of key/value pairs. Each `key` is an item in the `ctxKeys` array. Each\nvalue is the value of that key on the function's context. The value of a key\nthat is not found on the function context is `undefined` on the `ctx` object.\n`kontext` binds function values to the context.\n\n\n##### setCtx(props)\n\nType: `({k: *}) -\u003e {k: *}`\n\nUpdates the context with each of the provided key/value pairs. The values of\nexisting keys are overwritten. Any key in `props` which refers to an inherited\nproperty will become an own property when updated with `setCtx`.\n\n###### props\n\nType: `{k: *}`\n\nA collection of key/value pairs where each key is a context property to update\nand each value is the new value of that property.\n\n\n## License\n\nMIT © [Max Hallinan](https://github.com/maxhallinan)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxhallinan%2Fkontext","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmaxhallinan%2Fkontext","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmaxhallinan%2Fkontext/lists"}