@straylightagency/utils 1.1.3 → 1.1.5
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/package.json +1 -1
- package/src/date/reviver.js +3 -1
- package/src/dom/index.js +4 -2
- package/src/dom/stringToHtml.js +18 -0
- package/src/dom/whenResize.js +23 -9
- package/src/dom/whenScroll.js +32 -0
- package/src/index.js +1 -0
- package/src/misc/reviver.js +4 -2
package/package.json
CHANGED
package/src/date/reviver.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
* Function that can be used with JSON.parse to revive Date objects.
|
|
3
3
|
*
|
|
4
4
|
* @param {*} value
|
|
5
|
-
* @returns {Date
|
|
5
|
+
* @returns {Date|*}
|
|
6
6
|
*/
|
|
7
7
|
export default function reviver(value) {
|
|
8
8
|
if ( typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test( value ) ) {
|
|
9
9
|
return new Date( value );
|
|
10
10
|
}
|
|
11
|
+
|
|
12
|
+
return value;
|
|
11
13
|
}
|
package/src/dom/index.js
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
export { default as addEventListeners } from "./addEventListeners.js";
|
|
2
2
|
export { default as closestElement } from "./closestElement.js";
|
|
3
3
|
export { default as delegate } from "./delegate.js";
|
|
4
|
-
export { default as getFieldsValues } from "./getFieldsValues.js";
|
|
5
4
|
export { default as getElementSizing } from "./getElementSizing.js";
|
|
5
|
+
export { default as getFieldsValues } from "./getFieldsValues.js";
|
|
6
6
|
export { default as onEachElements } from "./onEachElements.js";
|
|
7
|
+
export { default as stringToHtml } from "./stringToHtml.js";
|
|
7
8
|
export { default as unlessElementExists } from "./unlessElementExists.js";
|
|
8
9
|
export { default as whenReady } from "./whenReady.js";
|
|
9
|
-
export { default as whenResize } from "./whenResize.js";
|
|
10
|
+
export { default as whenResize } from "./whenResize.js";
|
|
11
|
+
export { default as whenScroll } from "./whenScroll.js";
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @example stringToHtml('<p>my content</p>')
|
|
3
|
+
*
|
|
4
|
+
* @param {string} htmlString
|
|
5
|
+
* @returns {Document|DocumentFragment}
|
|
6
|
+
*/
|
|
7
|
+
export default function stringToHtml(htmlString)
|
|
8
|
+
{
|
|
9
|
+
const isFullDocument = /<html/i.test( htmlString );
|
|
10
|
+
|
|
11
|
+
if ( isFullDocument ) {
|
|
12
|
+
const parser = new DOMParser();
|
|
13
|
+
return parser.parseFromString( htmlString, 'text/html' );
|
|
14
|
+
} else {
|
|
15
|
+
const range = document.createRange();
|
|
16
|
+
return range.createContextualFragment( htmlString );
|
|
17
|
+
}
|
|
18
|
+
};
|
package/src/dom/whenResize.js
CHANGED
|
@@ -1,18 +1,32 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
2
|
+
* Listen when the window is resized. Returned an array with two methods:
|
|
3
|
+
* - init() : callback is called immediately with width and height values
|
|
4
|
+
* - cancel() : cancel the event listener
|
|
3
5
|
*
|
|
4
6
|
* @param {Function} callbackFn
|
|
5
|
-
* @param {boolean}
|
|
7
|
+
* @param {boolean} immediately
|
|
8
|
+
* @returns {[]}
|
|
6
9
|
*/
|
|
7
|
-
export default function whenResize(callbackFn,
|
|
8
|
-
let ww
|
|
10
|
+
export default function whenResize(callbackFn, immediately = true) {
|
|
11
|
+
let ww, wh;
|
|
9
12
|
|
|
10
|
-
|
|
13
|
+
const wrappedFn = e => {
|
|
11
14
|
ww = window.innerWidth;
|
|
12
15
|
wh = window.innerHeight;
|
|
13
16
|
|
|
14
|
-
callbackFn( { ww, wh } );
|
|
15
|
-
}
|
|
17
|
+
callbackFn( { ww, wh }, e );
|
|
18
|
+
};
|
|
16
19
|
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
window.addEventListener( 'resize', wrappedFn );
|
|
21
|
+
|
|
22
|
+
const init = () => wrappedFn();
|
|
23
|
+
const cancel = () => window.removeEventListener( 'resize', wrappedFn );
|
|
24
|
+
|
|
25
|
+
if ( immediately ) {
|
|
26
|
+
init();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [
|
|
30
|
+
init, cancel
|
|
31
|
+
];
|
|
32
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listen when the window document is scrolled. Returned an array with two methods:
|
|
3
|
+
* - init() : callback is called immediately
|
|
4
|
+
* - cancel() : cancel the event listener
|
|
5
|
+
*
|
|
6
|
+
* @param {Function} callbackFn
|
|
7
|
+
* @param {boolean} immediately
|
|
8
|
+
* @returns {[]}
|
|
9
|
+
*/
|
|
10
|
+
export default function whenScroll(callbackFn, immediately = true) {
|
|
11
|
+
let x, y;
|
|
12
|
+
|
|
13
|
+
const wrappedFn = e => {
|
|
14
|
+
x = window.scrollX;
|
|
15
|
+
y = window.scrollY;
|
|
16
|
+
|
|
17
|
+
callbackFn( { x, y }, e );
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
document.addEventListener( 'scroll', wrappedFn );
|
|
21
|
+
|
|
22
|
+
const init = () => wrappedFn();
|
|
23
|
+
const cancel = () => document.removeEventListener( 'scroll', wrappedFn );
|
|
24
|
+
|
|
25
|
+
if ( immediately ) {
|
|
26
|
+
init();
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [
|
|
30
|
+
init, cancel
|
|
31
|
+
];
|
|
32
|
+
}
|
package/src/index.js
CHANGED
|
@@ -30,6 +30,7 @@ export { default as onEachElements } from "./dom/onEachElements.js";
|
|
|
30
30
|
export { default as unlessElementExists } from "./dom/unlessElementExists.js";
|
|
31
31
|
export { default as whenReady } from "./dom/whenReady.js";
|
|
32
32
|
export { default as whenResize } from "./dom/whenResize.js";
|
|
33
|
+
export { default as whenScroll } from "./dom/whenScroll.js";
|
|
33
34
|
export { default as getWeekNumber } from "./date/getWeekNumber.js";
|
|
34
35
|
export { default as clone } from "./date/clone.js";
|
|
35
36
|
export { default as dateReviver } from "./date/reviver.js";
|
package/src/misc/reviver.js
CHANGED
|
@@ -2,10 +2,12 @@
|
|
|
2
2
|
* Function that can be used with JSON.parse to revive functions.
|
|
3
3
|
*
|
|
4
4
|
* @param {*} value
|
|
5
|
-
* @returns {
|
|
5
|
+
* @returns {*}
|
|
6
6
|
*/
|
|
7
7
|
export default function reviver(value) {
|
|
8
8
|
if ( typeof value === 'string' && value.indexOf('function ') === 0 ) {
|
|
9
|
-
return
|
|
9
|
+
return new Function(`return ${value}`)();
|
|
10
10
|
}
|
|
11
|
+
|
|
12
|
+
return value;
|
|
11
13
|
}
|