string.prototype.matchall

4.0.12last stable release 4 months ago
Complexity Score
Low
Open Issues
1
Dependent Projects
574
Weekly Downloadsglobal
24,597,665

License

  • MIT
    • Yesattribution
    • Permissivelinking
    • Permissivedistribution
    • Permissivemodification
    • Nopatent grant
    • Yesprivate use
    • Permissivesublicensing
    • Notrademark grant

Downloads

Readme

string.prototype.matchall

ES2020 spec-compliant shim for String.prototype.matchAll. Invoke its “shim” method to shim String.prototype.matchAll if it is unavailable or noncompliant.

This package implements the es-shim API interface. It works in an ES3-supported environment, and complies with the spec.

Most common usage:

const assert = require('assert');
const matchAll = require('string.prototype.matchall');

const str = 'aabc';
const nonRegexStr = 'ab';
const globalRegex = /[ac]/g;
const nonGlobalRegex = /[bc]/i;

// non-regex arguments are coerced into a global regex
assert.deepEqual(
    [...matchAll(str, nonRegexStr)],
    [...matchAll(str, new RegExp(nonRegexStr, 'g'))]
);

assert.deepEqual([...matchAll(str, globalRegex)], [
    Object.assign(['a'], { index: 0, input: str, groups: undefined }),
    Object.assign(['a'], { index: 1, input: str, groups: undefined }),
    Object.assign(['c'], { index: 3, input: str, groups: undefined }),
]);

assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw

matchAll.shim(); // will be a no-op if not needed

// non-regex arguments are coerced into a global regex
assert.deepEqual(
    [...str.matchAll(nonRegexStr)],
    [...str.matchAll(new RegExp(nonRegexStr, 'g'))]
);

assert.deepEqual([...str.matchAll(globalRegex)], [
    Object.assign(['a'], { index: 0, input: str, groups: undefined }),
    Object.assign(['a'], { index: 1, input: str, groups: undefined }),
    Object.assign(['c'], { index: 3, input: str, groups: undefined }),
]);

assert.throws(() => matchAll(str, nonGlobalRegex)); // non-global regexes throw

Tests

Simply clone the repo, npm install, and run npm test

Dependencies

CVE IssuesActive
0
Scorecards Score
4.40
Test Coverage
99.00%
Follows Semver
Yes
Github Stars
17
Dependenciestotal
32
DependenciesOutdated
3
DependenciesDeprecated
0
Threat Modelling
No
Repo Audits
No

Learn how to distribute string.prototype.matchall in your own private NPM registry

npm config set registry  https://npm.cloudsmith.com/owner/repo
Processing...
Done
npm install string.prototype.matchall
Processing...
Done

18 Releases

NPM on Cloudsmith

Getting started with NPM on Cloudsmith is fast and easy.