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-base-repeat

Package Overview
Dependencies
Maintainers
4
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stdlib/string-base-repeat - npm Package Compare versions

Comparing version
0.0.2
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 i=function(e,r){return function(){return r||e((r={exports:{}}).exports,r),r.exports}};var u=i(function(k,o){
var x=typeof String.prototype.repeat!="undefined";o.exports=x
});var s=i(function(m,n){
function q(e,r){var a,t;if(e.length===0||r===0)return"";for(a="",t=r;(t&1)===1&&(a+=e),t>>>=1,t!==0;)e+=e;return a}n.exports=q
});var c=i(function(A,f){
var y=String.prototype.repeat;f.exports=y
});var v=i(function(B,l){
var b=c();function g(e,r){return b.call(e,r)}l.exports=g
});var S=u(),d=s(),I=v(),p;S?p=I:p=d;module.exports=p;
/** @license Apache-2.0 */
//# sourceMappingURL=index.js.map
{
"version": 3,
"sources": ["../lib/has_builtin.js", "../lib/polyfill.js", "../lib/builtin.js", "../lib/main.js", "../lib/index.js"],
"sourcesContent": ["/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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// MAIN //\n\nvar bool = ( typeof String.prototype.repeat !== 'undefined' );\n\n\n// EXPORTS //\n\nmodule.exports = bool;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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// MAIN //\n\n/**\n* Repeats a string a specified number of times and returns the concatenated result.\n*\n* ## Method\n*\n* 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).\n*\n* For example,\n*\n* ```text\n* n = 10 => 1010 => 2^3 + 2^0 + 2^1 + 2^0\n* ```\n*\n* We can produce a 10-repeat string by \"adding\" the results of a 8-repeat string and a 2-repeat string.\n*\n* The implementation is then as follows:\n*\n* 1. Let `s` be the string to be repeated and `o` be an output string.\n*\n* 2. Initialize an output string `o`.\n*\n* 3. Check the least significant bit to determine if the current `s` string should be \"added\" to the output \"total\".\n*\n* - if the bit is a one, add\n* - otherwise, move on\n*\n* 4. Double the string `s` by adding `s` to `s`.\n*\n* 5. Right-shift the bits of `n`.\n*\n* 6. Check if we have shifted off all bits.\n*\n* - if yes, done.\n* - otherwise, move on\n*\n* 7. Repeat 3-6.\n*\n* 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\".\n*\n* The algorithm runs in `O(log_2(n))` compared to `O(n)`.\n*\n* @private\n* @param {string} str - string to repeat\n* @param {NonNegativeInteger} n - number of times to repeat the string\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\tvar rpt;\n\tvar cnt;\n\tif ( str.length === 0 || n === 0 ) {\n\t\treturn '';\n\t}\n\trpt = '';\n\tcnt = n;\n\tfor ( ; ; ) {\n\t\t// If the count is odd, append the current concatenated string:\n\t\tif ( (cnt&1) === 1 ) {\n\t\t\trpt += str;\n\t\t}\n\t\t// Right-shift the bits:\n\t\tcnt >>>= 1;\n\t\tif ( cnt === 0 ) {\n\t\t\tbreak;\n\t\t}\n\t\t// Double the string:\n\t\tstr += str;\n\t}\n\treturn rpt;\n}\n\n\n// EXPORTS //\n\nmodule.exports = repeat;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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// MAIN //\n\nvar repeat = String.prototype.repeat;\n\n\n// EXPORTS //\n\nmodule.exports = repeat;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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 builtin = require( './builtin.js' );\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* @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\treturn builtin.call( str, n );\n}\n\n\n// EXPORTS //\n\nmodule.exports = repeat;\n", "/**\n* @license Apache-2.0\n*\n* Copyright (c) 2022 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-base-repeat\n*\n* @example\n* var replace = require( '@stdlib/string-base-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 HAS_BUILTIN = require( './has_builtin.js' );\nvar polyfill = require( './polyfill.js' );\nvar main = require( './main.js' );\n\n\n// MAIN //\n\nvar repeat;\nif ( HAS_BUILTIN ) {\n\trepeat = main;\n} else {\n\trepeat = polyfill;\n}\n\n\n// EXPORTS //\n\nmodule.exports = repeat;\n"],
"mappings": "uGAAA,IAAAA,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,OAAO,OAAO,UAAU,QAAW,YAKhDD,EAAO,QAAUC,IC3BjB,IAAAC,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAgFA,SAASC,EAAQC,EAAKC,EAAI,CACzB,IAAIC,EACAC,EACJ,GAAKH,EAAI,SAAW,GAAKC,IAAM,EAC9B,MAAO,GAIR,IAFAC,EAAM,GACNC,EAAMF,GAGCE,EAAI,KAAO,IAChBD,GAAOF,GAGRG,KAAS,EACJA,IAAQ,GAIbH,GAAOA,EAER,OAAOE,CACR,CAKAJ,EAAO,QAAUC,IC3GjB,IAAAK,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAS,OAAO,UAAU,OAK9BD,EAAO,QAAUC,IC3BjB,IAAAC,EAAAC,EAAA,SAAAC,EAAAC,EAAA,cAsBA,IAAIC,EAAU,IAwBd,SAASC,EAAQC,EAAKC,EAAI,CACzB,OAAOH,EAAQ,KAAME,EAAKC,CAAE,CAC7B,CAKAJ,EAAO,QAAUE,ICbjB,IAAIG,EAAc,IACdC,EAAW,IACXC,EAAO,IAKPC,EACCH,EACJG,EAASD,EAETC,EAASF,EAMV,OAAO,QAAUE",
"names": ["require_has_builtin", "__commonJSMin", "exports", "module", "bool", "require_polyfill", "__commonJSMin", "exports", "module", "repeat", "str", "n", "rpt", "cnt", "require_builtin", "__commonJSMin", "exports", "module", "repeat", "require_main", "__commonJSMin", "exports", "module", "builtin", "repeat", "str", "n", "HAS_BUILTIN", "polyfill", "main", "repeat"]
}
+1
-1

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

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

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

