Open History to see the last commit
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 }
13}
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 )
21}
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(' ')
30}
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 }
36}
37β
38function parseThreshold(threshold) {
39 return !Array.isArray(threshold)
40 ? [typeof threshold !== 'undefined' ? threshold : 0]
41 : threshold
42}
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
53}