Added a string param substitution method in aria.utils.String #41

Merged
merged 2 commits into from Jul 10, 2012

Projects

None yet

5 participants

@captainbrosset
Contributor

Simply moving the string token substitution method from aria.core.Log to aria.utils.String so that it can be used in other use cases.
I think this method is quite handy and should be in string utils.

Now, sorry, but by lack of time, there's no jsunit tests attached to this pull request (anyway, unit tests are not in the GH repo yet ...). But hopefully this pull request can be used by someone in the core team to finalize the development with unit tests ...

Cheers

@piuccio piuccio and 1 other commented on an outdated diff Jul 5, 2012
src/aria/utils/String.js
@@ -22,6 +22,22 @@ Aria.classDefinition({
$dependencies : ["aria.utils.Type", "aria.utils.Json"],
$constructor : function () {},
$prototype : {
+ /**
+ * Substitute %n parameters in a string
+ * @param {String} string The source string to substitue %n occurences in
+ * @param {Array} params The parameters to use for the substitution. Index 0 will replace %1, index 1, %2 and so on
+ * @return {String} The final string, with %n occurences replaced with their equivalent
+ */
+ substitute : function(string, params) {
@piuccio
piuccio Jul 5, 2012 Contributor

prepareLoggedMessage used to support both string and array for msgArgs.
And we have a couple of places where we use a string instead of an array.

Either we change other classes or we allow strings in prepareLoggedMessage

@captainbrosset
captainbrosset Jul 5, 2012 Contributor

You're right, I overlooked this, ad42b70 solves this now

@captainbrosset captainbrosset commented on the diff Jul 5, 2012
src/aria/utils/String.js
* chunk('abcdefg', [1, 2], true);
* ['ab', 'c', 'defg']
- *
+ *
@captainbrosset
captainbrosset Jul 5, 2012 Contributor

And yes, sorry for these white space removal ... Sublime Text 2 is doing this by default. I would tend to say that there should never be white spaces after the last character of a line

@jakub-g
jakub-g Jul 6, 2012 Collaborator

Yep I totally agree. We should probably update our formatter and remove all the trailing whitespace.

@piuccio piuccio merged commit 8a01952 into ariatemplates:master Jul 10, 2012
@sfidanza sfidanza commented on the diff Jul 30, 2012
src/aria/utils/String.js
@@ -22,6 +22,24 @@ Aria.classDefinition({
$dependencies : ["aria.utils.Type", "aria.utils.Json"],
$constructor : function () {},
$prototype : {
+ /**
+ * Substitute %n parameters in a string
+ * @param {String} string The source string to substitue %n occurences in
+ * @param {Array|String} params The parameters to use for the substitution. Index 0 will replace %1, index 1, %2 and so on. If a string is passed, only %1 will be replaced
+ * @return {String} The final string, with %n occurences replaced with their equivalent
+ */
+ substitute : function(string, params) {
+ if (!aria.utils.Type.isArray(params)) {
+ params = [params];
+ }
+
+ string = string.replace(/%[0-9]+/g, function (token) {
+ var replacement = params[parseInt(token.substring(1), 10) - 1];
+ return typeof replacement !== "undefined" ? replacement : token;
@sfidanza
sfidanza Jul 30, 2012

why use typeof? replacement is defined in the line above. This is equivalent:
return replacement !== undefined ? replacement : token;

@bbaliguet
bbaliguet Jul 31, 2012

Depends on your browser support policy :
Open IE<8:

console.log((function(somethingUndefined){ return undefined === (somethingUndefined)})()); // true: ok everything seems fine
undefined = "toto"; // let's assume someone did that by mistake ...
console.log((function(somethingUndefined){ return undefined === (somethingUndefined)})()) // false: ... WTF?
@piuccio
piuccio Jul 31, 2012 Contributor

I guess because you can assign any value to undefined if you're not in strict mode

@sfidanza sfidanza commented on the diff Jul 30, 2012
src/aria/utils/String.js
@@ -22,6 +22,24 @@ Aria.classDefinition({
$dependencies : ["aria.utils.Type", "aria.utils.Json"],
$constructor : function () {},
$prototype : {
+ /**
+ * Substitute %n parameters in a string
+ * @param {String} string The source string to substitue %n occurences in
+ * @param {Array|String} params The parameters to use for the substitution. Index 0 will replace %1, index 1, %2 and so on. If a string is passed, only %1 will be replaced
+ * @return {String} The final string, with %n occurences replaced with their equivalent
+ */
+ substitute : function(string, params) {
+ if (!aria.utils.Type.isArray(params)) {
+ params = [params];
+ }
+
+ string = string.replace(/%[0-9]+/g, function (token) {
+ var replacement = params[parseInt(token.substring(1), 10) - 1];
@sfidanza
sfidanza Jul 30, 2012

Why use parseInt? It seems rather fragile. Regexp are made to simplify this use case already:

string.replace(/%([0-9]+)/g, function (token, index) {
  var replacement = params[index - 1];
  ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment