lib/urn.js
function(encoded) {
      if (encoded instanceof Array) {
        encoded = encoded.map(function(chunk) {return chunk.replace(/^0x/g, '')}).join('');
      }

      // Each symbol is 4 bits
      var result = [];
      var nextIndex = 0;
      while (nextIndex < encoded.length) {
        var schemaLength = parseInt(encoded.slice(nextIndex, nextIndex + 1 * 2), 16);
        var idLength = parseInt(encoded.slice(nextIndex + (1 + schemaLength) * 2, nextIndex + (1 + schemaLength + 2) * 2), 16);

        var schema = encoded.slice(nextIndex + 1 * 2, nextIndex + (1 + schemaLength) * 2);
        // Convert to string
        schema = hexToString(schema);
        var idHex = encoded.slice(nextIndex + (1 + schemaLength + 2) * 2, nextIndex + (1 + schemaLength + 2 + idLength) * 2);
        var id = schema.match(allowedProtocolsRegExp) ? idHex : hexToString(idHex);
        result.push(schema + ':' + id);

        // Get full cells
        var cellsPerId = Math.ceil((1 + schemaLength + 2 + idLength) / 32);
        nextIndex += cellsPerId * 32 * 2;
      }

      return result;
    }
function(encoded) {
      if (encoded instanceof Array) {
        encoded = encoded.map(function(chunk) {return chunk.replace(/^0x/g, '')}).join('');
      }

      // Each symbol is 4 bits
      var result = [];
      var nextIndex = 0;
      while (nextIndex < encoded.length) {
        var schemaLength = parseInt(encoded.slice(nextIndex, nextIndex + 1 * 2), 16);
        var idLength = parseInt(encoded.slice(nextIndex + (1 + schemaLength) * 2, nextIndex + (1 + schemaLength + 2) * 2), 16);

        var schema = encoded.slice(nextIndex + 1 * 2, nextIndex + (1 + schemaLength) * 2);
        // Convert to string
        schema = hexToString(schema);
        var idHex = encoded.slice(nextIndex + (1 + schemaLength + 2) * 2, nextIndex + (1 + schemaLength + 2 + idLength) * 2);
        var id = schema.match(allowedProtocolsRegExp) ? idHex : hexToString(idHex);
        result.push(schema + ':' + id);

        // Get full cells
        var cellsPerId = Math.ceil((1 + schemaLength + 2 + idLength) / 32);
        nextIndex += cellsPerId * 32 * 2;
      }

      return result;
    }
function ( encoded ) { if ( encoded instanceof Array ) { encoded = encoded . map ( function ( chunk ) { return chunk . replace ( / ^0x / g , '' ) } ) . join ( '' ) ; } // Each symbol is 4 bits var result = [ ] ; var nextIndex = 0 ; while ( nextIndex < encoded . length ) { var schemaLength = parseInt ( encoded . slice ( nextIndex , nextIndex + 1 * 2 ) , 16 ) ; var idLength = parseInt ( encoded . slice ( nextIndex + ( 1 + schemaLength ) * 2 , nextIndex + ( 1 + schemaLength + 2 ) * 2 ) , 16 ) ; var schema = encoded . slice ( nextIndex + 1 * 2 , nextIndex + ( 1 + schemaLength ) * 2 ) ; // Convert to string schema = hexToString ( schema ) ; var idHex = encoded . slice ( nextIndex + ( 1 + schemaLength + 2 ) * 2 , nextIndex + ( 1 + schemaLength + 2 + idLength ) * 2 ) ; var id = schema . match ( allowedProtocolsRegExp ) ? idHex : hexToString ( idHex ) ; result . push ( schema + ':' + id ) ; // Get full cells var cellsPerId = Math . ceil ( ( 1 + schemaLength + 2 + idLength ) / 32 ) ; nextIndex += cellsPerId * 32 * 2 ; } return result ; }
Accepts both chunks array and hex string
Accepts both chunks array and hex string