-
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 2 commits
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,26 @@ export function isArray(value) { | |
return Array.isArray(value); | ||
} | ||
|
||
/** | ||
* Converts an array-like object to an array. | ||
* @param {?IArrayLike<*>|string} arrayLike | ||
* @return {!Array.<*>} | ||
*/ | ||
export function toArray(arrayLike) { | ||
if (!arrayLike) { | ||
return []; | ||
} | ||
const length = arrayLike.length; | ||
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. This should be unnecessary now, just call 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. Removed. |
||
if (length > 0) { | ||
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 need for the if condition, since we don't have any legacy code. Everything will be a real array-like, which a numeric 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 |
||
const array = new Array(length); | ||
for (let i = 0; i < length; i++) { | ||
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. Cached 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 |
||
array[i] = arrayLike[i]; | ||
} | ||
return array; | ||
} | ||
return []; | ||
} | ||
|
||
/** | ||
* Determines if value is actually an Object. | ||
* @param {*} value | ||
|
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', () => { | ||
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 comment
The reason will be displayed to describe this comment to others. Learn more.
No
.
. Also, should we not be using the@template T
?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.
Not sure, we seem to be usingNevermind, I see we do use*
everywhere, @cramforce @erwinmombay any idea?@template
syntax.