0.2.1 • Published 2 months ago

@stdlib/string-base-right-pad v0.2.1

Weekly downloads
-
License
Apache-2.0
Repository
github
Last release
2 months ago

rpad

NPM version Build Status Coverage Status

Right pad a string.

Installation

npm install @stdlib/string-base-right-pad

Usage

var rpad = require( '@stdlib/string-base-right-pad' );

rpad( str, len, pad )

Right pads a string such that the padded string has a length of at least len.

var str = rpad( 'a', 5, ' ' );
// returns 'a    '

str = rpad( 'beep', 10, 'b' );
// returns 'beepbbbbbb'

str = rpad( 'boop', 12, 'beep' );
// returns 'boopbeepbeep'

Notes

  • An output string is not guaranteed to have a length of exactly len, but to have a length of at least len. To generate a padded string having a length equal to len

    var str = rpad( 'boop', 10, 'beep' ); // => length 12
    // returns 'boopbeepbeep'
    
    str = str.substring( 0, 10 ); // => length 10
    // returns 'boopbeepbe'
    
    str = str.substring( str.length-10 ); // => length 10
    // returns 'opbeepbeep'

Examples

var discreteUniform = require( '@stdlib/random-array-discrete-uniform' );
var papply = require( '@stdlib/utils-papply' );
var papplyRight = require( '@stdlib/utils-papply-right' );
var naryFunction = require( '@stdlib/utils-nary-function' );
var map = require( '@stdlib/utils-map' );
var logEach = require( '@stdlib/console-log-each' );
var rpad = require( '@stdlib/string-base-right-pad' );

// Define a string to pad:
var str = 'beep';

// Generate random lengths:
var lens = discreteUniform( 10, str.length, str.length+10 );

// Create a function for creating padded strings:
var fcn = naryFunction( papply( papplyRight( rpad, 'b' ), str ), 1 );

// Generate padded strings:
var out = map( lens, fcn );

// Print results:
logEach( '%s', out );

Notice

This package is part of stdlib, a standard library for JavaScript and Node.js, with an emphasis on numerical and scientific computing. The library provides a collection of robust, high performance libraries for mathematics, statistics, streams, utilities, and more.

For more information on the project, filing bug reports and feature requests, and guidance on how to develop stdlib, see the main project repository.

Community

Chat


License

See LICENSE.

Copyright

Copyright © 2016-2024. The Stdlib Authors.