@cedx/base 0.3.0 → 0.4.0
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/lib/String.d.ts +59 -0
- package/lib/String.d.ts.map +1 -0
- package/lib/String.js +92 -0
- package/lib/UI/MenuActivator.d.ts +0 -1
- package/lib/UI/MenuActivator.d.ts.map +1 -1
- package/lib/UI/MenuActivator.js +1 -5
- package/package.json +1 -1
- package/src/Client/String.ts +97 -0
- package/src/Client/UI/MenuActivator.ts +1 -6
- package/lib/Html/MenuAlignment.d.ts +0 -18
- package/lib/Html/MenuAlignment.d.ts.map +0 -1
- package/lib/Html/MenuAlignment.js +0 -13
- package/src/Client/Html/MenuAlignment.ts +0 -20
package/ReadMe.md
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
# Belin.io Base
|
|
2
|
-
   
|
|
3
3
|
|
|
4
4
|
Base library by [Cédric Belin](https://belin.io), full stack developer,
|
|
5
5
|
implemented in [C#](https://learn.microsoft.com/en-us/dotnet/csharp) and [TypeScript](https://www.typescriptlang.org).
|
package/lib/String.d.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts the first character to uppercase.
|
|
3
|
+
* @param value The string to process.
|
|
4
|
+
* @param locale The current locale.
|
|
5
|
+
* @returns The processed string.
|
|
6
|
+
*/
|
|
7
|
+
export declare function capitalize(value: string, locale?: Intl.Locale | string): string;
|
|
8
|
+
/**
|
|
9
|
+
* Replaces all new lines in the specified value by HTML line breaks.
|
|
10
|
+
* @param value The string to format.
|
|
11
|
+
* @returns The formatted string.
|
|
12
|
+
*/
|
|
13
|
+
export declare function newLineToBr(value: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Reverses the specified string.
|
|
16
|
+
* @param value The string to reverse.
|
|
17
|
+
* @returns The reversed string.
|
|
18
|
+
*/
|
|
19
|
+
export declare function reverse(value: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Converts a string to an array.
|
|
22
|
+
* @param value The string to split into characters or chunks.
|
|
23
|
+
* @param splitLength The maximum length of the chunks.
|
|
24
|
+
* @returns An array whose elements contain the characters or chunks.
|
|
25
|
+
*/
|
|
26
|
+
export declare function split(value: string, splitLength?: number): string[];
|
|
27
|
+
/**
|
|
28
|
+
* Removes the HTML tags from the specified string.
|
|
29
|
+
* @param value The string to process.
|
|
30
|
+
* @returns The processed string.
|
|
31
|
+
*/
|
|
32
|
+
export declare function stripTags(value: string): string;
|
|
33
|
+
/**
|
|
34
|
+
* Removes whitespace from both ends of the items of the specified array.
|
|
35
|
+
* @param array The array to process.
|
|
36
|
+
* @returns The input array.
|
|
37
|
+
*/
|
|
38
|
+
export declare function trimArray<T>(array: T[]): T[];
|
|
39
|
+
/**
|
|
40
|
+
* Removes whitespace from both ends of the properties of the specified object.
|
|
41
|
+
* @param object The object to process.
|
|
42
|
+
* @returns The input object.
|
|
43
|
+
*/
|
|
44
|
+
export declare function trimObject<T>(object: Record<string, T>): Record<string, T>;
|
|
45
|
+
/**
|
|
46
|
+
* Truncates the specified string to the given number of characters.
|
|
47
|
+
* @param value The string to be truncated.
|
|
48
|
+
* @param length The maximum length.
|
|
49
|
+
* @param ellipsis The ellipsis to append to the truncated text.
|
|
50
|
+
* @returns The truncated string.
|
|
51
|
+
*/
|
|
52
|
+
export declare function truncate(value: string, length: number, ellipsis?: string): string;
|
|
53
|
+
/**
|
|
54
|
+
* Replaces invalid XML characters in a string with their valid XML equivalent.
|
|
55
|
+
* @param value The string to process.
|
|
56
|
+
* @returns The processed string.
|
|
57
|
+
*/
|
|
58
|
+
export declare function xmlEscape(value: string): string;
|
|
59
|
+
//# sourceMappingURL=String.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"String.d.ts","sourceRoot":"","sources":["../src/Client/String.ts"],"names":[],"mappings":"AAWA;;;;;GAKG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,GAAE,IAAI,CAAC,MAAM,GAAC,MAA2B,GAAG,MAAM,CAEjG;AAED;;;;GAIG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAEjD;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE7C;AAED;;;;;GAKG;AACH,wBAAgB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,SAAI,GAAG,MAAM,EAAE,CAE9D;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/C;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,GAAG,CAAC,EAAE,CAG5C;AAED;;;;GAIG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAG1E;AAED;;;;;;GAMG;AACH,wBAAgB,QAAQ,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,QAAQ,SAAQ,GAAG,MAAM,CAEhF;AAED;;;;GAIG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAE/C"}
|
package/lib/String.js
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mapping between special characters and the corresponding XML entities.
|
|
3
|
+
*/
|
|
4
|
+
const xmlEntities = new Map([
|
|
5
|
+
["&", "&"],
|
|
6
|
+
["<", "<"],
|
|
7
|
+
[">", ">"],
|
|
8
|
+
['"', """],
|
|
9
|
+
["'", "'"]
|
|
10
|
+
]);
|
|
11
|
+
/**
|
|
12
|
+
* Converts the first character to uppercase.
|
|
13
|
+
* @param value The string to process.
|
|
14
|
+
* @param locale The current locale.
|
|
15
|
+
* @returns The processed string.
|
|
16
|
+
*/
|
|
17
|
+
export function capitalize(value, locale = navigator.language) {
|
|
18
|
+
return value.charAt(0).toLocaleUpperCase(locale) + value.slice(1);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Replaces all new lines in the specified value by HTML line breaks.
|
|
22
|
+
* @param value The string to format.
|
|
23
|
+
* @returns The formatted string.
|
|
24
|
+
*/
|
|
25
|
+
export function newLineToBr(value) {
|
|
26
|
+
return value.split(/\r?\n/g).join("<br>");
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Reverses the specified string.
|
|
30
|
+
* @param value The string to reverse.
|
|
31
|
+
* @returns The reversed string.
|
|
32
|
+
*/
|
|
33
|
+
export function reverse(value) {
|
|
34
|
+
return Array.from(value).reverse().join("");
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Converts a string to an array.
|
|
38
|
+
* @param value The string to split into characters or chunks.
|
|
39
|
+
* @param splitLength The maximum length of the chunks.
|
|
40
|
+
* @returns An array whose elements contain the characters or chunks.
|
|
41
|
+
*/
|
|
42
|
+
export function split(value, splitLength = 1) {
|
|
43
|
+
return splitLength == 1 ? Array.from(value) : (value.match(new RegExp(`.{1,${splitLength}}`, "gsy")) ?? []);
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Removes the HTML tags from the specified string.
|
|
47
|
+
* @param value The string to process.
|
|
48
|
+
* @returns The processed string.
|
|
49
|
+
*/
|
|
50
|
+
export function stripTags(value) {
|
|
51
|
+
return value.replace(/<[^>]+>/g, "");
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Removes whitespace from both ends of the items of the specified array.
|
|
55
|
+
* @param array The array to process.
|
|
56
|
+
* @returns The input array.
|
|
57
|
+
*/
|
|
58
|
+
export function trimArray(array) {
|
|
59
|
+
for (const [index, value] of array.entries())
|
|
60
|
+
if (typeof value == "string")
|
|
61
|
+
Reflect.set(array, index, value.trim());
|
|
62
|
+
return array;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Removes whitespace from both ends of the properties of the specified object.
|
|
66
|
+
* @param object The object to process.
|
|
67
|
+
* @returns The input object.
|
|
68
|
+
*/
|
|
69
|
+
export function trimObject(object) {
|
|
70
|
+
for (const [key, value] of Object.entries(object))
|
|
71
|
+
if (typeof value == "string")
|
|
72
|
+
Reflect.set(object, key, value.trim());
|
|
73
|
+
return object;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Truncates the specified string to the given number of characters.
|
|
77
|
+
* @param value The string to be truncated.
|
|
78
|
+
* @param length The maximum length.
|
|
79
|
+
* @param ellipsis The ellipsis to append to the truncated text.
|
|
80
|
+
* @returns The truncated string.
|
|
81
|
+
*/
|
|
82
|
+
export function truncate(value, length, ellipsis = "...") {
|
|
83
|
+
return value.length > length ? value.slice(0, length) + ellipsis : value;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Replaces invalid XML characters in a string with their valid XML equivalent.
|
|
87
|
+
* @param value The string to process.
|
|
88
|
+
* @returns The processed string.
|
|
89
|
+
*/
|
|
90
|
+
export function xmlEscape(value) {
|
|
91
|
+
return value.replace(/[&<>"']/g, character => xmlEntities.get(character) ?? character);
|
|
92
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MenuActivator.d.ts","sourceRoot":"","sources":["../../src/Client/UI/MenuActivator.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,aAAc,SAAQ,WAAW;;
|
|
1
|
+
{"version":3,"file":"MenuActivator.d.ts","sourceRoot":"","sources":["../../src/Client/UI/MenuActivator.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,qBAAa,aAAc,SAAQ,WAAW;IAS7C;;OAEG;IACH,iBAAiB,IAAI,IAAI;CAQzB;AAED;;GAEG;AACH,OAAO,CAAC,MAAM,CAAC;IAEd;;OAEG;IACH,UAAU,qBAAqB;QAC9B,gBAAgB,EAAE,aAAa,CAAC;KAChC;CACD"}
|
package/lib/UI/MenuActivator.js
CHANGED
|
@@ -2,10 +2,6 @@
|
|
|
2
2
|
* A component that activates the items of a menu based on the current document URL.
|
|
3
3
|
*/
|
|
4
4
|
export class MenuActivator extends HTMLElement {
|
|
5
|
-
/**
|
|
6
|
-
* The root element.
|
|
7
|
-
*/
|
|
8
|
-
#root = this.firstElementChild;
|
|
9
5
|
/**
|
|
10
6
|
* Registers the component.
|
|
11
7
|
*/
|
|
@@ -16,7 +12,7 @@ export class MenuActivator extends HTMLElement {
|
|
|
16
12
|
* Method invoked when this component is connected.
|
|
17
13
|
*/
|
|
18
14
|
connectedCallback() {
|
|
19
|
-
for (const anchor of this
|
|
15
|
+
for (const anchor of this.firstElementChild.getElementsByTagName("a"))
|
|
20
16
|
if (anchor.href != location.href)
|
|
21
17
|
anchor.classList.remove("active");
|
|
22
18
|
else {
|
package/package.json
CHANGED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The mapping between special characters and the corresponding XML entities.
|
|
3
|
+
*/
|
|
4
|
+
const xmlEntities = new Map([
|
|
5
|
+
["&", "&"],
|
|
6
|
+
["<", "<"],
|
|
7
|
+
[">", ">"],
|
|
8
|
+
['"', """],
|
|
9
|
+
["'", "'"]
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Converts the first character to uppercase.
|
|
14
|
+
* @param value The string to process.
|
|
15
|
+
* @param locale The current locale.
|
|
16
|
+
* @returns The processed string.
|
|
17
|
+
*/
|
|
18
|
+
export function capitalize(value: string, locale: Intl.Locale|string = navigator.language): string {
|
|
19
|
+
return value.charAt(0).toLocaleUpperCase(locale) + value.slice(1);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Replaces all new lines in the specified value by HTML line breaks.
|
|
24
|
+
* @param value The string to format.
|
|
25
|
+
* @returns The formatted string.
|
|
26
|
+
*/
|
|
27
|
+
export function newLineToBr(value: string): string {
|
|
28
|
+
return value.split(/\r?\n/g).join("<br>");
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Reverses the specified string.
|
|
33
|
+
* @param value The string to reverse.
|
|
34
|
+
* @returns The reversed string.
|
|
35
|
+
*/
|
|
36
|
+
export function reverse(value: string): string {
|
|
37
|
+
return Array.from(value).reverse().join("");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Converts a string to an array.
|
|
42
|
+
* @param value The string to split into characters or chunks.
|
|
43
|
+
* @param splitLength The maximum length of the chunks.
|
|
44
|
+
* @returns An array whose elements contain the characters or chunks.
|
|
45
|
+
*/
|
|
46
|
+
export function split(value: string, splitLength = 1): string[] {
|
|
47
|
+
return splitLength == 1 ? Array.from(value) : (value.match(new RegExp(`.{1,${splitLength}}`, "gsy")) ?? []);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Removes the HTML tags from the specified string.
|
|
52
|
+
* @param value The string to process.
|
|
53
|
+
* @returns The processed string.
|
|
54
|
+
*/
|
|
55
|
+
export function stripTags(value: string): string {
|
|
56
|
+
return value.replace(/<[^>]+>/g, "");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Removes whitespace from both ends of the items of the specified array.
|
|
61
|
+
* @param array The array to process.
|
|
62
|
+
* @returns The input array.
|
|
63
|
+
*/
|
|
64
|
+
export function trimArray<T>(array: T[]): T[] {
|
|
65
|
+
for (const [index, value] of array.entries()) if (typeof value == "string") Reflect.set(array, index, value.trim());
|
|
66
|
+
return array;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Removes whitespace from both ends of the properties of the specified object.
|
|
71
|
+
* @param object The object to process.
|
|
72
|
+
* @returns The input object.
|
|
73
|
+
*/
|
|
74
|
+
export function trimObject<T>(object: Record<string, T>): Record<string, T> {
|
|
75
|
+
for (const [key, value] of Object.entries(object)) if (typeof value == "string") Reflect.set(object, key, value.trim());
|
|
76
|
+
return object;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Truncates the specified string to the given number of characters.
|
|
81
|
+
* @param value The string to be truncated.
|
|
82
|
+
* @param length The maximum length.
|
|
83
|
+
* @param ellipsis The ellipsis to append to the truncated text.
|
|
84
|
+
* @returns The truncated string.
|
|
85
|
+
*/
|
|
86
|
+
export function truncate(value: string, length: number, ellipsis = "..."): string {
|
|
87
|
+
return value.length > length ? value.slice(0, length) + ellipsis : value;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Replaces invalid XML characters in a string with their valid XML equivalent.
|
|
92
|
+
* @param value The string to process.
|
|
93
|
+
* @returns The processed string.
|
|
94
|
+
*/
|
|
95
|
+
export function xmlEscape(value: string): string {
|
|
96
|
+
return value.replace(/[&<>"']/g, character => xmlEntities.get(character) ?? character);
|
|
97
|
+
}
|
|
@@ -3,11 +3,6 @@
|
|
|
3
3
|
*/
|
|
4
4
|
export class MenuActivator extends HTMLElement {
|
|
5
5
|
|
|
6
|
-
/**
|
|
7
|
-
* The root element.
|
|
8
|
-
*/
|
|
9
|
-
readonly #root = this.firstElementChild!;
|
|
10
|
-
|
|
11
6
|
/**
|
|
12
7
|
* Registers the component.
|
|
13
8
|
*/
|
|
@@ -19,7 +14,7 @@ export class MenuActivator extends HTMLElement {
|
|
|
19
14
|
* Method invoked when this component is connected.
|
|
20
15
|
*/
|
|
21
16
|
connectedCallback(): void {
|
|
22
|
-
for (const anchor of this
|
|
17
|
+
for (const anchor of this.firstElementChild!.getElementsByTagName("a"))
|
|
23
18
|
if (anchor.href != location.href) anchor.classList.remove("active");
|
|
24
19
|
else {
|
|
25
20
|
anchor.classList.add("active");
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Defines the alignment of a dropdown menu.
|
|
3
|
-
*/
|
|
4
|
-
export declare const MenuAlignment: Readonly<{
|
|
5
|
-
/**
|
|
6
|
-
* The dropdown menu is right aligned.
|
|
7
|
-
*/
|
|
8
|
-
End: "End";
|
|
9
|
-
/**
|
|
10
|
-
* The dropdown menu is left aligned.
|
|
11
|
-
*/
|
|
12
|
-
Start: "Start";
|
|
13
|
-
}>;
|
|
14
|
-
/**
|
|
15
|
-
* Defines the alignment of a dropdown menu.
|
|
16
|
-
*/
|
|
17
|
-
export type MenuAlignment = typeof MenuAlignment[keyof typeof MenuAlignment];
|
|
18
|
-
//# sourceMappingURL=MenuAlignment.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"MenuAlignment.d.ts","sourceRoot":"","sources":["../../src/Client/Html/MenuAlignment.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,eAAO,MAAM,aAAa;IAEzB;;OAEG;;IAGH;;OAEG;;EAEF,CAAC;AAEH;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,OAAO,aAAa,CAAC,MAAM,OAAO,aAAa,CAAC,CAAC"}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Defines the alignment of a dropdown menu.
|
|
3
|
-
*/
|
|
4
|
-
export const MenuAlignment = Object.freeze({
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* The dropdown menu is right aligned.
|
|
8
|
-
*/
|
|
9
|
-
End: "End",
|
|
10
|
-
|
|
11
|
-
/**
|
|
12
|
-
* The dropdown menu is left aligned.
|
|
13
|
-
*/
|
|
14
|
-
Start: "Start"
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Defines the alignment of a dropdown menu.
|
|
19
|
-
*/
|
|
20
|
-
export type MenuAlignment = typeof MenuAlignment[keyof typeof MenuAlignment];
|