{"id":15405160,"url":"https://github.com/fnando/computed","last_synced_at":"2025-04-17T00:50:34.927Z","repository":{"id":23806477,"uuid":"27182689","full_name":"fnando/computed","owner":"fnando","description":"Computed properties for regular JavaScript objects.","archived":false,"fork":false,"pushed_at":"2016-03-29T03:48:06.000Z","size":30,"stargazers_count":7,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T05:51:12.442Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fnando.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-11-26T15:18:06.000Z","updated_at":"2016-12-02T11:52:53.000Z","dependencies_parsed_at":"2022-08-21T23:20:37.473Z","dependency_job_id":null,"html_url":"https://github.com/fnando/computed","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fcomputed","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fcomputed/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fcomputed/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fnando%2Fcomputed/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fnando","download_url":"https://codeload.github.com/fnando/computed/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249294742,"owners_count":21245993,"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-10-01T16:15:17.069Z","updated_at":"2025-04-17T00:50:34.903Z","avatar_url":"https://github.com/fnando.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Computed.js\n\n[![Build Status](https://travis-ci.org/fnando/computed.svg)](https://travis-ci.org/fnando/computed)\n\nComputed properties for regular JavaScript objects.\n\nThis project is heavily inspired by [Ember](http://emberjs.com).\n\n## Requirements\n\nYou may have to include [es5-shim](https://github.com/es-shims/es5-shim)\nand [es6-shim](https://github.com/paulmillr/es6-shim) depending on the\nbrowser you're targeting.\n\n## Installing\n\n- NPM: `npm install fnando/computed` (latest) or `npm install fnando/computed#v0.2.0` (specific version)\n- Bower: `bower install fnando/computed` (latest) or `npm install fnando/computed#v0.2.0` (specific version)\n\n## Usage\n\nImporting module:\n\n- Node.js: `const Computed = require('computed');`\n- AMD: `define(['computed'], function(Computed) {});`\n- Browser: `Computed`\n\n### Computed.and\n\nA computed property that performs a logical `and` on the original values.\nAll values use JavaScript's truthy/falsy checking, with\nexception of numbers (even zero is considered truthy).\n\n```js\nvar hamster = {\n  readyForCamp: Computed.and('hasTent', 'hasBackpack')\n};\n\nhamster.readyForCamp(); // false\nhamster.hasTent = true;\nhamster.readyForCamp(); // false\nhamster.hasBackpack = true;\nhamster.readyForCamp(); // true\n```\n\n### Computed.alias\n\nCreates a new property that is an alias for another property on an object.\n\n```js\nvar post = {\n  id: 'some-post',\n  slug: Computed.alias('id')\n};\n\npost.slug(); // some-post\npost.slug('a-new-post');\npost.id; // a-new-post\n```\n\n### Computed.any\n\nA computed property that returns the first truthy value from a list of\ndependent properties.\n\n```js\nvar hamster = {\n  hasClothes: Computed.any('hat', 'shirt')\n};\n\nhamster.hasClothes(); // null\nhamster.shirt = 'Hawaiian Shirt';\nhamster.hasClothes(); // 'Hawaiian Shirt'\n```\n\n### Computed.attributes\n\nA computed property that returns an object containing all listed\nproperties. Aliased as `Computed.attrs`.\n\n```js\nvar hamster = {\n  name: 'John',\n  age: 42,\n  salary: 1000,\n  attributes: Computed.attributes('name', 'age')\n};\n\nhamster.attributes(); // {name: 'John', age: 42}\n```\n\n### Computed.bool\n\nA computed property that converts the provided dependent property into a boolean value.\n\n```js\nvar hamster = {\n  hasBananas: Computed.bool('numBananas')\n};\n\nhamster.hasBananas(); // false\nhamster.numBananas = 0;\nhamster.hasBananas(); // false\nhamster.numBananas = 1;\nhamster.hasBananas(); // true\nhamster.numBananas = null;\nhamster.hasBananas(); // false\n```\n\n### Computed.collect\n\nA computed property that returns the array of values for the provided\ndependent properties.\n\n```js\nvar hamster = {\n  clothes: Computed.collect('hat', 'shirt')\n};\n\nhamster.clothes(); // [undefined, undefined]\nhamster.hat = 'Camp Hat';\nhamster.shirt = 'Camp Shirt';\nhamster.clothes(); // ['Camp Hat', 'Camp Shirt']\n```\n\n### Computed.empty\n\nA computed property that returns true if the value of the dependent\nproperty is null/undefined, an empty string, or empty array.\n\n```js\nvar todoList = {\n  todos: ['Unit Test', 'Documentation', 'Release'],\n  done: Computed.empty('todos')\n};\n\ntodoList.done(); // false\ntodoList.todos.length = 0;\ntodoList.done(); // true\n```\n\n### Computed.equal\n\nA computed property that returns true if the provided dependent property\nis equal to the given value.\n\n```js\nvar hamster = {\n  napTime: Computed.equal('state', 'sleepy')\n};\n\nhamster.napTime(); // false\nhamster.state = 'sleepy';\nhamster.napTime(); // true\nhamster.state = 'hungry';\nhamster.napTime(); // false\n```\n\n### Computed.filter\n\nFilters the array by the callback.\nThe callback method you provide should have the following signature: `function(item, index){}`.\n\n- `item` is the current item in the iteration.\n- `index` is the integer index of the current item in the iteration.\n\n```js\nvar hamster = {\n  chores: [\n    {name: 'cook', done: true},\n    {name: 'clean', done: true},\n    {name: 'write more unit tests', done: false}\n  ],\n\n  remainingChores: Computed.filter('chores', function(chore, index) {\n    return !chore.done;\n  })\n};\n\nhamster.remainingChores(); // [{name: 'write more unit tests', done: false}]\n```\n\n### Computed.filterBy\n\nFilters the array by the property and value.\n\n```js\nvar hamster = {\n  chores: [\n    {name: 'cook', done: true},\n    {name: 'clean', done: true},\n    {name: 'write more unit tests', done: false}\n  ],\n\n  remainingChores: Computed.filterBy('chores', 'done', false)\n};\n\nhamster.remainingChores(); // [{name: 'write more unit tests', done: false}]\n```\n\n### Computed.gt\n\nA computed property that returns true if the provided dependent property is\ngreater than the provided value.\n\n```js\nvar hamster = {\n  hasTooManyBananas: Computed.gt('numBananas', 10)\n};\n\nhamster.hasTooManyBananas(); // false\nhamster.numBananas = 3;\nhamster.hasTooManyBananas(); // false\nhamster.numBananas = 11;\nhamster.hasTooManyBananas(); // true\n```\n\n### Computed.gte\n\nA computed property that returns true if the provided dependent property is\ngreater than or equal to the provided value.\n\n```js\nvar hamster = {\n  hasTooManyBananas: Computed.gte('numBananas', 10)\n};\n\nhamster.hasTooManyBananas(); // false\nhamster.numBananas = 3;\nhamster.hasTooManyBananas(); // false\nhamster.numBananas = 10;\nhamster.hasTooManyBananas(); // true\n```\n\n### Computed.lt\n\nA computed property that returns true if the provided dependent property\nis less than the provided value.\n\n```js\nvar hamster = {\n  needsMoreBananas: Computed.lt('numBananas', 3)\n};\n\nhamster.needsMoreBananas(); // true\nhamster.numBananas = 3;\nhamster.needsMoreBananas(); // false\nhamster.numBananas = 2;\nhamster.needsMoreBananas(); // true\n```\n\n### Computed.lte\n\nA computed property that returns true if the provided dependent property\nis less than or equal to the provided value.\n\n```js\nvar hamster = {\n  needsMoreBananas: Computed.lte('numBananas', 3)\n};\n\nhamster.needsMoreBananas(); // true\nhamster.numBananas = 5;\nhamster.needsMoreBananas(); // false\nhamster.numBananas = 3;\nhamster.needsMoreBananas(); // true\n```\n\n### Computed.map\n\nReturns an array mapped via the callback\nThe callback method you provide should have the following signature: `function(item, index){}`\n\n- item is the current item in the iteration.\n- index is the integer index of the current item in the iteration.\n\n```js\nvar hamster = {\n  chores: ['clean', 'write more unit tests'],\n\n  excitingChores: Computed.map('chores', function(chore, index) {\n    return chore.toUpperCase() + '!';\n  })\n};\n\nhamster.excitingChores(); // ['CLEAN!', 'WRITE MORE UNIT TESTS!']\n```\n\n### Computed.mapBy\n\nMaps the array by the property and value.\n\n```js\nvar info = {\n  countries: [{id: 1, name: 'Brazil'}, {id: 2, name: 'USA'}],\n  names: Computed.mapBy('countries', 'name')\n};\n\nobject.names(); // ['Brazil', 'USA']\n```\n\n### Computed.match\n\nA computed property which matches the original value for the dependent\nproperty against a given RegExp, returning true if they values matches\nthe RegExp and false if it does not.\n\n```js\nvar user = {\n  hasValidEmail: Computed.match('email', /^.+@.+\\..+$/)\n};\n\nuser.hasValidEmail(); // false\nuser.email = '';\nuser.hasValidEmail(); // false\nuser.email = 'john@example.com';\nuser.hasValidEmail(); // true\n```\n\n### Computed.max\n\nA computed property that calculates the maximum value in the dependent\narray. This will return `-Infinity` when the dependent array is empty.\n\n```js\nvar object = {\n  numbers: [1,2,3,4,5],\n  maxNumber: Computed.max('numbers')\n};\n\nobject.maxNumber(); // 5\n```\n\n### Computed.min\n\nA computed property that calculates the minimum value in the dependent\narray. This will return `Infinity` when the dependent array is empty.\n\n```js\nvar object = {\n  numbers: [1,2,3,4,5],\n  minNumber: Computed.min('numbers')\n};\n\nobject.minNumber(); // 1\n```\n\n### Computed.none\n\nA computed property that returns true if the value of the dependent\nproperty is `null` or `undefined`.\n\n```js\nvar hamster = {\n  isHungry: Computed.none('food')\n};\n\nhamster.isHungry(); // true\nhamster.food = 'Banana';\nhamster.isHungry(); // false\nhamster.food = null;\nhamster.isHungry(); // true\n```\n\n### Computed.not\n\nA computed property that returns the inverse boolean value of the original\nvalue for the dependent property.\n\n```js\nvar user = Ember.Object.extend({\n  loggedIn: false,\n  isAnonymous: Computed.not('loggedIn')\n});\n\nuser.isAnonymous(); // true\nuser.loggedIn = true;\nuser.isAnonymous(); // false\n```\n\n### Computed.notEmpty\n\nA computed property that returns true if the value of the dependent\nproperty is NOT null, an empty string, or empty array.\n\n```js\nvar hamster = {\n  backpack: ['Food', 'Sleeping Bag', 'Tent'],\n  hasStuff: Computed.notEmpty('backpack')\n};\n\nhamster.hasStuff();           // true\nhamster.backpack.length = 0;  // []\nhamster.hasStuff();           // false\n```\n\n### Computed.or\n\nA computed property which performs a logical or on the original values\nfor the provided dependent properties.\n\n```js\nvar hamster = {\n  readyForRain: Computed.or('hasJacket', 'hasUmbrella')\n});\n\nhamster.readyForRain(); // false\nhamster.hasJacket = true;\nhamster.readyForRain(); // true\n```\n\n### Computed.sort\n\nA computed property which returns a new array with all the properties from\nthe first dependent array sorted based on a property or sort function.\n\nThe callback method you provide should have the following signature: `function(itemA, itemB)`.\n\n- itemA the first item to compare.\n- itemB the second item to compare.\n\nThis function should return negative number (e.g. -1) when itemA should\ncome before itemB. It should return positive number (e.g. 1) when itemA\nshould come after itemB. If the itemA and itemB are equal this function\nshould return 0.\n\nTherefore, if this function is comparing some numeric values, simple\nitemA - itemB can be used instead of series of if.\n\n```js\nvar info = {\n  names: ['John', 'Bob', 'Mary'],\n  sortedNames: Computed.sort('names', function(a, b){\n    if (a \u003e b) {\n      return 1;\n    } else if (a \u003c b) {\n      return -1;\n    }\n\n    return 1;\n  })\n};\n\ninfo.sortedNames(); // ['Bob', 'John', 'Mary']\n```\n\n### Computed.sortBy\n\nSorts the array by the property and value.\n\n```js\nvar info = {\n  users: [{name: 'John'}, {name: 'Bob'}, {name: 'Mary'}],\n  sortedNames: Computed.sortBy('users', 'name')\n};\n\ninfo.sortedNames(); // ['Bob', 'John', 'Mary']\n```\n\n### Computed.uniq\n\nA computed property which returns a new array with all the unique elements\nfrom one or more dependent arrays.\n\n```js\nvar hamster = {\n  fruits: ['banana', 'grape', 'kale', 'banana'],\n  uniqueFruits: Computed.uniq('fruits')\n};\n\nhamster.uniqueFruits(); // ['banana', 'grape', 'kale']\n```\n\n### Getters and setters\n\nTo set a property:\n\n```js\nvar user = {profile: {}};\n\nComputed.set(user, 'name', 'John Doe');\nComputed.set(user, 'profile.twitter', 'johndoe');\n```\n\nTo read a property, even when it's computed:\n\n```js\nComputed.get(user, 'name');\nComputed.get(user, 'profile.twitter');\n```\n\nThe next section covers how you can use Computed in constructor functions.\n\n### Constructor functions\n\nComputed can be used to define prototype properties.\n\n```js\n// Set a base constructor function, so we don't need\n// to do this every time.\nfunction Base() {}\nBase.prototype = {\n  get: Computed.get,\n  set: Computed.set\n};\n\n// Define the User constructor function.\nfunction User() {}\nUser.prototype = Object.create(Base.prototype);\nUser.prototype.hasConfirmedAccount = Computed.bool('accountConfirmedAt');\n\n// Instantiate a new user.\nvar user = new User();\nuser.get('hasConfirmedAccount'); // false\nuser.set('accountConfirmedAt', new Date());\nuser.get('hasConfirmedAccount'); // true\n```\n\n## Maintainer\n\n- Nando Vieira - \u003chttp://nandovieira.com.br\u003e\n\n## Contributing\n\nOnce you've made your great commits:\n\n1. [Fork](http://help.github.com/forking/) Computed.js\n2. Create a topic branch - `git checkout -b my_branch`\n3. Push to your branch - `git push origin my_branch`\n4. [Create an Issue](http://github.com/fnando/computed/issues) with a link to your branch\n5. That's it!\n\nPlease respect the indentation rules and code style.\nAnd use 2 spaces, not tabs. And don't touch the versioning thing.\n\n## Development\n\nMake sure you have [Phantom.js](http://phantomjs.org) installed.\n\n```\n$ npm install\n$ bower install\n$ npm run watch\n```\n\n### Running tests manually\n\n```\n$ npm test\n```\n\n## License\n\n(The MIT License)\n\nPermission is hereby granted, free of charge, to any person obtaining\na copy of this software and associated documentation files (the\n'Software'), to deal in the Software without restriction, including\nwithout limitation the rights to use, copy, modify, merge, publish,\ndistribute, sublicense, and/or sell copies of the Software, and to\npermit persons to whom the Software is furnished to do so, subject to\nthe following conditions:\n\nThe above copyright notice and this permission notice shall be\nincluded in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,\nEXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\nMERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\nIN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY\nCLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,\nTORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE\nSOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fcomputed","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffnando%2Fcomputed","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffnando%2Fcomputed/lists"}