1
- // gist @https ://gist.github.com/KooiInc/5ce3520c54f19e27351ec9081318dff4
2
1
// $ etc. @https ://github.com/KooiInc/SBHelpers
3
2
// 20240612
4
3
// -----------------------------------------------------------------------
5
- import { default as interpolate , interpolateFactory } from "../Interpolate.module.js" ;
4
+ const { $, logFactory, } =
5
+ await import ( "https://cdn.jsdelivr.net/gh/KooiInc/SBHelpers/index.browser.bundled.js" ) ;
6
+ checkCBPage ( ) ;
7
+ import { default as interpolate , interpolateFactory } from "../index.js" ;
6
8
const tokenize = Symbol . for ( "interpolate" ) ;
7
9
const tokenize$ = Symbol . for ( "interpolate$" ) ;
8
10
// try out in developer screen
9
11
window . tokenize = tokenize ;
10
12
window . interpolate = interpolate ;
11
- const { $, logFactory, } =
12
- await import ( "https://cdn.jsdelivr.net/gh/KooiInc/SBHelpers/index.browser.bundled.js" ) ;
13
13
const { log } = logFactory ( ) ;
14
14
const insert = interpolateFactory ( "¡no value!" ) ;
15
15
const demoText = demoTexts ( ) ;
@@ -22,33 +22,52 @@ function demo() {
22
22
const tableTemplatesCode = demoText . tableTemplatesCode ;
23
23
24
24
// let's create a table
25
- const tableTemplate = `<table><caption>{caption}</caption><thead><tr><th>#</th><th>prename</th><th>surname</th></tr></thead><tbody>{rows}</tbody></table>` ;
26
- const tableRowTemplate = `<tr><td>{index}</td><td>{pre}</td><td>{last}</td></tr>` ;
25
+ const tableTemplate = `
26
+ <table>
27
+ <caption>{caption}</caption>
28
+ <thead>
29
+ <tr>
30
+ <th>#</th>
31
+ <th>prename</th>
32
+ <th>surname</th>
33
+ </tr>
34
+ </thead>
35
+ <tbody>{rows}</tbody>
36
+ </table>` ;
37
+ const tableRowTemplate = `
38
+ <tr>
39
+ <td>{index}</td>
40
+ <td>{pre}</td>
41
+ <td>{last}</td>
42
+ </tr>` ;
27
43
// the replacements (for tableRowTemplate);
28
44
const theNames = getNamesObj ( ) ;
29
45
30
46
setStyling ( ) ;
31
47
const theNamesClear = theNames . map ( row =>
32
- ! / i g n o r e d / i. test ( row . pre ) && row . pre ?. startsWith ( `<b` )
33
- ? { ...row , pre : `<b class="notifyHeader">Cleared values</b>` } : row ) ;
48
+ ! / i g n o r e d / i. test ( row . pre ) && / I n v a l i d \/ m i s s i n g / . test ( row . pre )
49
+ ? { ...row , pre : `<b class="notifyHeader">Invalid/missing token keys/ values are cleared </b>` } : row ) ;
34
50
const theNamesTokensAsArrays = {
35
- pre : `Mary,Muhammed,Missy,Hillary,Foo,Bar,小平,Володимир,zero (0),Row,,missing value = >`
51
+ pre : `Mary,Muhammed,Missy,Hillary,Foo,Bar,小平,Володимир,zero (0),Row,,missing value <span class="point right"></span >`
36
52
. split ( `,` ) . map ( v => ! v . length ? undefined : v ) ,
37
- last : `Peterson,Ali,Johnson,Clinton,Bar,Foo,邓,Зеленський,0,10,<= missing value,`
53
+ last : `Peterson,Ali,Johnson,Clinton,Bar,Foo,邓,Зеленський,0,10,<span class="point left"></span> missing value,`
38
54
. split ( `,` ) . map ( v => ! v . length ? null : v )
39
55
}
40
-
56
+
41
57
// show me the money!
42
58
const table1 = tableTemplate [ tokenize ] ( {
43
59
caption : `<code>tableRowTemplate[tokenize]</code> using <code>theNames</code>` ,
44
- rows : tableRowTemplate [ tokenize ] ( ...theNames ) } ) ;
60
+ rows : tableRowTemplate [ tokenize ] ( ...theNames )
61
+ } ) ;
45
62
const table2 = tableTemplate [ tokenize ] ( {
46
63
caption : `<code>tableRowTemplate[tokenize$]</code> (empty/invalid values => empty string)` ,
47
- rows : tableRowTemplate [ tokenize$ ] ( ...theNamesClear ) } ) ;
64
+ rows : tableRowTemplate [ tokenize$ ] ( ...theNamesClear )
65
+ } ) ;
48
66
const table3 = tableTemplate [ tokenize ] ( {
49
67
caption : `<code>tableRowTemplate[tokenize]</code> token values are arrays` ,
50
- rows : tableRowTemplate [ tokenize ] ( theNamesTokensAsArrays ) } ) ;
51
- log ( `!!${ tableTemplatesCode } ` , `!!${ table1 } ` , `!!${ table2 } ` , `!!${ code4Array } ` , `!!${ table3 } ` ) ;
68
+ rows : tableRowTemplate [ tokenize ] ( theNamesTokensAsArrays )
69
+ } ) ;
70
+ log ( `!!${ tableTemplatesCode } ` , `!!${ table1 } ` , `!!${ table2 } ` , `!!${ code4Array } ` , `!!${ table3 } ` ) ;
52
71
Prism . highlightAll ( ) ;
53
72
createContent ( ) ;
54
73
}
@@ -69,14 +88,15 @@ function setStyling() {
69
88
border-collapse: collapse;
70
89
vertical-align: top;
71
90
min-width: 500px;
91
+ max-width: 700px;
72
92
73
93
td, th {
74
94
padding: 2px 4px;
75
95
font-size: 14px;
76
96
height: 18px;
77
97
}
78
98
79
- td:nth-child(2n), th:nth-child(2n) { width: 200px; }
99
+ /* td:nth-child(2n), th:nth-child(2n) { width: 200px; }*/
80
100
81
101
th {
82
102
font-weight: bold;
@@ -88,8 +108,7 @@ function setStyling() {
88
108
89
109
td:first-child, th:first-child {
90
110
text-align: right; padding-right: 5px;
91
- min-width: 12px;
92
- max-width: 36px;
111
+ width: 24px;
93
112
}
94
113
caption {
95
114
border: 1px solid #ccc;
@@ -102,7 +121,17 @@ function setStyling() {
102
121
}` ,
103
122
`.largeArrowDown:before{
104
123
content: '${ repeat ( `⬇` , 3 ) } ';
105
- color:green; }` ,
124
+ color: red; }` ,
125
+ `span.point:before {
126
+ font-weight: bold;
127
+ color: red;
128
+ content: '➜';
129
+ vertical-align: middle;
130
+ }` ,
131
+ `span.point.left:before {
132
+ display: inline-block;
133
+ transform: rotate(-180deg);
134
+ }` ,
106
135
`b.notifyHeader { color: green; }` ,
107
136
`li.head {margin-left: -2rem !important;}` ,
108
137
`li.head table {margin-top: 1.2rem;}` ,
@@ -111,6 +140,18 @@ function setStyling() {
111
140
`a:hover { text-decoration:underline; }` ,
112
141
`a[target]:before { color:rgba(0,0,238,0.7);font-size: 1.1rem;vertical-align:bottom }` ,
113
142
`a[target="_blank"]:before {content: '\\2197'' '; }` ,
143
+ `a[target].cbBacklink {
144
+ &:before {
145
+ content: url(./codebergicon.ico)' ';
146
+ vertical-align: middle;
147
+ }
148
+ }` ,
149
+ `a[target].ghBacklink {
150
+ &:before {
151
+ content: url(./githubicon.png)' ';
152
+ vertical-align: middle;
153
+ }
154
+ }` ,
114
155
`a[target="_top"]:before {content: '\\21BA'' '; }` ,
115
156
`ul#log2screen { margin: 0 auto; max-width: 40vw; }` ,
116
157
`#log2screen pre.syntax {
@@ -153,30 +194,33 @@ function getNamesObj() {
153
194
{ pre : `zero (0)` , last : 0 } ,
154
195
{ pre : `Row` , last : 10 } ,
155
196
{ pre : `replacement-is-empty-string` , last : '' } , // ᐊ Empty string value IS replaced
156
- { pre : `<b class="notifyHeader">Not replaced</b>` , last : `<span class="largeArrowDown"></span>` } ,
197
+ {
198
+ pre : `<b class="notifyHeader">Invalid/missing token keys/values are kept</b>` ,
199
+ last : `<span class="largeArrowDown"></span>`
200
+ } ,
157
201
// if !defaultReplacer ...
158
- { pre : `replacement-Is-array` , last : [ 1 , 2 , 3 ] } , // ᐊ Array value IS NOT replaced/
202
+ { pre : `replacement-Is-array` , last : [ 1 , 2 , 3 ] } , // ᐊ Array value IS NOT replaced/
159
203
{ pre : `replacement-Is-null` , last : null } , // ᐊ null value IS NOT replaced
160
- { pre : `replacement-Is-object` , last : { } } , // ᐊ Object value IS NOT replaced
204
+ { pre : `replacement-Is-object` , last : { } } , // ᐊ Object value IS NOT replaced
161
205
{ pre : `replacement-Is-undefined` , last : undefined } , // ᐊ undefined value IS NOT replaced
162
- { pre : `<b class="notifyHeader">Ignored</b>` , last : `<span class="largeArrowDown"></span>` } ,
163
206
{ last : `key-pre-does-not-exist` } , // ᐊ undefined value IS NOT replaced
164
207
{ pre : `key-last-does-not-exist` } , // ᐊ incomplete object, what exists is replaced
165
- { some : `nothing-to-replace` , name : `nothing` } , // ᐊ non relevant keys, tokens ignored
208
+ { some : `nothing-to-replace` , name : `nothing` } , // ᐊ non existing keys, tokens ignored
166
209
{ } , // ᐊ empty object, tokens ignored
167
210
[ `nothing` , `nada` , `zip` , `没有什么` , // ᐊ replacement not an Object, tokens ignored
168
- `niente` , `rien` , `ничего` ]
211
+ `niente` , `rien` , `ничего` ]
169
212
] ;
170
213
}
171
214
172
215
function demoTexts ( ) {
173
- const isStackblitz = / s t a c k b l i t z / i. test ( location . href ) ;
216
+ const isGithub = / g i t h u b / i. test ( location . href ) ;
217
+ const back2repo = `(back to) repository` ;
218
+ const isLocal = / l o c a l h o s t / . test ( location . href ) ;
174
219
const links = [
175
- isStackblitz ? `!!<a target="_top" href="https://stackblitz.com/@KooiInc">All Stackblitz projects</a>` : `!!` ,
176
- `!!<a target="${ isStackblitz ? `_blank` : `_top` } " href="https://github.com/KooiInc/StringInterpolator"
177
- >Github Repository</a>` ,
178
- `!!<a target="_blank" href="https://github.com/KooiInc/es-string-fiddler">Used by es-string-fiddler (Github)</a>`
179
- ] ;
220
+ isGithub
221
+ ? `!!<a class="ghBacklink "target="_top" href="https://github.com/KooiInc/StringInterpolator">${ back2repo } </a>`
222
+ : `!!<a class="cbBacklink" target="_top" href="https://codeberg.org/KooiInc/JS-Interpolate">${ back2repo } </a>`
223
+ ] ;
180
224
const replacement = { blah : `FOOBLAH` , bar : `BARRED` } ;
181
225
const someStr = `Blah [{blah}] and blah and {foo}, but then again [\\{bar\\} | {bar}]` ;
182
226
const namesUsed = getNamesObj . toString ( )
@@ -185,7 +229,7 @@ function demoTexts() {
185
229
. replace ( / \n \s + ] / , `\n]` )
186
230
. replace ( / < / g, `<` ) ;
187
231
return {
188
- links,
232
+ links : ! isLocal && links || [ `!!LOCAL TEST` ] ,
189
233
preSyntax : `<div class="readme">The module exports the factory itself (<code>interpolateFactory</code>),
190
234
the <code>interpolate</code> function (default) and the <code>interpolateClear</code> function (which
191
235
clears empty placeholders).</div>
@@ -211,10 +255,10 @@ insert(templateStringEx,
211
255
{wrld: "WORLD", univrs: null},
212
256
{wrld: null, univrs: "UNIVERSE"},
213
257
{wrld: "WORLD", univrs: "AND UNIVERSE"} ); /* result =>\n${
214
- insert ( " hello {wrld} {univrs}\n" ,
215
- { wrld : "WORLD" , univrs : null } ,
216
- { wrld : null , univrs : "UNIVERSE" } ,
217
- { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
258
+ insert ( " hello {wrld} {univrs}\n" ,
259
+ { wrld : "WORLD" , univrs : null } ,
260
+ { wrld : null , univrs : "UNIVERSE" } ,
261
+ { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
218
262
219
263
// On importing interpolateFactory String.prototype
220
264
// was extended using 2 Symbols. Here we
@@ -225,23 +269,27 @@ const tokenize$ = Symbol.for("interpolate$");
225
269
// now we can use the 'symbolic extensions' (named tokenize/tokenize$)
226
270
// [tokenize]: keep tokens with empty values intact
227
271
templateStringEx[tokenize](
272
+ {wrld: "WORLD"},
228
273
{wrld: "WORLD", univrs: null},
229
274
{wrld: null, univrs: "UNIVERSE"},
230
275
{wrld: "WORLD", univrs: "AND UNIVERSE"} ); /* result => \n${
231
- " hello {wrld} {univrs}\n" [ tokenize ] (
232
- { wrld : "WORLD" , univrs : null } ,
233
- { wrld : null , univrs : "UNIVERSE" } ,
234
- { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
276
+ " hello {wrld} {univrs}\n" [ tokenize ] (
277
+ { wrld : "WORLD (Note: missing tokens are ignored)" } ,
278
+ { wrld : "WORLD" , univrs : null } ,
279
+ { wrld : null , univrs : "UNIVERSE" } ,
280
+ { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
235
281
236
- // [tokenize$]: cleanup empty values
282
+ // [tokenize$]: remove tokens without values (replace with empty string)
237
283
templateStringEx[tokenize$](
284
+ {univrs: "UNIVERSE"},
238
285
{wrld: "WORLD", univrs: null},
239
286
{wrld: null, univrs: "UNIVERSE"},
240
287
{wrld: "WORLD", univrs: "AND UNIVERSE"} ); /* result =>\n${
241
- " hello {wrld} {univrs}\n" [ tokenize$ ] (
242
- { wrld : "WORLD" , univrs : null } ,
243
- { wrld : null , univrs : "UNIVERSE" } ,
244
- { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
288
+ " hello {wrld} {univrs}\n" [ tokenize$ ] (
289
+ { univrs : "UNIVERSE (Note: missing tokens are cleared)" } ,
290
+ { wrld : "WORLD" , univrs : null } ,
291
+ { wrld : null , univrs : "UNIVERSE" } ,
292
+ { wrld : "WORLD" , univrs : "AND UNIVERSE" } ) } */
245
293
246
294
// escaped "{" and/or "}" and non existing token values are ignored
247
295
const replacement = { blah: "FOOBLAH", bar: "BARRED" };
@@ -255,9 +303,9 @@ someStr[tokenize$](replacement); => "${someStr[tokenize$](replacement)}"</code><
255
303
// to fill the next table, a single object with array values is used
256
304
const theNamesTokensAsArrays = {
257
305
pre: [ "Mary", "Muhammed", "Missy", "Hillary", "Foo", "Bar",
258
- "小平", "Володимир", "zero (0)", "Row", null, "missing value => " ],
306
+ "小平", "Володимир", "zero (0)", "Row", null, "missing value" ],
259
307
last: [ "Peterson", "Ali", "Johnson", "Clinton", "Bar", "Foo",
260
- "邓", "Зеленський", "0", "10", "<= missing value", null ]
308
+ "邓", "Зеленський", "0", "10", "missing value", null ]
261
309
};</code>` ,
262
310
263
311
tableTemplatesCode : `<br><pre class="syntax language-javascript line-numbers"><code class="language-javascript">
@@ -296,4 +344,10 @@ function createContent() {
296
344
$ . editCssRule ( `.bottomSpace { height: ${ $ . node ( `.container` ) . clientHeight } px; }` ) ;
297
345
$ ( `#log2screen` ) . afterMe ( `<div class="bottomSpace">` ) ;
298
346
}
347
+
348
+ function checkCBPage ( ) {
349
+ if ( location . href . includes ( `codeberg.page` ) ) {
350
+ $ ( `head` ) . append ( `<base href="https://kooiinc.codeberg.page/JS-Interpolate/">` ) ;
351
+ }
352
+ }
299
353
/* endregion indexCreatr */
0 commit comments