A deep copy is overkill here.

The only destructive change to the supplied data array is to change its
ordering, so a shallow copy is enough to avoid clobbering it.
This commit is contained in:
Olly Smith 2012-05-14 07:37:45 +01:00
parent 2108baaf4e
commit 942e121caa
4 changed files with 25 additions and 85 deletions

View File

@ -25,10 +25,6 @@ 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()
@ -79,7 +75,8 @@ class Morris.Line
# Do any necessary pre-processing for a new dataset
#
precalc: ->
# sort data
# shallow copy & sort data
@options.data = @options.data.slice(0)
@options.data.sort (a, b) => (a[@options.xkey] < b[@options.xkey]) - (b[@options.xkey] < a[@options.xkey])
# extract labels
@columnLabels = $.map @options.data, (d) => d[@options.xkey]
@ -522,31 +519,6 @@ 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, clone, minutesSpecHelper, secondsSpecHelper,
var $, Morris, minutesSpecHelper, secondsSpecHelper,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; };
$ = jQuery;
@ -38,8 +38,6 @@
}
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();
@ -85,6 +83,7 @@
Line.prototype.precalc = function() {
var d, series_data, touchHandler, ykey, ymax, ymin, _i, _j, _k, _len, _len1, _ref, _ref1, _ref2, _results,
_this = this;
this.options.data = this.options.data.slice(0);
this.options.data.sort(function(a, b) {
return (a[_this.options.xkey] < b[_this.options.xkey]) - (b[_this.options.xkey] < a[_this.options.xkey]);
});
@ -591,37 +590,6 @@
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

@ -12,28 +12,28 @@
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);
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("Nicer error when the placeholder was not found.", function () {
var my_data = [{x: 1, y: 1}, {x: 2, y: 2}];
raises(function() {
Morris.Line({
element: "thisplacedoesnotexist",
data: my_data,
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']})
}, "Graph placeholder not found.");
test("Raise an error when the placeholder element is not found.", function () {
var my_data = [{x: 1, y: 1}, {x: 2, y: 2}];
raises(function() {
Morris.Line({
element: "thisplacedoesnotexist",
data: my_data,
xkey: 'x',
ykeys: ['y'],
labels: ['dontcare']})
}, "Graph placeholder not found.");
});
test("Morris.commas", function () {