Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use array notation instead of at for accessing object at values #8189

Merged
merged 1 commit into from Sep 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 46 additions & 0 deletions packages/-ember-data/tests/unit/record-arrays/record-array-test.js
Expand Up @@ -183,6 +183,52 @@ module('unit/record-arrays/record-array - DS.RecordArray', function (hooks) {
}
);

deprecatedTest(
'#objectAt and #objectsAt',
{ id: 'ember-data:deprecate-array-like', until: '5.0', count: 5 },
async function (assert) {
this.owner.register('model:tag', Tag);
let store = this.owner.lookup('service:store');

let records = store.push({
data: [
{
type: 'tag',
id: '1',
attributes: {
name: 'first',
},
},
{
type: 'tag',
id: '3',
},
{
type: 'tag',
id: '5',
attributes: {
name: 'fifth',
},
},
],
});

let recordArray = new RecordArray({
type: 'recordType',
identifiers: records.map(recordIdentifierFor),
store,
});

assert.strictEqual(recordArray.length, 3);
assert.strictEqual(recordArray.objectAt(0).id, '1');
assert.strictEqual(recordArray.objectAt(-1).id, '5');
assert.deepEqual(
recordArray.objectsAt([2, 1]).map((r) => r.id),
['5', '3']
);
}
);

test('#update', async function (assert) {
let findAllCalled = 0;
let deferred = RSVP.defer();
Expand Down
Expand Up @@ -631,12 +631,14 @@ if (DEPRECATE_ARRAY_LIKE) {

IdentifierArray.prototype.objectAt = function (index: number) {
deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'objectAt', 'at');
return this.at(index);
//For negative index values go back from the end of the array
let arrIndex = Math.sign(index) === -1 ? this.length + index : index;
jrjohnson marked this conversation as resolved.
Show resolved Hide resolved
return this[arrIndex];
};

IdentifierArray.prototype.objectsAt = function (indeces: number[]) {
deprecateArrayLike(this.DEPRECATED_CLASS_NAME, 'objectsAt', 'at');
return indeces.map((index) => this.at(index)!);
return indeces.map((index) => this.objectAt(index)!);
};

IdentifierArray.prototype.removeAt = function (index: number) {
Expand Down