Skip to content

Commit 024c7fa

Browse files
authored
Merge pull request #41 from Distributive-Network/slightly-improved-determining-js-classes
Detect es6 classes via toString starting with word 'class'
2 parents adfa25f + 4c737a2 commit 024c7fa

2 files changed

Lines changed: 4 additions & 2 deletions

File tree

dcp/js/utils.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def isclass(ref):
2626
# if a js object prototype has more than one own property, it is a class
2727
proto_own_prop_names = pm.eval(
2828
'x=>(x?.prototype ? Object.getOwnPropertyNames(x?.prototype) : [])')
29-
return len(proto_own_prop_names(ref)) > 1
29+
# @TODO: what if instead of coming up with a better way, I just made it more cursed?
30+
is_es6_class = pm.eval('x => /^class /.test(Function.prototype.toString.call(x))')
31+
return len(proto_own_prop_names(ref)) > 1 or is_es6_class(ref)
3032

3133

3234
def class_name(JSClass):

tests/test_api/test_wallet.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class TestWallet(unittest.TestCase):
88

99
def test_smoke_keystore_address(self):
1010
ks = dcp.wallet.get()
11-
self.assertTrue(isinstance(ks, dcp.wallet.Keystore))
11+
self.assertTrue(pm.eval("(x)=> x instanceof dcp.wallet.Keystore")(ks.js_ref))
1212

1313
address = ks.address
1414
self.assertTrue(isinstance(address, dcp.wallet.Address))

0 commit comments

Comments
 (0)