Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

compose-regexp

Package Overview
Dependencies
Maintainers
1
Versions
42
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

compose-regexp - npm Package Compare versions

Comparing version 0.3.0 to 0.4.0

46

compose-regexp.es6.js

@@ -61,7 +61,12 @@ var empty = new RegExp('')

export function sequence() {
if (!arguments.length) return empty;
return new RegExp([].map.call(arguments, forSequence).join(''))
function _sequence() {
if (arguments.length === 0) return '';
if (arguments.length === 1) return normalize(arguments[0])
return [].map.call(arguments, forSequence).join('')
}
export function sequence () {
return new RegExp(_sequence.apply(null, arguments))
}
var validSuffix = sequence(

@@ -73,27 +78,20 @@ /^/,

),
/$/
/\??$/
)
var call = operator.call
var call = _suffix.call
var push = [].push
function operator(suffix) {
function _suffix(operator) {
if (arguments.length === 1) return empty
var res = call.apply(sequence, arguments).source
return new RegExp(isAtomic(res) ? res + suffix : '(?:' + res + ')' + suffix)
var res = call.apply(_sequence, arguments)
return new RegExp(isAtomic(res) ? res + operator : '(?:' + res + ')' + operator)
}
export function greedy(suffix) {
export function suffix(suffix) {
if (!validSuffix.test(suffix)) throw new Error("Invalid suffix '" + suffix+ "'.")
return (arguments.length === 1)
? operator.bind(null, suffix)
: operator.apply(null, arguments)
? _suffix.bind(null, suffix)
: _suffix.apply(null, arguments)
}
export function frugal(suffix) {
if (!validSuffix.test(suffix)) throw new Error("Invalid suffix '" + suffix+ "'.")
return (arguments.length === 1)
? operator.bind(null, suffix+'?')
: (arguments[0] = suffix + '?', operator.apply(null, arguments))
}
export function ref(n) {

@@ -105,3 +103,3 @@ return new RegExp('\\' + n)

if (!arguments.length) return empty;
return new RegExp('(?=' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(?=' + _sequence.apply(null, arguments) + ')')
}

@@ -111,11 +109,7 @@

if (!arguments.length) return empty;
return new RegExp('(?!' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(?!' + _sequence.apply(null, arguments) + ')')
}
export function flags(opts) {
var args = [].slice.call(arguments, 1)
var expr
if (!args.length) expr = '';
else expr = args.map(forSequence).join('')
return new RegExp(expr, opts)
return new RegExp(call.apply(_sequence, arguments), opts)
}

@@ -125,3 +119,3 @@

if (!arguments.length) return new RegExp('()');
return new RegExp('(' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(' + _sequence.apply(null, arguments) + ')')
}

@@ -67,7 +67,12 @@ (function (global, factory) {

function sequence() {
if (!arguments.length) return empty;
return new RegExp([].map.call(arguments, forSequence).join(''))
function _sequence() {
if (arguments.length === 0) return '';
if (arguments.length === 1) return normalize(arguments[0])
return [].map.call(arguments, forSequence).join('')
}
function sequence () {
return new RegExp(_sequence.apply(null, arguments))
}
var validSuffix = sequence(

@@ -79,26 +84,19 @@ /^/,

),
/$/
/\??$/
)
var call = operator.call
function operator(suffix) {
var call = _suffix.call
function _suffix(operator) {
if (arguments.length === 1) return empty
var res = call.apply(sequence, arguments).source
return new RegExp(isAtomic(res) ? res + suffix : '(?:' + res + ')' + suffix)
var res = call.apply(_sequence, arguments)
return new RegExp(isAtomic(res) ? res + operator : '(?:' + res + ')' + operator)
}
function greedy(suffix) {
function suffix(suffix) {
if (!validSuffix.test(suffix)) throw new Error("Invalid suffix '" + suffix+ "'.")
return (arguments.length === 1)
? operator.bind(null, suffix)
: operator.apply(null, arguments)
? _suffix.bind(null, suffix)
: _suffix.apply(null, arguments)
}
function frugal(suffix) {
if (!validSuffix.test(suffix)) throw new Error("Invalid suffix '" + suffix+ "'.")
return (arguments.length === 1)
? operator.bind(null, suffix+'?')
: (arguments[0] = suffix + '?', operator.apply(null, arguments))
}
function ref(n) {

@@ -110,3 +108,3 @@ return new RegExp('\\' + n)

if (!arguments.length) return empty;
return new RegExp('(?=' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(?=' + _sequence.apply(null, arguments) + ')')
}

@@ -116,11 +114,7 @@

if (!arguments.length) return empty;
return new RegExp('(?!' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(?!' + _sequence.apply(null, arguments) + ')')
}
function flags(opts) {
var args = [].slice.call(arguments, 1)
var expr
if (!args.length) expr = '';
else expr = args.map(forSequence).join('')
return new RegExp(expr, opts)
return new RegExp(call.apply(_sequence, arguments), opts)
}

@@ -130,3 +124,3 @@

if (!arguments.length) return new RegExp('()');
return new RegExp('(' + [].map.call(arguments, forSequence).join('') + ')')
return new RegExp('(' + _sequence.apply(null, arguments) + ')')
}

@@ -136,4 +130,3 @@

exports.sequence = sequence;
exports.greedy = greedy;
exports.frugal = frugal;
exports.suffix = suffix;
exports.ref = ref;

@@ -140,0 +133,0 @@ exports.lookAhead = lookAhead;

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

!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("compose-regexp",["exports"],e):e(n.composeRegexp={})}(this,function(n){"use strict";function e(n){return n instanceof RegExp?n.source:(n+"").replace(/[.?*+^$[\]\\(){}|-]/g,"\\$&")}function t(n){if(n.indexOf("|")===-1)return!0;var e,t=0,r=!1;for(w.lastIndex=0;e=w.exec(n);)if(null==e[1]&&(r||"("!==e[0]||t++,r||")"!==e[0]||t--,r||"["!==e[0]&&"[-"!==e[0]||(r=!0),r&&"]"===e&&(r=!1),0===t&&!r&&"|"===e[0]))return!1;return!0}function r(n){if("("!==n.charAt(0)||")"!==n.charAt(n.length-1))return!1;var e,t=0,r=!1;for(w.lastIndex=0;e=w.exec(n);)if(null==e[1]){if(r||"("!==e[0]||t++,!r&&")"===e[0]&&0===--t&&e.index!==n.length-1)return!1;r||"["!==e[0]&&"[-"!==e||(r=!0),r&&"]"===e[0]&&(r=!1)}else if(e.index===n.length-2)return!1;return!0}function u(n){return n=e(n),t(n)?n:"(?:"+n+")"}function l(n){return 1===n.length||/^\\[^]$|^\[(?:\\[^]|[^\]])*\]$/.test(n)||r(n)}function i(){return arguments.length?new RegExp([].map.call(arguments,e).join("|")):h}function o(){return arguments.length?new RegExp([].map.call(arguments,u).join("")):h}function f(n){if(1===arguments.length)return h;var e=R.apply(o,arguments).source;return new RegExp(l(e)?e+n:"(?:"+e+")"+n)}function c(n){if(!E.test(n))throw new Error("Invalid suffix '"+n+"'.");return 1===arguments.length?f.bind(null,n):f.apply(null,arguments)}function a(n){if(!E.test(n))throw new Error("Invalid suffix '"+n+"'.");return 1===arguments.length?f.bind(null,n+"?"):(arguments[0]=n+"?",f.apply(null,arguments))}function p(n){return new RegExp("\\"+n)}function g(){return arguments.length?new RegExp("(?="+[].map.call(arguments,u).join("")+")"):h}function s(){return arguments.length?new RegExp("(?!"+[].map.call(arguments,u).join("")+")"):h}function x(n){var e,t=[].slice.call(arguments,1);return e=t.length?t.map(u).join(""):"",new RegExp(e,n)}function d(){return arguments.length?new RegExp("("+[].map.call(arguments,u).join("")+")"):new RegExp("()")}var h=new RegExp(""),w=/(\\[^])|[-()|\[\]]|\[\-/g,E=o(/^/,i("+","*","?",/\{\s*\d+(?:\s*,\s*)?\d*\s*\}/),/$/),R=f.call;n.either=i,n.sequence=o,n.greedy=c,n.frugal=a,n.ref=p,n.lookAhead=g,n.avoid=s,n.flags=x,n.capture=d});
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define("compose-regexp",["exports"],e):e(n.composeRegexp={})}(this,function(n){"use strict";function e(n){return n instanceof RegExp?n.source:(n+"").replace(/[.?*+^$[\]\\(){}|-]/g,"\\$&")}function t(n){if(n.indexOf("|")===-1)return!0;var e,t=0,r=!1;for(w.lastIndex=0;e=w.exec(n);)if(null==e[1]&&(r||"("!==e[0]||t++,r||")"!==e[0]||t--,r||"["!==e[0]&&"[-"!==e[0]||(r=!0),r&&"]"===e&&(r=!1),0===t&&!r&&"|"===e[0]))return!1;return!0}function r(n){if("("!==n.charAt(0)||")"!==n.charAt(n.length-1))return!1;var e,t=0,r=!1;for(w.lastIndex=0;e=w.exec(n);)if(null==e[1]){if(r||"("!==e[0]||t++,!r&&")"===e[0]&&0===--t&&e.index!==n.length-1)return!1;r||"["!==e[0]&&"[-"!==e||(r=!0),r&&"]"===e[0]&&(r=!1)}else if(e.index===n.length-2)return!1;return!0}function u(n){return n=e(n),t(n)?n:"(?:"+n+")"}function l(n){return 1===n.length||/^\\[^]$|^\[(?:\\[^]|[^\]])*\]$/.test(n)||r(n)}function i(){return arguments.length?new RegExp([].map.call(arguments,e).join("|")):h}function f(){return 0===arguments.length?"":1===arguments.length?e(arguments[0]):[].map.call(arguments,u).join("")}function o(){return new RegExp(f.apply(null,arguments))}function p(n){if(1===arguments.length)return h;var e=R.apply(f,arguments);return new RegExp(l(e)?e+n:"(?:"+e+")"+n)}function c(n){if(!E.test(n))throw new Error("Invalid suffix '"+n+"'.");return 1===arguments.length?p.bind(null,n):p.apply(null,arguments)}function a(n){return new RegExp("\\"+n)}function g(){return arguments.length?new RegExp("(?="+f.apply(null,arguments)+")"):h}function x(){return arguments.length?new RegExp("(?!"+f.apply(null,arguments)+")"):h}function s(n){return new RegExp(R.apply(f,arguments),n)}function d(){return arguments.length?new RegExp("("+f.apply(null,arguments)+")"):new RegExp("()")}var h=new RegExp(""),w=/(\\[^])|[-()|\[\]]|\[\-/g,E=o(/^/,i("+","*","?",/\{\s*\d+(?:\s*,\s*)?\d*\s*\}/),/\??$/),R=p.call;n.either=i,n.sequence=o,n.suffix=c,n.ref=a,n.lookAhead=g,n.avoid=x,n.flags=s,n.capture=d});
{
"name": "compose-regexp",
"version": "0.3.0",
"version": "0.4.0",
"description": "A set of functions to build and compose complex regular expressions",

@@ -5,0 +5,0 @@ "main": "compose-regexp.js",

@@ -93,3 +93,3 @@

> either(/a/, /b/, /c/)
/(?:a|b|c)/
/a|b|c/
```

@@ -104,2 +104,9 @@

ComposeRegexp inserts non-capturing groups where needed:
```JS
> sequence(/a/, /b|c/)
/a(?:b|c)/
```
#### lookAhead(regexps...)

@@ -121,22 +128,13 @@

#### greedy(suffix, regexprs...), greedy(suffix)(regexprs...)
#### suffix(operator, regexprs...), greedy(suffix)(regexprs...)
Valid suffixes: (`?`, `*`, `+`, `{n}`, `{n,}` and `{m, n}`)
Valid suffixes: (`?`, `*`, `+`, `{n}`, `{n,}` and `{m, n}`; `??`, `*?`, `+?`, `{n}?`, `{n,}?` and `{m, n}?`)
```JS
> greedy("*", either(/a/, /b/, /c/))
> suffix("*", either(/a/, /b/, /c/))
/(?:a|b|c)*/
> maybe = greedy('?'); maybe(either('a', 'b'))
/(?:a|b)?/
> maybe = suffix('?'); maybe('a')
/a?/
```
#### frugal(suffix, regexprs...), frugal(suffix)(regexprs...)
Like `greedy()` but for non-greedy operators (`??`, `*?`, `+?`, `{n}?`, `{n,}?` and `{m, n}?`).
```JS
> frugal("{1,3}", either(/a/, /b/, /c/))
/(?:a|b|c){1,3}?/
```
#### capture (regexprs...)

@@ -143,0 +141,0 @@

@@ -11,6 +11,4 @@ var CR = require('../')

var either = CR.either
var group = CR.group
var sequence = CR.sequence
var greedy = CR.greedy
var frugal = CR.frugal
var suffix = CR.suffix
var ref = CR.ref

@@ -29,3 +27,3 @@ var lookAhead = CR.lookAhead

either, sequence,
greedy('*'), frugal('+'),
suffix('*'),
lookAhead, avoid,

@@ -49,3 +47,7 @@ flags.bind(null, ''), capture

req(sequence(/^/, 'b', /$/), /^b$/)
req(sequence(/a|b/), /a|b/)
req(either(sequence(sequence(/a|b/))), /a|b/)
req(avoid('a'), /(?!a)/)

@@ -66,10 +68,6 @@ req(avoid('a', 'b'), /(?!ab)/)

req(greedy('?', /foo/), /(?:foo)?/)
req(greedy('*', /foo/), /(?:foo)*/)
req(greedy('+', /foo/), /(?:foo)+/)
req(suffix('?', /foo/), /(?:foo)?/)
req(suffix('*', /foo/), /(?:foo)*/)
req(suffix('+', /foo/), /(?:foo)+/)
req(frugal('?', /foo/), /(?:foo)??/)
req(frugal('*', /foo/), /(?:foo)*?/)
req(frugal('+', /foo/), /(?:foo)+?/)
// this is a bit of a hack to take advantage of `eq` as assert equals.

@@ -79,16 +77,17 @@ req(sequence(flags('m', /o/).multiline), /true/)

;['*', '+', '?', '{2}', '{2,}', '{2,4}'].forEach(function(suffix){
req(greedy(suffix, 'a'), {source: 'a' + suffix})
req(greedy(suffix, /a|b/), {source: '(?:a|b)' + suffix})
req(greedy(suffix, /(a)b/), {source: '(?:(a)b)' + suffix})
req(frugal(suffix, 'a'), {source: 'a' + suffix + '?'})
req(greedy(suffix)('a'), {source: 'a' + suffix})
req(frugal(suffix)('a'), {source: 'a' + suffix + '?'})
;[
'*', '+', '?', '{2}', '{2,}', '{2,4}',
'*?', '+?', '??', '{2}?', '{2,}?', '{2,4}?',
].forEach(function(op){
req(suffix(op, 'a'), {source: 'a' + op})
req(suffix(op, /a|b/), {source: '(?:a|b)' + op})
req(suffix(op, /(a)b/), {source: '(?:(a)b)' + op})
req(suffix(op)('a'), {source: 'a' + op})
})
;['*', '+', '?', '{2}', '{2,}', '{2,4}'].forEach(function(suffix){
;['a', '5.', '{5.4}'].forEach(function(op){
var caught
caught = false
try {
greedy(suffix + '?', 'a')
suffix(op, 'a')
} catch (e) {

@@ -101,3 +100,3 @@ caught = true

try {
greedy(suffix + '?')
suffix(op)
} catch (e) {

@@ -107,55 +106,2 @@ caught = true

eq(caught, true)
caught = false
try {
frugal(suffix + '?', 'a')
} catch (e) {
caught = true
}
eq(caught, true)
caught = false
try {
frugal(suffix + '?')
} catch (e) {
caught = true
}
eq(caught, true)
})
;['a', '5.', '{5.4}'].forEach(function(suffix){
var caught
caught = false
try {
greedy(suffix, 'a')
} catch (e) {
caught = true
}
eq(caught, true)
caught = false
try {
greedy(suffix)
} catch (e) {
caught = true
}
eq(caught, true)
caught = false
try {
frugal(suffix, 'a')
} catch (e) {
caught = true
}
eq(caught, true)
caught = false
try {
frugal(suffix)
} catch (e) {
caught = true
}
eq(caught, true)
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc