[EDIT] FILE: string.prototype.repeat.tar
README.md 0000755 00000003223 15075211071 0006025 0 ustar 00 # ES6 `String.prototype.repeat` polyfill [](https://travis-ci.org/mathiasbynens/String.prototype.repeat) A robust & optimized polyfill for [the `String.prototype.repeat` method in ECMAScript 6](http://ecma-international.org/ecma-262/6.0/#sec-string.prototype.repeat). This package implements the [es-shim API](https://github.com/es-shims/api) interface. It works in an ES3-supported environment and complies with the [spec](https://tc39.es/ecma262/#sec-string.prototype.repeat). Other polyfills for `String.prototype.repeat` are available: * <https://github.com/paulmillr/es6-shim/blob/d8c4ec246a15e7df55da60b7f9b745af84ca9021/es6-shim.js#L146-L154> by [Paul Miller](http://paulmillr.com/) (~~[fails 8 tests](https://github.com/paulmillr/es6-shim/issues/164)~~ now passes all tests) ## Installation Via [npm](https://www.npmjs.com/): ```bash npm install string.prototype.repeat ``` Then, in [Node.js](https://nodejs.org/): ```js var repeat = require('string.prototype.repeat'); ``` In a browser: ```html <script src="https://bundle.run/string.prototype.repeat"></script> ``` > **NOTE**: It's recommended that you install this module using a package manager > such as `npm`, because loading multiple polyfills from a CDN (such as `bundle.run`) > will lead to duplicated code. ## Author | [](https://twitter.com/mathias "Follow @mathias on Twitter") | |---| | [Mathias Bynens](https://mathiasbynens.be/) | ## License This polyfill is available under the [MIT](https://mths.be/mit) license. tests/shimmed.js 0000755 00000001617 15075211071 0007701 0 ustar 00 'use strict'; var repeat = require('../'); repeat.shim(); var test = require('tape'); var defineProperties = require('define-properties'); var bind = require('function-bind'); var isEnumerable = Object.prototype.propertyIsEnumerable; var functionsHaveNames = require('functions-have-names')(); var runTests = require('./tests'); test('shimmed', function (t) { t.equal(String.prototype.repeat.length, 1, 'String#repeat has a length of 1'); t.test('Function name', { skip: !functionsHaveNames }, function (st) { st.equal(String.prototype.repeat.name, 'repeat', 'String#repeat has name "repeat"'); st.end(); }); t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (st) { st.equal(false, isEnumerable.call(String.prototype, 'repeat'), 'String#repeat is not enumerable'); st.end(); }); runTests(bind.call(Function.call, String.prototype.repeat), t); t.end(); }); tests/tests.js 0000755 00000002566 15075211071 0007421 0 ustar 00 'use strict'; module.exports = function(repeat, t) { t.test('cast count argument', function(st) { st.equal(repeat('abc'), ''); st.equal(repeat('abc', undefined), ''); st.equal(repeat('abc', null), ''); st.equal(repeat('abc', false), ''); st.equal(repeat('abc', NaN), ''); st.equal(repeat('abc', 'abc'), ''); st.end(); }); t.test('invalid numeric count', function(st) { st['throws'](function() { repeat('abc', -Infinity); }, RangeError); st['throws'](function() { repeat('abc', -1); }, RangeError); st['throws'](function() { repeat('abc', +Infinity); }, RangeError); st.end(); }); t.test('valid numeric count', function(st) { st.equal(repeat('abc', -0), ''); st.equal(repeat('abc', +0), ''); st.equal(repeat('abc', 1), 'abc'); st.equal(repeat('abc', 2), 'abcabc'); st.equal(repeat('abc', 3), 'abcabcabc'); st.equal(repeat('abc', 4), 'abcabcabcabc'); st.end(); }); t.test('nullish this object', function(st) { st['throws'](function() { repeat(undefined); }, TypeError); st['throws'](function() { repeat(undefined, 4); }, TypeError); st['throws'](function() { repeat(null); }, TypeError); st['throws'](function() { repeat(null, 4); }, TypeError); st.end(); }); t.test('cast this object', function(st) { st.equal(repeat(42, 4), '42424242'); st.equal(repeat({ 'toString': function() { return 'abc'; } }, 2), 'abcabc'); st.end(); }); }; tests/index.js 0000755 00000000267 15075211071 0007362 0 ustar 00 'use strict'; var repeat = require('../'); var test = require('tape'); var runTests = require('./tests'); test('as a function', function (t) { runTests(repeat, t); t.end(); }); implementation.js 0000755 00000001323 15075211071 0010130 0 ustar 00 /*! https://mths.be/repeat v1.0.0 by @mathias */ 'use strict'; var RequireObjectCoercible = require('es-abstract/2019/RequireObjectCoercible'); var ToString = require('es-abstract/2019/ToString'); var ToInteger = require('es-abstract/2019/ToInteger'); module.exports = function repeat(count) { var O = RequireObjectCoercible(this); var string = ToString(O); var n = ToInteger(count); // Account for out-of-bounds indices if (n < 0 || n == Infinity) { throw RangeError('String.prototype.repeat argument must be greater than or equal to 0 and not be Infinity'); } var result = ''; while (n) { if (n % 2 == 1) { result += string; } if (n > 1) { string += string; } n >>= 1; } return result; }; shim.js 0000755 00000000530 15075211071 0006042 0 ustar 00 /*! https://mths.be/repeat v1.0.0 by @mathias */ 'use strict'; var define = require('define-properties'); var getPolyfill = require('./polyfill'); module.exports = function shimRepeat() { var polyfill = getPolyfill(); if (String.prototype.repeat !== polyfill) { define(String.prototype, { repeat: polyfill }); } return polyfill; }; polyfill.js 0000755 00000000324 15075211071 0006735 0 ustar 00 /*! https://mths.be/repeat v1.0.0 by @mathias */ 'use strict'; var implementation = require('./implementation'); module.exports = function getPolyfill() { return String.prototype.repeat || implementation; }; auto.js 0000755 00000000107 15075211071 0006052 0 ustar 00 /*! https://mths.be/repeat v1.0.0 by @mathias */ require('./shim')(); .editorconfig 0000755 00000000121 15075211071 0007215 0 ustar 00 root = true [*] indent_style = tab end_of_line = lf insert_final_newline = true package.json 0000755 00000002365 15075211071 0007042 0 ustar 00 { "name": "string.prototype.repeat", "version": "1.0.0", "description": "A robust & optimized `String.prototype.repeat` polyfill, based on the ECMAScript 6 specification.", "homepage": "https://mths.be/repeat", "main": "index.js", "exports": { ".": "./index.js", "./auto": "./auto.js", "./shim": "./shim.js", "./getPolyfill": "./getPolyfill.js", "./implementation": "./implementation.js", "./package.json": "./package.json" }, "keywords": [ "string", "repeat", "es6", "ecmascript", "polyfill" ], "license": "MIT", "author": { "name": "Mathias Bynens", "url": "https://mathiasbynens.be/" }, "repository": { "type": "git", "url": "https://github.com/mathiasbynens/String.prototype.repeat.git" }, "bugs": "https://github.com/mathiasbynens/String.prototype.repeat/issues", "scripts": { "pretest": "es-shim-api --bound", "test": "npm run tests-only", "tests-only": "tape 'tests/*.js'", "cover": "istanbul cover --report html --verbose --dir coverage tape 'tests/*.js'" }, "dependencies": { "define-properties": "^1.1.3", "es-abstract": "^1.17.5" }, "devDependencies": { "@es-shims/api": "^2.1.2", "function-bind": "^1.1.1", "functions-have-names": "^1.2.1", "istanbul": "^0.4.5", "tape": "^5.0.0" } } LICENSE-MIT.txt 0000755 00000002065 15075211071 0007023 0 ustar 00 Copyright Mathias Bynens <https://mathiasbynens.be/> Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. .travis.yml 0000755 00000000175 15075211071 0006662 0 ustar 00 version: ~> 1.0 language: node_js os: - linux import: - ljharb/travis-ci:node/all.yml - ljharb/travis-ci:node/pretest.yml .gitattributes 0000755 00000000114 15075211071 0007435 0 ustar 00 # Automatically normalize line endings for all text-based files * text=auto index.js 0000755 00000000714 15075211071 0006215 0 ustar 00 /*! https://mths.be/repeat v1.0.0 by @mathias */ 'use strict'; var callBind = require('es-abstract/helpers/callBind'); var define = require('define-properties'); var implementation = require('./implementation'); var getPolyfill = require('./polyfill'); var shim = require('./shim'); var boundRepeat = callBind(getPolyfill()); define(boundRepeat, { getPolyfill: getPolyfill, implementation: implementation, shim: shim }); module.exports = boundRepeat;
SAVE
CANCEL