From: Morgan Harris Date: Wed, 24 Nov 2021 00:35:39 +0000 (+1100) Subject: Allow access to original Array.prototype methods on List prefixed with $ X-Git-Tag: 3.2.1~4^2~1 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e45843c71c73511ba0f499053184883ddc28f990;p=svg.js.git Allow access to original Array.prototype methods on List prefixed with $ --- diff --git a/spec/spec/types/List.js b/spec/spec/types/List.js index eb0b40b..9fd27db 100644 --- a/spec/spec/types/List.js +++ b/spec/spec/types/List.js @@ -71,6 +71,20 @@ describe('List.js', () => { delete List.prototype.fooBar }) + it('keeps Array prototype names prefixed with $', () => { + // We're picking a function that we know isn't part of core svg.js + // If we implement an 'unshift' function at some point, change this to something else + if (List.prototype.hasOwnProperty('unshift')) { + fail('List.unshift is already a function - change this test to use a different name!'); + return; + } + + List.extend([ 'unshift' ]) + expect(new List().unshift).toEqual(any(Function)) + expect(new List().$unshift).toEqual(Array.prototype.unshift) + delete List.prototype.unshift; + }); + it('skips reserved names', () => { const { constructor, each, toArray } = List.prototype List.extend([ 'constructor', 'each', 'toArray' ]) diff --git a/src/types/List.js b/src/types/List.js index 197a155..97536ed 100644 --- a/src/types/List.js +++ b/src/types/List.js @@ -47,6 +47,11 @@ List.extend = function (methods) { // Don't add private methods if (name[0] === '_') return obj + // Allow access to original Array methods through a prefix + if (name in Array.prototype) { + obj['$' + name] = Array.prototype[name] + } + // Relay every call to each() obj[name] = function (...attrs) { return this.each(name, ...attrs)