Work on copy of Array and Object in Sao.rpc.prepareObject

Replacing the data in-place is bad practice because it can introduce side
effect. The caller of Sao.rpc doesn't expect to have the parameters modified.

issue5608
review21021002
This commit is contained in:
Cédric Krier committed 2016-06-07 09:51:55 +02:00
1 parent a13196b80a
commit 07a9b6ee72
1 file changed
+6 -19
+6 -19
View File
@@ -165,11 +165,13 @@
Sao.rpc.prepareObject = function(value, index, parent) {
if (value instanceof Array) {
value = jQuery.extend([], value);
for (var i = 0, length = value.length; i < length; i++) {
Sao.rpc.prepareObject(value[i], i, value);
}
} else if ((typeof(value) != 'string') &&
(typeof(value) != 'number') &&
(typeof(value) != 'boolean') &&
(value !== null) &&
(value !== undefined)) {
if (value.isDate){
@@ -179,10 +181,6 @@
'month': value.month() + 1,
'day': value.date()
};
if (parent) {
parent[index] = value;
}
} else if (value.isDateTime) {
value = value.clone();
value = {
@@ -195,9 +193,6 @@
'second': value.utc().second(),
'microsecond': value.utc().millisecond() * 1000
};
if (parent) {
parent[index] = value;
}
} else if (value.isTime) {
value = {
'__class__': 'time',
@@ -206,25 +201,16 @@
'second': value.second(),
'microsecond': value.millisecond() * 1000
};
if (parent) {
parent[index] = value;
}
} else if (value.isTimeDelta) {
value = {
'__class__': 'timedelta',
'seconds': value.asSeconds()
};
if (parent) {
parent[index] = value;
}
} else if (value instanceof Sao.Decimal) {
value = {
'__class__': 'Decimal',
'decimal': value.toString()
};
if (parent) {
parent[index] = value;
}
} else if (value instanceof Uint8Array) {
var strings = [], chunksize = 0xffff;
// JavaScript Core has hard-coded argument limit of 65536
@@ -239,15 +225,16 @@
'__class__': 'bytes',
'base64': btoa(strings.join(''))
};
if (parent) {
parent[index] = value;
}
} else {
value = jQuery.extend({}, value);
for (var p in value) {
Sao.rpc.prepareObject(value[p], p, value);
}
}
}
if (parent) {
parent[index] = value;
}
return parent || value;
};