Skip to content
Permalink
Browse files
fix(utils): pass all Array/Object util shortcuts as functions, for ha…
…ndling 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>
  • Loading branch information
tmorehouse and jacobmllr95 committed Jan 21, 2020
1 parent 89ed8fc commit f5844256a03d2f4b8006900419acfa2c5e3803c3
Showing with 22 additions and 29 deletions.
  1. +2 −2 src/utils/array.js
  2. +8 −15 src/utils/dom.js
  3. +12 −12 src/utils/object.js
@@ -1,7 +1,7 @@
// --- Static ---

export const from = Array.from
export const isArray = Array.isArray
export const from = (...args) => Array.from(...args)
export const isArray = val => Array.isArray(val)

// --- Instance ---

@@ -78,11 +78,13 @@ export const eventOff = (el, evtName, handler, options) => {
export const removeNode = el => el && el.parentNode && el.parentNode.removeChild(el)

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

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

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

// Determine if an element matches a selector
export const matches = (el, selector) => {
if (!isElement(el)) {
return false
}
return matchesEl.call(el, selector)
}
export const matches = (el, selector) => (isElement(el) ? matchesEl.call(el, selector) : false)

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

// Returns true if the parent element contains the child element
export const contains = (parent, child) => {
if (!parent || !isFunction(parent.contains)) {
return false
}
return parent.contains(child)
}
export const contains = (parent, child) =>
parent && isFunction(parent.contains) ? parent.contains(child) : false

// Get an element given an ID
export const getById = id => d.getElementById(/^#/.test(id) ? id.slice(1) : id) || null
@@ -2,18 +2,18 @@ import { isArray } from './array'

// --- Static ---

export const assign = Object.assign
export const getOwnPropertyNames = Object.getOwnPropertyNames
export const keys = Object.keys
export const defineProperties = Object.defineProperties
export const defineProperty = Object.defineProperty
export const freeze = Object.freeze
export const getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor
export const getOwnPropertySymbols = Object.getOwnPropertySymbols
export const getPrototypeOf = Object.getPrototypeOf
export const create = Object.create
export const isFrozen = Object.isFrozen
export const is = Object.is
export const assign = (...args) => Object.assign(...args)
export const create = (proto, optionalProps) => Object.create(proto, optionalProps)
export const defineProperties = (obj, props) => Object.defineProperties(obj, props)
export const defineProperty = (obj, prop, descr) => Object.defineProperty(obj, prop, descr)
export const freeze = obj => Object.freeze(obj)
export const getOwnPropertyNames = obj => Object.getOwnPropertyNames(obj)
export const getOwnPropertyDescriptor = (obj, prop) => Object.getOwnPropertyDescriptor(obj, prop)
export const getOwnPropertySymbols = obj => Object.getOwnPropertySymbols(obj)
export const getPrototypeOf = obj => Object.getPrototypeOf(obj)
export const is = (value1, value2) => Object.is(value1, value2)
export const isFrozen = obj => Object.isFrozen(obj)
export const keys = obj => Object.keys(obj)

// --- "Instance" ---

0 comments on commit f584425

Please sign in to comment.