tool-shack 0.0.5 → 0.0.9
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/README.md +1 -1
- package/dist/browser/index.d.ts +1 -0
- package/dist/browser/index.js +1 -0
- package/dist/browser/index.js.map +1 -1
- package/dist/browser/interfaces/isTouchSupported.d.ts +3 -0
- package/dist/browser/interfaces/isTouchSupported.js +2 -0
- package/dist/browser/interfaces/isTouchSupported.js.map +1 -0
- package/dist/browser/isTabFocused.d.ts +5 -0
- package/dist/browser/isTabFocused.js +5 -0
- package/dist/browser/isTabFocused.js.map +1 -1
- package/dist/browser/isTouchSupported.d.ts +6 -0
- package/dist/browser/isTouchSupported.js +7 -0
- package/dist/browser/isTouchSupported.js.map +1 -0
- package/dist/browser/preferColorScheme.d.ts +15 -0
- package/dist/browser/preferColorScheme.js +15 -0
- package/dist/browser/preferColorScheme.js.map +1 -1
- package/dist/browser/tabFocusListener.d.ts +8 -1
- package/dist/browser/tabFocusListener.js +9 -2
- package/dist/browser/tabFocusListener.js.map +1 -1
- package/dist/dateTime/dateAsIso.d.ts +6 -0
- package/dist/dateTime/dateAsIso.js +6 -0
- package/dist/dateTime/dateAsIso.js.map +1 -1
- package/dist/dateTime/index.d.ts +1 -0
- package/dist/dateTime/index.js +1 -0
- package/dist/dateTime/index.js.map +1 -1
- package/dist/dateTime/interfaces/index.d.ts +1 -0
- package/dist/dateTime/interfaces/index.js +2 -0
- package/dist/dateTime/interfaces/index.js.map +1 -0
- package/dist/dateTime/interfaces/parseDuration.d.ts +5 -5
- package/dist/dateTime/parseDuration.d.ts +6 -0
- package/dist/dateTime/parseDuration.js +11 -5
- package/dist/dateTime/parseDuration.js.map +1 -1
- package/dist/dom/addAsyncEventListener.d.ts +8 -0
- package/dist/dom/addAsyncEventListener.js +8 -0
- package/dist/dom/addAsyncEventListener.js.map +1 -1
- package/dist/dom/addClickOutsideListener.d.ts +8 -0
- package/dist/dom/addClickOutsideListener.js +16 -0
- package/dist/dom/addClickOutsideListener.js.map +1 -0
- package/dist/dom/addEventListener.d.ts +7 -0
- package/dist/dom/addEventListener.js +7 -0
- package/dist/dom/addEventListener.js.map +1 -1
- package/dist/dom/appendAfter.d.ts +7 -0
- package/dist/dom/appendAfter.js +7 -0
- package/dist/dom/appendAfter.js.map +1 -1
- package/dist/dom/appendBefore.d.ts +7 -0
- package/dist/dom/appendBefore.js +7 -0
- package/dist/dom/appendBefore.js.map +1 -1
- package/dist/dom/createElement.d.ts +8 -1
- package/dist/dom/createElement.js +15 -1
- package/dist/dom/createElement.js.map +1 -1
- package/dist/dom/fireEvent.d.ts +7 -0
- package/dist/dom/fireEvent.js +7 -0
- package/dist/dom/fireEvent.js.map +1 -1
- package/dist/dom/index.d.ts +2 -0
- package/dist/dom/index.js +2 -0
- package/dist/dom/index.js.map +1 -1
- package/dist/dom/interfaces/createElement.d.ts +1 -0
- package/dist/dom/interfaces/index.d.ts +1 -0
- package/dist/dom/interfaces/index.js +2 -0
- package/dist/dom/interfaces/index.js.map +1 -0
- package/dist/general/index.d.ts +3 -0
- package/dist/general/index.js +4 -0
- package/dist/general/index.js.map +1 -0
- package/dist/general/isEmpty.d.ts +7 -0
- package/dist/general/isEmpty.js +18 -0
- package/dist/general/isEmpty.js.map +1 -0
- package/dist/general/isNil.d.ts +7 -0
- package/dist/general/isNil.js +8 -0
- package/dist/general/isNil.js.map +1 -0
- package/dist/general/isValidJson.d.ts +7 -0
- package/dist/general/isValidJson.js +16 -0
- package/dist/general/isValidJson.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/schedule/index.d.ts +1 -0
- package/dist/schedule/index.js +1 -0
- package/dist/schedule/index.js.map +1 -1
- package/dist/schedule/runAnimation.d.ts +11 -0
- package/dist/schedule/runAnimation.js +40 -0
- package/dist/schedule/runAnimation.js.map +1 -0
- package/dist/schedule/runAsync.d.ts +6 -0
- package/dist/schedule/runAsync.js +6 -0
- package/dist/schedule/runAsync.js.map +1 -1
- package/dist/string/escapeHTML.d.ts +6 -0
- package/dist/string/escapeHTML.js +6 -0
- package/dist/string/escapeHTML.js.map +1 -1
- package/dist/string/index.d.ts +3 -0
- package/dist/string/index.js +3 -0
- package/dist/string/index.js.map +1 -1
- package/dist/string/removeDiacritics.d.ts +7 -0
- package/dist/string/removeDiacritics.js +11 -0
- package/dist/string/removeDiacritics.js.map +1 -0
- package/dist/string/slugify.d.ts +8 -0
- package/dist/string/slugify.js +16 -0
- package/dist/string/slugify.js.map +1 -0
- package/dist/string/truncate.d.ts +9 -0
- package/dist/string/truncate.js +17 -0
- package/dist/string/truncate.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +22 -0
- package/docs/assets/icons.css +1043 -0
- package/docs/assets/icons.png +0 -0
- package/docs/assets/icons@2x.png +0 -0
- package/docs/assets/main.js +52 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1388 -0
- package/docs/assets/widgets.png +0 -0
- package/docs/assets/widgets@2x.png +0 -0
- package/docs/index.html +5 -0
- package/docs/interfaces/dateTime.IDuration.html +1 -0
- package/docs/interfaces/dom.IExtendingElementProps.html +1 -0
- package/docs/modules/browser.html +23 -0
- package/docs/modules/dateTime.html +11 -0
- package/docs/modules/dom.html +52 -0
- package/docs/modules/general.html +16 -0
- package/docs/modules/schedule.html +13 -0
- package/docs/modules/string.html +27 -0
- package/docs/modules.html +1 -0
- package/package.json +18 -11
- package/src/browser/index.ts +1 -0
- package/src/browser/interfaces/isTouchSupported.ts +3 -0
- package/src/browser/isTabFocused.ts +5 -0
- package/src/browser/isTouchSupported.ts +12 -0
- package/src/browser/preferColorScheme.ts +15 -0
- package/src/browser/tabFocusListener.ts +10 -3
- package/src/dateTime/dateAsIso.ts +6 -0
- package/src/dateTime/index.ts +2 -0
- package/src/dateTime/interfaces/index.ts +1 -0
- package/src/dateTime/interfaces/parseDuration.ts +5 -5
- package/src/dateTime/parseDuration.ts +11 -5
- package/src/dom/addAsyncEventListener.ts +8 -0
- package/src/dom/addClickOutsideListener.ts +16 -0
- package/src/dom/addEventListener.ts +7 -0
- package/src/dom/appendAfter.ts +7 -0
- package/src/dom/appendBefore.ts +7 -0
- package/src/dom/createElement.ts +21 -3
- package/src/dom/fireEvent.ts +7 -0
- package/src/dom/index.ts +3 -0
- package/src/dom/interfaces/createElement.ts +29 -4
- package/src/dom/interfaces/index.ts +1 -0
- package/src/general/index.ts +3 -0
- package/src/general/isEmpty.ts +21 -0
- package/src/general/isNil.ts +7 -0
- package/src/general/isValidJson.ts +15 -0
- package/src/index.ts +3 -2
- package/src/schedule/index.ts +1 -0
- package/src/schedule/runAnimation.ts +51 -0
- package/src/schedule/runAsync.ts +6 -0
- package/src/string/escapeHTML.ts +6 -0
- package/src/string/index.ts +3 -0
- package/src/string/removeDiacritics.ts +11 -0
- package/src/string/slugify.ts +17 -0
- package/src/string/truncate.ts +21 -0
- package/tsconfig.json +2 -7
package/src/browser/index.ts
CHANGED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { IExtendedWindow } from './interfaces/isTouchSupported';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Check if user's browser supports touch events
|
|
5
|
+
*
|
|
6
|
+
* @returns Boolean indicating if user's browser support touch events
|
|
7
|
+
*/
|
|
8
|
+
export const isTouchSupported = (): boolean => (
|
|
9
|
+
'ontouchstart' in window || (
|
|
10
|
+
(window as unknown as IExtendedWindow).DocumentTouch && document instanceof (window as unknown as IExtendedWindow).DocumentTouch
|
|
11
|
+
)
|
|
12
|
+
);
|
|
@@ -1,5 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if user prefers light color scheme
|
|
3
|
+
*
|
|
4
|
+
* @returns Boolean indicating if user prefers light color scheme
|
|
5
|
+
*/
|
|
1
6
|
export const preferLightColorScheme = (): boolean => !!(window.matchMedia('(prefers-color-scheme: light)').matches);
|
|
2
7
|
|
|
8
|
+
/**
|
|
9
|
+
* Check if user prefers dark color scheme
|
|
10
|
+
*
|
|
11
|
+
* @returns Boolean indicating if user prefers dark color scheme
|
|
12
|
+
*/
|
|
3
13
|
export const preferDarkColorScheme = (): boolean => !!(window.matchMedia('(prefers-color-scheme: dark)').matches);
|
|
4
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Check what color scheme (light or dark) is prefered by user
|
|
17
|
+
*
|
|
18
|
+
* @returns String indicating prefered light color scheme ('light') or dark color scheme ('dark')
|
|
19
|
+
*/
|
|
5
20
|
export const preferColorScheme = (): 'light'|'dark' => preferLightColorScheme() ? 'light' : 'dark';
|
|
@@ -1,11 +1,18 @@
|
|
|
1
1
|
import { isTabFocused } from './isTabFocused';
|
|
2
2
|
|
|
3
|
-
|
|
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 => {
|
|
4
11
|
document.addEventListener('visibilitychange', () => {
|
|
5
12
|
if (isTabFocused()) {
|
|
6
|
-
focusCallback
|
|
13
|
+
focusCallback?.call(this);
|
|
7
14
|
} else {
|
|
8
|
-
blurCallback
|
|
15
|
+
blurCallback?.call(this);
|
|
9
16
|
}
|
|
10
17
|
});
|
|
11
18
|
}
|
package/src/dateTime/index.ts
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parseDuration';
|
|
@@ -1,13 +1,19 @@
|
|
|
1
1
|
import { IDuration } from './interfaces/parseDuration';
|
|
2
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
|
+
*/
|
|
3
9
|
export const parseDuration = (durationInMs: number): IDuration => {
|
|
4
10
|
durationInMs = Math.abs(durationInMs);
|
|
5
11
|
|
|
6
12
|
return {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
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,
|
|
12
18
|
};
|
|
13
19
|
}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Listen to events fired on elements that may currently not exist in the DOM
|
|
3
|
+
*
|
|
4
|
+
* @param elementSelector Selector for element(s) to match event target
|
|
5
|
+
* @param listeners List of event types and related callback(s)
|
|
6
|
+
* @param acceptBubbling Boolean switch to allow bubbling or accept only direct element match
|
|
7
|
+
* @returns void
|
|
8
|
+
*/
|
|
1
9
|
export const addAsyncEventListener = (
|
|
2
10
|
elementSelector: string,
|
|
3
11
|
listeners: Record<string, EventListener | EventListener[]>,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add event listener to listen to clicks outside of given element
|
|
3
|
+
*
|
|
4
|
+
* @param element Node element outside which to listen to clicks
|
|
5
|
+
* @param fn Function to trigger when clicked outside
|
|
6
|
+
* @returns void
|
|
7
|
+
*/
|
|
8
|
+
export const addClickOutsideListener = (element: HTMLElement, fn: () => any): void => {
|
|
9
|
+
document.addEventListener('click', (event: Event) => {
|
|
10
|
+
const target = event.target as HTMLElement;
|
|
11
|
+
|
|
12
|
+
if (!!target && !element.contains(target)) {
|
|
13
|
+
fn.call(this);
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
};
|
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add event listener(s) to one or more Node elements
|
|
3
|
+
*
|
|
4
|
+
* @param element One or multiple Node element(s) to set listener(s) for
|
|
5
|
+
* @param listeners List of event types and related callback(s)
|
|
6
|
+
* @returns void
|
|
7
|
+
*/
|
|
1
8
|
export const addEventListener = (
|
|
2
9
|
element: HTMLElement | HTMLElement[] | NodeListOf<HTMLElement> | HTMLCollectionOf<HTMLElement>,
|
|
3
10
|
listeners: Record<string, EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]>
|
package/src/dom/appendAfter.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append given node after reference node in the DOM
|
|
3
|
+
*
|
|
4
|
+
* @param newNode New node element to be appended
|
|
5
|
+
* @param referenceNode Reference node element
|
|
6
|
+
* @returns Result of the action
|
|
7
|
+
*/
|
|
1
8
|
export const appendAfter = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
|
|
2
9
|
!!referenceNode.parentElement?.insertBefore(newNode, referenceNode.nextSibling);
|
package/src/dom/appendBefore.ts
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Append given node before reference node in the DOM
|
|
3
|
+
*
|
|
4
|
+
* @param newNode New node element to be appended
|
|
5
|
+
* @param referenceNode Reference node element
|
|
6
|
+
* @returns Result of the action
|
|
7
|
+
*/
|
|
1
8
|
export const appendBefore = (newNode: HTMLElement, referenceNode: HTMLElement): boolean =>
|
|
2
9
|
!!referenceNode.parentElement?.insertBefore(newNode, referenceNode);
|
package/src/dom/createElement.ts
CHANGED
|
@@ -1,6 +1,16 @@
|
|
|
1
1
|
import { IExtendingElementProps } from './interfaces/createElement';
|
|
2
2
|
|
|
3
|
-
|
|
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'>> & IExtendingElementProps,
|
|
13
|
+
): T => {
|
|
4
14
|
const element: HTMLElement = document.createElement(tagName);
|
|
5
15
|
|
|
6
16
|
if (props) {
|
|
@@ -18,10 +28,18 @@ export const createElement = <T = HTMLElement> (tagName: string, props?: Partial
|
|
|
18
28
|
} else if (key === 'listeners') {
|
|
19
29
|
for (let eventName in props.listeners!) {
|
|
20
30
|
const functionList = (
|
|
21
|
-
props.listeners![eventName] instanceof Array
|
|
31
|
+
props.listeners![eventName] instanceof Array
|
|
32
|
+
? props.listeners![eventName]
|
|
33
|
+
: [props.listeners![eventName]]
|
|
22
34
|
) as EventListenerOrEventListenerObject[];
|
|
23
35
|
|
|
24
|
-
functionList.forEach((fn: EventListenerOrEventListenerObject) =>
|
|
36
|
+
functionList.forEach((fn: EventListenerOrEventListenerObject) =>
|
|
37
|
+
element.addEventListener(eventName, fn),
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
|
+
} else if (key === 'dataset') {
|
|
41
|
+
for (let datasetKey in props.dataset) {
|
|
42
|
+
element.dataset[datasetKey] = props.dataset[datasetKey];
|
|
25
43
|
}
|
|
26
44
|
} else {
|
|
27
45
|
(element as any)[key] = value;
|
package/src/dom/fireEvent.ts
CHANGED
|
@@ -1 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Manually fire event on Node element
|
|
3
|
+
*
|
|
4
|
+
* @param element Node element on which should be fired event
|
|
5
|
+
* @param eventType Type name of event that should be fired
|
|
6
|
+
* @returns Result of the action
|
|
7
|
+
*/
|
|
1
8
|
export const fireEvent = (element: HTMLElement, eventType: string): boolean => element.dispatchEvent(new Event(eventType));
|
package/src/dom/index.ts
CHANGED
|
@@ -1,10 +1,35 @@
|
|
|
1
|
-
export type TAriaLabelKey =
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
export type TAriaLabelKey =
|
|
2
|
+
| 'atomic'
|
|
3
|
+
| 'busy'
|
|
4
|
+
| 'controls'
|
|
5
|
+
| 'current'
|
|
6
|
+
| 'describedby'
|
|
7
|
+
| 'details'
|
|
8
|
+
| 'disables'
|
|
9
|
+
| 'dropeffect'
|
|
10
|
+
| 'errormessage'
|
|
11
|
+
| 'expanded'
|
|
12
|
+
| 'flowto'
|
|
13
|
+
| 'grabbed'
|
|
14
|
+
| 'haspopup'
|
|
15
|
+
| 'hidden'
|
|
16
|
+
| 'invalid'
|
|
17
|
+
| 'keyshortcuts'
|
|
18
|
+
| 'label'
|
|
19
|
+
| 'labelledby'
|
|
20
|
+
| 'live'
|
|
21
|
+
| 'modal'
|
|
22
|
+
| 'owns'
|
|
23
|
+
| 'relevant'
|
|
24
|
+
| 'roledescription';
|
|
4
25
|
|
|
5
26
|
export interface IExtendingElementProps {
|
|
6
27
|
children?: HTMLElement[];
|
|
7
|
-
listeners?: Record<
|
|
28
|
+
listeners?: Record<
|
|
29
|
+
string,
|
|
30
|
+
EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]
|
|
31
|
+
>;
|
|
8
32
|
role?: string;
|
|
9
33
|
aria?: Partial<Record<TAriaLabelKey, string>>;
|
|
34
|
+
dataset: Record<string, string>;
|
|
10
35
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './createElement';
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { isNil } from './isNil';
|
|
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
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if string is valid JSON
|
|
3
|
+
*
|
|
4
|
+
* @param value String value to be checked
|
|
5
|
+
* @returns Boolean indicating if string value is a valid JSON
|
|
6
|
+
*/
|
|
7
|
+
export const isValidJson = (value: string): boolean => {
|
|
8
|
+
try {
|
|
9
|
+
JSON.parse(value);
|
|
10
|
+
} catch (error: any) {
|
|
11
|
+
return false;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
return true;
|
|
15
|
+
};
|
package/src/index.ts
CHANGED
package/src/schedule/index.ts
CHANGED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run function as animation using requestAnimationFrame
|
|
3
|
+
*
|
|
4
|
+
* @param fn Function to be run as animation
|
|
5
|
+
* @param autoStart If animation should start right away or just return callbacks
|
|
6
|
+
* @returns Callbacks to start animation (startAnimation) and stop animation (stopAnimation)
|
|
7
|
+
*/
|
|
8
|
+
export const runAnimation = (
|
|
9
|
+
fn: () => any,
|
|
10
|
+
autoStart = true,
|
|
11
|
+
): { startAnimation: () => void; stopAnimation: () => void } => {
|
|
12
|
+
let isRunning = false;
|
|
13
|
+
let requestedAnimationFrame: number | null = null;
|
|
14
|
+
|
|
15
|
+
const runAnimation = (): void => {
|
|
16
|
+
requestedAnimationFrame = requestAnimationFrame((): void => {
|
|
17
|
+
fn.call(this);
|
|
18
|
+
|
|
19
|
+
if (isRunning) {
|
|
20
|
+
runAnimation();
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const startAnimation = (): void => {
|
|
26
|
+
if (isRunning) {
|
|
27
|
+
return;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
isRunning = true;
|
|
31
|
+
runAnimation();
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const stopAnimation = (): void => {
|
|
35
|
+
if (!isRunning) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
isRunning = false;
|
|
40
|
+
|
|
41
|
+
if (!!requestedAnimationFrame) {
|
|
42
|
+
cancelAnimationFrame(requestedAnimationFrame);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
if (autoStart) {
|
|
47
|
+
startAnimation();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
return { startAnimation, stopAnimation };
|
|
51
|
+
};
|
package/src/schedule/runAsync.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Run function in asynchronously in Worker
|
|
3
|
+
*
|
|
4
|
+
* @param fn Function to be run asynchronously
|
|
5
|
+
* @returns Promise of expected result
|
|
6
|
+
*/
|
|
1
7
|
export const runAsync = <T> (fn: () => any): Promise<T> => {
|
|
2
8
|
const worker = new Worker(
|
|
3
9
|
URL.createObjectURL(new Blob([`postMessage((${fn})());`])),
|
package/src/string/escapeHTML.ts
CHANGED
|
@@ -1,3 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Escape special HTML related characters from string
|
|
3
|
+
*
|
|
4
|
+
* @param html String containing HTML related characters
|
|
5
|
+
* @returns Safe string with escaped HTML related characters
|
|
6
|
+
*/
|
|
1
7
|
export const escapeHTML = (html: string): string => {
|
|
2
8
|
const tempDiv = document.createElement('div');
|
|
3
9
|
tempDiv.innerText = html;
|
package/src/string/index.ts
CHANGED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Remove diacritics & accents from string
|
|
3
|
+
*
|
|
4
|
+
* @param value String from which should be removed diacritics & accents
|
|
5
|
+
* @returns ASCII string with removed diacritics & accents
|
|
6
|
+
*/
|
|
7
|
+
export const removeDiacritics = (value: string): string =>
|
|
8
|
+
value
|
|
9
|
+
.normalize('NFD')
|
|
10
|
+
.toString()
|
|
11
|
+
.replace(/[\u0300-\u036f]/g, '');
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { removeDiacritics } from './removeDiacritics';
|
|
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
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Truncate string to maximum given length if needed and append string at the end (by default "...")
|
|
3
|
+
*
|
|
4
|
+
* @param value String value to be truncated
|
|
5
|
+
* @param maxLength Maximum allowed length
|
|
6
|
+
* @param appendedString String to append at the end, be default "..."
|
|
7
|
+
* @returns Truncated value if needed otherwise original value
|
|
8
|
+
*/
|
|
9
|
+
export const truncate = (
|
|
10
|
+
value: string,
|
|
11
|
+
maxLength: number,
|
|
12
|
+
appendedString: string = '...',
|
|
13
|
+
): string => {
|
|
14
|
+
if (maxLength < appendedString.length) {
|
|
15
|
+
return appendedString.slice(0, maxLength);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return value.length > maxLength
|
|
19
|
+
? value.slice(0, maxLength - appendedString.length) + appendedString
|
|
20
|
+
: value;
|
|
21
|
+
};
|