├── .bowerrc ├── .editorconfig ├── .ember-cli ├── .eslintrc.js ├── .gitignore ├── .npmignore ├── .travis.yml ├── .watchmanconfig ├── LICENSE.md ├── README.md ├── addon ├── .gitkeep ├── index.js └── utils │ └── update.js ├── app ├── .gitkeep └── utils │ └── update.js ├── config ├── ember-try.js ├── environment.js └── release.js ├── ember-cli-build.js ├── index.d.ts ├── index.js ├── jsconfig.json ├── package.json ├── testem.js ├── tests ├── .eslintrc.js ├── acceptance │ ├── array-multiplier-test.js │ └── update-simple-counter-test.js ├── dummy │ ├── app │ │ ├── app.js │ │ ├── components │ │ │ ├── .gitkeep │ │ │ ├── array-multiplier.js │ │ │ └── simple-counter.js │ │ ├── controllers │ │ │ └── .gitkeep │ │ ├── helpers │ │ │ └── .gitkeep │ │ ├── index.html │ │ ├── models │ │ │ └── .gitkeep │ │ ├── resolver.js │ │ ├── router.js │ │ ├── routes │ │ │ ├── .gitkeep │ │ │ ├── application.js │ │ │ ├── counter.js │ │ │ └── multiplier.js │ │ ├── styles │ │ │ └── app.css │ │ └── templates │ │ │ ├── application.hbs │ │ │ ├── components │ │ │ ├── .gitkeep │ │ │ ├── array-multiplier.hbs │ │ │ └── simple-counter.hbs │ │ │ ├── counter.hbs │ │ │ └── multiplier.hbs │ ├── config │ │ ├── environment.js │ │ └── targets.js │ └── public │ │ ├── crossdomain.xml │ │ └── robots.txt ├── helpers │ ├── destroy-app.js │ ├── module-for-acceptance.js │ ├── resolver.js │ └── start-app.js ├── index.html ├── integration │ └── .gitkeep ├── pages │ ├── array-multiplier.js │ └── simple-counter.js ├── test-helper.js └── unit │ ├── .gitkeep │ └── utils │ ├── update-test.js │ └── update-with-pojo-test.js ├── vendor └── .gitkeep └── yarn.lock /.bowerrc: -------------------------------------------------------------------------------- 1 | { 2 | "directory": "bower_components", 3 | "analytics": false 4 | } 5 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # EditorConfig helps developers define and maintain consistent 2 | # coding styles between different editors and IDEs 3 | # editorconfig.org 4 | 5 | root = true 6 | 7 | 8 | [*] 9 | end_of_line = lf 10 | charset = utf-8 11 | trim_trailing_whitespace = true 12 | insert_final_newline = true 13 | indent_style = space 14 | indent_size = 2 15 | 16 | [*.hbs] 17 | insert_final_newline = false 18 | 19 | [*.{diff,md}] 20 | trim_trailing_whitespace = false 21 | -------------------------------------------------------------------------------- /.ember-cli: -------------------------------------------------------------------------------- 1 | { 2 | /** 3 | Ember CLI sends analytics information by default. The data is completely 4 | anonymous, but there are times when you might want to disable this behavior. 5 | 6 | Setting `disableAnalytics` to true will prevent any data from being sent. 7 | */ 8 | "disableAnalytics": false 9 | } 10 | -------------------------------------------------------------------------------- /.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | root: true, 3 | parserOptions: { 4 | ecmaVersion: 2017, 5 | sourceType: 'module' 6 | }, 7 | extends: 'eslint:recommended', 8 | env: { 9 | browser: true 10 | }, 11 | rules: { 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # See https://help.github.com/ignore-files/ for more about ignoring files. 2 | 3 | # compiled output 4 | /dist 5 | /tmp 6 | 7 | # dependencies 8 | /node_modules 9 | /bower_components 10 | 11 | # misc 12 | /.sass-cache 13 | /connect.lock 14 | /coverage/* 15 | /libpeerconnection.log 16 | npm-debug.log* 17 | yarn-error.log 18 | testem.log 19 | 20 | # ember-try 21 | .node_modules.ember-try/ 22 | bower.json.ember-try 23 | package.json.ember-try 24 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | /bower_components 2 | /config/ember-try.js 3 | /dist 4 | /tests 5 | /tmp 6 | **/.gitkeep 7 | .bowerrc 8 | .editorconfig 9 | .ember-cli 10 | .gitignore 11 | .eslintrc.js 12 | .watchmanconfig 13 | .travis.yml 14 | bower.json 15 | ember-cli-build.js 16 | testem.js 17 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | --- 2 | language: node_js 3 | node_js: 4 | # we recommend testing addons with the same minimum supported node version as Ember CLI 5 | # so that your addon works for all apps 6 | - "lts/*" 7 | 8 | sudo: false 9 | dist: trusty 10 | 11 | addons: 12 | chrome: stable 13 | 14 | cache: 15 | yarn: true 16 | 17 | env: 18 | global: 19 | # See https://git.io/vdao3 for details. 20 | - JOBS=1 21 | matrix: 22 | # we recommend new addons test the current and previous LTS 23 | # as well as latest stable release (bonus points to beta/canary) 24 | - EMBER_TRY_SCENARIO=ember-lts-2.8 25 | - EMBER_TRY_SCENARIO=ember-lts-2.12 26 | - EMBER_TRY_SCENARIO=ember-release 27 | - EMBER_TRY_SCENARIO=ember-beta 28 | - EMBER_TRY_SCENARIO=ember-canary 29 | - EMBER_TRY_SCENARIO=ember-default 30 | 31 | matrix: 32 | fast_finish: true 33 | allow_failures: 34 | - env: EMBER_TRY_SCENARIO=ember-canary 35 | 36 | before_install: 37 | - curl -o- -L https://yarnpkg.com/install.sh | bash 38 | - export PATH=$HOME/.yarn/bin:$PATH 39 | 40 | install: 41 | - yarn install --no-lockfile --non-interactive 42 | 43 | script: 44 | # Usually, it's ok to finish the test scenario without reverting 45 | # to the addon's original dependency state, skipping "cleanup". 46 | - node_modules/.bin/ember try:one $EMBER_TRY_SCENARIO --skip-cleanup 47 | -------------------------------------------------------------------------------- /.watchmanconfig: -------------------------------------------------------------------------------- 1 | { 2 | "ignore_dirs": ["tmp", "dist"] 3 | } 4 | -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ember-object-update 2 | 3 | Update Ember objects with ease 4 | 5 | ## Installation 6 | 7 | `ember install ember-object-update` 8 | 9 | Ever get tired of dealing with `Ember.get` and `Ember.set` when you're trying to update properties on an object, or component? 10 | 11 | Does this look familiar? 12 | 13 | ```js 14 | Ember.set(obj, 'name', Ember.get(obj, 'name').capitalize()) 15 | ``` 16 | 17 | Or this? 18 | 19 | ```js 20 | actions: { 21 | doubleAllValues() { 22 | let oldValue = Ember.get(this, 'someArray'); 23 | let newValue = oldValue.map(v => v * 2) 24 | Ember.set(this, 'someArray', newValue) 25 | } 26 | } 27 | ``` 28 | 29 | If you find yourself wishing there was an easier way to make these kinds of updates, then `ember-object-update` is for you! 30 | 31 | `ember-object-update` is a simple add-on that (currently) provides a single utility function: `update`. The `update` function lets you use a function to compute a new value for a property *and* update the property all with one function. 32 | 33 | `update` accepts three arguments: 34 | - *obj*: The object you want to update 35 | - *path*: A string that contains the path to the value you want to update, e.g. `names.nickname` 36 | - *updateFn*: A function whose first argument is the **current** value of the property you're updating, and whose result is the **new** value you'd like to set. 37 | 38 | ### Examples 39 | 40 | This'll probably be easier if you see it in action, so check out the examples below: 41 | 42 | ### Doubling all the values in an array property 43 | ```js 44 | import update from 'ember-object-update'; 45 | 46 | let obj = Ember.Object.create({ 47 | a: [1, 2, 3] 48 | }); 49 | }); 50 | 51 | // The old way 52 | Ember.set(obj, 'a', Ember.get(obj, 'a').map(v => v * 2)); // [2, 4, 6] 53 | 54 | // Using the `update` method 55 | update(obj, 'a', a => a.map(v => v * 2)); 56 | ``` 57 | 58 | Also, it works in components! 59 | 60 | ### Updating component properties from an action 61 | ```js 62 | import update from 'ember-object-update'; 63 | 64 | export default Ember.Component.extend({ 65 | layout, 66 | 67 | numClicks: 0, 68 | 69 | actions: { 70 | increment() { 71 | update(this, 'numClicks', v => v + 1) 72 | }, 73 | 74 | decrement() { 75 | update(this, 'numClicks', v => v - 1) 76 | } 77 | } 78 | }); 79 | ``` 80 | 81 | ### Crazy nested things (plus array indexes!) 82 | ```js 83 | import update from 'ember-object-update'; 84 | 85 | let crazyNestedPOJO = { 86 | a: { 87 | b: [ 88 | { 89 | c: "some deeply nested value" 90 | } 91 | ] 92 | } 93 | }; 94 | 95 | update(crazyNestedPOJO, 'a.b.0.c', oldValue => oldValue.toUpperCase()); 96 | ``` 97 | 98 | ### Closing over local variables in your updater function 99 | ```js 100 | import update from 'ember-object-update'; 101 | 102 | actions: { 103 | multiply(multiplier) { 104 | // close over the `multiplier` value when pass our updater function into `update` 105 | update(this, 'collection', collection => collection.map(v => v * multiplier)); 106 | } 107 | } 108 | ``` 109 | 110 | ## Running the dummy app 111 | 112 | * `ember serve` 113 | * Visit your app at [http://localhost:4200](http://localhost:4200). 114 | 115 | ## Running Tests 116 | 117 | * `npm test` (Runs `ember try:each` to test your addon against multiple Ember versions) 118 | * `ember test` 119 | * `ember test --server` 120 | 121 | ## Building 122 | 123 | * `ember build` 124 | 125 | For more information on using ember-cli, visit [http://ember-cli.com/](http://ember-cli.com/). 126 | -------------------------------------------------------------------------------- /addon/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/addon/.gitkeep -------------------------------------------------------------------------------- /addon/index.js: -------------------------------------------------------------------------------- 1 | export { default } from 'ember-object-update/utils/update'; 2 | -------------------------------------------------------------------------------- /addon/utils/update.js: -------------------------------------------------------------------------------- 1 | import { set, get } from '@ember/object'; 2 | 3 | // Usage: update(person, 'name', (name) => name.toUpperCase()) 4 | export default function update(obj, key, updateFn) { 5 | let property = get(obj, key); 6 | let newValue = updateFn(property); 7 | set(obj, key, newValue); 8 | return newValue; 9 | } 10 | -------------------------------------------------------------------------------- /app/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/app/.gitkeep -------------------------------------------------------------------------------- /app/utils/update.js: -------------------------------------------------------------------------------- 1 | export { default } from 'ember-object-update/utils/update'; 2 | -------------------------------------------------------------------------------- /config/ember-try.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | module.exports = { 3 | scenarios: [ 4 | { 5 | name: 'ember-lts-2.8', 6 | bower: { 7 | dependencies: { 8 | 'ember': 'components/ember#lts-2-8' 9 | }, 10 | resolutions: { 11 | 'ember': 'lts-2-8' 12 | } 13 | }, 14 | npm: { 15 | devDependencies: { 16 | 'ember-source': null 17 | } 18 | } 19 | }, 20 | { 21 | name: 'ember-lts-2.12', 22 | npm: { 23 | devDependencies: { 24 | 'ember-source': '~2.12.0' 25 | } 26 | } 27 | }, 28 | { 29 | name: 'ember-release', 30 | bower: { 31 | dependencies: { 32 | 'ember': 'components/ember#release' 33 | }, 34 | resolutions: { 35 | 'ember': 'release' 36 | } 37 | }, 38 | npm: { 39 | devDependencies: { 40 | 'ember-source': null 41 | } 42 | } 43 | }, 44 | { 45 | name: 'ember-beta', 46 | bower: { 47 | dependencies: { 48 | 'ember': 'components/ember#beta' 49 | }, 50 | resolutions: { 51 | 'ember': 'beta' 52 | } 53 | }, 54 | npm: { 55 | devDependencies: { 56 | 'ember-source': null 57 | } 58 | } 59 | }, 60 | { 61 | name: 'ember-canary', 62 | bower: { 63 | dependencies: { 64 | 'ember': 'components/ember#canary' 65 | }, 66 | resolutions: { 67 | 'ember': 'canary' 68 | } 69 | }, 70 | npm: { 71 | devDependencies: { 72 | 'ember-source': null 73 | } 74 | } 75 | }, 76 | { 77 | name: 'ember-default', 78 | npm: { 79 | devDependencies: {} 80 | } 81 | } 82 | ] 83 | }; 84 | -------------------------------------------------------------------------------- /config/environment.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | 'use strict'; 3 | 4 | module.exports = function(/* environment, appConfig */) { 5 | return { }; 6 | }; 7 | -------------------------------------------------------------------------------- /config/release.js: -------------------------------------------------------------------------------- 1 | /* jshint node:true */ 2 | // var RSVP = require('rsvp'); 3 | 4 | // For details on each option run `ember help release` 5 | module.exports = { 6 | // local: true, 7 | remote: 'https://github.com/cafreeman/ember-object-update.git', 8 | annotation: "Release %@", 9 | message: "Bumped version to %@", 10 | // manifest: [ 'package.json', 'bower.json', 'someconfig.json' ], 11 | publish: true, 12 | // strategy: 'date', 13 | // format: 'YYYY-MM-DD', 14 | timezone: 'America/Chicago', 15 | // 16 | // beforeCommit: function(project, versions) { 17 | // return new RSVP.Promise(function(resolve, reject) { 18 | // // Do custom things here... 19 | // }); 20 | // } 21 | }; 22 | -------------------------------------------------------------------------------- /ember-cli-build.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | 'use strict'; 3 | 4 | const EmberAddon = require('ember-cli/lib/broccoli/ember-addon'); 5 | 6 | module.exports = function(defaults) { 7 | let app = new EmberAddon(defaults, { 8 | // Add options here 9 | }); 10 | 11 | /* 12 | This build file specifies the options for the dummy test app of this 13 | addon, located in `/tests/dummy` 14 | This build file does *not* influence how the addon or the app using it 15 | behave. You most likely want to be modifying `./index.js` or app's build file 16 | */ 17 | 18 | return app.toTree(); 19 | }; 20 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | // Type definitions for ember-object-update 0.5 2 | // Project: https://github.com/cafreeman/ember-object-update 3 | // Definitions by: Chris Krycho 4 | // Definitions: https://github.com/cafreeman/ember-object-update 5 | // TypeScript Version: 2.4 6 | 7 | import ComputedProperty from '@ember/object/computed'; 8 | 9 | type ComputedProperties = { [K in keyof T]: ComputedProperty | T[K] }; 10 | 11 | // function get(obj: ComputedProperties, key: K): T[K]; 12 | 13 | export default function update( 14 | obj: ComputedProperties, 15 | key: K, 16 | updateFn: (property: T[K]) => T[K] 17 | ): T[K]; 18 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | 'use strict'; 3 | 4 | module.exports = { 5 | name: 'ember-object-update' 6 | }; 7 | -------------------------------------------------------------------------------- /jsconfig.json: -------------------------------------------------------------------------------- 1 | {"compilerOptions":{"target":"es6","experimentalDecorators":true},"exclude":["node_modules","bower_components","tmp","vendor",".git","dist"]} -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ember-object-update", 3 | "version": "0.5.1", 4 | "description": "Update Ember objects with ease", 5 | "keywords": [ 6 | "ember-addon", 7 | "object", 8 | "ember", 9 | "emberjs", 10 | "utils", 11 | "functional" 12 | ], 13 | "license": "MIT", 14 | "author": "Chris Freeman", 15 | "directories": { 16 | "doc": "doc", 17 | "test": "tests" 18 | }, 19 | "repository": "https://github.com/cafreeman/ember-object-update", 20 | "scripts": { 21 | "build": "ember build", 22 | "start": "ember server", 23 | "test": "ember try:each" 24 | }, 25 | "dependencies": { 26 | "ember-cli-babel": "^6.6.0" 27 | }, 28 | "devDependencies": { 29 | "broccoli-asset-rev": "^2.4.5", 30 | "ember-ajax": "^3.0.0", 31 | "ember-cli": "~2.16.2", 32 | "ember-cli-chai": "^0.4.0", 33 | "ember-cli-dependency-checker": "^2.0.0", 34 | "ember-cli-eslint": "^4.0.0", 35 | "ember-cli-htmlbars": "^2.0.1", 36 | "ember-cli-htmlbars-inline-precompile": "^1.0.0", 37 | "ember-cli-inject-live-reload": "^1.4.1", 38 | "ember-cli-mocha": "^0.14.4", 39 | "ember-cli-page-object": "^1.13.0-alpha.1", 40 | "ember-cli-shims": "^1.1.0", 41 | "ember-cli-sri": "^2.1.0", 42 | "ember-cli-uglify": "^2.0.0", 43 | "ember-disable-prototype-extensions": "^1.1.2", 44 | "ember-export-application-global": "^2.0.0", 45 | "ember-load-initializers": "^1.0.0", 46 | "ember-resolver": "^4.0.0", 47 | "ember-source": "~2.16.0", 48 | "ember-welcome-page": "^3.0.0", 49 | "loader.js": "^4.2.3" 50 | }, 51 | "engines": { 52 | "node": "^4.5 || 6.* || >= 7.*" 53 | }, 54 | "ember-addon": { 55 | "configPath": "tests/dummy/config" 56 | }, 57 | "types": "./index.d.ts" 58 | } 59 | -------------------------------------------------------------------------------- /testem.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | module.exports = { 3 | test_page: 'tests/index.html?hidepassed', 4 | disable_watching: true, 5 | launch_in_ci: [ 6 | 'Chrome' 7 | ], 8 | launch_in_dev: [ 9 | 'Chrome' 10 | ], 11 | browser_args: { 12 | Chrome: { 13 | mode: 'ci', 14 | args: [ 15 | '--disable-gpu', 16 | '--headless', 17 | '--remote-debugging-port=9222', 18 | '--window-size=1440,900' 19 | ] 20 | }, 21 | } 22 | }; 23 | -------------------------------------------------------------------------------- /tests/.eslintrc.js: -------------------------------------------------------------------------------- 1 | module.exports = { 2 | env: { 3 | embertest: true 4 | } 5 | }; 6 | -------------------------------------------------------------------------------- /tests/acceptance/array-multiplier-test.js: -------------------------------------------------------------------------------- 1 | /* jshint expr:true */ 2 | import { 3 | describe, 4 | it, 5 | beforeEach, 6 | afterEach 7 | } from 'mocha'; 8 | import { expect } from 'chai'; 9 | import startApp from '../helpers/start-app'; 10 | import destroyApp from '../helpers/destroy-app'; 11 | import arrayMultiplier from '../pages/array-multiplier'; 12 | 13 | describe('Acceptance: ArrayMultiplier', function() { 14 | let application; 15 | 16 | beforeEach(function() { 17 | application = startApp(); 18 | arrayMultiplier.visit(); 19 | }); 20 | 21 | afterEach(function() { 22 | destroyApp(application); 23 | }); 24 | 25 | it('starts with the right values', function() { 26 | expect(arrayMultiplier.multiplier.listItems()).to.deep.equal([1, 2, 3]); 27 | }); 28 | 29 | describe('entering a multiplier and clicking calculate', function() { 30 | beforeEach(function() { 31 | arrayMultiplier.multiplier.input(4); 32 | arrayMultiplier.multiplier.calculate(); 33 | }); 34 | 35 | it('correctly multiples all the values by 4', function() { 36 | expect(arrayMultiplier.multiplier.listItems()).to.deep.equal([4, 8, 12]); 37 | }); 38 | }); 39 | }); 40 | -------------------------------------------------------------------------------- /tests/acceptance/update-simple-counter-test.js: -------------------------------------------------------------------------------- 1 | /* jshint expr:true */ 2 | import { 3 | describe, 4 | it, 5 | beforeEach, 6 | afterEach 7 | } from 'mocha'; 8 | import { expect } from 'chai'; 9 | import startApp from '../helpers/start-app'; 10 | import destroyApp from '../helpers/destroy-app'; 11 | import simpleCounter from '../pages/simple-counter'; 12 | 13 | describe('Acceptance: UpdateSimpleCounter', function() { 14 | let application; 15 | 16 | beforeEach(function() { 17 | application = startApp(); 18 | simpleCounter.visit(); 19 | }); 20 | 21 | afterEach(function() { 22 | destroyApp(application); 23 | }); 24 | 25 | it('starts with the right value', function() { 26 | expect(simpleCounter.counter.numClicks).to.equal('0'); 27 | }); 28 | 29 | describe('clicking the increment button', function() { 30 | beforeEach(function() { 31 | simpleCounter.counter.increment(); 32 | }); 33 | 34 | it('increases the value by 1', function() { 35 | expect(simpleCounter.counter.numClicks).to.equal('1'); 36 | }); 37 | 38 | describe('clicking the decrement button', function() { 39 | beforeEach(function() { 40 | simpleCounter.counter.decrement(); 41 | }); 42 | 43 | it('goes back to 0', function() { 44 | expect(simpleCounter.counter.numClicks).to.equal('0'); 45 | }); 46 | }); 47 | }); 48 | 49 | describe('clicking the decrement button', function() { 50 | beforeEach(function() { 51 | simpleCounter.counter.decrement(); 52 | }); 53 | 54 | it('decreases the value by 1', function() { 55 | expect(simpleCounter.counter.numClicks).to.equal('-1'); 56 | }); 57 | }); 58 | }); 59 | -------------------------------------------------------------------------------- /tests/dummy/app/app.js: -------------------------------------------------------------------------------- 1 | import Application from '@ember/application'; 2 | import Resolver from './resolver'; 3 | import loadInitializers from 'ember-load-initializers'; 4 | import config from './config/environment'; 5 | 6 | const App = Application.extend({ 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix, 9 | Resolver 10 | }); 11 | 12 | loadInitializers(App, config.modulePrefix); 13 | 14 | export default App; 15 | -------------------------------------------------------------------------------- /tests/dummy/app/components/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/components/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/components/array-multiplier.js: -------------------------------------------------------------------------------- 1 | import Component from '@ember/component'; 2 | import layout from '../templates/components/array-multiplier'; 3 | import update from 'ember-object-update/utils/update'; 4 | 5 | export default Component.extend({ 6 | layout, 7 | 8 | classNames: ['array-multiplier'], 9 | 10 | multiplier: 0, 11 | 12 | init() { 13 | this._super(...arguments); 14 | this.set('collection', [1, 2, 3]); 15 | }, 16 | 17 | actions: { 18 | multiply(multiplier) { 19 | update(this, 'collection', collection => collection.map(v => v * multiplier)); 20 | } 21 | } 22 | }); 23 | -------------------------------------------------------------------------------- /tests/dummy/app/components/simple-counter.js: -------------------------------------------------------------------------------- 1 | import Component from '@ember/component'; 2 | import layout from '../templates/components/simple-counter'; 3 | import update from 'ember-object-update'; 4 | 5 | export default Component.extend({ 6 | layout, 7 | 8 | classNames: ['simple-counter'], 9 | 10 | numClicks: 0, 11 | 12 | actions: { 13 | increment() { 14 | update(this, 'numClicks', v => v + 1) 15 | }, 16 | 17 | decrement() { 18 | update(this, 'numClicks', v => v - 1) 19 | } 20 | } 21 | }); 22 | -------------------------------------------------------------------------------- /tests/dummy/app/controllers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/controllers/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/helpers/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/helpers/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | 12 | 13 | 14 | 15 | {{content-for "head-footer"}} 16 | 17 | 18 | {{content-for "body"}} 19 | 20 | 21 | 22 | 23 | {{content-for "body-footer"}} 24 | 25 | 26 | -------------------------------------------------------------------------------- /tests/dummy/app/models/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/models/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from 'ember-resolver'; 2 | 3 | export default Resolver; 4 | -------------------------------------------------------------------------------- /tests/dummy/app/router.js: -------------------------------------------------------------------------------- 1 | import EmberRouter from '@ember/routing/router'; 2 | import config from './config/environment'; 3 | 4 | const Router = EmberRouter.extend({ 5 | location: config.locationType, 6 | rootURL: config.rootURL 7 | }); 8 | 9 | Router.map(function() { 10 | this.route('counter'); 11 | this.route('multiplier'); 12 | }); 13 | 14 | export default Router; 15 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/routes/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/routes/application.js: -------------------------------------------------------------------------------- 1 | import Route from '@ember/routing/route'; 2 | 3 | export default Route.extend({ 4 | }); 5 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/counter.js: -------------------------------------------------------------------------------- 1 | import Route from '@ember/routing/route'; 2 | 3 | export default Route.extend({ 4 | }); 5 | -------------------------------------------------------------------------------- /tests/dummy/app/routes/multiplier.js: -------------------------------------------------------------------------------- 1 | import Route from '@ember/routing/route'; 2 | 3 | export default Route.extend({ 4 | }); 5 | -------------------------------------------------------------------------------- /tests/dummy/app/styles/app.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/styles/app.css -------------------------------------------------------------------------------- /tests/dummy/app/templates/application.hbs: -------------------------------------------------------------------------------- 1 | {{link-to 'Counter' 'counter'}} 2 | {{link-to 'Multiplier' 'multiplier'}} 3 | 4 |
5 |
6 |
7 | 8 | {{outlet}} 9 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/dummy/app/templates/components/.gitkeep -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/array-multiplier.hbs: -------------------------------------------------------------------------------- 1 |
    2 | {{#each collection as |item|}} 3 |
  • {{item}}
  • 4 | {{/each}} 5 |
6 | 7 | {{input class="array-multiplier-input" type="text" value=multiplier}} 8 | 9 | 10 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/components/simple-counter.hbs: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |
5 |
6 | 7 | Number of clicks: {{numClicks}} 8 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/counter.hbs: -------------------------------------------------------------------------------- 1 | {{simple-counter}} 2 | -------------------------------------------------------------------------------- /tests/dummy/app/templates/multiplier.hbs: -------------------------------------------------------------------------------- 1 | {{array-multiplier}} 2 | -------------------------------------------------------------------------------- /tests/dummy/config/environment.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | 'use strict'; 3 | 4 | module.exports = function(environment) { 5 | let ENV = { 6 | modulePrefix: 'dummy', 7 | environment, 8 | rootURL: '/', 9 | locationType: 'auto', 10 | EmberENV: { 11 | FEATURES: { 12 | // Here you can enable experimental features on an ember canary build 13 | // e.g. 'with-controller': true 14 | }, 15 | EXTEND_PROTOTYPES: { 16 | // Prevent Ember Data from overriding Date.parse. 17 | Date: false 18 | } 19 | }, 20 | 21 | APP: { 22 | // Here you can pass flags/options to your application instance 23 | // when it is created 24 | } 25 | }; 26 | 27 | if (environment === 'development') { 28 | // ENV.APP.LOG_RESOLVER = true; 29 | // ENV.APP.LOG_ACTIVE_GENERATION = true; 30 | // ENV.APP.LOG_TRANSITIONS = true; 31 | // ENV.APP.LOG_TRANSITIONS_INTERNAL = true; 32 | // ENV.APP.LOG_VIEW_LOOKUPS = true; 33 | } 34 | 35 | if (environment === 'test') { 36 | // Testem prefers this... 37 | ENV.locationType = 'none'; 38 | 39 | // keep test console output quieter 40 | ENV.APP.LOG_ACTIVE_GENERATION = false; 41 | ENV.APP.LOG_VIEW_LOOKUPS = false; 42 | 43 | ENV.APP.rootElement = '#ember-testing'; 44 | } 45 | 46 | if (environment === 'production') { 47 | // here you can enable a production-specific feature 48 | } 49 | 50 | return ENV; 51 | }; 52 | -------------------------------------------------------------------------------- /tests/dummy/config/targets.js: -------------------------------------------------------------------------------- 1 | /* eslint-env node */ 2 | module.exports = { 3 | browsers: [ 4 | 'ie 9', 5 | 'last 1 Chrome versions', 6 | 'last 1 Firefox versions', 7 | 'last 1 Safari versions' 8 | ] 9 | }; 10 | -------------------------------------------------------------------------------- /tests/dummy/public/crossdomain.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 15 | 16 | -------------------------------------------------------------------------------- /tests/dummy/public/robots.txt: -------------------------------------------------------------------------------- 1 | # http://www.robotstxt.org 2 | User-agent: * 3 | Disallow: 4 | -------------------------------------------------------------------------------- /tests/helpers/destroy-app.js: -------------------------------------------------------------------------------- 1 | import { run } from '@ember/runloop'; 2 | 3 | export default function destroyApp(application) { 4 | run(application, 'destroy'); 5 | } 6 | -------------------------------------------------------------------------------- /tests/helpers/module-for-acceptance.js: -------------------------------------------------------------------------------- 1 | import { module } from 'qunit'; 2 | import { resolve } from 'rsvp'; 3 | import startApp from '../helpers/start-app'; 4 | import destroyApp from '../helpers/destroy-app'; 5 | 6 | export default function(name, options = {}) { 7 | module(name, { 8 | beforeEach() { 9 | this.application = startApp(); 10 | 11 | if (options.beforeEach) { 12 | return options.beforeEach.apply(this, arguments); 13 | } 14 | }, 15 | 16 | afterEach() { 17 | let afterEach = options.afterEach && options.afterEach.apply(this, arguments); 18 | return resolve(afterEach).then(() => destroyApp(this.application)); 19 | } 20 | }); 21 | } 22 | -------------------------------------------------------------------------------- /tests/helpers/resolver.js: -------------------------------------------------------------------------------- 1 | import Resolver from '../../resolver'; 2 | import config from '../../config/environment'; 3 | 4 | const resolver = Resolver.create(); 5 | 6 | resolver.namespace = { 7 | modulePrefix: config.modulePrefix, 8 | podModulePrefix: config.podModulePrefix 9 | }; 10 | 11 | export default resolver; 12 | -------------------------------------------------------------------------------- /tests/helpers/start-app.js: -------------------------------------------------------------------------------- 1 | import Application from '../../app'; 2 | import config from '../../config/environment'; 3 | import { merge } from '@ember/polyfills'; 4 | import { run } from '@ember/runloop'; 5 | 6 | export default function startApp(attrs) { 7 | let attributes = merge({}, config.APP); 8 | attributes = merge(attributes, attrs); // use defaults, but you can override; 9 | 10 | return run(() => { 11 | let application = Application.create(attributes); 12 | application.setupForTesting(); 13 | application.injectTestHelpers(); 14 | return application; 15 | }); 16 | } 17 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Dummy Tests 7 | 8 | 9 | 10 | {{content-for "head"}} 11 | {{content-for "test-head"}} 12 | 13 | 14 | 15 | 16 | 17 | {{content-for "head-footer"}} 18 | {{content-for "test-head-footer"}} 19 | 20 | 21 | {{content-for "body"}} 22 | {{content-for "test-body"}} 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | {{content-for "body-footer"}} 31 | {{content-for "test-body-footer"}} 32 | 33 | 34 | -------------------------------------------------------------------------------- /tests/integration/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/integration/.gitkeep -------------------------------------------------------------------------------- /tests/pages/array-multiplier.js: -------------------------------------------------------------------------------- 1 | import $ from 'jquery'; 2 | 3 | import { 4 | create, 5 | visitable, 6 | fillable, 7 | clickable 8 | } from 'ember-cli-page-object'; 9 | 10 | export default create({ 11 | visit: visitable('/multiplier'), 12 | 13 | multiplier: { 14 | scope: '.array-multiplier', 15 | 16 | listItems: () => { 17 | return $('[data-test-multiplier-list-item]') 18 | .map((i, v) => parseInt($(v).text()), 10) 19 | .toArray(); 20 | }, 21 | 22 | input: fillable('.array-multiplier-input'), 23 | 24 | calculate: clickable('[data-test-multiplier-button]') 25 | } 26 | }); 27 | -------------------------------------------------------------------------------- /tests/pages/simple-counter.js: -------------------------------------------------------------------------------- 1 | import { 2 | create, 3 | visitable, 4 | clickable, 5 | text 6 | } from 'ember-cli-page-object'; 7 | 8 | export default create({ 9 | visit: visitable('/counter'), 10 | 11 | counter: { 12 | scope: '.simple-counter', 13 | 14 | numClicks: text('[data-test-counter-numclicks]'), 15 | 16 | increment: clickable('[data-test-counter-increment]'), 17 | 18 | decrement: clickable('[data-test-counter-decrement]') 19 | } 20 | }); 21 | -------------------------------------------------------------------------------- /tests/test-helper.js: -------------------------------------------------------------------------------- 1 | import resolver from './helpers/resolver'; 2 | import { setResolver } from 'ember-mocha'; 3 | 4 | setResolver(resolver); 5 | -------------------------------------------------------------------------------- /tests/unit/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/tests/unit/.gitkeep -------------------------------------------------------------------------------- /tests/unit/utils/update-test.js: -------------------------------------------------------------------------------- 1 | /* jshint expr:true */ 2 | import EmberObject from '@ember/object'; 3 | import { expect } from 'chai'; 4 | import { 5 | describe, 6 | beforeEach, 7 | it 8 | } from 'mocha'; 9 | import update from 'ember-object-update'; 10 | 11 | describe('update with a basic ember object', function() { 12 | let emberObj; 13 | beforeEach(function() { 14 | emberObj = EmberObject.create({ 15 | firstName: "John", 16 | lastName: "Doe", 17 | someBoolean: false 18 | }); 19 | }); 20 | 21 | it('updates a string property given a function', function() { 22 | const updated = update(emberObj, 'firstName', name => name.toUpperCase()); 23 | expect(updated).to.equal('JOHN') 24 | expect(emberObj.get('firstName')).to.equal('JOHN'); 25 | expect(emberObj.get('lastName')).to.equal('Doe'); 26 | expect(emberObj.get('someBoolean')).to.be.false; 27 | }); 28 | 29 | it('updates a boolean property', function() { 30 | const updated = update(emberObj, 'someBoolean', x => !x); 31 | expect(updated).to.be.true; 32 | expect(emberObj.get('someBoolean')).to.be.true; 33 | expect(emberObj.get('firstName')).to.equal('John'); 34 | expect(emberObj.get('lastName')).to.equal('Doe'); 35 | }); 36 | 37 | it("can set a root-level property that doesn't already exist", function() { 38 | const updated = update(emberObj, 'newProperty', () => 123); 39 | expect(updated).to.equal(123); 40 | expect(emberObj.get('newProperty')).to.equal(123); 41 | }); 42 | }); 43 | 44 | describe('update with a nested ember object', function() { 45 | let nestedObj; 46 | beforeEach(function() { 47 | nestedObj = EmberObject.create({ a: { b: { c: 2 } } }); 48 | }); 49 | 50 | it('can update a nested property', function() { 51 | let expected = EmberObject.create({ a: { b: { c: 4 } } }); 52 | const updated = update(nestedObj, 'a.b.c', v => v * 2); 53 | expect(updated).to.equal(4); 54 | expect(nestedObj).to.deep.equal(expected); 55 | }); 56 | 57 | it("can set a nested property that doesn't already exist", function() { 58 | let expected = EmberObject.create({ a: { b: { c: 2, d: "I'm here" } } }) 59 | const updated = update(nestedObj, 'a.b.d', () => "I'm here"); 60 | expect(updated).to.equal("I'm here"); 61 | expect(nestedObj).to.deep.equal(expected); 62 | }); 63 | }); 64 | 65 | describe('update with an ember object and arrays', function() { 66 | let obj; 67 | 68 | beforeEach(function() { 69 | obj = EmberObject.create({ 70 | a: [1, 2, 3] 71 | }); 72 | }); 73 | 74 | it('can update an entire array property with a function', function() { 75 | const updated = update(obj, 'a', a => a.map(v => v * 2)); 76 | expect(updated).to.deep.equal([2, 4, 6]); 77 | expect(obj.get('a')).to.deep.equal([2, 4, 6]); 78 | }); 79 | 80 | it('can update a single element in an array property', function() { 81 | const updated = update(obj, 'a.1', v => v + 1); 82 | expect(updated).to.deep.equal(3); 83 | expect(obj.get('a')).to.deep.equal([1, 3, 3]); 84 | }); 85 | }); 86 | -------------------------------------------------------------------------------- /tests/unit/utils/update-with-pojo-test.js: -------------------------------------------------------------------------------- 1 | /* jshint expr:true */ 2 | import { get } from '@ember/object'; 3 | 4 | import { expect } from 'chai'; 5 | import { 6 | describe, 7 | beforeEach, 8 | it 9 | } from 'mocha'; 10 | import update from 'ember-object-update'; 11 | 12 | describe('update basic POJO', function() { 13 | let simpleObj; 14 | beforeEach(function() { 15 | simpleObj = { 16 | firstName: "John", 17 | lastName: "Doe", 18 | someBoolean: false 19 | }; 20 | }); 21 | 22 | it('updates a string property given a function', function() { 23 | const updated = update(simpleObj, 'firstName', name => name.toUpperCase()); 24 | expect(updated).to.equal('JOHN') 25 | expect(get(simpleObj, 'firstName')).to.equal('JOHN'); 26 | expect(get(simpleObj, 'lastName')).to.equal('Doe'); 27 | expect(get(simpleObj, 'someBoolean')).to.be.false; 28 | }); 29 | 30 | it('updates a boolean property', function() { 31 | const updated = update(simpleObj, 'someBoolean', x => !x); 32 | expect(updated).to.be.true; 33 | expect(get(simpleObj, 'someBoolean')).to.be.true; 34 | expect(get(simpleObj, 'firstName')).to.equal('John'); 35 | expect(get(simpleObj, 'lastName')).to.equal('Doe'); 36 | }); 37 | 38 | it("can set a root-level property that doesn't already exist", function() { 39 | const updated = update(simpleObj, 'newProperty', () => 123); 40 | expect(updated).to.equal(123); 41 | expect(get(simpleObj, 'newProperty')).to.equal(123); 42 | }); 43 | }); 44 | 45 | describe('update with a nested POJO', function() { 46 | let nestedObj; 47 | beforeEach(function() { 48 | nestedObj = { a: { b: { c: 2 } } }; 49 | }); 50 | 51 | it('can update a nested property', function() { 52 | let expected = { a: { b: { c: 4 } } }; 53 | const updated = update(nestedObj, 'a.b.c', v => v * 2); 54 | expect(updated).to.equal(4); 55 | expect(nestedObj).to.deep.equal(expected); 56 | }); 57 | 58 | it("can set a nested property that doesn't already exist", function() { 59 | let expected = { a: { b: { c: 2, d: "I'm here" } } }; 60 | const updated = update(nestedObj, 'a.b.d', () => "I'm here"); 61 | expect(updated).to.equal("I'm here"); 62 | expect(nestedObj).to.deep.equal(expected); 63 | }); 64 | }); 65 | 66 | describe('update with a POJO and arrays', function() { 67 | let obj; 68 | 69 | beforeEach(function() { 70 | obj = { 71 | a: [1, 2, 3] 72 | }; 73 | }); 74 | 75 | it('can update an entire array property with a function', function() { 76 | const updated = update(obj, 'a', a => a.map(v => v * 2)); 77 | expect(updated).to.deep.equal([2, 4, 6]); 78 | expect(get(obj, 'a')).to.deep.equal([2, 4, 6]); 79 | }); 80 | 81 | it('can update a single element in an array property', function() { 82 | const updated = update(obj, 'a.1', v => v + 1); 83 | expect(updated).to.deep.equal(3); 84 | expect(get(obj, 'a')).to.deep.equal([1, 3, 3]); 85 | }); 86 | }); 87 | -------------------------------------------------------------------------------- /vendor/.gitkeep: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/cafreeman/ember-object-update/77ee962b0bed00300af255c90925ae6a93040027/vendor/.gitkeep --------------------------------------------------------------------------------