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

feat: add toArray function to convert NodeList to Array #603

Merged
merged 1 commit into from
Feb 9, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/components/controls.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { siblings } from '../utils/dom'
import { siblings, toArray } from '../utils/dom'
import { define } from '../utils/object'
import supportsPassive from '../utils/detect-passive-event'

Expand Down Expand Up @@ -147,7 +147,7 @@ export default function (Glide, Components, Events) {
const settings = Glide.settings

lists.forEach(function (list) {
list.forEach(function (element) {
toArray(list).forEach(function (element) {
element.classList.remove(settings.classes.arrow.disabled)
})
})
Expand All @@ -162,7 +162,7 @@ export default function (Glide, Components, Events) {
const settings = Glide.settings

lists.forEach(function (list) {
list.forEach(function (element) {
toArray(list).forEach(function (element) {
element.classList.add(settings.classes.arrow.disabled)
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/components/html.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { warn } from '../utils/log'
import { exist } from '../utils/dom'
import { exist, toArray } from '../utils/dom'
import { define } from '../utils/object'
import { isString } from '../utils/unit'

Expand All @@ -22,7 +22,7 @@ export default function (Glide, Components, Events) {
* Collect slides
*/
collectSlides () {
this.slides = Array.prototype.slice.call(this.wrapper.children).filter((slide) => {
this.slides = toArray(this.wrapper.children).filter((slide) => {
return !slide.classList.contains(Glide.settings.classes.slide.clone)
})
}
Expand Down
11 changes: 11 additions & 0 deletions src/utils/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,14 @@ export function exist (node) {

return false
}

/**
* Coerces a NodeList to an Array.
*
* @param {NodeList} nodeList
* @return {Array}
*/

export function toArray (nodeList) {
return Array.prototype.slice.call(nodeList)
}
13 changes: 12 additions & 1 deletion tests/unit/dom.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { exist, siblings } from '../../src/utils/dom'
import { exist, toArray, siblings } from '../../src/utils/dom'

describe('Function', () => {
beforeEach(() => {
Expand All @@ -21,4 +21,15 @@ describe('Function', () => {

expect(siblings(children[1])).toHaveLength(2)
})

test('`toArray` should return an array with the same elements when passed a NodeList', () => {
let children = document.querySelectorAll('.child')

// eslint-disable-next-line no-undef
expect(NodeList.prototype.isPrototypeOf(children)).toBe(true)
expect(Array.isArray(toArray(children))).toBe(true)
toArray(children).forEach((child, i) => {
expect(child).toEqual(children[i])
})
})
})