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

Add toArray utility function to convert array-like objects to arrays. #3389

Merged
merged 4 commits into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions src/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import {dashToCamelCase} from './string';
import {dev} from './log';
import {toArray} from './types';

/**
* Waits until the child element is constructed. Once the child is found, the
Expand Down Expand Up @@ -328,13 +329,7 @@ export function childElementsByAttr(parent, attr) {
scopeSelectorSupported = isScopeSelectorSupported(parent);
}
if (scopeSelectorSupported) {
const nodeList = parent.querySelectorAll(':scope > [' + attr + ']');
// Convert NodeList into Array.<Element>.
const children = [];
for (let i = 0; i < nodeList.length; i++) {
children.push(nodeList[i]);
}
return children;
return toArray(parent.querySelectorAll(':scope > [' + attr + ']'));
}
return childElements(parent, el => {
return el.hasAttribute(attr);
Expand Down
20 changes: 20 additions & 0 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,26 @@ export function isArray(value) {
return Array.isArray(value);
}

/**
* Converts an array-like object to an array.
* @param {?IArrayLike<*>|string} arrayLike
* @return {!Array.<*>}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No .. Also, should we not be using the @template T?

Copy link
Contributor Author

@mkhatib mkhatib Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure, we seem to be using * everywhere, @cramforce @erwinmombay any idea? Nevermind, I see we do use @template syntax.

*/
export function toArray(arrayLike) {
if (!arrayLike) {
return [];
}
const length = arrayLike.length;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be unnecessary now, just call Array(arrayLike.length) below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

if (length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the if condition, since we don't have any legacy code. Everything will be a real array-like, which a numeric #length.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

const array = new Array(length);
for (let i = 0; i < length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cached length iteration is actually slower than i < arrayLike.length in current browsers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

array[i] = arrayLike[i];
}
return array;
}
return [];
}

/**
* Determines if value is actually an Object.
* @param {*} value
Expand Down
67 changes: 67 additions & 0 deletions test/functional/test-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/**
* Copyright 2016 The AMP HTML Authors. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as types from '../../src/types';

describe('Types', () => {
describe('toArray', () => {

it('should return empty array if null is passed', () => {
expect(types.toArray(null).length).to.equal(0);
expect(types.toArray(undefined).length).to.equal(0);
});

it('should return empty array if non-array-like is passed', () => {
expect(types.toArray({}).length).to.equal(0);
expect(types.toArray(document.createElement('p')).length).to.equal(0);
});

it('should convert NodeList to array', () => {
const parent = document.createElement('div');
parent.appendChild(document.createElement('p'));
parent.appendChild(document.createElement('span'));
parent.appendChild(document.createElement('div'));
const arr = types.toArray(parent.childNodes);
expect(arr[0]).to.equal(parent.childNodes[0]);
expect(arr.length).to.equal(3);
expect(Array.isArray(arr)).to.be.true;
});

it('should convert HTMLCollection to array', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this test for toArray?

Copy link
Contributor Author

@mkhatib mkhatib Jun 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't hurt it just adds more tests to different types of objects (above is NodeList and this is HTMLCollection)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah ok

const parent = document.createElement('div');
parent.appendChild(document.createElement('form'));
parent.appendChild(document.createElement('form'));
document.body.appendChild(parent);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need to appendChild in this test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I am retrieving the elements through the document.forms API which would only be populated after the elements are appended.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see~

const arr = types.toArray(document.forms);
expect(arr[0]).to.equal(document.forms[0]);
expect(arr.length).to.equal(2);
expect(Array.isArray(arr)).to.be.true;
document.body.removeChild(parent);
});

it('should convert HTMLOptionsCollection to array', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this test for toArray?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above just getting it more robust with different types of objects

const parent = document.createElement('select');
parent.appendChild(document.createElement('option'));
parent.appendChild(document.createElement('option'));
parent.appendChild(document.createElement('option'));
parent.appendChild(document.createElement('option'));
const arr = types.toArray(parent.options);
expect(arr[0]).to.equal(parent.options[0]);
expect(arr.length).to.equal(4);
expect(Array.isArray(arr)).to.be.true;
});
});
});