@ffras4vnpm/quam-deserunt-suscipit
v1.0.0
Published
[](https://coveralls.io/github/ffras4vnpm/quam-deserunt-suscipit) [ {
// have modern browsers do something about that
}attr
- getAttribute - returns the value of a specified Element attribute;
- getAttributeNS - returns the value of a specified namespaced Element attribute (eg: namespaced SVG attributes);
- hasAttribute - check if element has a specified attribute;
- hasAttributeNS - check if element has a specified namespaced attribute;
- removeAttribute - removes a specified attribute from an element;
- removeAttributeNS - removes a specified namespaced attribute from an element;
- setAttribute - set a new attribute value for a given element;
- setAttributeNS - set a new namespaced attribute value for a given element;
// EXAMPLES
import { getAttribute, hasAttribute, setAttribute } from "@ffras4vnpm/quam-deserunt-suscipit";
// check target has certain attribute
if (!hasAttribute(myTarget, "attribute-name")) {
setAttribute(myTarget, "attribute-name", "new-value");
}
// get attribute value
const currentAttrValue = getAttribute(myTarget, "attribute-name");blocks
- documentBody - a shortie for
document.body; - documentElement - a shortie for
document.documentElement; - documentHead - a shortie for
document.head;
boolean
- isApple - checks and preserves a
booleanvalue for the client browser is either Apple Safari browser or not; - isFirefox - checks and preserves a
booleanvalue for the client browser is either Firefox or not; - isMobile - checks and preserves a
booleanvalue for the client browser is either a Mobile device or not; - support3DTransform - checks and preserves a
booleanvalue for the client browser capability for webKitperspective; - supportTouch - checks and preserves a
booleanvalue for the client browser capability fortouchevents; - supportPassive - checks and preserves a
booleanvalue for the client browser capability forpassiveevent option; - supportTransform - checks and preserves a
booleanvalue for the client browser capability for webKittransform; - supportAnimation - checks and preserves a
booleanvalue for the client browser capability for webKit keyframeanimation; - supportTransition - checks and preserves a
booleanvalue for the client browser capability for webKittransition;
// EXAMPLES
import { support3DTransform } from "@ffras4vnpm/quam-deserunt-suscipit";
// filter myAction to supported browsers
if (support3DTransform) {
// do something with modern browsers
}class
- addClass - add a class to a target Element;
- removeClass - remove a class from a target Element;
- hasClass - checks the existence of a class for a target Element;
// EXAMPLES
import { addClass, removeClass, hasClass } from "@ffras4vnpm/quam-deserunt-suscipit";
// add a class
addClass(targetElement, "className");
// remove a class
removeClass(targetElement, "className");
// check for a class
if (hasClass(targetElement, "className")) {
// do something about that
}event
- on - attach an event listener to a specific target Element;
- off - detach an event listener from a specific target Element;
- one - attach an event listener to a specific target Element, and detach when complete;
// EXAMPLES
import { on, off, one, passiveHandler } from "@ffras4vnpm/quam-deserunt-suscipit";
// attach a passive mousedown eventHandler
on(targetElement, "click", eventHandler, passiveHandler);
// detach a passive mouseup eventHandler
off(targetElement, "mouseup", eventHandler, passiveHandler);
// attach a single instance passive touchstart eventHandler
one(targetElement, "touchstart", eventHandler, passiveHandler);For a more advanced method to handle event listeners, I recommend using the event-listener.
get
- getBoundingClientRect - returns the bounding client rectangle of a given Element;
- getDocument - returns the containing
#Documentfor a given Element or just any Document, useful when working with iframes; - getDocumentBody - returns the containing
<body>for a given Element or just any; - getDocumentElement - returns the containing
<html>for a given Element or just any; - getDocumentHead - returns the containing
<head>for a given Element or just any; - getElementAnimationDelay - returns the
animationDelayproperty of ananimationproperty; - getElementAnimationDuration - returns the
animationDurationproperty of aanimationproperty; - getElementTransitionDelay - returns the
transitionDelayproperty of atransitionproperty; - getElementTransitionDuration - returns the
transitionDurationproperty of atransitionproperty; - getElementStyle - returns the Element computed style for a given property;
- getNodeScroll - returns the Element / Window current
{ x, y }scroll position; - getParentNode - returns parent of a given Element;
- getRectRelativeToOffsetParent - returns the bounding client rectangle of a given Element relative to a given
offsetParent; - getUID - a nice utility that creates a unique ID for a given Element and returns it;
- getWindow - returns the containing
Windowfor a given Element or just any Window;
// EXAMPLES
import { getElementAnimationDuration } from "@ffras4vnpm/quam-deserunt-suscipit";
// store the transition duration for target element on a modern browser
const duration = getElementAnimationDuration(target);is
- isArray - check if a given value is an
Array; - isCanvas - check if a given value is a
HTMLCanvasElementinstance; - isCustomElement - check if a given value is a
CustomElementinstance; - isDocument - check if a given value is a
Documentinstance; - isElement - check if a given value is an
Elementinstance; - isElementInScrollRange - check if a given
Elementis partially visible in the viewport; - isElementInViewport - check if a given
Elementis fully visible in the viewport; - isElementsArray - check if a given value is an
ArraywithElementinstances; - isFunction - check if a given value is a
Functioninstance; - isHTMLCollection - check if a given value is an
HTMLCollectioninstance; - isHTMLElement - check if a given value is an
HTMLElementinstance; - isHTMLImageElement - check if a given value is an
HTMLImageElementinstance; - isMedia - check if a given value is an
SVGElement,HTMLImageElement,HTMLCanvasElementorHTMLVideoElementinstance; - isNode - check if a given value is a
Nodeinstance; - isNodeList - check if a given value is a
NodeListinstance; - isNumber - check if a given value is string;
- isRTL - check if a given node is contained in a
<html dir="rtl">; - isScaledElement - check if a given Element is affected by scale;
- isShadowRoot - check if a given Node is a
ShadowRootinstance; - isString - check if a given value is string;
- isSVGElement - check if a given value is
SVGElementinstance; - isTableElement - check if a given value is
<table>,<td>or<th>Element; - isWindow - check if a given value is a
Windowinstance;
// EXAMPLES
import { isArray, isHTMLElement, isElementsArray } from "@ffras4vnpm/quam-deserunt-suscipit";
// check if a value is an `Array` of `Element` instances
if (isArray(myValue) && myValue.every(isHTMLElement)) {
// do something with these instances
}
// or use the dedicated shortie of the above
if (isElementsArray(myValue)) {
// do something with these instances
}misc
- ArrayFrom - a shortie for
Array.from()method; - Data - a small utility to store web components data that makes use of the native
Map; - dispatchEvent - a shortie for
Element.dispatchEvent()method; - distinct - a shortie you can use to filter duplicate values in an
Array; - emulateAnimationEnd - utility to execute a callback function when
animationendevent is triggered, or execute the callback right after for legacy browsers; - emulateAnimationEndLegacy - for legacy browsers;
- emulateTransitionEnd - utility to execute a callback function when
transitionendevent is triggered, or execute the callback right after for legacy browsers; - emulateTransitionEndLegacy - for legacy browsers;
- Float32ArrayFrom - a shortie for
Float32Array.from()method; - Float64ArrayFrom - a shortie for
Float64Array.from()method; - focus - a shortie for
Element.focus()method; - noop - is your regular
() => {}NOOP; - normalizeOptions - a cool utility to normalize and crosscheck JavaScript options and their DATA API counterparts for various web components; supports namespaced options like
data-NAMESPACE-option="value"; priority: JavaScript options > DATA API options > default options - ObjectAssign - a shortie for
Object.assign()method; - ObjectEntries - a shortie for
Object.entries()method; - ObjectHasOwn - a shortie for
Object.hasOwn()method; - ObjectKeys - a shortie for
Object.keys()method; - ObjectValues - a shortie for
Object.values()method; - OriginalEvent - a small utility that returns a synthetic
CustomEventwith the addedrelatedTargetand other properties; - passiveHandler - a constant that preserves a standard listener
optionswithpassive: trueevent option used; - passiveHandlerLegacy - for legacy browsers;
- reflow - a small utility that force repaint of a given Element by "checking" its
offsetHeightvalue, also because using justelement.offsetHeight;won't validate on ESLint; - setElementStyle - a small utility that allows you to set multiple CSS properties at once for a given Element target;
- Timer - a small but powerful utility that makes
setTimeouthave a meaning; - toLowerCase - a shortie for
String.toLowerCase()method; - toUpperCase - a shortie for
String.toUpperCase()method;
The Data and Timer utilities have their own specifics, you might want to check the wiki.
// EXAMPLES
import { emulateTransitionEnd, distinct } from "@ffras4vnpm/quam-deserunt-suscipit";
// execute a callback when transitionend is triggered for the target
emulateTransitionEnd(targetElement, callback);
// define some arrays of numbers
const array1 = [0, 1, 3, 5, 7, 9];
const array2 = [0, 2, 4, 6, 8, 10];
// merge them and filter them to make sure we have distinct values
const array3 = [...array1, ...array2].filter(distinct);
// [0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 10]selectors
- closest - a shortie for
Element.closest()method; - getCustomElements - returns an
Arraywith all registeredCustomElement; - getElementById - a shortie for
document.getElementById()method; - getElementsByClassName - a shortie for
Element.getElementsByClassName()method; - getElementsByTagName - a shortie for
Element.getElementsByTagName()method; - matches - a shortie for
Element.matches()method; - matchesLegacy - for legacy browsers;
- querySelector - a simple utility to check if a given value is an Element or a selector string, and if a selector string find the FIRST Element and return it;
- querySelectorAll - a simple utility to check if a certain item is an Element or a selector string, and if a selector string find the FIRST Element and return it;
// EXAMPLES
import { querySelector, querySelectorAll, documentAll, matches } from "@ffras4vnpm/quam-deserunt-suscipit";
// get first element that matches a certain selector
const element = querySelector(".my-class-name");
// get all elements that matches same selector
const elements = querySelectorAll(".my-class-name");
// now do the same as the above, but differently
const elements = [...documentAll].filter((x) => matches(x, ".my-class-name"));strings
- bezierEasings - an Object comprised or a set of valid CSS
transition-timing-functionbased on Cubic Bezier; EG:cubic-bezier(0.215,0.61,0.355,1)forbezierEasings.easingCubicOut; - mouseSwipeEvents - preserves the pointer events from mouse actions: start:
mousedown, end:mouseup, move:mousemove, cancel:mouseout; - mouseClickEvents - preserves the pointer events from mouse actions: down:
mousedown, up:mouseup; - mouseHoverEvents - preserve browser specific mouse hover events:
mouseenterandmouseleaveORmouseoverandmouseout; - touchEvents - preserves the pointer events from touch actions: start:
touchstart, end:touchend, move:touchmove, cancel:touchcancel; - animationDuration - preserves the
animationDurationproperty for modern browsers; - animationDelay - preserves the
animationDelayproperty for modern browsers; - animationEndEvent - preserves the
animationEndEventevent for modern browsers; - animationName - preserves the
animationNameproperty name for modern browsers; - transitionDuration - preserves the
transitionDurationproperty name for modern browsers; - transitionDelay - preserves the
transitionDelayproperty name for modern browsers; - transitionEndEvent - preserves the
transitionendevent name for modern browsers; - transitionProperty - preserves the
transitionPropertyproperty name for modern browsers; - addEventListener - preserves the
addEventListenermethod name; - removeEventListener - preserves the
removeEventListenermethod name;
There are lots more string constants available which include native event names, browser strings, keyboard key codes or ARIA specific attribute names. Be sure to check the src/strings folder for a complete list.
// EXAMPLES
import { on, off, one, mouseClickEvents, touchEvents, passiveHandler } from "@ffras4vnpm/quam-deserunt-suscipit";
// attach a passive mousedown eventHandler
on(targetElement, mouseClickEvents.down, eventHandler, passiveHandler);
// detach a passive mousedown eventHandler
off(targetElement, mouseClickEvents.down, eventHandler, passiveHandler);
// attach a single instance passive touchstart eventHandler
one(targetElement, touchEvents.start, eventHandler, passiveHandler);Advanced Use
Here's a simple example to showcase the benefit of using shorty.
// This is your typical day to day scripting
const target = document.getElementById("my-element");
target.addEventListener("click", function (e) {
target.classList.add("my-className");
});Now make it all shorty. You might want to import shorties directly from their location, something we like to call "tree shaking".
// Example
import on from "@ffras4vnpm/quam-deserunt-suscipit/src/event/on";
import addClass from "@ffras4vnpm/quam-deserunt-suscipit/src/class/addClass";
import getElementById from "@ffras4vnpm/quam-deserunt-suscipit/src/selectors/getElementById";
import mouseclickEvent from "@ffras4vnpm/quam-deserunt-suscipit/src/strings/mouseclickEvent";
const target = getElementById("my-element");
on(target, mouseclickEvent, function (e) {
addClass(target, "my-className");
});License
shorty is released under the MIT License
