Skip to content

Commit f584425

Browse files
fix(utils): pass all Array/Object util shortcuts as functions, for handling late loaded polyfills (#4647)
* chore(utils): pass all Array/Object shortcuts as functions My making each shortcut a method, it allows for handling of late loaded polyfills. See #3641 (comment) * Update object.js * Update object.js * Update dom.js * Update dom.js * Update dom.js Co-authored-by: Jacob Müller <jacob.mueller.elz@gmail.com>
1 parent 89ed8fc commit f584425

File tree

3 files changed

+22
-29
lines changed

3 files changed

+22
-29
lines changed

src/utils/array.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// --- Static ---
22

3-
export const from = Array.from
4-
export const isArray = Array.isArray
3+
export const from = (...args) => Array.from(...args)
4+
export const isArray = val => Array.isArray(val)
55

66
// --- Instance ---
77

src/utils/dom.js

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export const eventOff = (el, evtName, handler, options) => {
7878
export const removeNode = el => el && el.parentNode && el.parentNode.removeChild(el)
7979

8080
// Determine if an element is an HTML element
81-
export const isElement = el => Boolean(el && el.nodeType === Node.ELEMENT_NODE)
81+
export const isElement = el => !!(el && el.nodeType === Node.ELEMENT_NODE)
8282

8383
// Determine if an HTML element is visible - Faster than CSS check
8484
export const isVisible = el => {
85-
if (!isElement(el) || !contains(d.body, el)) {
85+
if (!isElement(el) || !el.parentNode || !contains(d.body, el)) {
86+
// Note this can fail for shadow dom elements since they
87+
// are not a direct descendant of document.body
8688
return false
8789
}
8890
if (el.style.display === 'none') {
@@ -94,7 +96,7 @@ export const isVisible = el => {
9496
// So any tests that need isVisible will fail in JSDOM
9597
// Except when we override the getBCR prototype in some tests
9698
const bcr = getBCR(el)
97-
return Boolean(bcr && bcr.height > 0 && bcr.width > 0)
99+
return !!(bcr && bcr.height > 0 && bcr.width > 0)
98100
}
99101

100102
// Determine if an element is disabled
@@ -117,12 +119,7 @@ export const select = (selector, root) =>
117119
(isElement(root) ? root : d).querySelector(selector) || null
118120

119121
// Determine if an element matches a selector
120-
export const matches = (el, selector) => {
121-
if (!isElement(el)) {
122-
return false
123-
}
124-
return matchesEl.call(el, selector)
125-
}
122+
export const matches = (el, selector) => (isElement(el) ? matchesEl.call(el, selector) : false)
126123

127124
// Finds closest element matching selector. Returns `null` if not found
128125
export const closest = (selector, root, includeRoot = false) => {
@@ -138,12 +135,8 @@ export const closest = (selector, root, includeRoot = false) => {
138135
}
139136

140137
// Returns true if the parent element contains the child element
141-
export const contains = (parent, child) => {
142-
if (!parent || !isFunction(parent.contains)) {
143-
return false
144-
}
145-
return parent.contains(child)
146-
}
138+
export const contains = (parent, child) =>
139+
parent && isFunction(parent.contains) ? parent.contains(child) : false
147140

148141
// Get an element given an ID
149142
export const getById = id => d.getElementById(/^#/.test(id) ? id.slice(1) : id) || null

src/utils/object.js

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ import { isArray } from './array'
22

33
// --- Static ---
44

5-
export const assign = Object.assign
6-
export const getOwnPropertyNames = Object.getOwnPropertyNames
7-
export const keys = Object.keys
8-
export const defineProperties = Object.defineProperties
9-
export const defineProperty = Object.defineProperty
10-
export const freeze = Object.freeze
11-
export const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
12-
export const getOwnPropertySymbols = Object.getOwnPropertySymbols
13-
export const getPrototypeOf = Object.getPrototypeOf
14-
export const create = Object.create
15-
export const isFrozen = Object.isFrozen
16-
export const is = Object.is
5+
export const assign = (...args) => Object.assign(...args)
6+
export const create = (proto, optionalProps) => Object.create(proto, optionalProps)
7+
export const defineProperties = (obj, props) => Object.defineProperties(obj, props)
8+
export const defineProperty = (obj, prop, descr) => Object.defineProperty(obj, prop, descr)
9+
export const freeze = obj => Object.freeze(obj)
10+
export const getOwnPropertyNames = obj => Object.getOwnPropertyNames(obj)
11+
export const getOwnPropertyDescriptor = (obj, prop) => Object.getOwnPropertyDescriptor(obj, prop)
12+
export const getOwnPropertySymbols = obj => Object.getOwnPropertySymbols(obj)
13+
export const getPrototypeOf = obj => Object.getPrototypeOf(obj)
14+
export const is = (value1, value2) => Object.is(value1, value2)
15+
export const isFrozen = obj => Object.isFrozen(obj)
16+
export const keys = obj => Object.keys(obj)
1717

1818
// --- "Instance" ---
1919

0 commit comments

Comments
 (0)