Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@stdlib/string-repeat

Package Overview
Dependencies
Maintainers
4
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stdlib/string-repeat - npm Package Compare versions

Comparing version
0.0.9
to
0.1.0
CITATION.cff

Sorry, the diff of this file is not supported yet

+3
/// <reference path="../docs/types/index.d.ts" />
import repeat from '../docs/types/index';
export = repeat;
"use strict";var n=function(r,e){return function(){return e||r((e={exports:{}}).exports,e),e.exports}};var a=n(function(p,t){
var u=require('@stdlib/assert-is-nonnegative-integer/dist').isPrimitive,s=require('@stdlib/assert-is-string/dist').isPrimitive,i=require('@stdlib/error-tools-fmtprodmsg/dist'),o=require('@stdlib/string-base-repeat/dist');function v(r,e){if(!s(r))throw new TypeError(i('1PH3F',r));if(!u(e))throw new TypeError(i('1PH3X',e));return o(r,e)}t.exports=v
});var g=a();module.exports=g;
/** @license Apache-2.0 */
//# sourceMappingURL=index.js.map
{
"version": 3,
"sources": ["../lib/main.js", "../lib/index.js"],
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n// MODULES //\n\nvar isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;\nvar isString = require( '@stdlib/assert-is-string' ).isPrimitive;\nvar format = require( '@stdlib/string-format' );\nvar base = require( '@stdlib/string-base-repeat' );\n\n\n// MAIN //\n\n/**\n* Repeats a string a specified number of times and returns the concatenated result.\n*\n* @param {string} str - string to repeat\n* @param {NonNegativeInteger} n - number of times to repeat the string\n* @throws {TypeError} first argument must be a string\n* @throws {TypeError} second argument must be a nonnegative integer\n* @throws {RangeError} output string length must not exceed maximum allowed string length\n* @returns {string} repeated string\n*\n* @example\n* var str = repeat( 'a', 5 );\n* // returns 'aaaaa'\n*\n* @example\n* var str = repeat( '', 100 );\n* // returns ''\n*\n* @example\n* var str = repeat( 'beep', 0 );\n* // returns ''\n*/\nfunction repeat( str, n ) {\n\tif ( !isString( str ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );\n\t}\n\tif ( !isNonNegativeInteger( n ) ) {\n\t\tthrow new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );\n\t}\n\treturn base( str, n );\n}\n\n\n// EXPORTS //\n\nmodule.exports = repeat;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2018 The Stdlib Authors.\n*\n* Licensed under the Apache License, Version 2.0 (the \"License\");\n* you may not use this file except in compliance with the License.\n* You may obtain a copy of the License at\n*\n* http://www.apache.org/licenses/LICENSE-2.0\n*\n* Unless required by applicable law or agreed to in writing, software\n* distributed under the License is distributed on an \"AS IS\" BASIS,\n* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n* See the License for the specific language governing permissions and\n* limitations under the License.\n*/\n\n'use strict';\n\n/**\n* Repeat a string a specified number of times and return the concatenated result.\n*\n* @module @stdlib/string-repeat\n*\n* @example\n* var replace = require( '@stdlib/string-repeat' );\n*\n* var str = repeat( 'a', 5 );\n* // returns 'aaaaa'\n*\n* str = repeat( '', 100 );\n* // returns ''\n*\n* str = repeat( 'beep', 0 );\n* // returns ''\n*/\n\n// MODULES //\n\nvar main = require( './main.js' );\n\n\n// EXPORTS //\n\nmodule.exports = main;\n"],
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAuB,QAAS,uCAAwC,EAAE,YAC1EC,EAAW,QAAS,0BAA2B,EAAE,YACjDC,EAAS,QAAS,uBAAwB,EAC1CC,EAAO,QAAS,4BAA6B,EA2BjD,SAASC,EAAQC,EAAKC,EAAI,CACzB,GAAK,CAACL,EAAUI,CAAI,EACnB,MAAM,IAAI,UAAWH,EAAQ,kEAAmEG,CAAI,CAAE,EAEvG,GAAK,CAACL,EAAsBM,CAAE,EAC7B,MAAM,IAAI,UAAWJ,EAAQ,gFAAiFI,CAAE,CAAE,EAEnH,OAAOH,EAAME,EAAKC,CAAE,CACrB,CAKAP,EAAO,QAAUK,ICzBjB,IAAIG,EAAO,IAKX,OAAO,QAAUA",
"names": ["require_main", "__commonJSMin", "exports", "module", "isNonNegativeInteger", "isString", "format", "base", "repeat", "str", "n", "main"]
}
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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';
// MODULES //
var isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
var format = require( '@stdlib/string-format' );
var base = require( '@stdlib/string-base-repeat' );
// MAIN //
/**
* Repeats a string a specified number of times and returns the concatenated result.
*
* @param {string} str - string to repeat
* @param {NonNegativeInteger} n - number of times to repeat the string
* @throws {TypeError} first argument must be a string
* @throws {TypeError} second argument must be a nonnegative integer
* @throws {RangeError} output string length must not exceed maximum allowed string length
* @returns {string} repeated string
*
* @example
* var str = repeat( 'a', 5 );
* // returns 'aaaaa'
*
* @example
* var str = repeat( '', 100 );
* // returns ''
*
* @example
* var str = repeat( 'beep', 0 );
* // returns ''
*/
function repeat( str, n ) {
if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
}
if ( !isNonNegativeInteger( n ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );
}
return base( str, n );
}
// EXPORTS //
module.exports = repeat;
+1
-1

