Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

A simple hierarchic resolver #2

Closed
wants to merge 1 commit into from

2 participants

@is
is commented

support to {obj.attr} format key. code is refrenced from string-format

https://github.com/davidchambers/string-format/blob/master/string-format.js

@fhellwig
Owner

I will require documentation sufficient for inclusion into the README.md file before accepting this pull request. Specifically: 1) features and benefits, 2) sample usage, and 3) API documentation. Thank you.

@is
is commented

feature and benefits, and sample usage.

strformat only support replace value in single level object.
When I want read value from multi objects like this:
strformat("home path is: {env.home}, cwd: {info.cwd}", {env:process.env, info:info});
current strformat don't resolve the value as I expected.

API documentation

API is not changed.

@fhellwig
Owner

The strformat package now handles nested properties (as well as having additional features for multiple objects).

The "Advanced Topics" section I the API offers additional details. (Adding new features should always be documented in the README file.)

The code in the pull request was not used - see the source code for how I implemented these features.

Regarding the example above: with the 0.0.3 version, the 'env' and 'info' objects could be passed individually, obviating the need for a single replacement object.

@fhellwig fhellwig closed this
@fhellwig
Owner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 6, 2012
  1. @is

    add a simple hierarchic resolver

    is authored
This page is out of date. Refresh to see the latest.
Showing with 38 additions and 2 deletions.
  1. +30 −2 strformat.js
  2. +8 −0 test/test.js
View
32 strformat.js
@@ -22,7 +22,7 @@
// Matches '{{', '}}', and '{<token>}' where <token> is one or more
// word characters (letter, number, or underscore).
-var RE = /\{\{|\}\}|\{(\w+)\}/g;
+var RE = /\{\{|\}\}|\{([a-zA-Z0-9_.]+)\}/g;
/**
* Formats the specified string by replacing placeholders in the string with
@@ -49,6 +49,32 @@ function strformat(str, args) {
if (typeof args !== 'object') {
args = Array.prototype.slice.call(arguments, 1);
}
+
+ var lookup, resolve;
+ lookup = function(object, key) {
+ var match;
+ //if (!/^(\d+)([.]|$)/.test(key)) {
+ // key = '0.' + key;
+ //}
+ while (match = /(.+?)[.](.+)/.exec(key)) {
+ object = resolve(object, match[1]);
+ key = match[2];
+ }
+ return resolve(object, key);
+ };
+
+ resolve = function(object, key) {
+ console.log(object);
+ console.log(key);
+ var value;
+ value = object[key];
+ if (typeof value === 'function') {
+ return value.call(object);
+ } else {
+ return value;
+ }
+ };
+
return str.replace(RE, function (m, n) {
if (m == '{{') {
return '{';
@@ -56,9 +82,11 @@ function strformat(str, args) {
if (m == '}}') {
return '}';
}
- var val = args[n];
+ var val = lookup(args, n);
return (typeof val === 'undefined') ? m : val;
});
+
+
}
module.exports = strformat;
View
8 test/test.js
@@ -42,5 +42,13 @@ vows.describe('strformat').addBatch({
"Then the result should be the original text with the replaced indexes in the array": function(res) {
assert.strictEqual(res, 'hello world');
}
+ },
+ "When I perform strformat on a text and the arguments are hierarchic key": {
+ topic: function() {
+ return strformat('hello {hello.world}', {hello:{ world: 'world'}});
+ },
+ "Then the result should be the original text with the replaced texts": function (res) {
+ assert.strictEqual(res, 'hello world');
+ }
}
}).run();
Something went wrong with that request. Please try again.