diff options
| author | (quasar) nebula <qznebula@protonmail.com> | 2026-01-26 16:10:31 -0400 |
|---|---|---|
| committer | (quasar) nebula <qznebula@protonmail.com> | 2026-01-26 16:10:31 -0400 |
| commit | 88e30b86922c3fe013d25dcfe0177961f1f11c77 (patch) | |
| tree | a0bb4de3b00e66a710257e6b40c604f782375303 /src/data/thing.js | |
| parent | a074fd54107c51c4fcbfedbbf6df6eca539d19d3 (diff) | |
data: Thing.clone(source, {as}) and related utilities
Diffstat (limited to 'src/data/thing.js')
| -rw-r--r-- | src/data/thing.js | 30 |
1 files changed, 29 insertions, 1 deletions
diff --git a/src/data/thing.js b/src/data/thing.js index cc7c82b7..0a6e3be4 100644 --- a/src/data/thing.js +++ b/src/data/thing.js @@ -81,9 +81,37 @@ export default class Thing extends CacheableObject { (reference ? ` (${reference})` : '')); } + static clone(source, {as = null} = {}) { + if (!(source instanceof this)) { + throw new TypeError( + `Passed thing is ${source.constructor.name}, ` + + `which is not a subclass of ${this.name}`); + } + + if (as && !(as.prototype instanceof this)) { + throw new TypeError( + `Passed constructor is ${as.name}, ` + + `which is not a subclass of ${this.name}`); + } + + let clone; + + if (as) { + clone = Reflect.construct(as, []); + } else { + clone = Reflect.construct(source.constructor, []); + } + + CacheableObject.copyUpdateValuesOnto(source, clone); + + return clone; + } + static getReference(thing) { if (!thing.constructor[Thing.referenceType]) { - throw TypeError(`Passed Thing is ${thing.constructor.name}, which provides no [Thing.referenceType]`); + throw TypeError( + `Passed Thing is ${thing.constructor.name}, ` + + `which provides no [Thing.referenceType]`); } if (!thing.directory) { |