@splendidlabz/utils 1.5.0-alpha.1 → 1.5.0-alpha.4
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/CHANGELOG.md +24 -0
- package/dom/accessibility.js +24 -13
- package/{actions → dom/actions}/index.js +0 -2
- package/dom/actions/masonry.js +42 -0
- package/{actions → dom/actions}/prefer-horizontal-scroll.js +2 -2
- package/{actions → dom/actions}/sticky.js +3 -6
- package/dom/css-vars.js +8 -0
- package/dom/events.js +2 -1
- package/dom/font-size.js +21 -10
- package/dom/get-element.js +16 -1
- package/dom/index.js +2 -0
- package/dom/media.js +1 -0
- package/dom/observers/index.js +3 -0
- package/dom/observers/intersection-observer.js +43 -0
- package/dom/observers/mutation-observer.js +51 -0
- package/dom/observers/observer.js +18 -0
- package/dom/observers/resize-observer.js +34 -0
- package/eslint.config.js +2 -0
- package/lib/arrays/index.js +49 -26
- package/lib/arrays/sort.spec.js +123 -0
- package/lib/form/sanitize.js +1 -0
- package/lib/numbers/index.js +2 -0
- package/lib/numbers/math.js +9 -0
- package/lib/objects/mix/mix.js +2 -0
- package/lib/objects/mix/mix.test.js +16 -0
- package/lib/objects/nested-property.js +5 -0
- package/node/file-cache.js +1 -1
- package/package.json +6 -8
- package/.eslintrc.cjs +0 -3
- package/.turbo/turbo-lint.log +0 -10
- package/.turbo/turbo-test.log +0 -10
- package/actions/intersection-observer.js +0 -37
- package/actions/masonry.js +0 -75
- package/actions/mutation-observer.js +0 -42
- package/actions/resize-observer.js +0 -33
- package/lib/index.test.js +0 -9
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,29 @@
|
|
|
1
1
|
# @splendidlabz/utils
|
|
2
2
|
|
|
3
|
+
## 1.5.0-alpha.4
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updates
|
|
8
|
+
|
|
9
|
+
## 1.5.0-alpha.3
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- 5fe7c51: - Just bumping
|
|
14
|
+
- Fix bug with `mix` erroring when a `null` or `undefined` source is passed to it. `mix` now ignores `null` and `undefined`.
|
|
15
|
+
- Adds `toDegrees`
|
|
16
|
+
- Adds `toRadians`
|
|
17
|
+
- Adds `getCSSValue` and `setCSSValue`. They're aliases for `getCSSVar` and `setCSSVar` but they're more descriptive for standard properties.
|
|
18
|
+
- Renamed `hasLabel` to `checkForAccessibleName`
|
|
19
|
+
- Add `getParentElement` to get the parent element of a DOM node. If it encounters an `ASTRO-ISLAND` and `ASTRO-SLOT`, it goes one level up.
|
|
20
|
+
|
|
21
|
+
## 1.5.0-alpha.2
|
|
22
|
+
|
|
23
|
+
### Minor Changes
|
|
24
|
+
|
|
25
|
+
- acf9faf: Improve sort function
|
|
26
|
+
|
|
3
27
|
## 1.5.0-alpha.1
|
|
4
28
|
|
|
5
29
|
### Patch Changes
|
package/dom/accessibility.js
CHANGED
|
@@ -1,21 +1,32 @@
|
|
|
1
|
-
export function stopBodyScroll() {
|
|
2
|
-
|
|
1
|
+
// export function stopBodyScroll(element) {
|
|
2
|
+
// if (!element) element = document.body
|
|
3
|
+
// const top = document.documentElement.scrollTop
|
|
4
|
+
// element.style.top = -1 * top + 'px'
|
|
5
|
+
// element.style.overflow = 'hidden'
|
|
6
|
+
// }
|
|
3
7
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
8
|
+
// export function allowBodyScroll() {
|
|
9
|
+
// const top = parseFloat(document.body.style.top) * -1
|
|
10
|
+
// document.body.style.top = null
|
|
11
|
+
// document.body.style.overflow = null
|
|
12
|
+
// document.documentElement.scrollTop = top
|
|
13
|
+
// }
|
|
7
14
|
|
|
8
|
-
export function
|
|
9
|
-
|
|
10
|
-
document.body.style.top = null
|
|
11
|
-
document.body.style.overflow = null
|
|
12
|
-
document.documentElement.scrollTop = top
|
|
15
|
+
export function hasAccessibleName(label, labelledBy) {
|
|
16
|
+
if (label || labelledBy) return true
|
|
13
17
|
}
|
|
14
18
|
|
|
15
|
-
export function
|
|
19
|
+
export function checkForAccessibleName({
|
|
20
|
+
name,
|
|
21
|
+
ariaLabel,
|
|
22
|
+
ariaLabelledBy,
|
|
23
|
+
severity = 'warn',
|
|
24
|
+
} = {}) {
|
|
25
|
+
if (ariaLabel || ariaLabelledBy) return true
|
|
16
26
|
|
|
17
|
-
|
|
18
|
-
if (
|
|
27
|
+
const message = `${name} is missing an accessible name. Please provide an accessible name with 'aria-label' or 'aria-labelledby'.`
|
|
28
|
+
if (severity === 'warn') console.warn(message)
|
|
29
|
+
if (severity === 'error') console.error(message)
|
|
19
30
|
}
|
|
20
31
|
|
|
21
32
|
export function isInvalidAriaPopup(value) {
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { getChildrenElements } from '../get-element.js'
|
|
2
|
+
import { mediaLoaded } from '../media.js'
|
|
3
|
+
import { resizeObserver } from '../observers/resize-observer.js'
|
|
4
|
+
|
|
5
|
+
export async function masonry(node) {
|
|
6
|
+
if (nativeMasonrySupport(node)) return
|
|
7
|
+
|
|
8
|
+
const colGap = parseFloat(getComputedStyle(node).columnGap)
|
|
9
|
+
const items = getChildrenElements(node)
|
|
10
|
+
|
|
11
|
+
node.style.setProperty('row-gap', '1px', 'important')
|
|
12
|
+
node.style.gridAutoRows = '0px'
|
|
13
|
+
|
|
14
|
+
try {
|
|
15
|
+
await mediaLoaded(node)
|
|
16
|
+
} catch (e) {}
|
|
17
|
+
|
|
18
|
+
layout({ colGap, items })
|
|
19
|
+
|
|
20
|
+
const obs = resizeObserver(node, {
|
|
21
|
+
async callback(opts) {
|
|
22
|
+
layout({ colGap, items })
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
async function layout({ colGap, items }) {
|
|
27
|
+
items.forEach(item => {
|
|
28
|
+
const ib = item.getBoundingClientRect()
|
|
29
|
+
item.style.gridRowEnd = `span ${Math.round(ib.height + colGap)}`
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return {
|
|
34
|
+
destroy() {
|
|
35
|
+
obs.disconnect()
|
|
36
|
+
},
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function nativeMasonrySupport(node) {
|
|
41
|
+
return getComputedStyle(node).gridTemplateRows === 'masonry'
|
|
42
|
+
}
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
} from '../dom/index.js'
|
|
5
|
-
import { resizeObserver } from './resize-observer.js'
|
|
1
|
+
import { boundingBoxRelativeToAncestor } from '../bounding-box.js'
|
|
2
|
+
import { resizeObserver } from '../observers/resize-observer.js'
|
|
3
|
+
import { findScrollContainer } from '../ui/scroll-container.js'
|
|
6
4
|
|
|
7
5
|
export function sticky(node, props = {}) {
|
|
8
6
|
const scrollContainer = findScrollContainer(node)
|
|
9
|
-
|
|
10
7
|
let stickyTBLR = getComputedTBLR(node)
|
|
11
8
|
|
|
12
9
|
const obs = resizeObserver(node, {
|
package/dom/css-vars.js
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
import { getElement } from './get-element.js'
|
|
2
2
|
|
|
3
|
+
export function getCSSValue(element, prop) {
|
|
4
|
+
return getCSSVar(element, prop)
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export function setCSSValue(element, prop, value) {
|
|
8
|
+
setCSSVar(element, prop, value)
|
|
9
|
+
}
|
|
10
|
+
|
|
3
11
|
export function getCSSVar(element, prop) {
|
|
4
12
|
element = getElement(element)
|
|
5
13
|
return getComputedStyle(element).getPropertyValue(prop)
|
package/dom/events.js
CHANGED
|
@@ -10,12 +10,13 @@ export function removeListeners(listeners) {
|
|
|
10
10
|
})
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
+
// Dispatch a custom event from a Node.
|
|
13
14
|
export function dispatchEvent(node, eventName, detail, options = {}) {
|
|
14
15
|
node.dispatchEvent(
|
|
15
16
|
new CustomEvent(eventName, {
|
|
16
17
|
...options,
|
|
17
18
|
detail,
|
|
18
|
-
})
|
|
19
|
+
})
|
|
19
20
|
)
|
|
20
21
|
}
|
|
21
22
|
|
package/dom/font-size.js
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
1
|
import { splitUnit } from '../lib/index.js'
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
export function getUnit(value) {
|
|
4
|
+
const [, unit] = splitUnit(value)
|
|
5
|
+
return unit
|
|
6
|
+
}
|
|
7
|
+
|
|
4
8
|
export function em(element = document.body, multiple = 1) {
|
|
5
|
-
const
|
|
6
|
-
return Math.round(
|
|
9
|
+
const value = parseFloat(getComputedStyle(element)['font-size'])
|
|
10
|
+
return Math.round(value * multiple)
|
|
7
11
|
}
|
|
8
12
|
|
|
9
13
|
export function rem(multiple = 1) {
|
|
10
|
-
const
|
|
11
|
-
return Math.round(
|
|
14
|
+
const value = parseFloat(getComputedStyle(document.body)['font-size'])
|
|
15
|
+
return Math.round(value * multiple)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function lh(element = document.body, multiple = 1) {
|
|
19
|
+
const lineHeight = getComputedStyle(element)['line-height']
|
|
20
|
+
const value = parseFloat(lineHeight)
|
|
21
|
+
return Math.round(value * multiple)
|
|
12
22
|
}
|
|
13
23
|
|
|
14
24
|
export function toPx(value, element = null) {
|
|
15
|
-
const [
|
|
25
|
+
const [numeric, unit] = splitUnit(value)
|
|
16
26
|
let finalValue = 0
|
|
17
27
|
|
|
18
|
-
if (unit === 'rem') finalValue = rem(
|
|
19
|
-
if (unit === 'em') finalValue = em(element,
|
|
20
|
-
if (unit === '
|
|
21
|
-
if (
|
|
28
|
+
if (unit === 'rem') finalValue = rem(numeric)
|
|
29
|
+
if (unit === 'em') finalValue = em(element, numeric)
|
|
30
|
+
if (unit === 'lh') finalValue = lh(element, numeric)
|
|
31
|
+
if (unit === 'px') finalValue = numeric
|
|
32
|
+
if (!unit) finalValue = numeric // Default to px offsets
|
|
22
33
|
|
|
23
34
|
return finalValue
|
|
24
35
|
}
|
package/dom/get-element.js
CHANGED
|
@@ -2,6 +2,15 @@
|
|
|
2
2
|
// If the selector is already an HTMLElement, it will return the selector itself.
|
|
3
3
|
// Otherwise, it will return the first element that matches the selector.
|
|
4
4
|
|
|
5
|
+
const astroNodes = ['ASTRO-SLOT', 'ASTRO-ISLAND']
|
|
6
|
+
|
|
7
|
+
// Returns the type of the node.
|
|
8
|
+
export function getNodeType(node) {
|
|
9
|
+
if (node instanceof Element) return 'element'
|
|
10
|
+
if (node instanceof NodeList) return 'nodelist'
|
|
11
|
+
if (Array.isArray(node)) return 'array'
|
|
12
|
+
}
|
|
13
|
+
|
|
5
14
|
export function getElement(selector) {
|
|
6
15
|
if (!selector) return
|
|
7
16
|
if (selector instanceof HTMLElement) return selector
|
|
@@ -16,9 +25,15 @@ export function getChildrenElements(node) {
|
|
|
16
25
|
return Array.from(children)
|
|
17
26
|
}
|
|
18
27
|
|
|
28
|
+
export function getParentElement(element) {
|
|
29
|
+
const parent = element.parentElement
|
|
30
|
+
if (astroNodes.includes(parent.nodeName)) return getParentElement(parent)
|
|
31
|
+
return parent
|
|
32
|
+
}
|
|
33
|
+
|
|
19
34
|
export function getSiblingElements(element) {
|
|
20
35
|
return Array.from(element.parentElement.children).filter(
|
|
21
|
-
child => child !== element
|
|
36
|
+
child => child !== element
|
|
22
37
|
)
|
|
23
38
|
}
|
|
24
39
|
|
package/dom/index.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export * from './accessibility.js'
|
|
2
|
+
export * from './actions/index.js'
|
|
2
3
|
export * from './bounding-box.js'
|
|
3
4
|
export * from './clipboard.js'
|
|
4
5
|
export * from './cookie.js'
|
|
@@ -11,6 +12,7 @@ export * from './hash.js'
|
|
|
11
12
|
export * from './keyboard.js'
|
|
12
13
|
export * from './local-store.js'
|
|
13
14
|
export * from './media.js'
|
|
15
|
+
export * from './observers/index.js'
|
|
14
16
|
export * from './pkce.js'
|
|
15
17
|
export * from './query-params.js'
|
|
16
18
|
export * from './random-string.js'
|
package/dom/media.js
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { dispatchEvent } from '../events.js'
|
|
2
|
+
import { useObserverMethodOnTarget } from './observer.js'
|
|
3
|
+
|
|
4
|
+
// See https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#options for options
|
|
5
|
+
export function intersectionObserver(target, options = {}) {
|
|
6
|
+
const { callback, ...opts } = options
|
|
7
|
+
const observer = new IntersectionObserver(observerFn, opts)
|
|
8
|
+
|
|
9
|
+
useObserverMethodOnTarget(target, observer, 'observe')
|
|
10
|
+
|
|
11
|
+
function observerFn(entries) {
|
|
12
|
+
for (const entry of entries) {
|
|
13
|
+
if (callback) callback({ entry, entries, observer })
|
|
14
|
+
else dispatchEvent(target, 'intersect', { entry, entries, observer })
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
observer,
|
|
20
|
+
observe(target, options) {
|
|
21
|
+
useObserverMethodOnTarget(target, observer, 'observe', options)
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
unobserve(target) {
|
|
25
|
+
useObserverMethodOnTarget(target, observer, 'unobserve')
|
|
26
|
+
},
|
|
27
|
+
|
|
28
|
+
takeRecords() {
|
|
29
|
+
return observer.takeRecords()
|
|
30
|
+
},
|
|
31
|
+
|
|
32
|
+
disconnect() {
|
|
33
|
+
// Take records before disconnecting.
|
|
34
|
+
const records = observer.takeRecords()
|
|
35
|
+
observer.disconnect()
|
|
36
|
+
if (records.length > 0) observerFn(records)
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
destroy() {
|
|
40
|
+
observer.disconnect()
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
import { dispatchEvent } from '../events.js'
|
|
3
|
+
import { useObserverMethodOnTarget } from './observer.js'
|
|
4
|
+
|
|
5
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
|
|
6
|
+
|
|
7
|
+
// These are simply options that will often be used. Check the docs for other options. Adding them here simply because it's easier to refer them here than reading the docs.
|
|
8
|
+
const defaultOptions = {
|
|
9
|
+
attributes: false,
|
|
10
|
+
childList: false,
|
|
11
|
+
subtree: false,
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function mutationObserver(target, options) {
|
|
15
|
+
options = { ...defaultOptions, ...options }
|
|
16
|
+
const { callback, ...opts } = options
|
|
17
|
+
const observer = new MutationObserver(observerFn)
|
|
18
|
+
|
|
19
|
+
if (target === window) target = document.body
|
|
20
|
+
useObserverMethodOnTarget(target, observer, 'observe', opts)
|
|
21
|
+
|
|
22
|
+
function observerFn(entries) {
|
|
23
|
+
for (const entry of entries) {
|
|
24
|
+
if (callback) callback({ entry, entries, observer })
|
|
25
|
+
else dispatchEvent(target, 'mutate', { entry, entries, observer })
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return {
|
|
30
|
+
observer,
|
|
31
|
+
observe(target, options) {
|
|
32
|
+
useObserverMethodOnTarget(target, observer, 'observe', options)
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
takeRecords() {
|
|
36
|
+
return observer.takeRecords()
|
|
37
|
+
},
|
|
38
|
+
|
|
39
|
+
disconnect() {
|
|
40
|
+
// Take records before disconnecting.
|
|
41
|
+
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/takeRecords
|
|
42
|
+
const records = observer.takeRecords()
|
|
43
|
+
observer.disconnect()
|
|
44
|
+
if (records.length > 0) observerFn(records)
|
|
45
|
+
},
|
|
46
|
+
|
|
47
|
+
destroy() {
|
|
48
|
+
observer.disconnect()
|
|
49
|
+
},
|
|
50
|
+
}
|
|
51
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { getNodeType } from '../get-element.js'
|
|
2
|
+
|
|
3
|
+
export function useObserverMethodOnTarget(
|
|
4
|
+
target,
|
|
5
|
+
observer,
|
|
6
|
+
method = 'observe',
|
|
7
|
+
options = undefined
|
|
8
|
+
) {
|
|
9
|
+
const targetType = getNodeType(target)
|
|
10
|
+
if (targetType === 'element') observer[method](target, options)
|
|
11
|
+
if (targetType === 'nodelist') {
|
|
12
|
+
const elements = Array.from(target)
|
|
13
|
+
elements.forEach(element => observer[method](element, options))
|
|
14
|
+
}
|
|
15
|
+
if (targetType === 'array') {
|
|
16
|
+
target.forEach(element => observer[method](element, options))
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/* eslint-env browser */
|
|
2
|
+
import { dispatchEvent } from '../events.js'
|
|
3
|
+
import { useObserverMethodOnTarget } from './observer.js'
|
|
4
|
+
|
|
5
|
+
export function resizeObserver(target, options) {
|
|
6
|
+
// We set the option here to prevent observer from being created unless necessary.
|
|
7
|
+
options = { observe: true, ...options }
|
|
8
|
+
if (!options.observe) return
|
|
9
|
+
const { callback, ...opts } = options
|
|
10
|
+
const observer = new ResizeObserver(observerFn)
|
|
11
|
+
|
|
12
|
+
if (target === window) target = document.body
|
|
13
|
+
useObserverMethodOnTarget(target, observer, 'observe', opts)
|
|
14
|
+
|
|
15
|
+
function observerFn(entries) {
|
|
16
|
+
for (const entry of entries) {
|
|
17
|
+
if (callback) callback({ entry, entries, observer })
|
|
18
|
+
else dispatchEvent(target, 'resize-obs', { entry, entries, observer })
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return {
|
|
23
|
+
observe(target, options) {
|
|
24
|
+
useObserverMethodOnTarget(target, observer, 'observe', options)
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
unobserve(target) {
|
|
28
|
+
useObserverMethodOnTarget(target, observer, 'unobserve')
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
disconnect: _ => observer.disconnect(),
|
|
32
|
+
destroy: _ => observer.disconnect(),
|
|
33
|
+
}
|
|
34
|
+
}
|
package/eslint.config.js
ADDED
package/lib/arrays/index.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { getNestedValue } from '../objects/nested-property.js'
|
|
2
|
+
|
|
1
3
|
export function last(array, index) {
|
|
2
4
|
return index === array.length - 1
|
|
3
5
|
}
|
|
@@ -25,38 +27,59 @@ export function shuffle(array) {
|
|
|
25
27
|
}
|
|
26
28
|
|
|
27
29
|
/**
|
|
28
|
-
* Sorts an array of objects or strings without mutating the array
|
|
30
|
+
* Sorts an array of objects, numbers, or strings without mutating the original array
|
|
31
|
+
* @param {Array} array - Array to sort
|
|
32
|
+
* @param {Object} options - Sort options
|
|
33
|
+
* @param {string|string[]|null} options.props - Property or array of properties to sort by. If null, sorts simple arrays
|
|
34
|
+
* @param {boolean} options.reverse - Whether to sort in reverse order
|
|
29
35
|
*/
|
|
30
|
-
export function sort(
|
|
31
|
-
array,
|
|
32
|
-
{
|
|
33
|
-
property, // Property to sort by. Used when sorting array of objects.
|
|
34
|
-
order = 'asc', // 'asc' or 'desc'
|
|
35
|
-
},
|
|
36
|
-
) {
|
|
36
|
+
export function sort(array, { props = null, reverse = false } = {}) {
|
|
37
37
|
const clone = array.slice()
|
|
38
|
-
const sorted = clone.sort((a, b) => {
|
|
39
|
-
let one = a
|
|
40
|
-
let two = b
|
|
41
|
-
|
|
42
|
-
// Use the `sortBy` property if sorting by objects
|
|
43
|
-
if (property) {
|
|
44
|
-
one = a[property]
|
|
45
|
-
two = b[property]
|
|
46
|
-
}
|
|
47
38
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
39
|
+
// Handle simple arrays (numbers or strings)
|
|
40
|
+
if (props === null) {
|
|
41
|
+
return clone.sort((a, b) => {
|
|
42
|
+
const comparison = compareValues(a, b)
|
|
43
|
+
return reverse ? -comparison : comparison
|
|
44
|
+
})
|
|
45
|
+
}
|
|
52
46
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
47
|
+
// Handle objects in arrays
|
|
48
|
+
const properties = Array.isArray(props) ? props : [props]
|
|
49
|
+
|
|
50
|
+
return clone.sort((a, b) => {
|
|
51
|
+
for (const property of properties) {
|
|
52
|
+
const aValue = getNestedValue(a, property)
|
|
53
|
+
const bValue = getNestedValue(b, property)
|
|
54
|
+
|
|
55
|
+
// Skip if values are equal
|
|
56
|
+
if (aValue === bValue) continue
|
|
57
57
|
|
|
58
|
+
// Push null/undefined values to the end
|
|
59
|
+
if (aValue == null) return 1
|
|
60
|
+
if (bValue == null) return -1
|
|
61
|
+
|
|
62
|
+
// Compare values and handle reverse sort
|
|
63
|
+
const comparison = compareValues(aValue, bValue)
|
|
64
|
+
return reverse ? -comparison : comparison
|
|
65
|
+
}
|
|
58
66
|
return 0
|
|
59
67
|
})
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Compare values for sorting
|
|
71
|
+
function compareValues(a, b) {
|
|
72
|
+
// Compare numbers
|
|
73
|
+
if (typeof a === 'number' && typeof b === 'number') return a - b
|
|
74
|
+
|
|
75
|
+
// Compare strings
|
|
76
|
+
if (typeof a === 'string' && typeof b === 'string') return a.localeCompare(b)
|
|
77
|
+
|
|
78
|
+
// Compare dates
|
|
79
|
+
const aDate = new Date(a)
|
|
80
|
+
const bDate = new Date(b)
|
|
81
|
+
if (!isNaN(aDate) && !isNaN(bDate)) return aDate.getTime() - bDate.getTime()
|
|
60
82
|
|
|
61
|
-
|
|
83
|
+
// Convert into strings and compare
|
|
84
|
+
return String(a).localeCompare(String(b))
|
|
62
85
|
}
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
import { describe, expect, it } from 'vitest'
|
|
2
|
+
|
|
3
|
+
import { sort } from './index.js'
|
|
4
|
+
|
|
5
|
+
describe('Sort by', _ => {
|
|
6
|
+
it('Standard array - numbers', async () => {
|
|
7
|
+
const array = [3, 1, 2]
|
|
8
|
+
const sorted = sort(array)
|
|
9
|
+
const reversed = sort(array, { reverse: true })
|
|
10
|
+
expect(sorted).toEqual([1, 2, 3])
|
|
11
|
+
expect(reversed).toEqual([3, 2, 1])
|
|
12
|
+
})
|
|
13
|
+
|
|
14
|
+
it('Standard array - strings', async () => {
|
|
15
|
+
const array = ['c', 'a', 'b']
|
|
16
|
+
const sorted = sort(array)
|
|
17
|
+
const reversed = sort(array, { reverse: true })
|
|
18
|
+
expect(sorted).toEqual(['a', 'b', 'c'])
|
|
19
|
+
expect(reversed).toEqual(['c', 'b', 'a'])
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
it('Standard array - dates', async () => {
|
|
23
|
+
const array = [
|
|
24
|
+
new Date('2021-05-18'),
|
|
25
|
+
new Date('2022-01-01'),
|
|
26
|
+
new Date('2025-03-09'),
|
|
27
|
+
]
|
|
28
|
+
const sorted = sort(array)
|
|
29
|
+
const reversed = sort(array, { reverse: true })
|
|
30
|
+
expect(sorted).toEqual([
|
|
31
|
+
new Date('2021-05-18'),
|
|
32
|
+
new Date('2022-01-01'),
|
|
33
|
+
new Date('2025-03-09'),
|
|
34
|
+
])
|
|
35
|
+
expect(reversed).toEqual([
|
|
36
|
+
new Date('2025-03-09'),
|
|
37
|
+
new Date('2022-01-01'),
|
|
38
|
+
new Date('2021-05-18'),
|
|
39
|
+
])
|
|
40
|
+
})
|
|
41
|
+
it('Array of objects — simple integer', async () => {
|
|
42
|
+
const array = [
|
|
43
|
+
{ name: 'Ace', age: 30 },
|
|
44
|
+
{ name: 'Betty', age: 25 },
|
|
45
|
+
{ name: 'Candy', age: 35 },
|
|
46
|
+
]
|
|
47
|
+
const sorted = sort(array, { props: 'age' })
|
|
48
|
+
const reversed = sort(array, { props: 'age', reverse: true })
|
|
49
|
+
expect(sorted).toEqual([
|
|
50
|
+
{ name: 'Betty', age: 25 },
|
|
51
|
+
{ name: 'Ace', age: 30 },
|
|
52
|
+
{ name: 'Candy', age: 35 },
|
|
53
|
+
])
|
|
54
|
+
expect(reversed).toEqual([
|
|
55
|
+
{ name: 'Candy', age: 35 },
|
|
56
|
+
{ name: 'Ace', age: 30 },
|
|
57
|
+
{ name: 'Betty', age: 25 },
|
|
58
|
+
])
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
it('Array of objects — simple string', async () => {
|
|
62
|
+
const array = [
|
|
63
|
+
{ name: 'Ace', age: 30 },
|
|
64
|
+
{ name: 'Betty', age: 25 },
|
|
65
|
+
{ name: 'Candy', age: 35 },
|
|
66
|
+
]
|
|
67
|
+
const sorted = sort(array, { props: 'name' })
|
|
68
|
+
const reversed = sort(array, { props: 'name', reverse: true })
|
|
69
|
+
expect(sorted).toEqual([
|
|
70
|
+
{ name: 'Ace', age: 30 },
|
|
71
|
+
{ name: 'Betty', age: 25 },
|
|
72
|
+
{ name: 'Candy', age: 35 },
|
|
73
|
+
])
|
|
74
|
+
expect(reversed).toEqual([
|
|
75
|
+
{ name: 'Candy', age: 35 },
|
|
76
|
+
{ name: 'Betty', age: 25 },
|
|
77
|
+
{ name: 'Ace', age: 30 },
|
|
78
|
+
])
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('Double property', async () => {
|
|
82
|
+
const array = [
|
|
83
|
+
{ name: 'Ace', age: 30 },
|
|
84
|
+
{ name: 'Betty', age: 25 },
|
|
85
|
+
{ name: 'Candy', age: 35 },
|
|
86
|
+
{ name: 'Danny', age: 30 },
|
|
87
|
+
]
|
|
88
|
+
const sorted = sort(array, { props: ['age', 'name'] })
|
|
89
|
+
const reversed = sort(array, { props: ['age', 'name'], reverse: true })
|
|
90
|
+
expect(sorted).toEqual([
|
|
91
|
+
{ name: 'Betty', age: 25 },
|
|
92
|
+
{ name: 'Ace', age: 30 },
|
|
93
|
+
{ name: 'Danny', age: 30 },
|
|
94
|
+
{ name: 'Candy', age: 35 },
|
|
95
|
+
])
|
|
96
|
+
expect(reversed).toEqual([
|
|
97
|
+
{ name: 'Candy', age: 35 },
|
|
98
|
+
{ name: 'Danny', age: 30 },
|
|
99
|
+
{ name: 'Ace', age: 30 },
|
|
100
|
+
{ name: 'Betty', age: 25 },
|
|
101
|
+
])
|
|
102
|
+
})
|
|
103
|
+
|
|
104
|
+
it('Nested value', async () => {
|
|
105
|
+
const array = [
|
|
106
|
+
{ data: { value: 1 } },
|
|
107
|
+
{ data: { value: 2 } },
|
|
108
|
+
{ data: { value: 3 } },
|
|
109
|
+
]
|
|
110
|
+
const sorted = sort(array, { props: 'data.value' })
|
|
111
|
+
const reversed = sort(array, { props: 'data.value', reverse: true })
|
|
112
|
+
expect(sorted).toEqual([
|
|
113
|
+
{ data: { value: 1 } },
|
|
114
|
+
{ data: { value: 2 } },
|
|
115
|
+
{ data: { value: 3 } },
|
|
116
|
+
])
|
|
117
|
+
expect(reversed).toEqual([
|
|
118
|
+
{ data: { value: 3 } },
|
|
119
|
+
{ data: { value: 2 } },
|
|
120
|
+
{ data: { value: 1 } },
|
|
121
|
+
])
|
|
122
|
+
})
|
|
123
|
+
})
|
package/lib/form/sanitize.js
CHANGED
package/lib/numbers/index.js
CHANGED
package/lib/objects/mix/mix.js
CHANGED
|
@@ -322,3 +322,19 @@ it('Safe Merge', () => {
|
|
|
322
322
|
expect(mixed.__proto__).toEqual({})
|
|
323
323
|
/* eslint-enable */
|
|
324
324
|
})
|
|
325
|
+
|
|
326
|
+
it('should skip null and undefined sources', () => {
|
|
327
|
+
const obj1 = { a: 1 }
|
|
328
|
+
const obj2 = { b: 2 }
|
|
329
|
+
const expected = { a: 1, b: 2 }
|
|
330
|
+
|
|
331
|
+
expect(mix(obj1, null, obj2)).toEqual(expected)
|
|
332
|
+
expect(mix(null, obj1, obj2)).toEqual(expected)
|
|
333
|
+
expect(mix(obj1, obj2, null)).toEqual(expected)
|
|
334
|
+
|
|
335
|
+
expect(mix(obj1, undefined, obj2)).toEqual(expected)
|
|
336
|
+
expect(mix(undefined, obj1, obj2)).toEqual(expected)
|
|
337
|
+
expect(mix(obj1, obj2, undefined)).toEqual(expected)
|
|
338
|
+
|
|
339
|
+
expect(mix(null, obj1, undefined, obj2, null)).toEqual(expected)
|
|
340
|
+
})
|
|
@@ -34,3 +34,8 @@ export function getNestedProperty2(object, path) {
|
|
|
34
34
|
const pathArray = path.split('.')
|
|
35
35
|
return getNestedProperty(object, pathArray)
|
|
36
36
|
}
|
|
37
|
+
|
|
38
|
+
// Same as dot syntax, but terser and more readable name
|
|
39
|
+
export function getNestedValue(object, path) {
|
|
40
|
+
return path.split('.').reduce((acc, part) => acc?.[part], object)
|
|
41
|
+
}
|
package/node/file-cache.js
CHANGED
|
@@ -119,7 +119,7 @@ export async function getLastModifiedTime(globPath) {
|
|
|
119
119
|
files = await Promise.all(files.map(fs.stat))
|
|
120
120
|
files = files.map(file => file.mtimeMs)
|
|
121
121
|
|
|
122
|
-
const sorted = sort(files, {
|
|
122
|
+
const sorted = sort(files, { props: 'mtimeMs', reverse: true })
|
|
123
123
|
return sorted[0] || 0
|
|
124
124
|
}
|
|
125
125
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@splendidlabz/utils",
|
|
3
|
-
"version": "1.5.0-alpha.
|
|
3
|
+
"version": "1.5.0-alpha.4",
|
|
4
4
|
"description": "",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -9,8 +9,6 @@
|
|
|
9
9
|
"./lib/*": "./lib/*.js",
|
|
10
10
|
"./dom": "./dom/index.js",
|
|
11
11
|
"./dom/*": "./dom/*.js",
|
|
12
|
-
"./actions": "./actions/index.js",
|
|
13
|
-
"./actions/*": "./actions/*.js",
|
|
14
12
|
"./node": "./node/index.js",
|
|
15
13
|
"./node/*": "./node/*.js"
|
|
16
14
|
},
|
|
@@ -23,16 +21,16 @@
|
|
|
23
21
|
"dependencies": {
|
|
24
22
|
"dompurify": "^3.2.4",
|
|
25
23
|
"glob-promise": "^6.0.7",
|
|
26
|
-
"pluralize": "^8.0.0",
|
|
27
|
-
"statuses": "^2.0.1",
|
|
28
24
|
"marked": "^15.0.7",
|
|
29
25
|
"marked-gfm-heading-id": "^4.1.1",
|
|
30
|
-
"marked-mangle": "^1.1.10"
|
|
26
|
+
"marked-mangle": "^1.1.10",
|
|
27
|
+
"pluralize": "^8.0.0",
|
|
28
|
+
"statuses": "^2.0.1"
|
|
31
29
|
},
|
|
32
30
|
"devDependencies": {
|
|
33
|
-
"@splendidlabz/eslint-config": "
|
|
31
|
+
"@splendidlabz/eslint-config": "2.0.0-alpha.1",
|
|
34
32
|
"jsdom": "^26.0.0",
|
|
35
33
|
"np": "^10.2.0",
|
|
36
|
-
"vitest": "^3.
|
|
34
|
+
"vitest": "^3.1.1"
|
|
37
35
|
}
|
|
38
36
|
}
|
package/.eslintrc.cjs
DELETED
package/.turbo/turbo-lint.log
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @splendidlabs/utils@1.0.3 lint
|
|
3
|
-
> eslint . --fix
|
|
4
|
-
|
|
5
|
-
[0m[0m
|
|
6
|
-
[0m[4m/Users/zellwk/projects/@splendidlabs/packages/utils/lib/index.test.js[24m[0m
|
|
7
|
-
[0m [2m4:10[22m [31merror[39m 'expect' is defined but never used [2mno-unused-vars[22m[0m
|
|
8
|
-
[0m[0m
|
|
9
|
-
[0m[31m[1m✖ 1 problem (1 error, 0 warnings)[22m[39m[0m
|
|
10
|
-
[0m[31m[1m[22m[39m[0m
|
package/.turbo/turbo-test.log
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
|
|
2
|
-
> @splendidlabs/utils@1.0.3 test
|
|
3
|
-
> vitest run
|
|
4
|
-
|
|
5
|
-
sh: vitest: command not found
|
|
6
|
-
[37;40mnpm[0m [0m[31;40mERR![0m [0m[35mLifecycle script `test` failed with error:[0m
|
|
7
|
-
[0m[37;40mnpm[0m [0m[31;40mERR![0m [0m[35mError: command failed[0m
|
|
8
|
-
[0m[37;40mnpm[0m [0m[31;40mERR![0m [0m[35m in workspace: @splendidlabs/utils@1.0.3[0m
|
|
9
|
-
[0m[37;40mnpm[0m [0m[31;40mERR![0m [0m[35m at location: /Users/zellwk/projects/@splendidlabs/packages/utils[0m
|
|
10
|
-
[0m
|
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
// See https://developer.mozilla.org/en-US/docs/Web/API/IntersectionObserver/IntersectionObserver#options for options
|
|
2
|
-
const options = {}
|
|
3
|
-
|
|
4
|
-
export function intersectionObserver(node, options) {
|
|
5
|
-
const { callback, opts } = options
|
|
6
|
-
const observer = new IntersectionObserver(observerFn, opts)
|
|
7
|
-
|
|
8
|
-
function observerFn(entries, observer) {
|
|
9
|
-
entries.forEach(entry => {
|
|
10
|
-
for (const entry of entries) {
|
|
11
|
-
// Needs testing to see what's in entry
|
|
12
|
-
if (callback) callback({ node, entry, observer })
|
|
13
|
-
else {
|
|
14
|
-
node.dispatchEvent(
|
|
15
|
-
new CustomEvent('intersect', {
|
|
16
|
-
detail: { node, entry, observer },
|
|
17
|
-
}),
|
|
18
|
-
)
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
})
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
observer.observe(node)
|
|
25
|
-
|
|
26
|
-
return {
|
|
27
|
-
observer,
|
|
28
|
-
disconnect() {
|
|
29
|
-
observer.disconnect()
|
|
30
|
-
},
|
|
31
|
-
|
|
32
|
-
// Destroy is used for Svelte actions
|
|
33
|
-
destroy() {
|
|
34
|
-
observer.disconnect()
|
|
35
|
-
},
|
|
36
|
-
}
|
|
37
|
-
}
|
package/actions/masonry.js
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { getChildrenElements, mediaLoaded } from '../dom'
|
|
2
|
-
|
|
3
|
-
import { resizeObserver } from './resize-observer'
|
|
4
|
-
|
|
5
|
-
export async function masonry(node) {
|
|
6
|
-
if (nativeMasonrySupport(node)) return
|
|
7
|
-
|
|
8
|
-
const colGap = parseFloat(getComputedStyle(node).columnGap)
|
|
9
|
-
const items = getChildrenElements(node)
|
|
10
|
-
|
|
11
|
-
node.style.setProperty('row-gap', '1px', 'important')
|
|
12
|
-
node.style.gridAutoRows = '0px'
|
|
13
|
-
|
|
14
|
-
try {
|
|
15
|
-
await mediaLoaded(node)
|
|
16
|
-
} catch (e) {}
|
|
17
|
-
|
|
18
|
-
layout({ colGap, items })
|
|
19
|
-
|
|
20
|
-
const obs = resizeObserver(node, {
|
|
21
|
-
async callback(opts) {
|
|
22
|
-
layout({ colGap, items })
|
|
23
|
-
},
|
|
24
|
-
})
|
|
25
|
-
|
|
26
|
-
async function layout({ colGap, items }) {
|
|
27
|
-
items.forEach(item => {
|
|
28
|
-
const ib = item.getBoundingClientRect()
|
|
29
|
-
item.style.gridRowEnd = `span ${Math.round(ib.height + colGap)}`
|
|
30
|
-
})
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
return {
|
|
34
|
-
destroy() {
|
|
35
|
-
obs.disconnect()
|
|
36
|
-
},
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
// export async function masonry(node) {
|
|
41
|
-
// if (typeof window === 'undefined') return
|
|
42
|
-
// if (nativeMasonrySupport(node)) return
|
|
43
|
-
|
|
44
|
-
// let items
|
|
45
|
-
// let gap
|
|
46
|
-
|
|
47
|
-
// node.style.setProperty('row-gap', '1px', 'important')
|
|
48
|
-
// node.style.gridAutoRows = '0px'
|
|
49
|
-
// // await mediaLoaded(node)
|
|
50
|
-
|
|
51
|
-
// const obs = resizeObserver(node, {
|
|
52
|
-
// async callback(opts) {
|
|
53
|
-
// items = getChildrenElements(node)
|
|
54
|
-
// gap = parseFloat(getComputedStyle(node).columnGap)
|
|
55
|
-
// layout(node)
|
|
56
|
-
// },
|
|
57
|
-
// })
|
|
58
|
-
|
|
59
|
-
// async function layout() {
|
|
60
|
-
// items.forEach(item => {
|
|
61
|
-
// const itemBox = item.getBoundingClientRect()
|
|
62
|
-
// // item.style.gridRowEnd = `span ${Math.round(itemBox.height + gap)}`
|
|
63
|
-
// })
|
|
64
|
-
// }
|
|
65
|
-
|
|
66
|
-
// return {
|
|
67
|
-
// destroy() {
|
|
68
|
-
// obs.disconnect()
|
|
69
|
-
// },
|
|
70
|
-
// }
|
|
71
|
-
// }
|
|
72
|
-
|
|
73
|
-
function nativeMasonrySupport(node) {
|
|
74
|
-
return getComputedStyle(node).gridTemplateRows === 'masonry'
|
|
75
|
-
}
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
/* eslint-env browser */
|
|
2
|
-
// https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe
|
|
3
|
-
|
|
4
|
-
// These are simply options that will often be used. Check the docs for other options.
|
|
5
|
-
const defaultOptions = {
|
|
6
|
-
attributes: false,
|
|
7
|
-
childList: false,
|
|
8
|
-
subtree: false,
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export function mutationObserver(node, options) {
|
|
12
|
-
options = { ...defaultOptions, ...options }
|
|
13
|
-
const { callback, ...opts } = options
|
|
14
|
-
const observer = new MutationObserver(observerFn)
|
|
15
|
-
|
|
16
|
-
function observerFn(entries) {
|
|
17
|
-
for (const entry of entries) {
|
|
18
|
-
if (callback) callback({ node, entry, observer })
|
|
19
|
-
else {
|
|
20
|
-
node.dispatchEvent(
|
|
21
|
-
new CustomEvent('mutate', {
|
|
22
|
-
detail: { node, entry, observer },
|
|
23
|
-
}),
|
|
24
|
-
)
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
if (node === window) node = document.body
|
|
30
|
-
observer.observe(node, opts)
|
|
31
|
-
|
|
32
|
-
return {
|
|
33
|
-
disconnect() {
|
|
34
|
-
observer.disconnect()
|
|
35
|
-
},
|
|
36
|
-
|
|
37
|
-
// Destroy is used for Svelte actions
|
|
38
|
-
destroy() {
|
|
39
|
-
observer.disconnect()
|
|
40
|
-
},
|
|
41
|
-
}
|
|
42
|
-
}
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
/* eslint-env browser */
|
|
2
|
-
export function resizeObserver(node, options) {
|
|
3
|
-
// We set the option here to prevent observer from being created unless necessary.
|
|
4
|
-
options = { observe: true, ...options }
|
|
5
|
-
if (!options.observe) return
|
|
6
|
-
|
|
7
|
-
const observer = new ResizeObserver(observerFn)
|
|
8
|
-
|
|
9
|
-
function observerFn(entries) {
|
|
10
|
-
for (const entry of entries) {
|
|
11
|
-
if (options.callback) options.callback({ node, entry, entries, observer })
|
|
12
|
-
else {
|
|
13
|
-
node.dispatchEvent(
|
|
14
|
-
new CustomEvent('resize-obs', {
|
|
15
|
-
detail: { node, entry, entries, observer },
|
|
16
|
-
}),
|
|
17
|
-
)
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
observer.observe(node)
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
disconnect() {
|
|
26
|
-
observer.unobserve(node)
|
|
27
|
-
},
|
|
28
|
-
|
|
29
|
-
destroy() {
|
|
30
|
-
observer.unobserve(node)
|
|
31
|
-
},
|
|
32
|
-
}
|
|
33
|
-
}
|