1+ /**
2+ * Gets the file name from a path
3+ *
4+ * @param path
5+ */
16export function getFileName ( path : string ) {
27 return path . split ( '/' ) . at ( - 1 ) ;
38}
49
10+ /**
11+ * Checks if a path is a path or a file name
12+ *
13+ * @param path
14+ */
515export function isPath ( path : string ) {
616 return path . split ( '/' ) . length > 1 ;
717}
818
19+ /**
20+ * Removes the file ending of a file name
21+ *
22+ * @param fileName
23+ */
924export function removeFileEnding ( fileName : string ) {
1025 const fileNameParts = fileName . split ( '.' ) ;
1126 if ( fileNameParts . length === 1 ) {
@@ -19,16 +34,33 @@ export function removeFileEnding(fileName: string) {
1934 }
2035}
2136
37+ /**
38+ * Clamp, unused
39+ *
40+ * @param num
41+ * @param min
42+ * @param max
43+ */
2244export function clamp ( num : number , min : number , max : number ) : number {
2345 return Math . min ( Math . max ( num , min ) , max ) ;
2446}
2547
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+ */
2854export function mod ( n : number , m : number ) : number {
2955 return ( ( n % m ) + m ) % m ;
3056}
3157
58+ /**
59+ * Checks if 2 arrays are equal, the arrays should have the same datatype
60+ *
61+ * @param arr1
62+ * @param arr2
63+ */
3264export function arrayEquals ( arr1 : any [ ] , arr2 : any [ ] ) {
3365 if ( arr1 . length !== arr2 . length ) {
3466 return false ;
@@ -42,3 +74,83 @@ export function arrayEquals(arr1: any[], arr2: any[]) {
4274
4375 return true ;
4476}
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