left_pad.js (2790B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var isNonNegativeInteger = require( '@stdlib/assert/is-nonnegative-integer' ).isPrimitive; 24 var isString = require( '@stdlib/assert/is-string' ).isPrimitive; 25 var repeat = require( './../../repeat' ); 26 var ceil = require( '@stdlib/math/base/special/ceil' ); 27 var format = require( './../../format' ); 28 var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants/float64/max-safe-integer' ); 29 30 31 // MAIN // 32 33 /** 34 * Left pads a string such that the padded string has a length of at least `len`. 35 * 36 * @param {string} str - string to pad 37 * @param {NonNegativeInteger} len - minimum string length 38 * @param {string} [pad=' '] - string used to pad 39 * @throws {TypeError} first argument must be a string 40 * @throws {TypeError} second argument must be a nonnegative integer 41 * @throws {TypeError} third argument must be a string 42 * @throws {RangeError} padding must have a length greater than `0` 43 * @returns {string} padded string 44 * 45 * @example 46 * var str = lpad( 'a', 5 ); 47 * // returns ' a' 48 * 49 * @example 50 * var str = lpad( 'beep', 10, 'b' ); 51 * // returns 'bbbbbbbeep' 52 * 53 * @example 54 * var str = lpad( 'boop', 12, 'beep' ); 55 * // returns 'beepbeepboop' 56 */ 57 function lpad( str, len, pad ) { 58 var n; 59 var p; 60 if ( !isString( str ) ) { 61 throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); 62 } 63 if ( !isNonNegativeInteger( len ) ) { 64 throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', len ) ); 65 } 66 if ( arguments.length > 2 ) { 67 p = pad; 68 if ( !isString( p ) ) { 69 throw new TypeError( format( 'invalid argument. Third argument must be a string. Value: `%s`.', p ) ); 70 } 71 if ( p.length === 0 ) { 72 throw new RangeError( 'invalid argument. Third argument must not be an empty string.' ); 73 } 74 } else { 75 p = ' '; 76 } 77 if ( len > FLOAT64_MAX_SAFE_INTEGER ) { 78 throw new RangeError( format( 'invalid argument. Output string length exceeds maximum allowed string length. Value: `%u`.', len ) ); 79 } 80 n = ( len - str.length ) / p.length; 81 if ( n <= 0 ) { 82 return str; 83 } 84 n = ceil( n ); 85 return repeat( p, n ) + str; 86 } 87 88 89 // EXPORTS // 90 91 module.exports = lpad;