@@ -19,3 +19,3 @@ /*

// TypeScript Version: 2.0
// TypeScript Version: 4.1

@@ -22,0 +22,0 @@ /**

@@ -41,3 +41,3 @@ /**

var repeat = require( './repeat.js' );
var main = require( './main.js' );

@@ -47,2 +47,2 @@

module.exports = repeat;
module.exports = main;
+1
-1

@@ -1,1 +0,1 @@

Copyright (c) 2016-2022 The Stdlib Authors.
Copyright (c) 2016-2023 The Stdlib Authors.
{
"name": "@stdlib/string-repeat",
"version": "0.0.9",
"version": "0.1.0",
"description": "Repeat a string a specified number of times and return the concatenated result.",

@@ -16,5 +16,2 @@ "license": "Apache-2.0",

],
"bin": {
"repstr": "./bin/cli"
},
"main": "./lib",

@@ -44,27 +41,12 @@ "directories": {

"dependencies": {
"@stdlib/assert-is-nonnegative-integer": "^0.0.x",
"@stdlib/assert-is-regexp-string": "^0.0.x",
"@stdlib/assert-is-string": "^0.0.x",
"@stdlib/cli-ctor": "^0.0.x",
"@stdlib/constants-float64-max-safe-integer": "^0.0.x",
"@stdlib/fs-read-file": "^0.0.x",
"@stdlib/process-read-stdin": "^0.0.x",
"@stdlib/regexp-eol": "^0.0.x",
"@stdlib/streams-node-stdin": "^0.0.x",
"@stdlib/string-format": "^0.0.x",
"@stdlib/utils-regexp-from-string": "^0.0.x"
"@stdlib/assert-is-nonnegative-integer": "^0.1.0",
"@stdlib/assert-is-string": "^0.1.0",
"@stdlib/string-base-repeat": "^0.1.0",
"@stdlib/string-format": "^0.1.0"
},
"devDependencies": {
"@stdlib/assert-is-browser": "^0.0.x",
"@stdlib/assert-is-windows": "^0.0.x",
"@stdlib/bench": "^0.0.x",
"@stdlib/math-base-special-round": "^0.0.x",
"@stdlib/process-exec-path": "^0.0.x",
"@stdlib/random-base-randu": "^0.0.x",
"@stdlib/string-from-code-point": "^0.0.x",
"@stdlib/string-replace": "^0.0.x",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
"proxyquire": "^2.0.0",
"istanbul": "^0.4.1",
"tap-spec": "5.x.x"
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
},

@@ -106,5 +88,5 @@ "engines": {

"funding": {
"type": "patreon",
"url": "https://www.patreon.com/athan"
"type": "opencollective",
"url": "https://opencollective.com/stdlib"
}
}
+26
-105

@@ -21,4 +21,15 @@ <!--

# Repeat
<details>
<summary>
About stdlib...
</summary>
<p>We believe in a future in which the web is a preferred environment for numerical computation. To help realize this future, we've built stdlib. stdlib is a standard library, with an emphasis on numerical and scientific computation, written in JavaScript (and C) for execution in browsers and in Node.js.</p>
<p>The library is fully decomposable, being architected in such a way that you can swap out and mix and match APIs and functionality to cater to your exact preferences and use cases.</p>
<p>When you use stdlib, you can be absolutely certain that you are using the most thorough, rigorous, well-written, studied, documented, tested, measured, and high-quality code out there.</p>
<p>To join us in bringing numerical computing to the web, get started by checking us out on <a href="https://github.com/stdlib-js/stdlib">GitHub</a>, and please consider <a href="https://opencollective.com/stdlib">financially supporting stdlib</a>. We greatly appreciate your continued support!</p>
</details>
# repeat
[![NPM version][npm-image]][npm-url] [![Build Status][test-image]][test-url] [![Coverage Status][coverage-image]][coverage-url] <!-- [![dependencies][dependencies-image]][dependencies-url] -->

@@ -72,13 +83,8 @@

```javascript
var round = require( '@stdlib/math-base-special-round' );
var randu = require( '@stdlib/random-base-randu' );
var discreteUniform = require( '@stdlib/random-base-discrete-uniform' );
var repeat = require( '@stdlib/string-repeat' );
var str = 'beep';
var n;
var i;
for ( i = 0; i < 100; i++ ) {
n = round( randu()*3.0 );
console.log( repeat( str, n ) );
console.log( repeat( 'beep', discreteUniform( 0, 3 ) ) );
}

@@ -91,94 +97,4 @@ ```

* * *
<section class="cli">
## CLI
<section class="installation">
## Installation
To use the module as a general utility, install the module globally
```bash
npm install -g @stdlib/string-repeat
```
</section>
<!-- CLI usage documentation. -->
<section class="usage">
### Usage
```text
Usage: repstr [options] [<string>] --n <repeats>
Options:
-h, --help Print this message.
-V, --version Print the package version.
--n repeats Number of repetitions.
```
</section>
<!-- /.usage -->
<!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
<section class="notes">
### Notes
- If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes.
```bash
# Not escaped...
$ echo -n $'beep\nboop' | repstr --n 3 --split /\r?\n/
# Escaped...
$ echo -n $'beep\nboop' | repstr --n 3 --split /\\r?\\n/
```
- The implementation ignores trailing delimiters.
</section>
<!-- /.notes -->
<section class="examples">
### Examples
```bash
$ repstr beep --n 5
beepbeepbeepbeepbeep
```
To use as a [standard stream][standard-streams],
```bash
$ echo -n $'ab' | repstr --n 3
ababab
```
By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option.
```bash
$ echo -n $'beep\tboop' | repstr --n 3 --split '\t'
beepbeepbeep
boopboopboop
```
</section>
<!-- /.examples -->
</section>
<!-- /.cli -->
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->

@@ -188,7 +104,6 @@

* * *
## See Also
- <span class="package-name">[`@stdlib/string/pad`][@stdlib/string/pad]</span><span class="delimiter">: </span><span class="description">pad a string.</span>
- <span class="package-name">[`@stdlib/string-repeat-cli`][@stdlib/string-repeat-cli]</span><span class="delimiter">: </span><span class="description">CLI package for use as a command-line utility.</span>
- <span class="package-name">[`@stdlib/string-pad`][@stdlib/string/pad]</span><span class="delimiter">: </span><span class="description">pad a string.</span>

@@ -225,3 +140,3 @@ </section>

Copyright &copy; 2016-2022. The Stdlib [Authors][stdlib-authors].
Copyright &copy; 2016-2023. The Stdlib [Authors][stdlib-authors].

@@ -236,7 +151,9 @@ </section>

[@stdlib/string-repeat-cli]: https://www.npmjs.com/package/@stdlib/string-repeat-cli
[npm-image]: http://img.shields.io/npm/v/@stdlib/string-repeat.svg
[npm-url]: https://npmjs.org/package/@stdlib/string-repeat
[test-image]: https://github.com/stdlib-js/string-repeat/actions/workflows/test.yml/badge.svg?branch=v0.0.9
[test-url]: https://github.com/stdlib-js/string-repeat/actions/workflows/test.yml?query=branch:v0.0.9
[test-image]: https://github.com/stdlib-js/string-repeat/actions/workflows/test.yml/badge.svg?branch=v0.1.0
[test-url]: https://github.com/stdlib-js/string-repeat/actions/workflows/test.yml?query=branch:v0.1.0

@@ -254,3 +171,3 @@ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-repeat/main.svg

[chat-image]: https://img.shields.io/gitter/room/stdlib-js/stdlib.svg
[chat-url]: https://gitter.im/stdlib-js/stdlib/
[chat-url]: https://app.gitter.im/#/room/#stdlib-js_stdlib:gitter.im

@@ -261,2 +178,6 @@ [stdlib]: https://github.com/stdlib-js/stdlib

[cli-section]: https://github.com/stdlib-js/string-repeat#cli
[cli-url]: https://github.com/stdlib-js/string-repeat/tree/cli
[@stdlib/string-repeat]: https://github.com/stdlib-js/string-repeat/tree/main
[umd]: https://github.com/umdjs/umd

@@ -263,0 +184,0 @@ [es-module]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules

Sorry, the diff of this file is not supported yet

{{alias}}( str, n )
Repeats a string `n` times and returns the concatenated result.
Parameters
----------
str: string
Input string.
n: integer
Number of repetitions.
Returns
-------
out: string
Repeated string.
Examples
--------
> var out = {{alias}}( 'a', 5 )
'aaaaa'
> out = {{alias}}( '', 100 )
''
> out = {{alias}}( 'beep', 0 )
''
See Also
--------
/*
* @license Apache-2.0
*
* Copyright (c) 2019 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.
*/
import repeat = require( './index' );
// TESTS //
// The function returns a string...
{
repeat( 'a', 2 ); // $ExpectType string
}
// The function does not compile if provided values other than a string and a number...
{
repeat( true, 3 ); // $ExpectError
repeat( false, 2 ); // $ExpectError
repeat( 3, 1 ); // $ExpectError
repeat( [], 1 ); // $ExpectError
repeat( {}, 2 ); // $ExpectError
repeat( ( x: number ): number => x, 2 ); // $ExpectError
repeat( 'a', true ); // $ExpectError
repeat( 'a', false ); // $ExpectError
repeat( 'a', '5' ); // $ExpectError
repeat( 'a', [] ); // $ExpectError
repeat( 'a', {} ); // $ExpectError
repeat( 'a', ( x: number ): number => x ); // $ExpectError
repeat( [], true ); // $ExpectError
repeat( {}, false ); // $ExpectError
repeat( false, '5' ); // $ExpectError
repeat( {}, [] ); // $ExpectError
repeat( '5', ( x: number ): number => x ); // $ExpectError
}
// The function does not compile if provided insufficient arguments...
{
repeat(); // $ExpectError
repeat( 3 ); // $ExpectError
repeat( 'abc' ); // $ExpectError
}
Usage: repstr [options] [<string>] --n <repeats>
Options:
-h, --help Print this message.
-V, --version Print the package version.
--n repeats Number of repetitions.
--split sep Delimiter for stdin data. Default: '/\\r?\\n/'.
{
"boolean": [
"help",
"version"
],
"string": [
"n",
"split"
],
"alias": {
"help": [
"h"
],
"version": [
"V"
]
}
}
/**
* @license Apache-2.0
*
* Copyright (c) 2018 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';
// MODULES //
var isNonNegativeInteger = require( '@stdlib/assert-is-nonnegative-integer' ).isPrimitive;
var isString = require( '@stdlib/assert-is-string' ).isPrimitive;
var FLOAT64_MAX_SAFE_INTEGER = require( '@stdlib/constants-float64-max-safe-integer' );
var format = require( '@stdlib/string-format' );
// MAIN //
/**
* Repeats a string a specified number of times and returns the concatenated result.
*
* ## Method
*
* The algorithmic trick used in the implementation is to treat string concatenation the same as binary addition (i.e., any natural number (nonnegative integer) can be expressed as a sum of powers of two).
*
* For example,
*
* ```text
* n = 10 => 1010 => 2^3 + 2^0 + 2^1 + 2^0
* ```
*
* We can produce a 10-repeat string by "adding" the results of a 8-repeat string and a 2-repeat string.
*
* The implementation is then as follows:
*
* 1. Let `s` be the string to be repeated and `o` be an output string.
*
* 2. Initialize an output string `o`.
*
* 3. Check the least significant bit to determine if the current `s` string should be "added" to the output "total".
*
* - if the bit is a one, add
* - otherwise, move on
*
* 4. Double the string `s` by adding `s` to `s`.
*
* 5. Right-shift the bits of `n`.
*
* 6. Check if we have shifted off all bits.
*
* - if yes, done.
* - otherwise, move on
*
* 7. Repeat 3-6.
*
* The result is that, as the string is repeated, we continually check to see if the doubled string is one which we want to add to our "total".
*
* The algorithm runs in `O(log_2(n))` compared to `O(n)`.
*
*
* @param {string} str - string to repeat
* @param {NonNegativeInteger} n - number of times to repeat the string
* @throws {TypeError} first argument must be a string
* @throws {TypeError} second argument must be a nonnegative integer
* @throws {RangeError} output string length must not exceed maximum allowed string length
* @returns {string} repeated string
*
* @example
* var str = repeat( 'a', 5 );
* // returns 'aaaaa'
*
* @example
* var str = repeat( '', 100 );
* // returns ''
*
* @example
* var str = repeat( 'beep', 0 );
* // returns ''
*/
function repeat( str, n ) {
var rpt;
var cnt;
if ( !isString( str ) ) {
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', str ) );
}
if ( !isNonNegativeInteger( n ) ) {
throw new TypeError( format( 'invalid argument. Second argument must be a nonnegative integer. Value: `%s`.', n ) );
}
if ( str.length === 0 || n === 0 ) {
return '';
}
// Check that output string will not exceed the maximum string length:
if ( str.length * n > FLOAT64_MAX_SAFE_INTEGER ) {
throw new RangeError( format( 'invalid argument. Output string length exceeds maximum allowed string length. Value: `%u`.', str.length * n ) );
}
rpt = '';
cnt = n;
for ( ; ; ) {
// If the count is odd, append the current concatenated string:
if ( (cnt&1) === 1 ) {
rpt += str;
}
// Right-shift the bits:
cnt >>>= 1;
if ( cnt === 0 ) {
break;
}
// Double the string:
str += str;
}
return rpt;
}
// EXPORTS //
module.exports = repeat;