Skip to content

Commit

Permalink
Merge pull request #9 from i11ume/master
Browse files Browse the repository at this point in the history
Fixed bug with replacing url in serialized data
  • Loading branch information
davidthou committed Jun 14, 2014
2 parents 3367a66 + 4958ceb commit 1af61cb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
8 changes: 5 additions & 3 deletions tasks/lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ exports.init = function (grunt) {

exports.replace_urls_in_serialized = function(search, replace, string) {
var length_delta = search.length - replace.length;
var searchRegExp = new RegExp(search, 'g');

// Replace for serialized data
var matches, length, delimiter, old_serialized_data, target_string, new_url;
var matches, length, delimiter, old_serialized_data, target_string, new_url, occurences;
var regexp = /s:(\d+):([\\]*['"])(.*?)\2;/g;

while (matches = regexp.exec(string)) {
Expand All @@ -111,12 +112,13 @@ exports.init = function (grunt) {

// If the string contains the url make the substitution
if (target_string.indexOf(search) !== -1) {
occurences = target_string.match(searchRegExp).length;
length = matches[1];
delimiter = matches[2];

// Replace the url
new_url = target_string.replace(search, replace);
length -= length_delta;
new_url = target_string.replace(searchRegExp, replace);
length -= length_delta * occurences;

// Compose the new serialized data
var new_serialized_data = 's:' + length + ':' + delimiter + new_url + delimiter + ';';
Expand Down
7 changes: 7 additions & 0 deletions test/deployments_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ module.exports = {
"Replace multiple urls into serialized data."
);

var string4 = '{s:19:"payment_success_url";s:74:"http://loremipsum/payment-successful/ and http://loremipsum/error-message/";s:16:"payment_fail_url";s:33:"http://loremipsum/payment-failed/";s:13:"currency_unit"}';
test.equal(
util.replace_urls_in_serialized(search, replace, string4),
'{s:19:"payment_success_url";s:90:"http://www.loremipsum.com/payment-successful/ and http://www.loremipsum.com/error-message/";s:16:"payment_fail_url";s:41:"http://www.loremipsum.com/payment-failed/";s:13:"currency_unit"}',
"Replace multiple urls in a single serialized object into serialized data."
);

test.done();
},

Expand Down

0 comments on commit 1af61cb

Please sign in to comment.