Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

utils.js

53 lines1.4 KB

1export function isDOMNode(node) {
2 return (
3 node && Object.prototype.hasOwnProperty.call(node, 'getBoundingClientRect')
4 )
5}
6​
7export function parseOptions(options = {}) {
8 return {
9 root: options.root || null,
10 rootMargin: parseRootMargin(options.rootMargin),
11 threshold: parseThreshold(options.threshold)
12 }
14​
15export function hasEqualOptions(observer, options) {
16 return (
17 equalPair(options.root, observer.root) &&
18 equalPair(options.rootMargin, observer.rootMargin) &&
19 equalPair(options.threshold, observer.thresholds)
20 )
22​
23function parseRootMargin(rootMargin) {
24 const margins = (rootMargin || '0px').trim().split(/\s+/)
25 margins.forEach(validateRootMargin)
26 margins[1] = margins[1] || margins[0]
27 margins[2] = margins[2] || margins[0]
28 margins[3] = margins[3] || margins[1]
29 return margins.join(' ')
31​
32function validateRootMargin(margin) {
33 if (!/^-?\d*\.?\d+(px|%)$/.test(margin)) {
34 throw new Error('rootMargin must be specified as a CSS margin property')
35 }
37​
38function parseThreshold(threshold) {
39 return !Array.isArray(threshold)
40 ? [typeof threshold !== 'undefined' ? threshold : 0]
41 : threshold
43​
44function equalPair(optionA, optionB) {
45 if (Array.isArray(optionA) && Array.isArray(optionB)) {
46 if (optionA.length === optionB.length) {
47 return optionA.every((element, idx) =>
48 equalPair(optionA[idx], optionB[idx])
49 )
50 }
51 }
52 return optionA === optionB