From 86833d97fb5e446d799d1d8240db7c3d4f995c7b Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 13 Apr 2022 08:58:10 -0400 Subject: [PATCH 1/4] Fix shallow copying by repeatAppend() --- src/prebase.ts | 10 +++++++--- tests/moduleTests/stream.ts | 4 ++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/prebase.ts b/src/prebase.ts index e6a4d28d..63115910 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; } @@ -155,9 +157,11 @@ export class ProtoM21Object { ret[key] = clonedVersion; } else { try { - // for deep this should be: - // music21.common.merge(ret[key], this[key]); - ret[key] = this[key]; + if (deep) { + 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 cf39db61..93538298 100644 --- a/tests/moduleTests/stream.ts +++ b/tests/moduleTests/stream.ts @@ -247,8 +247,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 => { From 9dababb5d06b0a344a6411401031e6d24bb8b565 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 25 May 2022 17:16:48 -0400 Subject: [PATCH 2/4] handle arrays --- src/prebase.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/prebase.ts b/src/prebase.ts index 63115910..d08fba70 100644 --- a/src/prebase.ts +++ b/src/prebase.ts @@ -118,9 +118,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; } @@ -155,6 +154,11 @@ 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 { if (deep) { From 4de96c22aeae36a1dce0834aad2635e399c8a172 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 25 May 2022 17:30:33 -0400 Subject: [PATCH 3/4] filter out strings --- src/prebase.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/prebase.ts b/src/prebase.ts index d08fba70..21db4b55 100644 --- a/src/prebase.ts +++ b/src/prebase.ts @@ -5,6 +5,7 @@ * */ +import { type } from 'jquery'; import * as common from './common'; declare interface ProtoM21ObjectConstructorInterface extends Function { @@ -161,7 +162,7 @@ export class ProtoM21Object { ret[key] = Array.from(this[key] as any); } else { try { - if (deep) { + if (deep && typeof this[key] !== 'string') { common.merge(ret[key], this[key] as any); } else { ret[key] = this[key]; From 29cff31b5222396808913054253174ea9766a004 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Wed, 25 May 2022 17:37:43 -0400 Subject: [PATCH 4/4] vs code "helping" too much --- src/prebase.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/prebase.ts b/src/prebase.ts index 21db4b55..28518b83 100644 --- a/src/prebase.ts +++ b/src/prebase.ts @@ -5,7 +5,6 @@ * */ -import { type } from 'jquery'; import * as common from './common'; declare interface ProtoM21ObjectConstructorInterface extends Function {