diff --git a/bin/string-templ.js b/bin/string-templ.js index ce623d6..1407447 100644 --- a/bin/string-templ.js +++ b/bin/string-templ.js @@ -1,8 +1,9 @@ #!/usr/bin/env node // example: -// ls -1 | node ./src/cli.js '${all}' 'X-${all}-Y' -// ls -1 | node ./src/cli.js '${all}' 'X-${all}-Y' '{"all":"(\d*)]"}' +// ls -1 | node ./bin/string-templ.js '${all}' 'X-${all}-Y' +// ls -1 | node ./bin/string-templ.js '${all}' 'X-${all}-Y' '{"all":"(\d*)]"}' +// ls -1 | node ./bin/string-templ.js -f '${1}.${garb}' -t '${1} - ${0}' const VERSION = "0.0.1"; var program = require("commander"); @@ -15,8 +16,9 @@ program .option('-m, --regex-map ', 'a map for matching pattern with key') .option('-e, --throws ', 'verbosely throw exception', parseBoolean) .description(`Example: - string-templ -f '\${1} and \${2}' -t '\${2} scares \${1}' 'dog and bird and cat' //bird and cat scares dog - string-templ -f '\${1} and \${2}' -m '{"1":".*"}' -t '\${2} scares \${1}' 'dog and bird and cat' //cat scares dog and bird`); + string-templ -f '\${1} and \${2}' -t '\${2} scares \${1}' 'dog and bird and cat' //bird and cat scares dog + string-templ -f '\${1} and \${2}' -t '\${2} scares \${1} (\${0} tales)' 'dog and cat' //cat scares dog (dog and cat tales) + string-templ -f '\${1} and \${2}' -m '{"1":".*"}' -t '\${2} scares \${1}' 'dog and bird and cat' //cat scares dog and bird`); program.parse(process.argv); @@ -33,10 +35,10 @@ Object.assign(params, { "throws": program.throws }); var transf = require("../src/index").reformat(params, templateFrom, templateTo); // Process with args -var couter = program.args.length; +var counter = program.args.length; program.args.forEach(function(element, index) { console.log(transf(element)); - if (--couter === 0) { process.exit(0); } + if (--counter === 0) { process.exit(0); } }); // Process with pipeline diff --git a/src/string-templating.js b/src/string-templating.js index dc27324..5d99fd0 100644 --- a/src/string-templating.js +++ b/src/string-templating.js @@ -34,8 +34,16 @@ function reformat(option, oldTempl, newTempl, inputString) { if (typeof option === "string") { throw new Error("option should not be string"); } - return substitute(newTempl, parse(option, oldTempl, inputString)); + var map = parse(option, oldTempl, inputString); + return substitute(newTempl, addFullMatchValue(map, inputString)); +} +// @Util +// Add input string to parsed map of key-values for full match. +function addFullMatchValue(map, str) { + var fullMatchPattern = '0'; + map && (map[fullMatchPattern] = str); + return map; } // @Util diff --git a/test/reformat-tescase.js b/test/reformat-tescase.js index 01e5937..e98bbab 100644 --- a/test/reformat-tescase.js +++ b/test/reformat-tescase.js @@ -8,6 +8,11 @@ describe("Verify reformat function", function() { assert.equal(r, "me-test"); }); + it("Should be able to reformat string #2 with full match", function() { + var r = extractor.reformat({}, "${1} and ${2}", "${2} scares ${1} (${0} tales)", "dog and cat"); + assert.equal(r, "cat scares dog (dog and cat tales)"); + }); + it.skip("motivation - ES6 replace version", function() { // explains the motivaton // this is how you would write it in pure ES