Reputation: 1503
Is there some better/official way, how to compare CRM 2011 GUIDs in JavaScript
2e9565c4-fc5b-e211-993c-000c29208ee5=={2E9565C4-FC5B-E211-993C-000C29208EE5}
without using .replace()
and .toLowerCase()
?
First one is got thru XMLHttpRequest/JSON:
JSON.parse(r.responseText).d.results[0].id
Second one is got from form:
Xrm.Page.getAttribute("field").getValue()[0].id
Upvotes: 2
Views: 4786
Reputation: 716
var rgx = /[\{\-\}]/g;
function _guidsAreEqual(left, right) {
var txtLeft = left.replace(rgx, '').toUpperCase();
var txtRight = right.replace(rgx, '').toUpperCase();
return txtLeft === txtRight;
};
Upvotes: 2
Reputation: 2668
You can use the node-uuid (https://github.com/broofa/node-uuid) library and do a byte comparison after parsing the strings into bytes. The bytes are returned as arrays and can be compared using the lodash _.difference method. This will handle cases where the GUID's aren't using the same case or if they don't have '-' dashes.
Coffeescript:
compareGuids: (guid1, guid2) ->
bytes1 = uuid.parse(guid1)
bytes2 = uuid.parse(guid2)
# difference returns [] for equal arrays
difference = _.difference(bytes1, bytes2)
return difference.length == 0
Javascript (update):
compareGuids: function(guid1, guid2) {
var bytes1, bytes2, difference;
bytes1 = uuid.parse(guid1);
bytes2 = uuid.parse(guid2);
difference = _.difference(bytes1, bytes2);
return difference.length === 0;
}
Upvotes: 1
Reputation: 30671
There is no official way to compare GUIDs in JavaScript because there is no primitive GUID type. Thus you should treat GUIDs as strings.
If you must not use replace()
and toLowerCase()
you can use a regular expression:
// "i" is for ignore case
var regExp = new RegExp("2e9565c4-fc5b-e211-993c-000c29208ee5", "i");
alert(regExp.test("{2E9565C4-FC5B-E211-993C-000C29208EE5}"));
It would probably be slower than replace/toLowerCase().
Upvotes: 3