Skip to content

Commit

Permalink
Merge pull request #7 from mathiasbynens/nr/es-shim-api
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed May 20, 2020
2 parents 91279cb + 0affed6 commit 917b56b
Show file tree
Hide file tree
Showing 15 changed files with 302 additions and 180 deletions.
6 changes: 6 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
root = true

[*]
indent_style = tab
end_of_line = lf
insert_final_newline = true
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ coverage

# Installed npm modules
node_modules
package-lock.json

# Folder view configuration files
.DS_Store
Expand Down
1 change: 1 addition & 0 deletions .npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock=false
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
version: ~> 1.0
language: node_js
node_js:
- "0.10"
os:
- linux
import:
- ljharb/travis-ci:node/all.yml
- ljharb/travis-ci:node/pretest.yml
24 changes: 13 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# ES6 `String.prototype.includes` polyfill [![Build status](https://travis-ci.org/mathiasbynens/String.prototype.includes.svg?branch=master)](https://travis-ci.org/mathiasbynens/String.prototype.includes)

A robust & optimized ES3-compatible polyfill for [the `String.prototype.includes` method (previously known as `String.prototype.contains`) in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.includes).
A robust & optimized polyfill for [the `String.prototype.includes` method (previously known as `String.prototype.contains`) in ECMAScript 6](http://people.mozilla.org/~jorendorff/es6-draft.html#sec-string.prototype.includes).

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.includes).

Other polyfills for `String.prototype.includes` are available:

Expand All @@ -9,12 +11,6 @@ Other polyfills for `String.prototype.includes` are available:

## Installation

In a browser:

```html
<script src="includes.js"></script>
```

Via [npm](http://npmjs.org/):

```bash
Expand All @@ -24,13 +20,19 @@ npm install string.prototype.includes
Then, in [Node.js](http://nodejs.org/):

```js
require('string.prototype.includes');
var includes = require('string.prototype.includes');
```

In a browser:

// On Windows and on Mac systems with default settings, case doesn’t matter,
// which allows you to do this instead:
require('String.prototype.includes');
```html
<script src="https://bundle.run/string.prototype.includes"></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.
## Notes

Polyfills + test suites for [`String.prototype.startsWith`](https://mths.be/startswith) and [`String.prototype.endsWith`](https://mths.be/endswith) are available, too.
Expand Down
3 changes: 3 additions & 0 deletions auto.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/*! https://mths.be/includes v2.0.0 by @mathias */

require('./shim')();
32 changes: 32 additions & 0 deletions implementation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*! https://mths.be/includes v2.0.0 by @mathias */

'use strict';

var callBound = require('es-abstract/helpers/callBound')
var RequireObjectCoercible = require('es-abstract/2019/RequireObjectCoercible');
var ToString = require('es-abstract/2019/ToString');
var ToInteger = require('es-abstract/2019/ToInteger');
var IsRegExp = require('es-abstract/2019/IsRegExp');

var min = Math.min;
var max = Math.max;
var indexOf = callBound('String.prototype.indexOf');

module.exports = function includes(searchString) {
var O = RequireObjectCoercible(this);
var S = ToString(O);
if (IsRegExp(searchString)) {
throw TypeError('Argument to String.prototype.includes cannot be a RegExp');
}
var searchStr = String(searchString);
var searchLength = searchStr.length;
var position = arguments.length > 1 ? arguments[1] : undefined;
var pos = ToInteger(position);
var len = S.length;
var start = min(max(pos, 0), len);
// Avoid the `indexOf` call if no match is possible
if (searchLength + start > len) {
return false;
}
return indexOf(S, searchStr, pos) != -1;
};
50 changes: 0 additions & 50 deletions includes.js

This file was deleted.

20 changes: 20 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*! https://mths.be/includes v2.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 boundIncludes = callBind(getPolyfill());

define(boundIncludes, {
getPolyfill: getPolyfill,
implementation: implementation,
shim: shim
});

module.exports = boundIncludes;
31 changes: 24 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@
"version": "1.0.0",
"description": "A robust & optimized `String.prototype.includes` polyfill, based on the ECMAScript 6 specification.",
"homepage": "https://mths.be/includes",
"main": "includes.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",
"includes",
Expand All @@ -21,12 +29,21 @@
"url": "https://github.com/mathiasbynens/String.prototype.includes.git"
},
"bugs": "https://github.com/mathiasbynens/String.prototype.includes/issues",
"files": [
"LICENSE-MIT.txt",
"includes.js"
],
"scripts": {
"test": "node tests/tests.js",
"cover": "istanbul cover --report html --verbose --dir coverage tests/tests.js"
"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"
}
}
9 changes: 9 additions & 0 deletions polyfill.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/*! https://mths.be/includes v2.0.0 by @mathias */

'use strict';

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

module.exports = function getPolyfill() {
return String.prototype.includes || implementation;
};
17 changes: 17 additions & 0 deletions shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*! https://mths.be/includes v2.0.0 by @mathias */

'use strict';

var define = require('define-properties');

var getPolyfill = require('./polyfill');

module.exports = function shimIncludes() {
var polyfill = getPolyfill();

if (String.prototype.includes !== polyfill) {
define(String.prototype, { includes: polyfill });
}

return polyfill;
};
12 changes: 12 additions & 0 deletions tests/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict';

var includes = require('../');
var test = require('tape');

var runTests = require('./tests');

test('as a function', function (t) {
runTests(includes, t);

t.end();
});
30 changes: 30 additions & 0 deletions tests/shimmed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
'use strict';

var includes = require('../');
includes.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.includes.length, 1, 'String#includes has a length of 1');

t.test('Function name', { skip: !functionsHaveNames }, function (st) {
st.equal(String.prototype.includes.name, 'includes', 'String#includes has name "includes"');
st.end();
});

t.test('enumerability', { skip: !defineProperties.supportsDescriptors }, function (st) {
st.equal(false, isEnumerable.call(String.prototype, 'includes'), 'String#includes is not enumerable');
st.end();
});

runTests(bind.call(Function.call, String.prototype.includes), t);

t.end();
});
Loading

0 comments on commit 917b56b

Please sign in to comment.