{"id":19346445,"url":"https://github.com/canjs/can-derive","last_synced_at":"2025-04-23T05:30:26.236Z","repository":{"id":33239107,"uuid":"36883435","full_name":"canjs/can-derive","owner":"canjs","description":"Derive live can.List and can.Maps from source lists.","archived":false,"fork":false,"pushed_at":"2019-06-13T23:12:27.000Z","size":9342,"stargazers_count":17,"open_issues_count":12,"forks_count":2,"subscribers_count":20,"default_branch":"master","last_synced_at":"2024-10-09T09:50:43.590Z","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/canjs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-06-04T16:48:49.000Z","updated_at":"2019-07-15T22:53:08.000Z","dependencies_parsed_at":"2022-08-17T22:45:21.940Z","dependency_job_id":null,"html_url":"https://github.com/canjs/can-derive","commit_stats":null,"previous_names":[],"tags_count":14,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-derive","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-derive/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-derive/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/canjs%2Fcan-derive/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/canjs","download_url":"https://codeload.github.com/canjs/can-derive/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":223909971,"owners_count":17223592,"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-10T04:10:23.089Z","updated_at":"2024-11-10T04:10:23.806Z","avatar_url":"https://github.com/canjs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# can-derive\n\n[![Build Status](https://travis-ci.org/canjs/can-derive.svg?branch=master)](https://travis-ci.org/canjs/can-derive)\n\n**can-derive** is a plugin that creates observable filtered lists that remain\nin sync with their original source list.\n\nFor example, a todo list might contain todo objects with a `completed` property.\nTraditionally `can.List.filter` enables you to create a new `can.List`\ncontaining only the \"completed\" todo objects. However, if the source list were\nto change in any way - for instance via an \"add\" or \"remove\" - the returned\n`can.List` may become an innaccurate representation of the source list.\nThe same filtered list of \"completed\" todo objects created\nwith `can-derive`'s `can.List.dFilter` would always be an accurate\nrepresentation of with the source list regardless of how it was manipulated.\n\n**can-derive** is ideal for cases where the source list contains at least\n10 items and is expected to be \"changed\" frequently (3 or more times).\n\nCheck out \u003ca href=\"http://canjs.github.io/can-derive/\" target=\"_blank\"\u003ethe demo\u003c/a\u003e.\n\n\u003c!-- START doctoc generated TOC please keep comment here to allow auto update --\u003e\n\u003c!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE --\u003e\n\n\n- [Install](#install)\n- [Use](#use)\n  - [With can.Map.define](#with-canmapdefine)\n  - [Accessing FilteredList values](#accessing-filteredlist-values)\n- [API](#api)\n  - [can.List](#canlist)\n    - [.dFilter()](#dfilter)\n  - [FilteredList](#filteredlist)\n    - [Inherited can.RBTreeList methods](#inherited-canrbtreelist-methods)\n    - [Disabled can.RBTreeList methods](#disabled-canrbtreelist-methods)\n- [Performance](#performance)\n  - [When to Use](#when-to-use)\n- [Contributing](#contributing)\n\n\u003c!-- END doctoc generated TOC please keep comment here to allow auto update --\u003e\n\n## Install\n\nUse npm to install `can-derive`:\n\n```\nnpm install can-derive --save\n```\n\n## Use\n\nUse `require` in Node/Browserify workflows to import the `can-derive` plugin\nlike:\n\n```js\nrequire('can');\nrequire('can-derive');\n```\n\nUse `define`, `require`, or `import` in [StealJS](http://stealjs.com/) workflows\nto import the `can-derive` plugin like:\n\n```js\nimport 'can';\nimport 'can-derive';\n```\n\nOnce you've imported `can-derive` into your project use\n`can.List.dFilter` to generate a derived list based on a `predicate` function.\nThe following example derives a list of \"completed\" items from a `can.List`\nof `todo` objects:\n\n```js\nvar todos = new can.List([\n    { name: 'Hop', complete: true },\n    { name: 'Skip', complete: false },\n    //...\n]);\n\nvar completed = todos.dFilter(function(todo) {\n    return todo.attr(\"complete\") === true;\n});\n```\n\nAny changes to `todos` will be propagated to the derived `completed`\nlist:\n\n```js\ncompleted.bind('add', function(ev, newItems) {\n    console.log(newItems.length, 'item(s) added');\n});\n\ntodos.push({ name: 'Jump', complete: true },\n    { name: 'Sleep', complete: false }); //-\u003e \"1 item(s) added\"\n```\n\n### With can.Map.define\n\nIf you're using the [can.Map.define\nplugin](http://canjs.com/docs/can.Map.prototype.define.html), you can define a\nderived list like so:\n\n```js\n{\n    define: {\n        todos: {\n            Value: can.List\n        },\n        completedTodos: {\n            get: function() {\n                return this.attr('todos').dFilter(function(todo){\n                    return todo.attr('complete') === true;\n                });\n            }\n        }\n    }\n}\n```\n\nNote: The `can-derive` plugin ensures that the define plugin's `get` method will\nnot observe \"length\" like it would a traditional [can.List](http://canjs.com/docs/can.List.html)\nwhen calling `.filter()`.\n\n\n### Accessing FilteredList values\n\nUnlike `can.List` and `Array`, indexes of a `FilteredList` **cannot** be\naccessed using bracket notation:\n\n```js\nfilteredList[1]; //-\u003e undefined\n```\n\nTo access a `FilteredList`'s values, use [`.attr()`](https://github.com/canjs/can-binarytree#attr):\n\n```js\nfilteredList.attr(); //-\u003e [\"a\", \"b\", \"c\"]\nfilteredList.attr(0); //-\u003e \"a\"\nfilteredList.attr(1); //-\u003e \"b\"\nfilteredList.attr(2); //-\u003e \"c\"\nfilteredList.attr('length'); //-\u003e 3\n```\n\nThis is due to the fact that a `FilteredList` inherits a [`can.RBTreeList`](https://github.com/canjs/can-binarytree#canrbtreelist)\nwhich stores its values in a [Red-black tree](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree)\nfor [performance](#performance) - rather than a series of numeric keys.\n\n\n## API\n\n### can.List\n\n#### .dFilter()\n\n`sourceList.filter(predicateFn) -\u003e FilteredList`\n\nSimilar to [`.filter()`](https://github.com/canjs/can-derive#filter) except\nthat the returned `FilteredList` is bound to `sourceList`.\n\nReturns a `FilteredList`.\n\n### FilteredList\n\n#### Inherited can.RBTreeList methods\n\nSince `FilteredList` inherits from [can.RBTreeList](https://github.com/canjs/can-binarytree#canrbtreelist),\nthe following methods are available:\n\n- [`.attr()`](https://github.com/canjs/can-binarytree#attr)\n- [`.each()`](https://github.com/canjs/can-binarytree#each)\n- [`.eachNode()`](https://github.com/canjs/can-binarytree#eachnode)\n- [`.filter()`](https://github.com/canjs/can-binarytree#filter)\n- [`.indexOf()`](https://github.com/canjs/can-binarytree#indexof)\n- [`.indexOfNode()`](https://github.com/canjs/can-binarytree#indexofnode)\n- [`.map()`](https://github.com/canjs/can-binarytree#map)\n- `.slice()` *(coming soon)*\n\n#### Disabled can.RBTreeList methods\n\nA `FilteredList` is bound to its source list and manipulted as it changes.\nBecause of this, it is read-only and the following `can.RBTreeList`\nmethods are disabled:\n\n- `.push()`\n- `.pop()`\n- `.removeAttr()`\n- `.replace()`\n- `.shift()`\n- `.splice()`\n- `.unshift()`\n\n## Performance\n\n`can-derive` optimizes for insertions and removals, completing them in `O(log n)`\ntime. This means that changes to the source list will automatically update the\nderived list in `O(log n)` time, compared to the standard `O(n)` time you would\nexpect in other implementations.\n\nIt does this by:\n\n- Keeping the derived list in a [Red-black tree](https://en.wikipedia.org/wiki/Red%E2%80%93black_tree)\n  modeled after an `Array`\n- Listening for additions or removals in the source list\n- Listening for predicate function result changes for any item\n\nThis algorithm was originally discussed in [this StackExchange\nthread](http://cs.stackexchange.com/questions/43447/order-preserving-update-of-a\n-sublist-of-a-list-of-mutable-objects-in-sublinear-t/44502#44502).\n\n### When to Use\n\nIn general, it is preferable to use `can-derive` over alternative approaches\nwhen:\n\n- Your source list contains 10 or more elements\n- You need to know how the filtered list changed, for instance when rendering\n  in the DOM.\n\n\n## Contributing\n\nTo set up your dev environment:\n\n1. Clone and fork this repo.\n2. Run `npm install`.\n3. Open `list/test.html` in your browser. Everything should pass.\n4. Run `npm test`. Everything should pass.\n5. Run `npm run-script build`. Everything should build ok\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanjs%2Fcan-derive","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcanjs%2Fcan-derive","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcanjs%2Fcan-derive/lists"}