Created
March 26, 2023 22:51
-
-
Save goldsail/37a48b6ee4fb2bf363dac0012fa0263b to your computer and use it in GitHub Desktop.
IonStruct object does not match instanceof dom.Value when used inside JSON stringify replacer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { load, dom } = require('ion-js'); | |
const ionStruct = load(`$ion_1_0 | |
{ | |
foo:"bar" | |
}`); | |
const ionList = load(`$ion_1_0 | |
[ | |
"hello", | |
"world" | |
]`); | |
console.log(ionStruct instanceof dom.Value); // returns true | |
console.log(ionStruct['foo'] instanceof dom.Value); // returns true | |
console.log(ionList instanceof dom.Value); // returns true | |
console.log(ionList[0] instanceof dom.Value); // returns true | |
console.log(ionList[1] instanceof dom.Value); // returns true | |
// So far, it all looks good. | |
const replacer = (key, value) => { | |
if (value instanceof dom.Value) { | |
console.log('instance of dom.Value:', value); | |
} else { | |
console.log('not instance of dom.Value:', value, 'Actual type:', typeof value); | |
} | |
return value; | |
} | |
const jsonObject = { | |
ionStruct: ionStruct, | |
ionList: ionList, | |
} | |
JSON.stringify(jsonObject, replacer, 2); | |
// Output: | |
` | |
true | |
true | |
true | |
true | |
true | |
not instance of dom.Value: { | |
ionStruct: Struct { _fields: [Object: null prototype] { foo: [Array] } }, | |
ionList: List(2) [ [String: 'hello'], [String: 'world'] ] | |
} Actual type: object | |
not instance of dom.Value: [Object: null prototype] { foo: [String: 'bar'] } Actual type: object | |
instance of dom.Value: [String: 'bar'] | |
instance of dom.Value: List(2) [ [String: 'hello'], [String: 'world'] ] | |
instance of dom.Value: [String: 'hello'] | |
instance of dom.Value: [String: 'world'] | |
` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 50 expected (because line 41 returns true):
Line 50 actual: