lib/format.js
function encodeName(name) {
    var littleEndian = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

    if (typeof name !== "string") throw new TypeError("name parameter is a required string");

    if (name.length > 13) throw new TypeError("A name can be up to 13 characters long");

    var bitstr = "";
    for (var i = 0; i <= 12; i++) {
        // process all 64 bits (even if name is short)
        var c = i < name.length ? charidx(name[i]) : 0;
        var bitlen = i < 12 ? 5 : 4;
        var bits = Number(c).toString(2);
        if (bits.length > bitlen) {
            throw new TypeError("Invalid name " + name);
        }
        bits = "0".repeat(bitlen - bits.length) + bits;
        bitstr += bits;
    }

    var value = Long.fromString(bitstr, true, 2);

    // convert to LITTLE_ENDIAN
    var leHex = "";
    var bytes = littleEndian ? value.toBytesLE() : value.toBytesBE();
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        for (var _iterator = (0, _getIterator3.default)(bytes), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var b = _step.value;

            var n = Number(b).toString(16);
            leHex += (n.length === 1 ? "0" : "") + n;
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

    var ulName = Long.fromString(leHex, true, 16).toString();

    // console.log('encodeName', name, value.toString(), ulName.toString(), JSON.stringify(bitstr.split(/(.....)/).slice(1)))
    return ulName.toString();
}
function encodeName(name) {
    var littleEndian = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;

    if (typeof name !== "string") throw new TypeError("name parameter is a required string");

    if (name.length > 13) throw new TypeError("A name can be up to 13 characters long");

    var bitstr = "";
    for (var i = 0; i <= 12; i++) {
        // process all 64 bits (even if name is short)
        var c = i < name.length ? charidx(name[i]) : 0;
        var bitlen = i < 12 ? 5 : 4;
        var bits = Number(c).toString(2);
        if (bits.length > bitlen) {
            throw new TypeError("Invalid name " + name);
        }
        bits = "0".repeat(bitlen - bits.length) + bits;
        bitstr += bits;
    }

    var value = Long.fromString(bitstr, true, 2);

    // convert to LITTLE_ENDIAN
    var leHex = "";
    var bytes = littleEndian ? value.toBytesLE() : value.toBytesBE();
    var _iteratorNormalCompletion = true;
    var _didIteratorError = false;
    var _iteratorError = undefined;

    try {
        for (var _iterator = (0, _getIterator3.default)(bytes), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
            var b = _step.value;

            var n = Number(b).toString(16);
            leHex += (n.length === 1 ? "0" : "") + n;
        }
    } catch (err) {
        _didIteratorError = true;
        _iteratorError = err;
    } finally {
        try {
            if (!_iteratorNormalCompletion && _iterator.return) {
                _iterator.return();
            }
        } finally {
            if (_didIteratorError) {
                throw _iteratorError;
            }
        }
    }

    var ulName = Long.fromString(leHex, true, 16).toString();

    // console.log('encodeName', name, value.toString(), ulName.toString(), JSON.stringify(bitstr.split(/(.....)/).slice(1)))
    return ulName.toString();
}
Original Name encode and decode logic is in github.com/eosio/eos  native.hpp 
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.