Make a deep copy of the given data set.

This commit is contained in:
Thomas Pelletier 2012-05-11 17:41:39 +01:00
parent d3d793919b
commit c66a723acc
4 changed files with 81 additions and 3 deletions

View File

@ -21,6 +21,10 @@ class Morris.Line
# bail if there's no data
if @options.data is undefined or @options.data.length is 0
return
else
# otherwise create a deep copy of the input data: we'll perform
# operations on it later so better not taint the user's data.
@options.data = clone(@options.data)
@el.addClass 'graph-initialised'
@precalc()
@redraw()
@ -514,6 +518,31 @@ Morris.labelSeries = (dmin, dmax, pxwidth, specName, xLabelFormat) ->
spec.incr(d)
return ret
# Perform a deep copy of the given object.
# Source: http://coffeescriptcookbook.com/chapters/classes_and_objects/cloning
clone = (obj) ->
if not obj? or typeof obj isnt 'object'
return obj
if obj instanceof Date
return new Date(obj.getTime())
if obj instanceof RegExp
flags = ''
flags += 'g' if obj.global?
flags += 'i' if obj.ignoreCase?
flags += 'm' if obj.multiline?
flags += 'y' if obj.sticky?
return new RegExp(obj.source, flags)
newInstance = new obj.constructor()
for key of obj
newInstance[key] = clone obj[key]
return newInstance
minutesSpecHelper = (interval) ->
span: interval * 60 * 1000
start: (d) -> new Date(d.getFullYear(), d.getMonth(), d.getDate(), d.getHours())

View File

@ -1,6 +1,6 @@
// Generated by CoffeeScript 1.3.1
(function() {
var $, Morris, minutesSpecHelper, secondsSpecHelper,
var $, Morris, clone, minutesSpecHelper, secondsSpecHelper,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$ = jQuery;
@ -35,6 +35,8 @@
}
if (this.options.data === void 0 || this.options.data.length === 0) {
return;
} else {
this.options.data = clone(this.options.data);
}
this.el.addClass('graph-initialised');
this.precalc();
@ -586,6 +588,37 @@
return ret;
};
clone = function(obj) {
var flags, key, newInstance;
if (!(obj != null) || typeof obj !== 'object') {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (obj instanceof RegExp) {
flags = '';
if (obj.global != null) {
flags += 'g';
}
if (obj.ignoreCase != null) {
flags += 'i';
}
if (obj.multiline != null) {
flags += 'm';
}
if (obj.sticky != null) {
flags += 'y';
}
return new RegExp(obj.source, flags);
}
newInstance = new obj.constructor();
for (key in obj) {
newInstance[key] = clone(obj[key]);
}
return newInstance;
};
minutesSpecHelper = function(interval) {
return {
span: interval * 60 * 1000,

2
morris.min.js vendored

File diff suppressed because one or more lines are too long

View File

@ -3,6 +3,7 @@
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/qunit/git/qunit.css" type="text/css" media="screen" />
<script type="text/javascript" src="http://code.jquery.com/qunit/git/qunit.js"></script>
<script type="text/javascript" src="http://github.com/DmitryBaranovskiy/raphael/raw/master/raphael-min.js"></script>
<script type="text/javascript" src="../morris.js"></script>
<script>
@ -10,6 +11,19 @@
module("Morris");
test("Input data remains untouched", function() {
var my_data = [{x: 1, y: 1}, {x: 2, y: 2}];
var expected_data = [{x: 1, y: 1}, {x: 2, y: 2}];
Morris.Line({
element: "graph-placeholder",
data: my_data,
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']
});
deepEqual(my_data, expected_data);
});
test("Morris.commas", function () {
// zero
equal(Morris.commas(0), "0", "commas(0) = 0")
@ -215,6 +229,8 @@
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture">test markup, will be hidden</div>
<div id="qunit-fixture">
<div id="graph-placeholder" style="width: 100px; height: 50px"></div>
</div>
</body>
</html>