最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Two strings are not equal in node js, but look equal to me - Stack Overflow

programmeradmin7浏览0评论

I don't understand what's happening. I work in node js, here is my code snippet:

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(expectBody != body) {
    console.log("Response body != expected file");
    console.log("Response body: " + body);
    console.log("Expected body: " + expectBody);
}

And here is the output I am seeing:

Response body != expected file
Response body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
Expected body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

As far as I can see, the strings are identical, but node js thinks otherwise.

I took the printed strings and saved them into two files, then did a diff - got nothing!

Does this have something to do with the way the files are read?

As far as I understand, != is for non-strict parison, so should only check the actual text in the variables, right?

=== update: ===

Following your suggestions, I tried this:

if(JSON.stringify(expectBody) != JSON.stringify(body)) {
       console.log(" stringify, not equal!");
}

and

if(expectBody.toString() != body.toString()) {
    console.log(" to string, not equal!");
}

I'm still getting "stringify, not equal" and "to string, not equal" printed out :(

===== solved: ======

This is what worked for me in the end:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

Thanks everyone for helping!

I don't understand what's happening. I work in node js, here is my code snippet:

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(expectBody != body) {
    console.log("Response body != expected file");
    console.log("Response body: " + body);
    console.log("Expected body: " + expectBody);
}

And here is the output I am seeing:

Response body != expected file
Response body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}
Expected body: {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

As far as I can see, the strings are identical, but node js thinks otherwise.

I took the printed strings and saved them into two files, then did a diff - got nothing!

Does this have something to do with the way the files are read?

As far as I understand, != is for non-strict parison, so should only check the actual text in the variables, right?

=== update: ===

Following your suggestions, I tried this:

if(JSON.stringify(expectBody) != JSON.stringify(body)) {
       console.log(" stringify, not equal!");
}

and

if(expectBody.toString() != body.toString()) {
    console.log(" to string, not equal!");
}

I'm still getting "stringify, not equal" and "to string, not equal" printed out :(

===== solved: ======

This is what worked for me in the end:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

Thanks everyone for helping!

Share Improve this question edited Aug 30, 2018 at 12:12 Monday to Friday asked Aug 30, 2018 at 10:50 Monday to FridayMonday to Friday 2295 silver badges16 bronze badges 10
  • Compare the length of the strings. – Teemu Commented Aug 30, 2018 at 10:53
  • @Teemu I do this before this check with if(expectBody.size == body.size), and this returns true. – Monday to Friday Commented Aug 30, 2018 at 10:56
  • Then check the type of the "operands". – Teemu Commented Aug 30, 2018 at 10:58
  • stringify the both value before pare. – narayansharma91 Commented Aug 30, 2018 at 10:59
  • 1 That's true, but the size of a string is undefined. If the size isn't undefined you're prpobably dealing with objects and not strings. Try doing a .toString() on expectedBody in the parison. – Asgeirr Commented Aug 30, 2018 at 11:04
 |  Show 5 more ments

5 Answers 5

Reset to default 2

The reason for this behavior is that internally JavaScript has two different approaches for validating equality.

The latest ECMAScript standard defines seven data types:

Six data types that are primitives:

  • Boolean
  • Null
  • Undefined
  • Number
  • String
  • Symbol (new in ECMAScript 6)

and then there is a data type as

  • Object

Primitives are pared by their value, while objects are pared by their reference.

example 01

let a = { "one":"a"};
let b = { "one":"a"};
console.log(a!=b);//true

example 02

let a = { "one":"a"};
let b = a;
console.log(a!=b);//false

In your case you are paring Objects then you will have to

  • Convert object into primitive(string) and then pare

or

  • Write a separate deep object parison function(you could find lot of them with simple google search)

UPDATE - test script for your case

let a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};
let b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]};

console.log(a!=b); //true
console.log(JSON.stringify(b) != JSON.stringify(b)); //false



// Some other approach to make sure you are not fooled by your eye parison
var crypto = require('crypto');
console.log(crypto.createHash('md5').update(a).digest("hex"));
console.log(crypto.createHash('md5').update(b).digest("hex"));

i hope this will work :)

var a = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}

var b = {"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]}


// check your condition

if( JSON.stringify(a) !=  JSON.stringify(b)){
   console.log("Objects are different")
}else{
   console.log("objects are same")
}

What you can do is.

let obj1 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});
let obj2 = JSON.stringify({"version":1,"jobs":[{"asset_id":"asset_1","status":"queued","status_info":null}]});

