Skip to content

Commit

Permalink
[breaking] Implement the es-shim-api interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 20, 2020
1 parent b2ee9b1 commit 1b4f196
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 55 deletions.
3 changes: 3 additions & 0 deletions auto.js
@@ -0,0 +1,3 @@
/*! https://mths.be/repeat v1.0.0 by @mathias */

require('./shim')();
27 changes: 27 additions & 0 deletions implementation.js
@@ -0,0 +1,27 @@
/*! 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 S = ToString(O);
var n = ToInteger(count);
// Account for out-of-bounds indices
if (n < 0 || n == Infinity) throw RangeError();

var result = '';
while (n) {
if (n % 2 == 1) {
result += string;
}
if (n > 1) {
string += string;
}
n >>= 1;
}
return result;
};
21 changes: 21 additions & 0 deletions index.js
@@ -0,0 +1,21 @@
/*! 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;

18 changes: 13 additions & 5 deletions package.json
Expand Up @@ -3,7 +3,15 @@
"version": "0.2.0",
"description": "A robust & optimized `String.prototype.repeat` polyfill, based on the ECMAScript 6 specification.",
"homepage": "https://mths.be/repeat",
"main": "repeat.js",
"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",
Expand All @@ -21,12 +29,12 @@
"url": "https://github.com/mathiasbynens/String.prototype.repeat.git"
},
"bugs": "https://github.com/mathiasbynens/String.prototype.repeat/issues",
"files": [
"LICENSE-MIT.txt",
"repeat.js"
],
"scripts": {
"test": "node tests/tests.js",
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
},
"dependencies": {
"define-properties": "^1.1.3",
"es-abstract": "^1.17.5"
}
}
9 changes: 9 additions & 0 deletions polyfill.js
@@ -0,0 +1,9 @@
/*! https://mths.be/repeat v1.0.0 by @mathias */

'use strict';

var implementation = require('./implementation');

module.exports = function getPolyfill() {
return String.prototype.repeat || implementation;
};
50 changes: 0 additions & 50 deletions repeat.js

This file was deleted.

17 changes: 17 additions & 0 deletions shim.js
@@ -0,0 +1,17 @@
/*! 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;
};

0 comments on commit 1b4f196

Please sign in to comment.