1
+ /**
2
+ * Gets the file name from a path
3
+ *
4
+ * @param path
5
+ */
1
6
export function getFileName ( path : string ) {
2
7
return path . split ( '/' ) . at ( - 1 ) ;
3
8
}
4
9
10
+ /**
11
+ * Checks if a path is a path or a file name
12
+ *
13
+ * @param path
14
+ */
5
15
export function isPath ( path : string ) {
6
16
return path . split ( '/' ) . length > 1 ;
7
17
}
8
18
19
+ /**
20
+ * Removes the file ending of a file name
21
+ *
22
+ * @param fileName
23
+ */
9
24
export function removeFileEnding ( fileName : string ) {
10
25
const fileNameParts = fileName . split ( '.' ) ;
11
26
if ( fileNameParts . length === 1 ) {
@@ -19,16 +34,33 @@ export function removeFileEnding(fileName: string) {
19
34
}
20
35
}
21
36
37
+ /**
38
+ * Clamp, unused
39
+ *
40
+ * @param num
41
+ * @param min
42
+ * @param max
43
+ */
22
44
export function clamp ( num : number , min : number , max : number ) : number {
23
45
return Math . min ( Math . max ( num , min ) , max ) ;
24
46
}
25
47
26
-
27
- // js can't even implement modulo correctly...
48
+ /**
49
+ * js can't even implement modulo correctly...
50
+ *
51
+ * @param n
52
+ * @param m
53
+ */
28
54
export function mod ( n : number , m : number ) : number {
29
55
return ( ( n % m ) + m ) % m ;
30
56
}
31
57
58
+ /**
59
+ * Checks if 2 arrays are equal, the arrays should have the same datatype
60
+ *
61
+ * @param arr1
62
+ * @param arr2
63
+ */
32
64
export function arrayEquals ( arr1 : any [ ] , arr2 : any [ ] ) {
33
65
if ( arr1 . length !== arr2 . length ) {
34
66
return false ;
@@ -42,3 +74,83 @@ export function arrayEquals(arr1: any[], arr2: any[]) {
42
74
43
75
return true ;
44
76
}
77
+
78
+ /**
79
+ * Template "engine" from my media db plugin
80
+ *
81
+ * @param template
82
+ * @param dataModel
83
+ */
84
+ export function replaceTags ( template : string , dataModel : any ) : string {
85
+ const resolvedTemplate = template . replace ( new RegExp ( '{{.*?}}' , 'g' ) , ( match : string ) => replaceTag ( match , dataModel ) ) ;
86
+
87
+ return resolvedTemplate ;
88
+ }
89
+
90
+ /**
91
+ * Takes in a template match and returns the replacement data
92
+ *
93
+ * @param match
94
+ * @param dataModel
95
+ */
96
+ function replaceTag ( match : string , dataModel : any ) : string {
97
+ let tag = match ;
98
+ tag = tag . substring ( 2 ) ;
99
+ tag = tag . substring ( 0 , tag . length - 2 ) ;
100
+ tag = tag . trim ( ) ;
101
+
102
+ let parts = tag . split ( ':' ) ;
103
+ if ( parts . length === 1 ) {
104
+ let path = parts [ 0 ] . split ( '.' ) ;
105
+
106
+ let obj = traverseObject ( path , dataModel ) ;
107
+
108
+ if ( obj === undefined ) {
109
+ return '{{ INVALID TEMPLATE TAG - object undefined }}' ;
110
+ }
111
+
112
+ return obj ;
113
+ } else if ( parts . length === 2 ) {
114
+ let operator = parts [ 0 ] ;
115
+
116
+ let path = parts [ 1 ] . split ( '.' ) ;
117
+
118
+ let obj = traverseObject ( path , dataModel ) ;
119
+
120
+ if ( obj === undefined ) {
121
+ return '{{ INVALID TEMPLATE TAG - object undefined }}' ;
122
+ }
123
+
124
+ if ( operator === 'LIST' ) {
125
+ if ( ! Array . isArray ( obj ) ) {
126
+ return '{{ INVALID TEMPLATE TAG - operator LIST is only applicable on an array }}' ;
127
+ }
128
+ return obj . map ( ( e : any ) => `- ${ e } ` ) . join ( '\n' ) ;
129
+ } else if ( operator === 'ENUM' ) {
130
+ if ( ! Array . isArray ( obj ) ) {
131
+ return '{{ INVALID TEMPLATE TAG - operator ENUM is only applicable on an array }}' ;
132
+ }
133
+ return obj . join ( ', ' ) ;
134
+ }
135
+
136
+ return `{{ INVALID TEMPLATE TAG - unknown operator ${ operator } }}` ;
137
+ }
138
+
139
+ return '{{ INVALID TEMPLATE TAG }}' ;
140
+ }
141
+
142
+ /**
143
+ * Traverses the object along a property path
144
+ *
145
+ * @param path
146
+ * @param obj
147
+ */
148
+ function traverseObject ( path : Array < string > , obj : any ) : any {
149
+ for ( let part of path ) {
150
+ if ( obj !== undefined ) {
151
+ obj = obj [ part ] ;
152
+ }
153
+ }
154
+
155
+ return obj ;
156
+ }
0 commit comments