-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,6 +35,22 @@ export function isArray(value) { | |
return Array.isArray(value); | ||
} | ||
|
||
/** | ||
* Converts an array-like object to an array. | ||
* @param {?HTMLCollection|?NodeList|*} arrayLike | ||
* @return {!Array.<*>} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
*/ | ||
export function toArray(arrayLike) { | ||
if (!arrayLike) { | ||
return []; | ||
} | ||
const array = []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can preallocate the array length for a small win. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
for (let i = 0; i < arrayLike.length; i++) { | ||
array.push(arrayLike[i]); | ||
} | ||
return array; | ||
} | ||
|
||
/** | ||
* Determines if value is actually an Object. | ||
* @param {*} value | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/** | ||
* 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 passed', () => { | ||
expect(types.toArray(null).length).to.equal(0); | ||
expect(types.toArray(undefined).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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this test for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need to appendChild in this test? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because I am retrieving the elements through the There was a problem hiding this comment. Choose a reason for hiding this commentThe 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', () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this test for There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
}); | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing semicolon There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For reference, Closure's doc comment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Used the closure compiler
IArrayLike
type here.