@@ -64,3 +64,2 @@ /**

*
*
* @private

@@ -67,0 +66,0 @@ * @param {string} str - string to repeat

+1
-1

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

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

@@ -41,9 +41,9 @@ "license": "Apache-2.0",

"devDependencies": {
"@stdlib/assert-is-string": "^0.0.x",
"@stdlib/bench": "^0.0.x",
"@stdlib/random-base-discrete-uniform": "^0.0.x",
"@stdlib/assert-is-string": "^0.0.8",
"@stdlib/bench": "^0.0.12",
"@stdlib/random-base-discrete-uniform": "^0.0.6",
"proxyquire": "^2.0.0",
"tape": "git+https://github.com/kgryte/tape.git#fix/globby",
"istanbul": "^0.4.1",
"tap-spec": "5.x.x"
"tap-min": "git+https://github.com/Planeshifter/tap-min.git"
},

@@ -86,5 +86,5 @@ "engines": {

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

@@ -21,2 +21,13 @@ <!--

<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

@@ -119,3 +130,3 @@

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

@@ -133,4 +144,4 @@ </section>

[test-image]: https://github.com/stdlib-js/string-base-repeat/actions/workflows/test.yml/badge.svg?branch=v0.0.2
[test-url]: https://github.com/stdlib-js/string-base-repeat/actions/workflows/test.yml?query=branch:v0.0.2
[test-image]: https://github.com/stdlib-js/string-base-repeat/actions/workflows/test.yml/badge.svg?branch=v0.1.0
[test-url]: https://github.com/stdlib-js/string-base-repeat/actions/workflows/test.yml?query=branch:v0.1.0

@@ -148,3 +159,3 @@ [coverage-image]: https://img.shields.io/codecov/c/github/stdlib-js/string-base-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

@@ -151,0 +162,0 @@ [stdlib]: https://github.com/stdlib-js/stdlib

{{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) 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.
*/
import repeat = require( './index' );
// TESTS //
// The function returns a string...
{
repeat( 'a', 2 ); // $ExpectType string
}
// The compiler throws an error if the function is 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 compiler throws an error if the function is provided insufficient arguments...
{
repeat(); // $ExpectError
repeat( 3 ); // $ExpectError
repeat( 'abc' ); // $ExpectError
}