Skip to content

Commit

Permalink
Variables wrong value fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Junaid committed Mar 26, 2015
1 parent e3f01ba commit 1014333
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
14 changes: 10 additions & 4 deletions lib/app/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,16 @@ angular.module('sgApp', [
if (!str) {
return '';
}
angular.forEach(variables, function(variable) {
if (str.match(new RegExp('\\$' + variable.name + ';', 'g'))) {
str = str.replace(new RegExp('\\$' + variable.name, 'g'), variable.value);
}

var sortedVariables;
if (variables) {
sortedVariables = variables.slice().sort(function(a, b) {
return b.name.length - a.name.length;
});
}

angular.forEach(sortedVariables, function(variable) {
str = str.replace(new RegExp('\\$' + variable.name, 'g'), variable.value);
});
return str;
};
Expand Down
14 changes: 2 additions & 12 deletions test/angular/unit/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,12 @@ describe('sgApp module', function() {
});

it('should set variables correctly if the first vairbale name is seconds substring', function() {
var input = 'background: $bgColor; color: $bgColor;',
var input = 'background: $bgColor; color: $bgColor-light;',
variables = [
{name: 'bgColor', value: '#FF0000'},
{name: 'bgColor-light', value: '#00FF00'}
],
result = 'background: #FF0000; color: #FF0000;';
expect(setVariables(input, variables)).to.eql(result);
});

it('should set variables correctly if the second vairbale name has first vairbale name as substring', function() {
var input = 'background: $bgColor-light; color: $bgColor-light;',
variables = [
{name: 'bgColor', value: '#FF0000'},
{name: 'bgColor-light', value: '#00FF00'}
],
result = 'background: #00FF00; color: #00FF00;';
result = 'background: #FF0000; color: #00FF00;';
expect(setVariables(input, variables)).to.eql(result);
});

Expand Down

0 comments on commit 1014333

Please sign in to comment.