tool-shack 0.0.6 → 0.0.10
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/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/createElement.d.ts +1 -1
- package/dist/dom/createElement.js +8 -1
- package/dist/dom/createElement.js.map +1 -1
- package/dist/dom/index.d.ts +1 -0
- package/dist/dom/index.js +1 -0
- package/dist/dom/index.js.map +1 -1
- package/dist/dom/interfaces/createElement.d.ts +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/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 +0 -0
- package/docs/assets/highlight.css +22 -22
- package/docs/assets/icons.css +1043 -1043
- package/docs/assets/main.js +52 -52
- package/docs/assets/search.js +1 -1
- package/docs/assets/style.css +1388 -1388
- package/docs/index.html +5 -5
- package/docs/interfaces/dateTime.IDuration.html +1 -1
- package/docs/interfaces/dom.IExtendingElementProps.html +1 -1
- package/docs/modules/browser.html +23 -23
- package/docs/modules/dateTime.html +11 -11
- package/docs/modules/dom.html +52 -45
- package/docs/modules/general.html +16 -0
- package/docs/modules/schedule.html +13 -6
- package/docs/modules/string.html +27 -6
- package/docs/modules.html +1 -1
- package/package.json +2 -2
- package/src/dom/addClickOutsideListener.ts +16 -0
- package/src/dom/createElement.ts +14 -3
- package/src/dom/index.ts +1 -0
- package/src/dom/interfaces/createElement.ts +35 -10
- 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/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/.husky/pre-commit +0 -4
- package/.husky/pre-push +0 -4
- package/.prettierignore +0 -6
- package/.prettierrc +0 -9
package/src/dom/createElement.ts
CHANGED
|
@@ -7,7 +7,10 @@ import { IExtendingElementProps } from './interfaces/createElement';
|
|
|
7
7
|
* @param props List of attributes, event listeners or/and children to assign to the newly created element
|
|
8
8
|
* @returns Resulting Node element
|
|
9
9
|
*/
|
|
10
|
-
export const createElement = <T = HTMLElement>
|
|
10
|
+
export const createElement = <T = HTMLElement>(
|
|
11
|
+
tagName: string,
|
|
12
|
+
props?: Partial<Omit<T, 'children' | 'dataset'>> & IExtendingElementProps,
|
|
13
|
+
): T => {
|
|
11
14
|
const element: HTMLElement = document.createElement(tagName);
|
|
12
15
|
|
|
13
16
|
if (props) {
|
|
@@ -25,10 +28,18 @@ export const createElement = <T = HTMLElement> (tagName: string, props?: Partial
|
|
|
25
28
|
} else if (key === 'listeners') {
|
|
26
29
|
for (let eventName in props.listeners!) {
|
|
27
30
|
const functionList = (
|
|
28
|
-
props.listeners![eventName] instanceof Array
|
|
31
|
+
props.listeners![eventName] instanceof Array
|
|
32
|
+
? props.listeners![eventName]
|
|
33
|
+
: [props.listeners![eventName]]
|
|
29
34
|
) as EventListenerOrEventListenerObject[];
|
|
30
35
|
|
|
31
|
-
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];
|
|
32
43
|
}
|
|
33
44
|
} else {
|
|
34
45
|
(element as any)[key] = value;
|
package/src/dom/index.ts
CHANGED
|
@@ -1,10 +1,35 @@
|
|
|
1
|
-
export type TAriaLabelKey =
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
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';
|
|
25
|
+
|
|
26
|
+
export interface IExtendingElementProps {
|
|
27
|
+
children?: HTMLElement[];
|
|
28
|
+
listeners?: Record<
|
|
29
|
+
string,
|
|
30
|
+
EventListenerOrEventListenerObject | EventListenerOrEventListenerObject[]
|
|
31
|
+
>;
|
|
32
|
+
role?: string;
|
|
33
|
+
aria?: Partial<Record<TAriaLabelKey, string>>;
|
|
34
|
+
dataset?: Record<string, string>;
|
|
35
|
+
}
|
|
@@ -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/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
|
+
};
|
package/.husky/pre-commit
DELETED
package/.husky/pre-push
DELETED
package/.prettierignore
DELETED