tool-shack 0.0.16 → 0.0.17
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 +3 -3
- package/src/browser/index.ts +7 -7
- package/src/browser/isScrollBehaviorSupported.ts +7 -7
- package/src/browser/isTouchSupported.ts +11 -11
- package/src/browser/tabFocusListener.ts +18 -18
- package/src/dateTime/index.ts +4 -4
- package/src/dateTime/interfaces/index.ts +1 -1
- package/src/dateTime/parseDuration.ts +19 -19
- package/src/dom/createElement.ts +55 -55
- package/src/dom/index.ts +9 -9
- package/src/dom/interfaces/index.ts +1 -1
- package/src/general/index.ts +3 -3
- package/src/general/isEmpty.ts +21 -21
- package/src/index.ts +6 -6
- package/src/schedule/index.ts +2 -2
- package/src/string/index.ts +5 -5
- package/src/string/slugify.ts +17 -17
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tool-shack",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.17",
|
|
4
4
|
"description": "Collection of usefull functions to ease work",
|
|
5
5
|
"author": {
|
|
6
6
|
"name": "David Myška",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"husky": "^8.0.1",
|
|
43
43
|
"prettier": "^2.7.1",
|
|
44
44
|
"pretty-quick": "^3.1.3",
|
|
45
|
-
"typedoc": "^0.23.
|
|
46
|
-
"typescript": "^4.
|
|
45
|
+
"typedoc": "^0.23.11",
|
|
46
|
+
"typescript": "^4.8.2"
|
|
47
47
|
}
|
|
48
48
|
}
|
package/src/browser/index.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export * from './isPushNotificationSupported.js';
|
|
2
|
-
export * from './isScrollBehaviorSupported.js';
|
|
3
|
-
export * from './isShareSupported.js';
|
|
4
|
-
export * from './isTabFocused.js';
|
|
5
|
-
export * from './isTouchSupported.js';
|
|
6
|
-
export * from './preferColorScheme.js';
|
|
7
|
-
export * from './tabFocusListener.js';
|
|
1
|
+
export * from './isPushNotificationSupported.js';
|
|
2
|
+
export * from './isScrollBehaviorSupported.js';
|
|
3
|
+
export * from './isShareSupported.js';
|
|
4
|
+
export * from './isTabFocused.js';
|
|
5
|
+
export * from './isTouchSupported.js';
|
|
6
|
+
export * from './preferColorScheme.js';
|
|
7
|
+
export * from './tabFocusListener.js';
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Check if user's browser supports setting scroll behavior
|
|
3
|
-
*
|
|
4
|
-
* @returns Boolean indicating if user's browser supports setting scroll behavior
|
|
5
|
-
*/
|
|
6
|
-
export const isScrollBehaviorSupported = (): boolean =>
|
|
7
|
-
!!('scrollBehavior' in document.documentElement.style);
|
|
1
|
+
/**
|
|
2
|
+
* Check if user's browser supports setting scroll behavior
|
|
3
|
+
*
|
|
4
|
+
* @returns Boolean indicating if user's browser supports setting scroll behavior
|
|
5
|
+
*/
|
|
6
|
+
export const isScrollBehaviorSupported = (): boolean =>
|
|
7
|
+
!!('scrollBehavior' in document.documentElement.style);
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { IExtendedWindow } from './interfaces/isTouchSupported.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Check if user's browser supports touch events
|
|
5
|
-
*
|
|
6
|
-
* @returns Boolean indicating if user's browser supports touch events
|
|
7
|
-
*/
|
|
8
|
-
export const isTouchSupported = (): boolean =>
|
|
9
|
-
'ontouchstart' in window ||
|
|
10
|
-
((window as unknown as IExtendedWindow).DocumentTouch &&
|
|
11
|
-
document instanceof (window as unknown as IExtendedWindow).DocumentTouch);
|
|
1
|
+
import { IExtendedWindow } from './interfaces/isTouchSupported.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if user's browser supports touch events
|
|
5
|
+
*
|
|
6
|
+
* @returns Boolean indicating if user's browser supports touch events
|
|
7
|
+
*/
|
|
8
|
+
export const isTouchSupported = (): boolean =>
|
|
9
|
+
'ontouchstart' in window ||
|
|
10
|
+
((window as unknown as IExtendedWindow).DocumentTouch &&
|
|
11
|
+
document instanceof (window as unknown as IExtendedWindow).DocumentTouch);
|
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
import { isTabFocused } from './isTabFocused.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Set callbacks for if current browser tab is in focus
|
|
5
|
-
*
|
|
6
|
-
* @param focusCallback Function to be called when current browser tab goes into focus
|
|
7
|
-
* @param blurCallback Function to be called when current browser tab goes out of focus
|
|
8
|
-
* @returns void
|
|
9
|
-
*/
|
|
10
|
-
export const tabFocusListener = (focusCallback?: () => void, blurCallback?: () => void): void => {
|
|
11
|
-
document.addEventListener('visibilitychange', () => {
|
|
12
|
-
if (isTabFocused()) {
|
|
13
|
-
focusCallback?.call(this);
|
|
14
|
-
} else {
|
|
15
|
-
blurCallback?.call(this);
|
|
16
|
-
}
|
|
17
|
-
});
|
|
18
|
-
};
|
|
1
|
+
import { isTabFocused } from './isTabFocused.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Set callbacks for if current browser tab is in focus
|
|
5
|
+
*
|
|
6
|
+
* @param focusCallback Function to be called when current browser tab goes into focus
|
|
7
|
+
* @param blurCallback Function to be called when current browser tab goes out of focus
|
|
8
|
+
* @returns void
|
|
9
|
+
*/
|
|
10
|
+
export const tabFocusListener = (focusCallback?: () => void, blurCallback?: () => void): void => {
|
|
11
|
+
document.addEventListener('visibilitychange', () => {
|
|
12
|
+
if (isTabFocused()) {
|
|
13
|
+
focusCallback?.call(this);
|
|
14
|
+
} else {
|
|
15
|
+
blurCallback?.call(this);
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
};
|
package/src/dateTime/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export * from './interfaces/index.js';
|
|
2
|
-
|
|
3
|
-
export * from './dateAsIso.js';
|
|
4
|
-
export * from './parseDuration.js';
|
|
1
|
+
export * from './interfaces/index.js';
|
|
2
|
+
|
|
3
|
+
export * from './dateAsIso.js';
|
|
4
|
+
export * from './parseDuration.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './parseDuration.js';
|
|
1
|
+
export * from './parseDuration.js';
|
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
import { IDuration } from './interfaces/parseDuration.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Parse duration in milliseconds into days, hours, minutes, seconds, milliseconds
|
|
5
|
-
*
|
|
6
|
-
* @param durationInMs Integer of duration in milliseconds
|
|
7
|
-
* @returns Object containing days, hours, minutes, seconds and milliseconds of provided duration
|
|
8
|
-
*/
|
|
9
|
-
export const parseDuration = (durationInMs: number): IDuration => {
|
|
10
|
-
durationInMs = Math.abs(durationInMs);
|
|
11
|
-
|
|
12
|
-
return {
|
|
13
|
-
days: Math.floor(durationInMs / (24 * 60 * 60 * 1000)),
|
|
14
|
-
hours: Math.floor(durationInMs / (60 * 60 * 1000)) % 24,
|
|
15
|
-
minutes: Math.floor(durationInMs / (60 * 1000)) % 60,
|
|
16
|
-
seconds: Math.floor(durationInMs / 1000) % 60,
|
|
17
|
-
milliseconds: Math.floor(durationInMs) % 1000,
|
|
18
|
-
};
|
|
19
|
-
};
|
|
1
|
+
import { IDuration } from './interfaces/parseDuration.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Parse duration in milliseconds into days, hours, minutes, seconds, milliseconds
|
|
5
|
+
*
|
|
6
|
+
* @param durationInMs Integer of duration in milliseconds
|
|
7
|
+
* @returns Object containing days, hours, minutes, seconds and milliseconds of provided duration
|
|
8
|
+
*/
|
|
9
|
+
export const parseDuration = (durationInMs: number): IDuration => {
|
|
10
|
+
durationInMs = Math.abs(durationInMs);
|
|
11
|
+
|
|
12
|
+
return {
|
|
13
|
+
days: Math.floor(durationInMs / (24 * 60 * 60 * 1000)),
|
|
14
|
+
hours: Math.floor(durationInMs / (60 * 60 * 1000)) % 24,
|
|
15
|
+
minutes: Math.floor(durationInMs / (60 * 1000)) % 60,
|
|
16
|
+
seconds: Math.floor(durationInMs / 1000) % 60,
|
|
17
|
+
milliseconds: Math.floor(durationInMs) % 1000,
|
|
18
|
+
};
|
|
19
|
+
};
|
package/src/dom/createElement.ts
CHANGED
|
@@ -1,55 +1,55 @@
|
|
|
1
|
-
import { IExtendingElementProps } from './interfaces/createElement.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Method for creating Node element and assigning multiple parameters, event listeners & children in one method call
|
|
5
|
-
*
|
|
6
|
-
* @param tagName Node tag name
|
|
7
|
-
* @param props List of attributes, event listeners or/and children to assign to the newly created element
|
|
8
|
-
* @returns Resulting Node element
|
|
9
|
-
*/
|
|
10
|
-
export const createElement = <T = HTMLElement>(
|
|
11
|
-
tagName: string,
|
|
12
|
-
props?: Partial<Omit<T, 'children' | 'dataset' | 'style'>> & IExtendingElementProps,
|
|
13
|
-
): T => {
|
|
14
|
-
const element: HTMLElement = document.createElement(tagName);
|
|
15
|
-
|
|
16
|
-
if (props) {
|
|
17
|
-
for (let key in props) {
|
|
18
|
-
const value = (props as any)[key];
|
|
19
|
-
|
|
20
|
-
if (key === 'role') {
|
|
21
|
-
element.setAttribute(key, value);
|
|
22
|
-
} else if (key === 'aria') {
|
|
23
|
-
for (let ariaKey in props.aria!) {
|
|
24
|
-
element.setAttribute(`aria-${ariaKey}`, value[ariaKey]);
|
|
25
|
-
}
|
|
26
|
-
} else if (key === 'style') {
|
|
27
|
-
for (let styleKey in props.style!) {
|
|
28
|
-
(element.style as any)[styleKey] = value[styleKey];
|
|
29
|
-
}
|
|
30
|
-
} else if (key === 'children') {
|
|
31
|
-
props.children!.forEach((child: HTMLElement) => element.appendChild(child));
|
|
32
|
-
} else if (key === 'listeners') {
|
|
33
|
-
for (let eventName in props.listeners!) {
|
|
34
|
-
const functionList = (
|
|
35
|
-
props.listeners![eventName] instanceof Array
|
|
36
|
-
? props.listeners![eventName]
|
|
37
|
-
: [props.listeners![eventName]]
|
|
38
|
-
) as EventListenerOrEventListenerObject[];
|
|
39
|
-
|
|
40
|
-
functionList.forEach((fn: EventListenerOrEventListenerObject) =>
|
|
41
|
-
element.addEventListener(eventName, fn),
|
|
42
|
-
);
|
|
43
|
-
}
|
|
44
|
-
} else if (key === 'dataset') {
|
|
45
|
-
for (let datasetKey in props.dataset) {
|
|
46
|
-
element.dataset[datasetKey] = props.dataset[datasetKey];
|
|
47
|
-
}
|
|
48
|
-
} else {
|
|
49
|
-
(element as any)[key] = value;
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return element as unknown as T;
|
|
55
|
-
};
|
|
1
|
+
import { IExtendingElementProps } from './interfaces/createElement.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Method for creating Node element and assigning multiple parameters, event listeners & children in one method call
|
|
5
|
+
*
|
|
6
|
+
* @param tagName Node tag name
|
|
7
|
+
* @param props List of attributes, event listeners or/and children to assign to the newly created element
|
|
8
|
+
* @returns Resulting Node element
|
|
9
|
+
*/
|
|
10
|
+
export const createElement = <T = HTMLElement>(
|
|
11
|
+
tagName: string,
|
|
12
|
+
props?: Partial<Omit<T, 'children' | 'dataset' | 'style'>> & IExtendingElementProps,
|
|
13
|
+
): T => {
|
|
14
|
+
const element: HTMLElement = document.createElement(tagName);
|
|
15
|
+
|
|
16
|
+
if (props) {
|
|
17
|
+
for (let key in props) {
|
|
18
|
+
const value = (props as any)[key];
|
|
19
|
+
|
|
20
|
+
if (key === 'role') {
|
|
21
|
+
element.setAttribute(key, value);
|
|
22
|
+
} else if (key === 'aria') {
|
|
23
|
+
for (let ariaKey in props.aria!) {
|
|
24
|
+
element.setAttribute(`aria-${ariaKey}`, value[ariaKey]);
|
|
25
|
+
}
|
|
26
|
+
} else if (key === 'style') {
|
|
27
|
+
for (let styleKey in props.style!) {
|
|
28
|
+
(element.style as any)[styleKey] = value[styleKey];
|
|
29
|
+
}
|
|
30
|
+
} else if (key === 'children') {
|
|
31
|
+
props.children!.forEach((child: HTMLElement) => element.appendChild(child));
|
|
32
|
+
} else if (key === 'listeners') {
|
|
33
|
+
for (let eventName in props.listeners!) {
|
|
34
|
+
const functionList = (
|
|
35
|
+
props.listeners![eventName] instanceof Array
|
|
36
|
+
? props.listeners![eventName]
|
|
37
|
+
: [props.listeners![eventName]]
|
|
38
|
+
) as EventListenerOrEventListenerObject[];
|
|
39
|
+
|
|
40
|
+
functionList.forEach((fn: EventListenerOrEventListenerObject) =>
|
|
41
|
+
element.addEventListener(eventName, fn),
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
} else if (key === 'dataset') {
|
|
45
|
+
for (let datasetKey in props.dataset) {
|
|
46
|
+
element.dataset[datasetKey] = props.dataset[datasetKey];
|
|
47
|
+
}
|
|
48
|
+
} else {
|
|
49
|
+
(element as any)[key] = value;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return element as unknown as T;
|
|
55
|
+
};
|
package/src/dom/index.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export * from './interfaces/index.js';
|
|
2
|
-
|
|
3
|
-
export * from './addAsyncEventListener.js';
|
|
4
|
-
export * from './addClickOutsideListener.js';
|
|
5
|
-
export * from './addEventListener.js';
|
|
6
|
-
export * from './appendAfter.js';
|
|
7
|
-
export * from './appendBefore.js';
|
|
8
|
-
export * from './createElement.js';
|
|
9
|
-
export * from './fireEvent.js';
|
|
1
|
+
export * from './interfaces/index.js';
|
|
2
|
+
|
|
3
|
+
export * from './addAsyncEventListener.js';
|
|
4
|
+
export * from './addClickOutsideListener.js';
|
|
5
|
+
export * from './addEventListener.js';
|
|
6
|
+
export * from './appendAfter.js';
|
|
7
|
+
export * from './appendBefore.js';
|
|
8
|
+
export * from './createElement.js';
|
|
9
|
+
export * from './fireEvent.js';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export * from './createElement.js';
|
|
1
|
+
export * from './createElement.js';
|
package/src/general/index.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from './isEmpty.js';
|
|
2
|
-
export * from './isNil.js';
|
|
3
|
-
export * from './isValidJson.js';
|
|
1
|
+
export * from './isEmpty.js';
|
|
2
|
+
export * from './isNil.js';
|
|
3
|
+
export * from './isValidJson.js';
|
package/src/general/isEmpty.ts
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
import { isNil } from './isNil.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Check if given value is empty
|
|
5
|
-
*
|
|
6
|
-
* @param value Value to be checked
|
|
7
|
-
* @returns Boolean indicating if given value is empty
|
|
8
|
-
*/
|
|
9
|
-
export const isEmpty = (value: any): boolean => {
|
|
10
|
-
if (value instanceof Function) {
|
|
11
|
-
return false;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
return (
|
|
15
|
-
isNil(value) ||
|
|
16
|
-
(value instanceof Object &&
|
|
17
|
-
!Object.keys(value).length &&
|
|
18
|
-
Object.getPrototypeOf(value) === Object.prototype) ||
|
|
19
|
-
!value.valueOf().toString().length
|
|
20
|
-
);
|
|
21
|
-
};
|
|
1
|
+
import { isNil } from './isNil.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if given value is empty
|
|
5
|
+
*
|
|
6
|
+
* @param value Value to be checked
|
|
7
|
+
* @returns Boolean indicating if given value is empty
|
|
8
|
+
*/
|
|
9
|
+
export const isEmpty = (value: any): boolean => {
|
|
10
|
+
if (value instanceof Function) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return (
|
|
15
|
+
isNil(value) ||
|
|
16
|
+
(value instanceof Object &&
|
|
17
|
+
!Object.keys(value).length &&
|
|
18
|
+
Object.getPrototypeOf(value) === Object.prototype) ||
|
|
19
|
+
!value.valueOf().toString().length
|
|
20
|
+
);
|
|
21
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
export * from './browser/index.js';
|
|
2
|
-
export * from './dateTime/index.js';
|
|
3
|
-
export * from './dom/index.js';
|
|
4
|
-
export * from './general/index.js';
|
|
5
|
-
export * from './schedule/index.js';
|
|
6
|
-
export * from './string/index.js';
|
|
1
|
+
export * from './browser/index.js';
|
|
2
|
+
export * from './dateTime/index.js';
|
|
3
|
+
export * from './dom/index.js';
|
|
4
|
+
export * from './general/index.js';
|
|
5
|
+
export * from './schedule/index.js';
|
|
6
|
+
export * from './string/index.js';
|
package/src/schedule/index.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export * from './runAnimation.js';
|
|
2
|
-
export * from './runAsync.js';
|
|
1
|
+
export * from './runAnimation.js';
|
|
2
|
+
export * from './runAsync.js';
|
package/src/string/index.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export * from './byteSize.js';
|
|
2
|
-
export * from './escapeHTML.js';
|
|
3
|
-
export * from './removeDiacritics.js';
|
|
4
|
-
export * from './slugify.js';
|
|
5
|
-
export * from './truncate.js';
|
|
1
|
+
export * from './byteSize.js';
|
|
2
|
+
export * from './escapeHTML.js';
|
|
3
|
+
export * from './removeDiacritics.js';
|
|
4
|
+
export * from './slugify.js';
|
|
5
|
+
export * from './truncate.js';
|
package/src/string/slugify.ts
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { removeDiacritics } from './removeDiacritics.js';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Create slug from given string
|
|
5
|
-
*
|
|
6
|
-
* @param value String to be slugified
|
|
7
|
-
* @param separator One character to be used as separator (if provided longer than one character, will be automatically shortened), dash ('-') by default
|
|
8
|
-
* @returns Slugified string
|
|
9
|
-
*/
|
|
10
|
-
export const slugify = (value: string, separator: string = '-'): string => {
|
|
11
|
-
separator = separator.trim().substring(0, 1) || '-';
|
|
12
|
-
const removeCharsRegex = new RegExp(`[^\\\w\\\s${separator}]`, 'g');
|
|
13
|
-
|
|
14
|
-
return removeDiacritics(value.trim().toLowerCase())
|
|
15
|
-
.replace(removeCharsRegex, '')
|
|
16
|
-
.replace(/\s+/g, separator);
|
|
17
|
-
};
|
|
1
|
+
import { removeDiacritics } from './removeDiacritics.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Create slug from given string
|
|
5
|
+
*
|
|
6
|
+
* @param value String to be slugified
|
|
7
|
+
* @param separator One character to be used as separator (if provided longer than one character, will be automatically shortened), dash ('-') by default
|
|
8
|
+
* @returns Slugified string
|
|
9
|
+
*/
|
|
10
|
+
export const slugify = (value: string, separator: string = '-'): string => {
|
|
11
|
+
separator = separator.trim().substring(0, 1) || '-';
|
|
12
|
+
const removeCharsRegex = new RegExp(`[^\\\w\\\s${separator}]`, 'g');
|
|
13
|
+
|
|
14
|
+
return removeDiacritics(value.trim().toLowerCase())
|
|
15
|
+
.replace(removeCharsRegex, '')
|
|
16
|
+
.replace(/\s+/g, separator);
|
|
17
|
+
};
|