@thednp/shorty 1.0.3 → 2.0.0-alpha10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +224 -0
- package/.lgtm.yml +9 -0
- package/.prettierrc.json +15 -0
- package/README.md +195 -198
- package/cypress/e2e/att.cy.ts +46 -0
- package/cypress/e2e/boolean.cy.ts +44 -0
- package/cypress/e2e/class.cy.ts +28 -0
- package/cypress/e2e/event.cy.ts +51 -0
- package/cypress/e2e/get.cy.ts +168 -0
- package/cypress/e2e/is.cy.ts +233 -0
- package/cypress/e2e/misc.cy.ts +331 -0
- package/cypress/e2e/selectors.cy.ts +85 -0
- package/cypress/fixtures/custom-elem.js +18 -0
- package/cypress/plugins/esbuild-istanbul.ts +50 -0
- package/cypress/plugins/tsCompile.ts +34 -0
- package/cypress/support/commands.ts +37 -0
- package/cypress/support/e2e.ts +21 -0
- package/cypress/support/index.js +22 -0
- package/cypress/test.html +63 -0
- package/cypress.config.ts +30 -0
- package/dist/shorty.cjs +2 -0
- package/dist/shorty.cjs.map +1 -0
- package/dist/shorty.d.ts +1502 -0
- package/dist/shorty.js +2 -2628
- package/dist/shorty.js.map +1 -0
- package/dist/shorty.mjs +552 -0
- package/dist/shorty.mjs.map +1 -0
- package/dts.config.ts +22 -0
- package/package.json +33 -38
- package/src/attr/getAttribute.ts +11 -0
- package/src/attr/getAttributeNS.ts +12 -0
- package/src/attr/hasAttribute.ts +10 -0
- package/src/attr/hasAttributeNS.ts +12 -0
- package/src/attr/removeAttribute.ts +9 -0
- package/src/attr/removeAttributeNS.ts +11 -0
- package/src/attr/setAttribute.ts +11 -0
- package/src/attr/setAttributeNS.ts +12 -0
- package/src/blocks/{documentBody.js → documentBody.ts} +0 -0
- package/src/blocks/{documentElement.js → documentElement.ts} +0 -0
- package/src/blocks/{documentHead.js → documentHead.ts} +0 -0
- package/src/boolean/isApple.ts +14 -0
- package/src/boolean/{isFirefox.js → isFirefox.ts} +2 -3
- package/src/boolean/{isMobile.js → isMobile.ts} +1 -2
- package/src/boolean/support3DTransform.ts +8 -0
- package/src/boolean/supportAnimation.ts +8 -0
- package/src/boolean/{supportPassive.js → supportPassive.ts} +3 -3
- package/src/boolean/supportTouch.ts +7 -0
- package/src/boolean/supportTransform.ts +8 -0
- package/src/boolean/supportTransition.ts +8 -0
- package/src/class/addClass.ts +11 -0
- package/src/class/hasClass.ts +11 -0
- package/src/class/removeClass.ts +11 -0
- package/src/event/off.ts +14 -0
- package/src/event/on.ts +14 -0
- package/src/event/one.ts +25 -0
- package/src/get/{getBoundingClientRect.js → getBoundingClientRect.ts} +14 -12
- package/src/get/{getDocument.js → getDocument.ts} +11 -7
- package/src/get/getDocumentBody.ts +13 -0
- package/src/get/getDocumentElement.ts +13 -0
- package/src/get/getDocumentHead.ts +12 -0
- package/src/get/{getElementAnimationDelay.js → getElementAnimationDelay.ts} +10 -9
- package/src/get/{getElementAnimationDuration.js → getElementAnimationDuration.ts} +10 -8
- package/src/get/getElementStyle.ts +25 -0
- package/src/get/{getElementTransitionDelay.js → getElementTransitionDelay.ts} +12 -8
- package/src/get/{getElementTransitionDuration.js → getElementTransitionDuration.ts} +12 -8
- package/src/get/{getNodeScroll.js → getNodeScroll.ts} +9 -5
- package/src/get/{getParentNode.js → getParentNode.ts} +11 -6
- package/src/get/{getRectRelativeToOffsetParent.js → getRectRelativeToOffsetParent.ts} +15 -7
- package/src/get/getUID.ts +42 -0
- package/src/get/{getWindow.js → getWindow.ts} +9 -6
- package/src/{index.js → index.ts} +37 -43
- package/src/interface/boundingClientRect.d.ts +10 -0
- package/src/interface/css4Declaration.d.ts +4 -0
- package/src/interface/customElement.d.ts +8 -0
- package/src/interface/navigatorUA.d.ts +15 -0
- package/src/interface/offsetRect.d.ts +6 -0
- package/src/interface/originalEvent.d.ts +4 -0
- package/src/is/isArray.ts +9 -0
- package/src/is/isCanvas.ts +13 -0
- package/src/is/isCustomElement.ts +13 -0
- package/src/is/isDocument.ts +13 -0
- package/src/is/{isElement.js → isElement.ts} +10 -5
- package/src/is/{isElementInScrollRange.js → isElementInScrollRange.ts} +4 -4
- package/src/is/{isElementInViewport.js → isElementInViewport.ts} +6 -12
- package/src/is/isElementsArray.ts +13 -0
- package/src/is/isFunction.ts +10 -0
- package/src/is/isHTMLCollection.ts +12 -0
- package/src/is/isHTMLElement.ts +13 -0
- package/src/is/isHTMLImageElement.ts +12 -0
- package/src/is/isJSON.ts +20 -0
- package/src/is/isMap.ts +11 -0
- package/src/is/isMedia.ts +20 -0
- package/src/is/isNode.ts +17 -0
- package/src/is/isNodeList.ts +13 -0
- package/src/is/isNumber.ts +9 -0
- package/src/is/isObject.ts +10 -0
- package/src/is/isRTL.ts +11 -0
- package/src/is/isSVGElement.ts +13 -0
- package/src/is/{isScaledElement.js → isScaledElement.ts} +9 -7
- package/src/is/isShadowRoot.ts +12 -0
- package/src/is/isString.ts +9 -0
- package/src/is/isTableElement.ts +14 -0
- package/src/is/isWeakMap.ts +11 -0
- package/src/is/isWindow.ts +13 -0
- package/src/misc/ArrayFrom.ts +11 -0
- package/src/misc/Float32ArrayFrom.ts +10 -0
- package/src/misc/Float64ArrayFrom.ts +10 -0
- package/src/misc/ObjectAssign.ts +43 -0
- package/src/misc/ObjectEntries.ts +11 -0
- package/src/misc/ObjectHasOwn.ts +17 -0
- package/src/misc/ObjectKeys.ts +10 -0
- package/src/misc/ObjectValues.ts +11 -0
- package/src/misc/createCustomEvent.ts +28 -0
- package/src/misc/createElement.ts +40 -0
- package/src/misc/createElementNS.ts +45 -0
- package/src/misc/data.ts +82 -0
- package/src/misc/dispatchEvent.ts +9 -0
- package/src/misc/distinct.ts +18 -0
- package/src/misc/{emulateAnimationEnd.js → emulateAnimationEnd.ts} +8 -9
- package/src/misc/{emulateTransitionEnd.js → emulateTransitionEnd.ts} +8 -9
- package/src/misc/focus.ts +9 -0
- package/src/misc/getInstance.ts +9 -0
- package/src/misc/{noop.js → noop.ts} +4 -1
- package/src/misc/normalizeOptions.ts +55 -0
- package/src/misc/normalizeValue.ts +30 -0
- package/src/misc/{passiveHandler.js → passiveHandler.ts} +1 -2
- package/src/misc/reflow.ts +9 -0
- package/src/misc/setElementStyle.ts +23 -0
- package/src/misc/timer.ts +84 -0
- package/src/misc/toLowerCase.ts +9 -0
- package/src/misc/toUpperCase.ts +9 -0
- package/src/selectors/closest.ts +20 -0
- package/src/selectors/{getCustomElements.js → getCustomElements.ts} +8 -4
- package/src/selectors/getElementById.ts +16 -0
- package/src/selectors/getElementsByClassName.ts +22 -0
- package/src/selectors/getElementsByTagName.ts +20 -0
- package/src/selectors/matches.ts +10 -0
- package/src/selectors/querySelector.ts +22 -0
- package/src/selectors/{querySelectorAll.js → querySelectorAll.ts} +7 -5
- package/src/strings/{DOMContentLoadedEvent.js → DOMContentLoadedEvent.ts} +0 -1
- package/src/strings/{DOMMouseScrollEvent.js → DOMMouseScrollEvent.ts} +0 -1
- package/src/strings/{abortEvent.js → abortEvent.ts} +0 -1
- package/src/strings/{addEventListener.js → addEventListener.ts} +0 -1
- package/src/strings/{animationDelay.js → animationDelay.ts} +0 -1
- package/src/strings/{animationDuration.js → animationDuration.ts} +0 -1
- package/src/strings/{animationEndEvent.js → animationEndEvent.ts} +0 -1
- package/src/strings/{animationName.js → animationName.ts} +0 -1
- package/src/strings/{ariaChecked.js → ariaChecked.ts} +0 -1
- package/src/strings/{ariaDescribedBy.js → ariaDescribedBy.ts} +0 -1
- package/src/strings/{ariaDescription.js → ariaDescription.ts} +0 -1
- package/src/strings/{ariaExpanded.js → ariaExpanded.ts} +0 -1
- package/src/strings/{ariaHasPopup.js → ariaHasPopup.ts} +0 -1
- package/src/strings/{ariaHidden.js → ariaHidden.ts} +0 -1
- package/src/strings/{ariaLabel.js → ariaLabel.ts} +0 -1
- package/src/strings/{ariaLabelledBy.js → ariaLabelledBy.ts} +0 -1
- package/src/strings/{ariaModal.js → ariaModal.ts} +0 -1
- package/src/strings/{ariaPressed.js → ariaPressed.ts} +0 -1
- package/src/strings/{ariaSelected.js → ariaSelected.ts} +0 -1
- package/src/strings/{ariaValueMax.js → ariaValueMax.ts} +0 -1
- package/src/strings/{ariaValueMin.js → ariaValueMin.ts} +0 -1
- package/src/strings/{ariaValueNow.js → ariaValueNow.ts} +0 -1
- package/src/strings/{ariaValueText.js → ariaValueText.ts} +0 -1
- package/src/strings/{beforeunloadEvent.js → beforeunloadEvent.ts} +0 -1
- package/src/strings/{bezierEasings.js → bezierEasings.ts} +0 -1
- package/src/strings/{blurEvent.js → blurEvent.ts} +0 -1
- package/src/strings/{changeEvent.js → changeEvent.ts} +0 -1
- package/src/strings/{contextmenuEvent.js → contextmenuEvent.ts} +0 -1
- package/src/strings/dragEvent.ts +5 -0
- package/src/strings/dragendEvent.ts +5 -0
- package/src/strings/dragenterEvent.ts +5 -0
- package/src/strings/dragleaveEvent.ts +5 -0
- package/src/strings/dragoverEvent.ts +5 -0
- package/src/strings/dragstartEvent.ts +5 -0
- package/src/strings/{errorEvent.js → errorEvent.ts} +0 -1
- package/src/strings/{focusEvent.js → focusEvent.ts} +0 -1
- package/src/strings/{focusEvents.js → focusEvents.ts} +0 -1
- package/src/strings/{focusinEvent.js → focusinEvent.ts} +0 -1
- package/src/strings/{focusoutEvent.js → focusoutEvent.ts} +0 -1
- package/src/strings/{gesturechangeEvent.js → gesturechangeEvent.ts} +0 -1
- package/src/strings/{gestureendEvent.js → gestureendEvent.ts} +0 -1
- package/src/strings/{gesturestartEvent.js → gesturestartEvent.ts} +0 -1
- package/src/strings/{keyAlt.js → keyAlt.ts} +1 -1
- package/src/strings/{keyArrowDown.js → keyArrowDown.ts} +1 -1
- package/src/strings/{keyArrowLeft.js → keyArrowLeft.ts} +1 -1
- package/src/strings/{keyArrowRight.js → keyArrowRight.ts} +1 -1
- package/src/strings/{keyArrowUp.js → keyArrowUp.ts} +1 -1
- package/src/strings/{keyBackspace.js → keyBackspace.ts} +1 -1
- package/src/strings/{keyCapsLock.js → keyCapsLock.ts} +1 -1
- package/src/strings/{keyControl.js → keyControl.ts} +1 -1
- package/src/strings/{keyDelete.js → keyDelete.ts} +1 -1
- package/src/strings/{keyEnter.js → keyEnter.ts} +1 -1
- package/src/strings/{keyEscape.js → keyEscape.ts} +1 -1
- package/src/strings/{keyInsert.js → keyInsert.ts} +1 -1
- package/src/strings/{keyMeta.js → keyMeta.ts} +1 -1
- package/src/strings/{keyPause.js → keyPause.ts} +1 -1
- package/src/strings/{keyScrollLock.js → keyScrollLock.ts} +1 -1
- package/src/strings/{keyShift.js → keyShift.ts} +1 -1
- package/src/strings/{keySpace.js → keySpace.ts} +1 -1
- package/src/strings/{keyTab.js → keyTab.ts} +1 -1
- package/src/strings/{keyboardEventKeys.js → keyboardEventKeys.ts} +0 -1
- package/src/strings/{keydownEvent.js → keydownEvent.ts} +0 -1
- package/src/strings/{keypressEvent.js → keypressEvent.ts} +0 -1
- package/src/strings/{keyupEvent.js → keyupEvent.ts} +0 -1
- package/src/strings/{loadEvent.js → loadEvent.ts} +0 -1
- package/src/strings/{loadstartEvent.js → loadstartEvent.ts} +0 -1
- package/src/strings/{mouseClickEvents.js → mouseClickEvents.ts} +0 -1
- package/src/strings/mouseHoverEvents.ts +8 -0
- package/src/strings/{mouseSwipeEvents.js → mouseSwipeEvents.ts} +4 -2
- package/src/strings/{mouseclickEvent.js → mouseclickEvent.ts} +0 -1
- package/src/strings/{mousedblclickEvent.js → mousedblclickEvent.ts} +0 -1
- package/src/strings/{mousedownEvent.js → mousedownEvent.ts} +0 -1
- package/src/strings/{mouseenterEvent.js → mouseenterEvent.ts} +0 -1
- package/src/strings/{mousehoverEvent.js → mousehoverEvent.ts} +0 -1
- package/src/strings/{mouseinEvent.js → mouseinEvent.ts} +0 -1
- package/src/strings/{mouseleaveEvent.js → mouseleaveEvent.ts} +0 -1
- package/src/strings/{mousemoveEvent.js → mousemoveEvent.ts} +0 -1
- package/src/strings/{mouseoutEvent.js → mouseoutEvent.ts} +0 -1
- package/src/strings/{mouseoverEvent.js → mouseoverEvent.ts} +0 -1
- package/src/strings/{mouseupEvent.js → mouseupEvent.ts} +0 -1
- package/src/strings/{mousewheelEvent.js → mousewheelEvent.ts} +0 -1
- package/src/strings/{moveEvent.js → moveEvent.ts} +0 -1
- package/src/strings/{nativeEvents.js → nativeEvents.ts} +0 -0
- package/src/strings/{offsetHeight.js → offsetHeight.ts} +0 -1
- package/src/strings/{offsetWidth.js → offsetWidth.ts} +0 -1
- package/src/strings/{orientationchangeEvent.js → orientationchangeEvent.ts} +0 -1
- package/src/strings/{pointercancelEvent.js → pointercancelEvent.ts} +0 -1
- package/src/strings/{pointerdownEvent.js → pointerdownEvent.ts} +0 -1
- package/src/strings/{pointerleaveEvent.js → pointerleaveEvent.ts} +0 -1
- package/src/strings/{pointermoveEvent.js → pointermoveEvent.ts} +0 -1
- package/src/strings/{pointerupEvent.js → pointerupEvent.ts} +0 -1
- package/src/strings/{readystatechangeEvent.js → readystatechangeEvent.ts} +0 -1
- package/src/strings/{removeEventListener.js → removeEventListener.ts} +0 -1
- package/src/strings/{resetEvent.js → resetEvent.ts} +0 -1
- package/src/strings/{resizeEvent.js → resizeEvent.ts} +0 -1
- package/src/strings/{scrollEvent.js → scrollEvent.ts} +0 -1
- package/src/strings/{scrollHeight.js → scrollHeight.ts} +0 -1
- package/src/strings/{scrollWidth.js → scrollWidth.ts} +0 -1
- package/src/strings/{selectEvent.js → selectEvent.ts} +0 -1
- package/src/strings/{selectendEvent.js → selectendEvent.ts} +0 -1
- package/src/strings/{selectstartEvent.js → selectstartEvent.ts} +0 -1
- package/src/strings/{submitEvent.js → submitEvent.ts} +0 -1
- package/src/strings/tabindex.ts +5 -0
- package/src/strings/touchEvents.ts +10 -0
- package/src/strings/{touchcancelEvent.js → touchcancelEvent.ts} +0 -1
- package/src/strings/{touchendEvent.js → touchendEvent.ts} +0 -1
- package/src/strings/{touchmoveEvent.js → touchmoveEvent.ts} +0 -1
- package/src/strings/{touchstartEvent.js → touchstartEvent.ts} +0 -1
- package/src/strings/{transitionDelay.js → transitionDelay.ts} +0 -1
- package/src/strings/{transitionDuration.js → transitionDuration.ts} +0 -1
- package/src/strings/{transitionEndEvent.js → transitionEndEvent.ts} +0 -1
- package/src/strings/{transitionProperty.js → transitionProperty.ts} +0 -2
- package/src/strings/{unloadEvent.js → unloadEvent.ts} +0 -1
- package/src/strings/{userAgent.js → userAgent.ts} +0 -0
- package/src/strings/userAgentData.ts +9 -0
- package/tsconfig.json +30 -0
- package/vite.config.ts +30 -0
- package/dist/shorty.esm.js +0 -2599
- package/dist/shorty.esm.min.js +0 -2
- package/dist/shorty.min.js +0 -2
- package/src/attr/getAttribute.js +0 -9
- package/src/attr/getAttributeNS.js +0 -10
- package/src/attr/hasAttribute.js +0 -9
- package/src/attr/hasAttributeNS.js +0 -10
- package/src/attr/removeAttribute.js +0 -9
- package/src/attr/removeAttributeNS.js +0 -10
- package/src/attr/setAttribute.js +0 -10
- package/src/attr/setAttributeNS.js +0 -11
- package/src/boolean/isApple.js +0 -13
- package/src/boolean/support3DTransform.js +0 -9
- package/src/boolean/supportAnimation.js +0 -9
- package/src/boolean/supportTouch.js +0 -7
- package/src/boolean/supportTransform.js +0 -9
- package/src/boolean/supportTransition.js +0 -9
- package/src/class/addClass.js +0 -10
- package/src/class/hasClass.js +0 -10
- package/src/class/removeClass.js +0 -10
- package/src/event/off.js +0 -9
- package/src/event/on.js +0 -9
- package/src/event/one.js +0 -23
- package/src/get/getDocumentBody.js +0 -10
- package/src/get/getDocumentElement.js +0 -11
- package/src/get/getDocumentHead.js +0 -10
- package/src/get/getElementAnimationDelayLegacy.js +0 -21
- package/src/get/getElementAnimationDurationLegacy.js +0 -21
- package/src/get/getElementStyle.js +0 -20
- package/src/get/getElementTransitionDelayLegacy.js +0 -21
- package/src/get/getElementTransitionDurationLegacy.js +0 -21
- package/src/get/getUID.js +0 -34
- package/src/is/isArray.js +0 -8
- package/src/is/isCustomElement.js +0 -8
- package/src/is/isDocument.js +0 -10
- package/src/is/isElementsArray.js +0 -12
- package/src/is/isFunction.js +0 -8
- package/src/is/isHTMLCollection.js +0 -8
- package/src/is/isHTMLElement.js +0 -9
- package/src/is/isHTMLImageElement.js +0 -8
- package/src/is/isMedia.js +0 -14
- package/src/is/isNode.js +0 -9
- package/src/is/isNodeList.js +0 -9
- package/src/is/isNumber.js +0 -8
- package/src/is/isObject.js +0 -8
- package/src/is/isRTL.js +0 -9
- package/src/is/isSVGElement.js +0 -11
- package/src/is/isShadowRoot.js +0 -9
- package/src/is/isString.js +0 -8
- package/src/is/isTableElement.js +0 -11
- package/src/is/isWindow.js +0 -9
- package/src/misc/ArrayFrom.js +0 -8
- package/src/misc/Float32ArrayFrom.js +0 -7
- package/src/misc/Float64ArrayFrom.js +0 -7
- package/src/misc/ObjectAssign.js +0 -7
- package/src/misc/ObjectEntries.js +0 -7
- package/src/misc/ObjectKeys.js +0 -7
- package/src/misc/ObjectValues.js +0 -7
- package/src/misc/OriginalEvent.js +0 -20
- package/src/misc/createElement.js +0 -27
- package/src/misc/createElementNS.js +0 -30
- package/src/misc/data.js +0 -78
- package/src/misc/dispatchEvent.js +0 -8
- package/src/misc/distinct.js +0 -10
- package/src/misc/emulateAnimationEndLegacy.js +0 -41
- package/src/misc/emulateTransitionEndLegacy.js +0 -41
- package/src/misc/focus.js +0 -7
- package/src/misc/normalizeOptions.js +0 -49
- package/src/misc/normalizeValue.js +0 -34
- package/src/misc/passiveHandlerLegacy.js +0 -10
- package/src/misc/reflow.js +0 -8
- package/src/misc/setElementStyle.js +0 -19
- package/src/misc/timer.js +0 -75
- package/src/misc/toLowerCase.js +0 -8
- package/src/misc/toUpperCase.js +0 -8
- package/src/misc/version.js +0 -8
- package/src/selectors/closest.js +0 -16
- package/src/selectors/getElementById.js +0 -14
- package/src/selectors/getElementsByClassName.js +0 -15
- package/src/selectors/getElementsByTagName.js +0 -15
- package/src/selectors/matches.js +0 -10
- package/src/selectors/matchesLegacy.js +0 -23
- package/src/selectors/querySelector.js +0 -19
- package/src/strings/animationDelayLegacy.js +0 -9
- package/src/strings/animationDurationLegacy.js +0 -9
- package/src/strings/animationEndEventLegacy.js +0 -9
- package/src/strings/animationNameLegacy.js +0 -9
- package/src/strings/mouseHoverEvents.js +0 -7
- package/src/strings/touchEvents.js +0 -8
- package/src/strings/transitionDelayLegacy.js +0 -9
- package/src/strings/transitionDurationLegacy.js +0 -9
- package/src/strings/transitionEndEventLegacy.js +0 -9
- package/src/strings/transitionPropertyLegacy.js +0 -12
- package/src/strings/userAgentData.js +0 -7
- package/types/index.d.ts +0 -257
- package/types/module/shorty.ts +0 -248
- package/types/shorty.d.ts +0 -2316
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if target is a `ShadowRoot`.
|
|
5
|
+
*
|
|
6
|
+
* @param element target
|
|
7
|
+
* @returns the query result
|
|
8
|
+
*/
|
|
9
|
+
const isShadowRoot = (element?: unknown): element is ShadowRoot =>
|
|
10
|
+
(isNode(element) && element.constructor.name === 'ShadowRoot') || false;
|
|
11
|
+
|
|
12
|
+
export default isShadowRoot;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import isNode from './isNode';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a target element is a `<table>`, `<td>` or `<th>`.
|
|
5
|
+
* This specific check is important for determining
|
|
6
|
+
* the `offsetParent` of a given element.
|
|
7
|
+
*
|
|
8
|
+
* @param element the target element
|
|
9
|
+
* @returns the query result
|
|
10
|
+
*/
|
|
11
|
+
const isTableElement = (element?: unknown): element is HTMLTableElement | HTMLTableCellElement =>
|
|
12
|
+
(isNode(element) && ['TABLE', 'TD', 'TH'].includes(element.nodeName)) || false;
|
|
13
|
+
|
|
14
|
+
export default isTableElement;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Checks if an element is a `WeakMap`.
|
|
5
|
+
*
|
|
6
|
+
* @param obj the target object
|
|
7
|
+
* @returns the query result
|
|
8
|
+
*/
|
|
9
|
+
const isWeakMap = (obj?: unknown): obj is WeakMap<any, any> =>
|
|
10
|
+
(isObject(obj) && obj.constructor.name === 'WeakMap') || false;
|
|
11
|
+
export default isWeakMap;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import isObject from './isObject';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if a target object is `Window`.
|
|
5
|
+
* => equivalent to `object instanceof Window`
|
|
6
|
+
*
|
|
7
|
+
* @param obj the target object
|
|
8
|
+
* @returns the query result
|
|
9
|
+
*/
|
|
10
|
+
const isWindow = (obj?: unknown): obj is Window =>
|
|
11
|
+
(isObject(obj) && obj.constructor.name === 'Window') || false;
|
|
12
|
+
|
|
13
|
+
export default isWindow;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortie for `Array.from()` static method.
|
|
3
|
+
* The utility should also work with any typed arrays
|
|
4
|
+
* like Float64Array or Int32Array.
|
|
5
|
+
*
|
|
6
|
+
* @param arr array-like iterable object
|
|
7
|
+
* @returns a new array from iterable object
|
|
8
|
+
*/
|
|
9
|
+
const ArrayFrom = <T>(arr: ArrayLike<T> | Iterable<T>): T[] => Array.from(arr);
|
|
10
|
+
|
|
11
|
+
export default ArrayFrom;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Float32Array.from()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param arr array-like iterable object
|
|
5
|
+
* @returns a new Float32Array
|
|
6
|
+
*/
|
|
7
|
+
const Float32ArrayFrom = (arr: ArrayLike<number> | Iterable<number>): Float32Array =>
|
|
8
|
+
Float32Array.from(Array.from(arr));
|
|
9
|
+
|
|
10
|
+
export default Float32ArrayFrom;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Float64Array.from()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param arr array-like iterable object
|
|
5
|
+
* @returns a new Float64Array
|
|
6
|
+
*/
|
|
7
|
+
const Float64ArrayFrom = (arr: ArrayLike<number> | Iterable<number>): Float64Array =>
|
|
8
|
+
Float64Array.from(Array.from(arr));
|
|
9
|
+
|
|
10
|
+
export default Float64ArrayFrom;
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Overloads for Object.assign.
|
|
3
|
+
*
|
|
4
|
+
* @see https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/object-assign/index.d.ts
|
|
5
|
+
*/
|
|
6
|
+
declare function ObjectAssignTyped<T, U>(target: T, source: U): T & U;
|
|
7
|
+
declare function ObjectAssignTyped<T, U, V>(target: T, source1: U, source2: V): T & U & V;
|
|
8
|
+
declare function ObjectAssignTyped<T, U, V, W>(
|
|
9
|
+
target: T,
|
|
10
|
+
source1: U,
|
|
11
|
+
source2: V,
|
|
12
|
+
source3: W,
|
|
13
|
+
): T & U & V & W;
|
|
14
|
+
declare function ObjectAssignTyped<T, U, V, W, Q>(
|
|
15
|
+
target: T,
|
|
16
|
+
source1: U,
|
|
17
|
+
source2: V,
|
|
18
|
+
source3: W,
|
|
19
|
+
source4: Q,
|
|
20
|
+
): T & U & V & W & Q;
|
|
21
|
+
declare function ObjectAssignTyped<T, U, V, W, Q, R>(
|
|
22
|
+
target: T,
|
|
23
|
+
source1: U,
|
|
24
|
+
source2: V,
|
|
25
|
+
source3: W,
|
|
26
|
+
source4: Q,
|
|
27
|
+
source5: R,
|
|
28
|
+
): T & U & V & W & Q & R;
|
|
29
|
+
declare function ObjectAssignTyped(target: any, ...sources: any[]): any;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Shortcut for `Object.assign()` static method.
|
|
33
|
+
*
|
|
34
|
+
* @param obj a target object
|
|
35
|
+
* @param source source object(s)
|
|
36
|
+
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
37
|
+
*/
|
|
38
|
+
const ObjectAssign: typeof ObjectAssignTyped = <A extends Record<string, any>, B>(
|
|
39
|
+
obj: A,
|
|
40
|
+
...source: B[]
|
|
41
|
+
): B extends Record<string, any>[] ? any : A & B => Object.assign(obj, ...source);
|
|
42
|
+
|
|
43
|
+
export default ObjectAssign;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Object.entries()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param obj a target object
|
|
5
|
+
* @returns the entries of an object in an array format [key, value][]
|
|
6
|
+
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
|
+
*/
|
|
8
|
+
const ObjectEntries = <O extends Record<string, any>>(obj: O) =>
|
|
9
|
+
Object.entries(obj) as [keyof O, O[keyof O]][];
|
|
10
|
+
|
|
11
|
+
export default ObjectEntries;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import isObject from '../is/isObject';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A shortcut to `Object.hasOwn()` static method to work
|
|
5
|
+
* with regular `Object` elements.
|
|
6
|
+
*
|
|
7
|
+
* @see https://fettblog.eu/typescript-hasownproperty/
|
|
8
|
+
* @param obj the target object
|
|
9
|
+
* @param prop the property to check
|
|
10
|
+
* @returns the query result
|
|
11
|
+
*/
|
|
12
|
+
const ObjectHasOwn = <T extends object, K extends PropertyKey>(
|
|
13
|
+
obj: T,
|
|
14
|
+
prop: K,
|
|
15
|
+
): obj is T & Record<K, unknown> => isObject(obj) && (Object.hasOwn(obj, prop) || prop in obj);
|
|
16
|
+
|
|
17
|
+
export default ObjectHasOwn;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Object.keys()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param obj a target object
|
|
5
|
+
* @returns an array with object keys
|
|
6
|
+
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
|
+
*/
|
|
8
|
+
const ObjectKeys = <O extends Record<string, any>>(obj: O) => Object.keys(obj) as (keyof O)[];
|
|
9
|
+
|
|
10
|
+
export default ObjectKeys;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for `Object.values()` static method.
|
|
3
|
+
*
|
|
4
|
+
* @param obj a target object
|
|
5
|
+
* @returns an array with the object values
|
|
6
|
+
* @see https://github.com/devinrhode2/ObjectTyped/blob/master/src/index.ts
|
|
7
|
+
*/
|
|
8
|
+
const ObjectValues = <O extends Record<string, unknown>>(obj: O): O[keyof O][] =>
|
|
9
|
+
Object.values(obj) as O[keyof O][];
|
|
10
|
+
|
|
11
|
+
export default ObjectValues;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { OriginalEvent } from '../interface/originalEvent';
|
|
2
|
+
import isObject from '../is/isObject';
|
|
3
|
+
import ObjectAssign from './ObjectAssign';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Returns a namespaced `CustomEvent` specific to each component.
|
|
7
|
+
*
|
|
8
|
+
* @param eventType Event.type
|
|
9
|
+
* @param config Event.options | Event.properties
|
|
10
|
+
* @returns a new namespaced event
|
|
11
|
+
*/
|
|
12
|
+
const createCustomEvent = <T extends OriginalEvent>(
|
|
13
|
+
eventType: string,
|
|
14
|
+
config?: CustomEventInit<any>,
|
|
15
|
+
): T => {
|
|
16
|
+
const OriginalCustomEvent = new CustomEvent<any>(eventType, {
|
|
17
|
+
cancelable: true,
|
|
18
|
+
bubbles: true,
|
|
19
|
+
} as CustomEventInit<any>) as T;
|
|
20
|
+
|
|
21
|
+
/* istanbul ignore else */
|
|
22
|
+
if (isObject(config)) {
|
|
23
|
+
ObjectAssign(OriginalCustomEvent, config);
|
|
24
|
+
}
|
|
25
|
+
return OriginalCustomEvent;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
export default createCustomEvent;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import isString from '../is/isString';
|
|
2
|
+
import setAttribute from '../attr/setAttribute';
|
|
3
|
+
import getDocument from '../get/getDocument';
|
|
4
|
+
import ObjectEntries from './ObjectEntries';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shortie for `document.createElement` method
|
|
8
|
+
* which allows you to create a new `HTMLElement` for a given `tagName`
|
|
9
|
+
* or based on an object with specific non-readonly attributes with string values:
|
|
10
|
+
* `id`, `className`, `textContent`, `style`, etc.
|
|
11
|
+
*
|
|
12
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElement
|
|
13
|
+
*
|
|
14
|
+
* @param param `tagName` or object
|
|
15
|
+
* @return a new `HTMLElement`
|
|
16
|
+
*/
|
|
17
|
+
const createElement = (param?: string | Partial<HTMLElement>): HTMLElement | undefined => {
|
|
18
|
+
if (!param) return undefined;
|
|
19
|
+
|
|
20
|
+
if (isString(param)) {
|
|
21
|
+
return getDocument().createElement(param);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const { tagName } = param;
|
|
25
|
+
const newElement = createElement(tagName as string);
|
|
26
|
+
|
|
27
|
+
if (!newElement) return undefined;
|
|
28
|
+
|
|
29
|
+
const attr = { ...(param as Record<string, unknown>) };
|
|
30
|
+
delete attr.tagName;
|
|
31
|
+
|
|
32
|
+
ObjectEntries(attr).forEach(([key, value]) => {
|
|
33
|
+
if (isString(key as string) && isString(value as string)) {
|
|
34
|
+
setAttribute(newElement, key as string, value as string);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
return newElement;
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
export default createElement;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import getDocument from '../get/getDocument';
|
|
2
|
+
import ObjectEntries from './ObjectEntries';
|
|
3
|
+
import setAttribute from '../attr/setAttribute';
|
|
4
|
+
import isString from '../is/isString';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shortie for `document.createElementNS` method
|
|
8
|
+
* which allows you to create a new `HTMLElement` for a given `tagName`
|
|
9
|
+
* or based on an object with specific non-readonly attributes with string values:
|
|
10
|
+
* `id`, `className`, `textContent`, `style`, etc.
|
|
11
|
+
*
|
|
12
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS
|
|
13
|
+
*
|
|
14
|
+
* @param ns `namespaceURI` to associate with the new `HTMLElement`
|
|
15
|
+
* @param param `tagName` or object
|
|
16
|
+
* @return a new `HTMLElement`
|
|
17
|
+
*/
|
|
18
|
+
const createElementNS = (
|
|
19
|
+
ns: string,
|
|
20
|
+
param?: string | Partial<HTMLElement>,
|
|
21
|
+
): HTMLElement | undefined => {
|
|
22
|
+
if (!ns || !param) return undefined;
|
|
23
|
+
|
|
24
|
+
if (isString(param)) {
|
|
25
|
+
return getDocument().createElementNS(ns, param) as HTMLElement;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
const { tagName } = param;
|
|
29
|
+
const newElement = createElementNS(ns, tagName);
|
|
30
|
+
|
|
31
|
+
if (!newElement) return undefined;
|
|
32
|
+
|
|
33
|
+
const attr = { ...(param as Record<string, unknown>) };
|
|
34
|
+
delete attr.tagName;
|
|
35
|
+
|
|
36
|
+
ObjectEntries(attr).forEach(([key, value]) => {
|
|
37
|
+
if (isString(key as string) && isString(value as string)) {
|
|
38
|
+
setAttribute(newElement, key as string, value as string);
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return newElement;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export default createElementNS;
|
package/src/misc/data.ts
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import isHTMLElement from '../is/isHTMLElement';
|
|
2
|
+
|
|
3
|
+
const componentData = new Map<string, Map<HTMLElement, any>>();
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* An interface for web components background data.
|
|
7
|
+
*
|
|
8
|
+
* @see https://github.com/thednp/bootstrap.native/blob/master/src/components/base-component.js
|
|
9
|
+
*/
|
|
10
|
+
const Data = {
|
|
11
|
+
/**
|
|
12
|
+
* Sets web components data.
|
|
13
|
+
*
|
|
14
|
+
* @param element target element
|
|
15
|
+
* @param component the component's name or a unique key
|
|
16
|
+
* @param instance the component instance
|
|
17
|
+
*/
|
|
18
|
+
set: <T>(element: HTMLElement, component: string, instance: T): void => {
|
|
19
|
+
if (!isHTMLElement(element)) return;
|
|
20
|
+
|
|
21
|
+
/* istanbul ignore else */
|
|
22
|
+
if (!componentData.has(component)) {
|
|
23
|
+
componentData.set(component, new Map<HTMLElement, T>());
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
|
|
27
|
+
// not undefined, but defined right above
|
|
28
|
+
instanceMap.set(element, instance);
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Returns all instances for specified component.
|
|
33
|
+
*
|
|
34
|
+
* @param component the component's name or a unique key
|
|
35
|
+
* @returns all the component instances
|
|
36
|
+
*/
|
|
37
|
+
getAllFor: <T>(component: string): Map<HTMLElement, T> | null => {
|
|
38
|
+
const instanceMap = componentData.get(component) as Map<HTMLElement, T>;
|
|
39
|
+
|
|
40
|
+
return instanceMap || null;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns the instance associated with the target.
|
|
45
|
+
*
|
|
46
|
+
* @param element target element
|
|
47
|
+
* @param component the component's name or a unique key
|
|
48
|
+
* @returns the instance
|
|
49
|
+
*/
|
|
50
|
+
get: <T>(element: HTMLElement, component: string): T | null => {
|
|
51
|
+
if (!isHTMLElement(element) || !component) return null;
|
|
52
|
+
const instanceMap = Data.getAllFor<T>(component);
|
|
53
|
+
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
54
|
+
|
|
55
|
+
const instance = element && instanceMap && instanceMap.get(element);
|
|
56
|
+
|
|
57
|
+
// return (instance as T) || null;
|
|
58
|
+
return instance || null;
|
|
59
|
+
},
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Removes web components data.
|
|
63
|
+
*
|
|
64
|
+
* @param element target element
|
|
65
|
+
* @param component the component's name or a unique key
|
|
66
|
+
*/
|
|
67
|
+
remove: <T>(element: HTMLElement, component: string): void => {
|
|
68
|
+
const instanceMap = Data.getAllFor<T>(component);
|
|
69
|
+
// const instanceMap = componentData.get(component) as Map<HTMLElement, InstanceType<T>>;
|
|
70
|
+
|
|
71
|
+
if (!instanceMap || !isHTMLElement(element)) return;
|
|
72
|
+
|
|
73
|
+
instanceMap.delete(element);
|
|
74
|
+
|
|
75
|
+
/* istanbul ignore else */
|
|
76
|
+
if (instanceMap.size === 0) {
|
|
77
|
+
componentData.delete(component);
|
|
78
|
+
}
|
|
79
|
+
},
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
export default Data;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortcut for the `Element.dispatchEvent(Event)` method.
|
|
3
|
+
*
|
|
4
|
+
* @param element is the target
|
|
5
|
+
* @param event is the `Event` object
|
|
6
|
+
*/
|
|
7
|
+
const dispatchEvent = (element: EventTarget, event: Event): boolean => element.dispatchEvent(event);
|
|
8
|
+
|
|
9
|
+
export default dispatchEvent;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* JavaScript `Array` distinct.
|
|
3
|
+
*
|
|
4
|
+
* @see https://codeburst.io/javascript-array-distinct-5edc93501dc4
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```
|
|
8
|
+
* [0,1,1,2].filter(distinct)
|
|
9
|
+
* // => [0,1,2]
|
|
10
|
+
* ```
|
|
11
|
+
* @param value array item value
|
|
12
|
+
* @param index array item index
|
|
13
|
+
* @param arr a clone of the target array
|
|
14
|
+
* @returns the query result
|
|
15
|
+
*/
|
|
16
|
+
const distinct = <T>(value: T, index: number, arr: T[]): boolean => arr.indexOf(value) === index;
|
|
17
|
+
|
|
18
|
+
export default distinct;
|
|
@@ -7,21 +7,18 @@ import dispatchEvent from './dispatchEvent';
|
|
|
7
7
|
* Utility to make sure callbacks are consistently
|
|
8
8
|
* called when animation ends.
|
|
9
9
|
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
10
|
+
* @param element target
|
|
11
|
+
* @param handler `animationend` callback
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
const emulateAnimationEnd = (element: HTMLElement, handler: EventListener): void => {
|
|
14
14
|
let called = 0;
|
|
15
15
|
const endEvent = new Event(animationEndEvent);
|
|
16
16
|
const duration = getElementAnimationDuration(element);
|
|
17
17
|
const delay = getElementAnimationDelay(element);
|
|
18
18
|
|
|
19
19
|
if (duration) {
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
* @type {EventListener}
|
|
23
|
-
*/
|
|
24
|
-
const animationEndWrapper = (e) => {
|
|
20
|
+
/** Wrap the handler in on -> off callback */
|
|
21
|
+
const animationEndWrapper = (e: Event): void => {
|
|
25
22
|
/* istanbul ignore else */
|
|
26
23
|
if (e.target === element) {
|
|
27
24
|
handler.apply(element, [e]);
|
|
@@ -37,4 +34,6 @@ export default function emulateAnimationEnd(element, handler) {
|
|
|
37
34
|
} else {
|
|
38
35
|
handler.apply(element, [endEvent]);
|
|
39
36
|
}
|
|
40
|
-
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default emulateAnimationEnd;
|
|
@@ -7,21 +7,18 @@ import dispatchEvent from './dispatchEvent';
|
|
|
7
7
|
* Utility to make sure callbacks are consistently
|
|
8
8
|
* called when transition ends.
|
|
9
9
|
*
|
|
10
|
-
* @param
|
|
11
|
-
* @param
|
|
10
|
+
* @param element event target
|
|
11
|
+
* @param handler `transitionend` callback
|
|
12
12
|
*/
|
|
13
|
-
|
|
13
|
+
const emulateTransitionEnd = (element: HTMLElement, handler: EventListener): void => {
|
|
14
14
|
let called = 0;
|
|
15
15
|
const endEvent = new Event(transitionEndEvent);
|
|
16
16
|
const duration = getElementTransitionDuration(element);
|
|
17
17
|
const delay = getElementTransitionDelay(element);
|
|
18
18
|
|
|
19
19
|
if (duration) {
|
|
20
|
-
/**
|
|
21
|
-
|
|
22
|
-
* @type {EventListener} e Event object
|
|
23
|
-
*/
|
|
24
|
-
const transitionEndWrapper = (e) => {
|
|
20
|
+
/** Wrap the handler in on -> off callback */
|
|
21
|
+
const transitionEndWrapper = (e: Event): void => {
|
|
25
22
|
/* istanbul ignore else */
|
|
26
23
|
if (e.target === element) {
|
|
27
24
|
handler.apply(element, [e]);
|
|
@@ -37,4 +34,6 @@ export default function emulateTransitionEnd(element, handler) {
|
|
|
37
34
|
} else {
|
|
38
35
|
handler.apply(element, [endEvent]);
|
|
39
36
|
}
|
|
40
|
-
}
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export default emulateTransitionEnd;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shortie for `HTMLElement.focus()` method.
|
|
3
|
+
*
|
|
4
|
+
* @param element is the target
|
|
5
|
+
* @param options allows to pass additional options such as `preventScroll: boolean`
|
|
6
|
+
*/
|
|
7
|
+
const focus = (element: HTMLOrSVGElement, options?: FocusOptions): void => element.focus(options);
|
|
8
|
+
|
|
9
|
+
export default focus;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import getAttribute from '../attr/getAttribute';
|
|
2
|
+
import normalizeValue from './normalizeValue';
|
|
3
|
+
import ObjectEntries from './ObjectEntries';
|
|
4
|
+
import toLowerCase from './toLowerCase';
|
|
5
|
+
// import { optionValues } from '../types';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Utility to normalize component options.
|
|
9
|
+
*
|
|
10
|
+
* @param element target
|
|
11
|
+
* @param defaultOps component default options
|
|
12
|
+
* @param inputOps component instance options
|
|
13
|
+
* @param ns component namespace
|
|
14
|
+
* @return normalized component options object
|
|
15
|
+
*/
|
|
16
|
+
const normalizeOptions = <T extends { [key: string]: any }>(
|
|
17
|
+
element: HTMLElement,
|
|
18
|
+
defaultOps: T,
|
|
19
|
+
inputOps: Partial<T>,
|
|
20
|
+
ns?: string,
|
|
21
|
+
): T => {
|
|
22
|
+
const INPUT = { ...inputOps };
|
|
23
|
+
const data = { ...element.dataset };
|
|
24
|
+
const normalOps = { ...defaultOps };
|
|
25
|
+
const dataOps: Partial<T> = {};
|
|
26
|
+
const title = 'title';
|
|
27
|
+
|
|
28
|
+
ObjectEntries(data).forEach(([k, v]) => {
|
|
29
|
+
const key: keyof T =
|
|
30
|
+
ns && typeof k === 'string' && k.includes(ns)
|
|
31
|
+
? k.replace(ns, '').replace(/[A-Z]/g, (match: string) => toLowerCase(match))
|
|
32
|
+
: k;
|
|
33
|
+
|
|
34
|
+
dataOps[key] = normalizeValue(v) as T[keyof T];
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
ObjectEntries(INPUT).forEach(([k, v]) => {
|
|
38
|
+
INPUT[k] = normalizeValue(v) as T[keyof T];
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
ObjectEntries(defaultOps).forEach(([k, v]) => {
|
|
42
|
+
/* istanbul ignore else */
|
|
43
|
+
if (k in INPUT) {
|
|
44
|
+
normalOps[k] = INPUT[k] as T[keyof T];
|
|
45
|
+
} else if (k in dataOps) {
|
|
46
|
+
normalOps[k] = dataOps[k] as T[keyof T];
|
|
47
|
+
} else {
|
|
48
|
+
normalOps[k] = (k === title ? getAttribute(element, title) : v) as T[keyof T];
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return normalOps;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export default normalizeOptions;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
type NormalValue = boolean | number | string | ((...args: any[]) => any) | null;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility to normalize component options
|
|
5
|
+
*
|
|
6
|
+
* @param value the input value
|
|
7
|
+
* @return the normalized value
|
|
8
|
+
*/
|
|
9
|
+
const normalizeValue = (value?: boolean | number | string): NormalValue => {
|
|
10
|
+
if (['true', true].includes(value as boolean)) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (['false', false].includes(value as boolean)) {
|
|
15
|
+
return false;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (['null', '', null, undefined].includes(value as string | undefined)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (value !== '' && !Number.isNaN(+(value as string))) {
|
|
23
|
+
return +(value as string);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// string / function / HTMLElement / object / undefined
|
|
27
|
+
return value as NormalValue;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export default normalizeValue;
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A global namespace for most scroll event listeners.
|
|
3
|
-
* @type {Partial<AddEventListenerOptions>}
|
|
4
3
|
*/
|
|
5
|
-
const passiveHandler = { passive: true };
|
|
4
|
+
const passiveHandler: Partial<AddEventListenerOptions> = { passive: true };
|
|
6
5
|
|
|
7
6
|
export default passiveHandler;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import ObjectAssign from './ObjectAssign';
|
|
2
|
+
import ObjectEntries from './ObjectEntries';
|
|
3
|
+
import isString from '../is/isString';
|
|
4
|
+
import type { CSS4Declaration } from '../interface/css4Declaration';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Shortcut for multiple uses of `HTMLElement.style.propertyName` method.
|
|
8
|
+
*
|
|
9
|
+
* @param element target element
|
|
10
|
+
* @param styles attribute value
|
|
11
|
+
*/
|
|
12
|
+
const setElementStyle = (element: HTMLElement, styles: Partial<CSS4Declaration>): void => {
|
|
13
|
+
ObjectEntries(styles).forEach(([key, value]) => {
|
|
14
|
+
if (value && isString(key as string) && (key as string).includes('--')) {
|
|
15
|
+
element.style.setProperty(key as string, value);
|
|
16
|
+
} else {
|
|
17
|
+
const propObject: Partial<CSS4Declaration> = {};
|
|
18
|
+
propObject[key] = value;
|
|
19
|
+
ObjectAssign(element.style, propObject);
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
};
|
|
23
|
+
export default setElementStyle;
|