Realm Functions magic behavior

It looks like a comparison is broken between Double and Long numbers in Realm functions. Moreover, it returns Long when calling valueOf() on the Int type.

Parameters:

exports(
  EJSON.parse('{"$numberDouble":"2"}'),
  EJSON.parse('{"$numberInt":"2"}'),
)

Code:

exports = async function(a, b) {
      console.log(a)
      console.log(a.valueOf())
      console.log(a.toString())
      console.log(b)
      console.log(b.valueOf())
      console.log(b.toString())
      console.log(a)
      console.log(a.valueOf())
      console.log(b)
      console.log(b.valueOf())
      console.log(a == b)
      console.log(a.valueOf() == b)
      console.log(a == b.valueOf())
      console.log(a.valueOf() == b.valueOf())
      console.log(a === b)
      console.log(a.valueOf() === b)
      console.log(a === b.valueOf())
      console.log(a.valueOf() === b.valueOf())
      console.log(typeof a)
      console.log(typeof b)
      console.log(typeof a.valueOf())
      console.log(typeof b.valueOf())
      console.log(Object.prototype.toString.call(a))
      console.log(Object.prototype.toString.call(b))
      console.log(Object.prototype.toString.call(a.valueOf()))
      console.log(Object.prototype.toString.call(b.valueOf()))
      console.log(EJSON.stringify(a))
      console.log(EJSON.stringify(b))
      console.log(EJSON.stringify(a.valueOf()))
      console.log(EJSON.stringify(b.valueOf()))
};

Output:

2
2
2
2
2
2
2
2
2
2
true
true
false
false
true
true
false
false
number
number
number
number
[object Number]
[object Number]
[object Number]
[object Number]
{"$numberDouble":"2"}
{"$numberInt":"2"}
{"$numberDouble":"2"}
{"$numberLong":"2"}

It would be good if someone has an explanation for this magic behavior

Hi @Anton_P, thanks for surfacing this issue! We filed a ticket internally to investigate the behavior, will provide an update once available.

1 Like

Hi @Laura_Zhukas1, thanks :+1:

@Laura_Zhukas1 I will just post all magic behaviors here.

context.http.get response status code is some weird number that JSON is unable to stringify if valueOf() is called

const response = await context.http.get({ url: 'https://www.mongodb.com/wtf' });
console.log(response.statusCode); // 404
console.log(JSON.stringify(response.statusCode)); // 404
console.log(EJSON.stringify(response.statusCode)); // {"$numberInt":"404"}
console.log(response.statusCode.valueOf()); // 404
console.log(JSON.stringify(response.statusCode.valueOf())); // undefined - WTF O_o
console.log(JSON.stringify({ statusCode: response.statusCode.valueOf() })); // {} - WTF O_o
console.log(EJSON.stringify(response.statusCode.valueOf())); // {"$numberLong":"404"}