bifocal/node_modules/@stdlib/string-base-format-interpolate/lib/space_pad.js

66 lines
1.4 KiB
JavaScript

/**
* @license Apache-2.0
*
* Copyright (c) 2022 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
// FUNCTIONS //
/**
* Returns `n` spaces.
*
* @private
* @param {number} n - number of spaces
* @returns {string} string of spaces
*/
function spaces( n ) {
var out = '';
var i;
for ( i = 0; i < n; i++ ) {
out += ' ';
}
return out;
}
// MAIN //
/**
* Pads a token with spaces to the specified width.
*
* @private
* @param {string} str - token argument
* @param {number} width - token width
* @param {boolean} [right=false] - boolean indicating whether to pad to the right
* @returns {string} padded token argument
*/
function spacePad( str, width, right ) {
var pad = width - str.length;
if ( pad < 0 ) {
return str;
}
str = ( right ) ?
str + spaces( pad ) :
spaces( pad ) + str;
return str;
}
// EXPORTS //
module.exports = spacePad;