|
| 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 format = require( '@stdlib/string/format' ); |
| 26 | +var builtin = require( './builtin.js' ); |
| 27 | + |
| 28 | + |
| 29 | +// MAIN // |
| 30 | + |
| 31 | +/** |
| 32 | +* Repeats a string a specified number of times and returns the concatenated result. |
| 33 | +* |
| 34 | +* @param {string} str - string to repeat |
| 35 | +* @param {NonNegativeInteger} n - number of times to repeat the string |
| 36 | +* @throws {TypeError} first argument must be a string |
| 37 | +* @throws {TypeError} second argument must be a nonnegative integer |
| 38 | +* @throws {RangeError} output string length must not exceed maximum allowed string length |
| 39 | +* @returns {string} repeated string |
| 40 | +* |
| 41 | +* @example |
| 42 | +* var str = repeat( 'a', 5 ); |
| 43 | +* // returns 'aaaaa' |
| 44 | +* |
| 45 | +* @example |
| 46 | +* var str = repeat( '', 100 ); |
| 47 | +* // returns '' |
| 48 | +* |
| 49 | +* @example |
| 50 | +* var str = repeat( 'beep', 0 ); |
| 51 | +* // returns '' |
| 52 | +*/ |
| 53 | +function repeat( str, n ) { |
| 54 | + if ( !isString( str ) ) { |
| 55 | + throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) ); |
| 56 | + } |
| 57 | + if ( !isNonNegativeInteger( n ) ) { |
| 58 | + throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) ); |
| 59 | + } |
| 60 | + return builtin.call( str, n ); |
| 61 | +} |
| 62 | + |
| 63 | + |
| 64 | +// EXPORTS // |
| 65 | + |
| 66 | +module.exports = repeat; |
0 commit comments