Skip to content

Commit

Permalink
Merge pull request #34 from BillyRayPreachersSon/add-brace-expansion
Browse files Browse the repository at this point in the history
Add the ability to use Ember's brace expansion with the keys
  • Loading branch information
workmanw authored Oct 31, 2022
2 parents 6d55528 + f8b0c9d commit c5b14d0
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
6 changes: 5 additions & 1 deletion addon/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { expandProperties } from '@ember/object/computed';

function isEqual(key, a, b) {
return a === b;
Expand Down Expand Up @@ -36,7 +37,10 @@ export default function(keys, hook) {

oldValues = oldValuesMap.get(this);

keys.forEach(key => {
const expandedKeys = [];
keys.forEach(key => expandProperties(key, expandedKey => expandedKeys.push(expandedKey)));

expandedKeys.forEach(key => {
let value = this.get(key);
if (!isEqualFunc(key, oldValues[key], value)) {
changedAttrs[key] = [oldValues[key], value];
Expand Down
109 changes: 109 additions & 0 deletions tests/integration/diff-attrs-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,112 @@ test('Options - Compare', function(assert) {
this.set('isAdmin', true);
assert.ok(changedAttrs.isAdmin, '`isAdmin` fell back to the default comparer');
});

[true, false].forEach(shorthandUsage => {
const usage = shorthandUsage ? 'shorthand' : 'extended';

test(`With ${usage} usage, brace expansion does not affect tracked properties outside of the braces`, function(assert) {
let changedAttrs = {};
if (shorthandUsage) {
registerComponent(this, {
didReceiveAttrs: diffAttrs('isAdmin', 'user.{forename,email}', function(changedAttrsArg) {
changedAttrs = changedAttrsArg;
})
});
} else {
registerComponent(this, {
didReceiveAttrs: diffAttrs({
keys: ['isAdmin', 'user.{forename,email}'],
hook(changedAttrsArg) {
changedAttrs = changedAttrsArg;
}
})
});
}

this.setProperties({
user: {
forename: 'Bob',
surname: 'Smith',
email: 'bob@smith'
},
isAdmin: false
});

this.render(hbs`{{x-changer user=user isAdmin=isAdmin}}`);
assert.notOk(changedAttrs, '`changedAttrs` is null initially');

this.set('isAdmin', true);
assert.equal(Object.keys(changedAttrs).join(), 'isAdmin', '`isAdmin` should be the only changed property');
});

test(`With ${usage} usage, brace expansion works on multiple properties across multiple sets of braces`, function(assert) {
let changedAttrs = {};
if (shorthandUsage) {
registerComponent(this, {
didReceiveAttrs: diffAttrs('user.{forename,age}', 'admin.{surname,email,age}', function(changedAttrsArg) {
changedAttrs = changedAttrsArg;
})
});
} else {
registerComponent(this, {
didReceiveAttrs: diffAttrs({
keys: ['user.{forename,age}', 'admin.{surname,email,age}'],
hook(changedAttrsArg) {
changedAttrs = changedAttrsArg;
}
})
});
}

this.setProperties({
user: {
forename: 'Bob',
surname: 'Smith',
email: 'bob@smith',
age: 22
},
admin: {
forename: 'Fred',
surname: 'Jones',
email: 'fred@jones',
age: 42
}
});

this.render(hbs`{{x-changer user=user admin=admin}}`);
assert.notOk(changedAttrs, '`changedAttrs` is null initially');

this.setProperties({
user: {
forename: 'Bob',
surname: 'Smythe',
email: 'bob@smythe',
age: 22
},
admin: {
forename: 'Freddy',
surname: 'Jones',
email: 'fred@jones',
age: 42
}
});
assert.equal(Object.keys(changedAttrs).join(), '', 'No tracked properties should have changed');

this.setProperties({
user: {
forename: 'Robert',
surname: 'Smythe',
email: 'robert@smythe',
age: 23
},
admin: {
forename: 'Freddy',
surname: 'Johnston',
email: 'freddy@johnston',
age: 42
}
});
assert.equal(Object.keys(changedAttrs).join(), 'user.forename,user.age,admin.surname,admin.email', 'Brace expansion should have worked');
});
});
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8097,9 +8097,9 @@ tmp@^0.0.29:
os-tmpdir "~1.0.1"

[email protected]:
version "1.0.4"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.4.tgz#23640dd7b42d00433911140820e5cf440e521dd1"
integrity sha1-I2QN17QtAEM5ERQIIOXPRA5SHdE=
version "1.0.5"
resolved "https://registry.yarnpkg.com/tmpl/-/tmpl-1.0.5.tgz#8683e0b902bb9c20c4f726e3c0b69f36518c07cc"
integrity sha512-3f0uOEAQwIqGuWW2MVzYg8fV/QNnc/IpuJNG837rLuczAaLVHslWHZQj4IGiEl5Hs3kkbhwL9Ab7Hrsmuj+Smw==

[email protected]:
version "0.1.4"
Expand Down

0 comments on commit c5b14d0

Please sign in to comment.