and then pare them, obj1 != obj2

So like this

var body = Buffer.concat(responseBody).toString();
var expectBody = fs.readFileSync(expectfile);
if(JSON.stringify(expectBody) != JSON.stringify(body)) {
  console.log("Response body != expected file");
  console.log("Response body: " + body);
  console.log("Expected body: " + expectBody);
}

Note this will fail if the order of object is not same.

I met the same issue with 2 strings that are the same letter by letter after rendered to screen, but they are slightly different byte by byte.

By creating hash they are really different, so in test case I have to implement a customized stringEquals instead of using assert.equal.

// Buffer.from(stringA).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223ee2808b3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a

// Buffer.from(stringB).toString('hex')

3c703e57656972642d6f3a203c7370616e20636c6173733d226b61746578223e3c7370616e20636c6173733d226b617465782d6d6174686d6c223e3c6d61746820786d6c6e733d22687474703a2f2f7777772e77332e6f72672f313939382f4d6174682f4d6174684d4c223e3c73656d616e746963733e3c6d726f773e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2274727565223e3c6d726f773e3c6d6f2066656e63653d2274727565223e283c2f6d6f3e3c6d7461626c6520726f7773706163696e673d22302e31363030656d2220636f6c756d6e616c69676e3d2263656e7465722063656e7465722220636f6c756d6e73706163696e673d2231656d223e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d6e3e313c2f6d6e3e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c6d74723e3c6d74643e3c6d7374796c65207363726970746c6576656c3d22302220646973706c61797374796c653d2266616c7365223e3c6d69206d61746876617269616e743d226e6f726d616c223e243c2f6d693e3c2f6d7374796c653e3c2f6d74643e3c2f6d74723e3c2f6d7461626c653e3c6d6f2066656e63653d2274727565223e293c2f6d6f3e3c2f6d726f773e3c2f6d7374796c653e3c2f6d726f773e3c616e6e6f746174696f6e20656e636f64696e673d226170706c69636174696f6e2f782d746578223e5c646973706c61797374796c657b5c626567696e7b706d61747269787d205c242026616d703b20315c5c5c24205c656e647b706d61747269787d7d3c2f616e6e6f746174696f6e3e3c2f73656d616e746963733e3c2f6d6174683e3c2f7370616e3e3c7370616e20636c6173733d226b617465782d68746d6c2220617269612d68696464656e3d2274727565223e3c7370616e20636c6173733d2262617365223e3c7370616e20636c6173733d22737472757422207374796c653d226865696768743a322e3430303033656d3b766572746963616c2d616c69676e3a2d302e3935303033656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d696e6e6572223e3c7370616e20636c6173733d226d6f70656e2064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e283c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d7461626c65223e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d7420766c6973742d7432223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e207374796c653d22746f703a2d322e34303939393939393939393939393937656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e243c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d73223e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a302e39353030303030303030303030303034656d3b223e3c7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226172726179636f6c73657022207374796c653d2277696474683a302e35656d3b223e3c2f7370616e3e3c7370616e20636c6173733d22636f6c2d616c69676e2d63223e3c7370616e20636c6173733d22766c6973742d74223e3c7370616e20636c6173733d22766c6973742d72223e3c7370616e20636c6173733d22766c69737422207374796c653d226865696768743a312e3435656d3b223e3c7370616e207374796c653d22746f703a2d332e3631656d3b223e3c7370616e20636c6173733d2270737472757422207374796c653d226865696768743a33656d3b223e3c2f7370616e3e3c7370616e20636c6173733d226d6f7264223e3c7370616e20636c6173733d226d6f7264223e313c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c7370616e20636c6173733d226d636c6f73652064656c696d63656e74657222207374796c653d22746f703a30656d3b223e3c7370616e20636c6173733d2264656c696d73697a696e672073697a6533223e293c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e3c2f7370616e3e2e3c2f703e0a

This is what worked for me in the end:

var filecheck = require('./file_checks.js');
var expectjson = JSON.parse(expectBody);
var receivedjson = JSON.parse(body);
if(filecheck.jsonCompareSync(expectjson, receivedjson)) {
    // not equal
}

Thanks everyone for helping!

发布评论

评论列表(0)

  1. 暂无评论