Comparing version 1.0.0 to 2.0.0
{ | ||
"name": "regexp-tpl", | ||
"version": "1.0.0", | ||
"version": "2.0.0", | ||
"description": "Templated regular expressions.", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha tests/*.mocha.js", | ||
"coveralls": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report lcovonly -- tests/*.mocha.js -R spec -t 5000 && cat ./coverage/lcov.info | coveralls && rm -rf ./coverage", | ||
"cover": "./node_modules/istanbul/lib/cli.js cover ./node_modules/mocha/bin/_mocha --report html -- tests/*.mocha.js -R spec -t 5000" | ||
"test": "mocha src/*.mocha.js", | ||
"cover": "istanbul cover --report html _mocha -- src/**/*.mocha.js -R spec -t 5000", | ||
"coveralls:build": "istanbul cover --include-all-sources _mocha --report lcovonly -- src/**/*.mocha.js -R spec -t 5000", | ||
"coveralls:send": "cat ./coverage/lcov.info | coveralls && rm -rf ./coverage/lcov.info", | ||
"coveralls": "npm run coveralls:build && npm run coveralls:send", | ||
"lint": "eslint src/**/*.js", | ||
"cli": "env NPM_RUN_CLI=1", | ||
"preversion": "npm test && npm run lint" | ||
}, | ||
"engines": { | ||
"node": ">=4" | ||
}, | ||
"repository": { | ||
@@ -27,6 +35,9 @@ "type": "git", | ||
"coveralls": "^2.11.2", | ||
"istanbul": "^0.3.5", | ||
"mocha": "^2.0.1" | ||
"eslint": "^3.2.2", | ||
"eslint-config-simplifield": "^3.0.0", | ||
"istanbul": "^0.4.4", | ||
"mocha": "^3.0.1" | ||
}, | ||
"dependencies": { | ||
"debug": "^2.2.0", | ||
"escape-regexp-component": "^1.0.2", | ||
@@ -33,0 +44,0 @@ "miniquery": "^1.0.0" |
@@ -5,3 +5,9 @@ # regexp-tpl | ||
[![NPM version](https://badge.fury.io/js/regexp-tpl.png)](https://npmjs.org/package/regexp-tpl) [![Build status](https://secure.travis-ci.org/SimpliField/regexp-tpl.png)](https://travis-ci.org/SimpliField/regexp-tpl) [![Dependency Status](https://david-dm.org/SimpliField/regexp-tpl.png)](https://david-dm.org/SimpliField/regexp-tpl) [![devDependency Status](https://david-dm.org/SimpliField/regexp-tpl/dev-status.png)](https://david-dm.org/SimpliField/regexp-tpl#info=devDependencies) [![Coverage Status](https://coveralls.io/repos/SimpliField/regexp-tpl/badge.png?branch=master)](https://coveralls.io/r/SimpliField/regexp-tpl?branch=master) [![Code Climate](https://codeclimate.com/github/SimpliField/regexp-tpl.png)](https://codeclimate.com/github/SimpliField/regexp-tpl) | ||
[![NPM version](https://badge.fury.io/js/regexp-tpl.svg)](https://npmjs.org/package/regexp-tpl) | ||
[![Build status](https://secure.travis-ci.org/SimpliField/regexp-tpl.svg)](https://travis-ci.org/SimpliField/regexp-tpl) | ||
[![Dependency Status](https://david-dm.org/SimpliField/regexp-tpl.svg)](https://david-dm.org/SimpliField/regexp-tpl) | ||
[![devDependency Status](https://david-dm.org/SimpliField/regexp-tpl/dev-status.svg)](https://david-dm.org/SimpliField/regexp-tpl#info=devDependencies) | ||
[![Coverage Status](https://coveralls.io/repos/SimpliField/regexp-tpl/badge.svg?branch=master)](https://coveralls.io/r/SimpliField/regexp-tpl?branch=master) | ||
[![Code Climate](https://codeclimate.com/github/SimpliField/regexp-tpl.svg)](https://codeclimate.com/github/SimpliField/regexp-tpl) | ||
[![Package Quality](http://npm.packagequality.com/badge/regexp-tpl.png)](http://packagequality.com/#?package=regexp-tpl) | ||
@@ -20,17 +26,17 @@ ## Installation | ||
```js | ||
var regexpTpl = require('regexp-tpl'); | ||
var assert = require('assert'); | ||
const regexpTpl = require('regexp-tpl'); | ||
const assert = require('assert'); | ||
var fruits = [{ | ||
const fruits = [{ | ||
name: 'orange', | ||
count: 2, | ||
colors: ['orange'] | ||
colors: ['orange'], | ||
}, { | ||
name: 'banana', | ||
count: 0, | ||
colors: ['yellow', 'white'] | ||
colors: ['yellow', 'white'], | ||
}, { | ||
name: 'kiwi', | ||
count: 8, | ||
colors: ['brown', 'green'] | ||
colors: ['brown', 'green'], | ||
}]; | ||
@@ -43,3 +49,3 @@ | ||
Note that `regexp-tpl` template values are evaluated with | ||
[`miniquery`'s' syntax](https://github.com/SimpliField/miniquery). | ||
[`miniquery`'s syntax](https://github.com/SimpliField/miniquery). | ||
@@ -49,5 +55,6 @@ | ||
### regExp:RegExp regexpTpl(objs:Array, regExpTemplate:String, regExpflags:String) | ||
Return a `RegExp` instance made with the given `regExpTemplate``and `regExplags` | ||
filled with the values picked up in the given `objs`. | ||
### regExp:RegExp regexpTpl(objs:Array, regExpTemplate:String, regExpflags:String, tplRegExp:RegExp) | ||
Return a `RegExp` instance made with the given `regExpTemplate` and `regExplags` | ||
filled with the values picked up in the given `objs`. An optionnal `tplRegExp` | ||
value can be provided for custom template syntax. | ||
@@ -54,0 +61,0 @@ ## Contribute |
@@ -1,8 +0,12 @@ | ||
var escRegExp = require('escape-regexp-component'); | ||
var miniquery = require('miniquery'); | ||
'use strict'; | ||
var DEFAULT_TEMPLATE = /(.*|^)\{([a-z0-9_\-\.\*\@\#]+)\}(.*|$)/i; | ||
const escRegExp = require('escape-regexp-component'); | ||
const miniquery = require('miniquery'); | ||
const debug = require('debug')('regexp-tpl'); | ||
const DEFAULT_TEMPLATE = /(.*|^)\{([a-z0-9_\-\.\*\@\#]+)\}(.*|$)/i; | ||
function regexpTpl(objs, regExpTemplate, regExpFlags, templateRegExp) { | ||
var hasUnmatchedTemplateValues = false; | ||
let hasUnmatchedTemplateValues = false; | ||
let regExp; | ||
@@ -16,23 +20,38 @@ if(!(objs instanceof Array)) { | ||
debug('Creating a regExp from:', regExpTemplate); | ||
function findTemplateValue($, $1, $2, $3) { | ||
const values = miniquery($2, objs); | ||
debug('New template value', $2, values); | ||
if(values.length) { | ||
return $1 + (1 === values.length ? | ||
escRegExp(values[0]) : | ||
'(' + values.map(escRegExp).join('|') + ')') + $3; | ||
} | ||
hasUnmatchedTemplateValues = true; | ||
return ''; | ||
} | ||
while((templateRegExp || DEFAULT_TEMPLATE).test(regExpTemplate)) { | ||
regExpTemplate = regExpTemplate.replace( | ||
(templateRegExp || DEFAULT_TEMPLATE), | ||
function($, $1, $2, $3) { | ||
var values = miniquery($2, objs); | ||
if(values.length) { | ||
return $1 + (1 === values.length ? | ||
escRegExp(values[0]) : | ||
'(' + values.map(escRegExp).join('|') + ')') + $3; | ||
} | ||
hasUnmatchedTemplateValues = true; | ||
}); | ||
findTemplateValue | ||
); | ||
} | ||
if(hasUnmatchedTemplateValues) { | ||
debug('Could not build (unmatched template value):', regExpTemplate); | ||
return {}.undef; | ||
} | ||
return new RegExp(regExpTemplate, regExpFlags); | ||
regExp = new RegExp(regExpTemplate, regExpFlags); | ||
debug('Built:', regExp); | ||
return regExp; | ||
} | ||
module.exports = regexpTpl; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
8891
8
139
78
3
5
1
+ Addeddebug@^2.2.0
+ Addeddebug@2.6.9(transitive)
+ Addedms@2.0.0(transitive)