UNPKG

@shi-corp/objectid-converter

Version:
30 lines (29 loc) 1.66 kB
import * as __typia_transform__isFormatUuid from "typia/lib/internal/_isFormatUuid"; import * as __typia_transform__assertGuard from "typia/lib/internal/_assertGuard"; import { assertGuardEquals } from 'typia'; import { parse } from 'uuid'; export function convertToSid(objectId) { (() => { const __is = (input, _exceptionable = true) => "string" === typeof input && __typia_transform__isFormatUuid._isFormatUuid(input); let _errorFactory; return (input, errorFactory) => { if (false === __is(input)) { _errorFactory = errorFactory; ((input, _path, _exceptionable = true) => "string" === typeof input && (__typia_transform__isFormatUuid._isFormatUuid(input) || __typia_transform__assertGuard._assertGuard(true, { method: "assertGuardEquals", path: _path + "", expected: "string & Format<\"uuid\">", value: input }, _errorFactory)) || __typia_transform__assertGuard._assertGuard(true, { method: "assertGuardEquals", path: _path + "", expected: "(string & Format<\"uuid\">)", value: input }, _errorFactory))(input, "$input", true); } }; })()(objectId); const bytes = [...parse(objectId)]; [bytes[4], bytes[5]] = [bytes[5], bytes[4]]; [bytes[6], bytes[7]] = [bytes[7], bytes[6]]; const dataView = new DataView(new Uint8Array(bytes).buffer); const sidComponents = Array.from({ 'length': 4 }, (_element, index) => dataView.getUint32(index * 4, index > 0)); const resultantSid = `S-1-12-1-${sidComponents.join('-')}`; return resultantSid; }