Skip to content

Instantly share code, notes, and snippets.

@simonneutert
Created June 7, 2021 14:58
Show Gist options
  • Save simonneutert/d1824b4885c1d4581d0ab416d483e222 to your computer and use it in GitHub Desktop.
Save simonneutert/d1824b4885c1d4581d0ab416d483e222 to your computer and use it in GitHub Desktop.
format objectGUID
// source: https://github.com/ldapjs/node-ldapjs/issues/297#issuecomment-137765214
const formatGUID = function (objectGUID) {
var data = Buffer.from(objectGUID, 'binary');
// GUID_FORMAT_D
var template = '{3}{2}{1}{0}-{5}{4}-{7}{6}-{8}{9}-{10}{11}{12}{13}{14}{15}';
// check each byte
for (var i = 0; i < data.length; i++) {
// get the current character from that byte
var dataStr = data[i].toString(16);
dataStr = data[i] >= 16 ? dataStr : '0' + dataStr;
// insert that character into the template
template = template.replace(new RegExp('\\{' + i + '\\}', 'g'), dataStr);
}
return template;
};
module.exports = formatGUID;
@simonneutert
Copy link
Author

i just extracted the code that @hildoer had written here ldapjs/node-ldapjs#297 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment