How to use the lpad function from underscore.string

Find comprehensive JavaScript underscore.string.lpad code examples handpicked from public code repositorys.

The underscore.string.lpad function is used to pad the beginning of a string with specified characters until it reaches a desired length.

46
47
48
49
50
51
52
53
54
55
56


const toUTF16 = function(number) {
    /* Converts a number to a utf16 binary string */
    if ((0x0 <= number && number <= 0xD7FF) || (0xE000 <= number && number <= 0xFFFF)) {
        // In these code point ranges we simply encode directly to 16 bits
        return s.lpad(number.toString('2'), 16, '0');
    } else if (0x10000 <= number) {
        // Else use the algorithm for converting to 32 bit surrogate pairs
        const shifted = number - 0x10000;
        const bits = s.lpad(shifted.toString('2'), 20, '0');
fork icon75
star icon325
watch icon0

+ 2 other calls in file

78
79
80
81
82
83
84
85
86
87
namepad = new Buffer(namepad);
var ip = localIP.split(".");
var ipHex = "";
ip.forEach(function(e) {
	tmp = parseInt(e).toString(16);
	ipHex = ipHex + _s.lpad(tmp, 2, "0");
});

switch(MessageHex.substr(MessageHex.length - 14, 2)) {
	case "01":
fork icon6
star icon13
watch icon0

How does underscore.string.lpad work?

underscore.string.lpad is a method in the Underscore.string library that adds a specified character to the beginning of a string until it reaches a specified length. The method takes three parameters: the original string, the desired length of the final string, and the character to add to the beginning of the string. If the original string is already equal to or longer than the desired length, the method returns the original string. If the character to add is not provided, it defaults to a space character.

979
980
981
982
983
984
985
986
987
988
// Aliases
s.strip     = s.trim;
s.lstrip    = s.ltrim;
s.rstrip    = s.rtrim;
s.center    = s.lrpad;
s.rjust     = s.lpad;
s.ljust     = s.rpad;
s.contains  = s.include;
s.q         = s.quote;
s.toBool    = s.toBoolean;
fork icon0
star icon6
watch icon0

154
155
156
157
158
159
160
161
162
163
164
165
166


    return '5' + paramStringList.join('');
}


function strPack(string, width) {
    return _str.lpad(string.length, width, '0') + string;
}


function prependCount(string) {
    return String.fromCharCode(string.length) + string;
fork icon2
star icon3
watch icon0

Ai Example

1
2
3
4
5
6
const _ = require("underscore.string");

const str = "hello";
const paddedStr = _.lpad(str, 10, "-");

console.log(paddedStr); // outputs "-----hello"

In this example, underscore.string is imported and assigned to the _ variable. The lpad function is called on a string 'hello' with a total length of 10 characters and padding character -. The resulting string is stored in the paddedStr variable and printed to the console using console.log(). The output will be "-----hello".

236
237
238
239
240
241
242
243
244
245
246
247
function isFrameComplete(buffer) {
    return buffer[0] !== NUL || buffer[1] !== NUL || _.contains(buffer, EOT);
}


function extractAndPad(string, idx) {
    return _str.lpad(string.charCodeAt(idx).toString(16), 4, '0');
}


function createResponse(responseMessage, errorMessage) {
    var response = {
fork icon1
star icon0
watch icon2

57
58
59
60
61
62
63
64
65
66
67
68
69


// Don't use this, it's really just a namespace object
function RpcSerializer() {}


function strPack(string) {
    return _str.lpad(string.length, 3, '0') + string;
}


function prependCount(string) {
    return String.fromCharCode(string.length) + string;
fork icon1
star icon0
watch icon2