Is there some better/official way, how to pare 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
Is there some better/official way, how to pare 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
Share
Improve this question
asked Feb 21, 2013 at 12:39
jcjrjcjr
1,50325 silver badges40 bronze badges
3 Answers
Reset to default 3There is no official way to pare 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().
var rgx = /[\{\-\}]/g;
function _guidsAreEqual(left, right) {
var txtLeft = left.replace(rgx, '').toUpperCase();
var txtRight = right.replace(rgx, '').toUpperCase();
return txtLeft === txtRight;
};
You can use the node-uuid (https://github./broofa/node-uuid) library and do a byte parison after parsing the strings into bytes. The bytes are returned as arrays and can be pared 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:
pareGuids: (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):
pareGuids: function(guid1, guid2) {
var bytes1, bytes2, difference;
bytes1 = uuid.parse(guid1);
bytes2 = uuid.parse(guid2);
difference = _.difference(bytes1, bytes2);
return difference.length === 0;
}