string substitution and template library
リビジョン | 900200da272152ddcc5b657b3e6ee416295772f8 (tree) |
---|---|
日時 | 2019-08-07 17:04:03 |
作者 | frostbane <frostbane@prog...> |
コミッター | frostbane |
merge feature/opener
@@ -4,3 +4,5 @@ | ||
4 | 4 | bin/* |
5 | 5 | doc/* |
6 | 6 | node_modules/* |
7 | +syntax: glob | |
8 | +.idea/* |
@@ -1,3 +1,21 @@ | ||
1 | +/* jshint | |
2 | +forin: false, | |
3 | +nonew: false, | |
4 | +proto: true, | |
5 | +maxlen: 180 | |
6 | + */ | |
7 | + | |
8 | +/* global | |
9 | +root, | |
10 | +window, | |
11 | +Promise, | |
12 | +$, | |
13 | +$$, | |
14 | +exports, | |
15 | +module, | |
16 | +define | |
17 | + */ | |
18 | + | |
1 | 19 | /*! |
2 | 20 | .. ┌ -: . |
3 | 21 | ([]▄▄├]▄▄▄¿┐`, |
@@ -29,7 +47,7 @@ | ||
29 | 47 | ⌐ ,⌐ |
30 | 48 | |
31 | 49 | |
32 | - string format 0.2.1 | |
50 | + string format 0.2.2 | |
33 | 51 | Copyright 2015, 2016, 2017 Frostbane Ac |
34 | 52 | 2015.04.24 |
35 | 53 |
@@ -123,6 +141,11 @@ | ||
123 | 141 | return result; |
124 | 142 | }; |
125 | 143 | |
144 | + var escapeExpression = function(string) { | |
145 | + // $& means the whole matched string | |
146 | + return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); | |
147 | + }; | |
148 | + | |
126 | 149 | /** |
127 | 150 | * |
128 | 151 | * @param {Object} settings |
@@ -747,7 +770,7 @@ | ||
747 | 770 | var padIn, |
748 | 771 | padOut, |
749 | 772 | //filterExp = " *\\|?[a-zA-Z_\\| 0-9]*"; |
750 | - filterExp = "(?=[: \\|]? *[:| |\\|]?[^}]*) *[:| |\\|]?[^}]*"; | |
773 | + filterExp = "(?=[: \\|]? *[:| |\\|]?[^(" + closer + ")]*) *[:| |\\|]?[^(" + closer + ")]*"; | |
751 | 774 | |
752 | 775 | if( |
753 | 776 | (/^-?[\d]+(?:e-?\d+)?$/) |
@@ -1214,7 +1237,15 @@ | ||
1214 | 1237 | // useExplicitArrays = value === true; |
1215 | 1238 | //}); |
1216 | 1239 | |
1217 | - format.VERSION = "0.2.1"; | |
1240 | + format.setOpener = function(left){ | |
1241 | + opener = escapeExpression(left); | |
1242 | + }; | |
1243 | + | |
1244 | + format.setCloser = function(right){ | |
1245 | + closer = escapeExpression(right); | |
1246 | + }; | |
1247 | + | |
1248 | + format.VERSION = "0.2.2"; | |
1218 | 1249 | |
1219 | 1250 | return format; |
1220 | 1251 |
@@ -298,6 +298,40 @@ | ||
298 | 298 | |
299 | 299 | }, |
300 | 300 | |
301 | + testChangeOpenerParentheesis: function(){ | |
302 | + format.setOpener("(("); | |
303 | + format.setCloser("))"); | |
304 | + | |
305 | + var formatString = "{{ who }} is {{ what }} (( who )) is (( what ))."; | |
306 | + | |
307 | + var param = { | |
308 | + who :"MR. BIG", | |
309 | + what :"small", | |
310 | + }; | |
311 | + | |
312 | + var expected = "{{ who }} is {{ what }} " + param.who + " is " + param.what + ".", | |
313 | + actual = format(formatString, param); | |
314 | + | |
315 | + assertEqual(expected, actual); | |
316 | + }, | |
317 | + | |
318 | + testChangeOpenerBracket: function(){ | |
319 | + format.setOpener("[["); | |
320 | + format.setCloser("]]"); | |
321 | + | |
322 | + var formatString = "{{ who }} is {{ what }} [[ who | toUpperCase ]] is [[ what | toLowerCase ]]."; | |
323 | + | |
324 | + var param = { | |
325 | + who :"mr. big", | |
326 | + what :"SMALL", | |
327 | + }; | |
328 | + | |
329 | + var expected = "{{ who }} is {{ what }} " + param.who.toUpperCase() + " is " + param.what.toLowerCase() + ".", | |
330 | + actual = format(formatString, param); | |
331 | + | |
332 | + assertEqual(expected, actual); | |
333 | + }, | |
334 | + | |
301 | 335 | testAdditionalFilter :function(){ |
302 | 336 | var myCondFilter = function(s){ |
303 | 337 | var f = parseFloat(s); |
@@ -337,6 +371,11 @@ | ||
337 | 371 | format.resetFilters(); |
338 | 372 | }, |
339 | 373 | |
374 | + tearDown: function(){ | |
375 | + format.setOpener("{{"); | |
376 | + format.setCloser("}}"); | |
377 | + }, | |
378 | + | |
340 | 379 | shutDown :function(){ |
341 | 380 | result.log(); |
342 | 381 | }, |