Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reformat supports full match now #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions bin/string-templ.js
Original file line number Diff line number Diff line change
@@ -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");
Expand All @@ -15,8 +16,9 @@ program
.option('-m, --regex-map <json-format-regex-map>', 'a map for matching pattern with key')
.option('-e, --throws <boolean>', '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);
Expand All @@ -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
Expand Down
10 changes: 9 additions & 1 deletion src/string-templating.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions test/reformat-tescase.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down