diff --git a/src/prebase.ts b/src/prebase.ts index 4c26ad87..5a0d61b1 100644 --- a/src/prebase.ts +++ b/src/prebase.ts @@ -5,6 +5,8 @@ * */ +import * as common from './common'; + declare interface ProtoM21ObjectConstructorInterface extends Function { className: string; } @@ -129,9 +131,8 @@ export class ProtoM21Object { memo = new WeakMap(); } - // TODO(msc): test if Arrays work? for (const key in this) { - // not that we ONLY copy the keys in Ret -- it's easier that way. + // note that we ONLY copy the keys in Ret -- it's easier that way. if ({}.hasOwnProperty.call(this, key) === false) { continue; } @@ -170,11 +171,18 @@ export class ProtoM21Object { clonedVersion = m21Obj.clone(deep, memo); } ret[key] = clonedVersion; + } else if ( + deep + && this[key] instanceof Array + ) { + ret[key] = Array.from(this[key] as any); } else { try { - // for deep this should be: - // music21.common.merge(ret[key], this[key]); - ret[key] = this[key]; + if (deep && typeof this[key] !== 'string') { + common.merge(ret[key], this[key] as any); + } else { + ret[key] = this[key]; + } } catch (e) { if (e instanceof TypeError) { console.log('typeError:', e, key); diff --git a/tests/moduleTests/stream.ts b/tests/moduleTests/stream.ts index 9531b2ce..3831a1d1 100644 --- a/tests/moduleTests/stream.ts +++ b/tests/moduleTests/stream.ts @@ -262,8 +262,12 @@ export default function tests() { test('music21.stream.Stream.repeatAppend', assert => { const a = new music21.stream.Stream(); const n = new music21.note.Note(); + n.groups.push('all-notes-get-this-group'); a.repeatAppend(n, 10); assert.equal(a.notes.length, 10); + a.notes.first().groups.push('first-note-only'); + assert.equal(a.notes.first().groups, ['all-notes-get-this-group', 'first-note-only']); + assert.equal(a.notes.last().groups, ['all-notes-get-this-group']); }); test('music21.stream.Stream.insert and offsets', assert => {