src/format.js
function encodeName128(name) {
    if(typeof name !== 'string')
      throw new TypeError('name parameter is a required string');
  
    if(name.length > 21)
      throw new TypeError('A name can be up to 21 characters long');
  
    let bitstr = '';
    for(let i = 0; i < 21; i++) { // process all 64 bits (even if name is short)
      const c = i < name.length ? charidx128(name[i]) : 0;
      let bits = Number(c).toString(2);
      if (bits.length > 6) {
        throw new TypeError('Invalid name ' + name);
      }
      bits = '0'.repeat(6 - bits.length) + bits;
      bitstr = bits + bitstr;
    }

    let cutSize = 4;
    if (name.length <= 5) {
        bitstr += "00"
        cutSize = 4
    } else if (name.length <= 10) {
        bitstr += "01"
        cutSize = 8
    } else if (name.length <= 15) {
        bitstr += "10"
        cutSize = 12
    } else {
        bitstr += "11"
        cutSize = 16
    }

    let bn = new BN(bitstr, 2);
    // bn = bn.toTwos(128);
    return bn.toArrayLike(Buffer, 'le', 128 / 8 / 16 * cutSize);
}
function encodeName128(name) {
    if(typeof name !== 'string')
      throw new TypeError('name parameter is a required string');
  
    if(name.length > 21)
      throw new TypeError('A name can be up to 21 characters long');
  
    let bitstr = '';
    for(let i = 0; i < 21; i++) { // process all 64 bits (even if name is short)
      const c = i < name.length ? charidx128(name[i]) : 0;
      let bits = Number(c).toString(2);
      if (bits.length > 6) {
        throw new TypeError('Invalid name ' + name);
      }
      bits = '0'.repeat(6 - bits.length) + bits;
      bitstr = bits + bitstr;
    }

    let cutSize = 4;
    if (name.length <= 5) {
        bitstr += "00"
        cutSize = 4
    } else if (name.length <= 10) {
        bitstr += "01"
        cutSize = 8
    } else if (name.length <= 15) {
        bitstr += "10"
        cutSize = 12
    } else {
        bitstr += "11"
        cutSize = 16
    }

    let bn = new BN(bitstr, 2);
    // bn = bn.toTwos(128);
    return bn.toArrayLike(Buffer, 'le', 128 / 8 / 16 * cutSize);
}
Encode a name (a base32 string) to a number.

For performance reasons, the blockchain uses the numerical encoding of strings
for very common types like account names.

@see types.hpp string_to_name

@arg {string} name - A string to encode, up to 12 characters long.
@return {string<uint64>} - compressed string (from name arg).  A string is
always used because a number could exceed JavaScript's 52 bit limit.