Skip to content

Commit

Permalink
Merge pull request #1 from orchitech/rollup-build
Browse files Browse the repository at this point in the history
Add project bundling with base linter
  • Loading branch information
martincizek authored Nov 15, 2019
2 parents 8a04404 + 8427a03 commit faebc2c
Show file tree
Hide file tree
Showing 13 changed files with 1,555 additions and 236 deletions.
5 changes: 5 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"presets": [
["@babel/env", {}]
]
}
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist
7 changes: 7 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"extends": "airbnb-base",
"rules": {
"no-plusplus": "off",
"max-classes-per-file": "off"
}
}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,6 @@ typings/

# next.js build output
.next

# Build directory
dist
1,421 changes: 1,345 additions & 76 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 23 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,44 @@
{
"name": "union-replacer",
"version": "0.0.9",
"version": "1.0.0",
"description": "One-pass String.prototype.replace-like processor with multiple regexps and replacements",
"main": "index.js",
"main": "dist/union-replacer.cjs.js",
"module": "dist/union-replacer.esm.js",
"browser": "dist/union-replacer.umd.js",
"scripts": {
"test": "node test/union-replacer-test.js && node test/readme-test.js"
"lint": "eslint .",
"prebuild": "npm run lint",
"build": "rollup -c",
"pretest": "npm run build",
"test": "jasmine JASMINE_CONFIG_PATH=test/jasmine.json",
"prepublish": "npm test"
},
"repository": {
"type": "git",
"url": "git+https://github.com/orchitech/union-replacer.git"
},
"devDependencies": {
"standard": "^14.0.0",
"@babel/core": "^7.7.2",
"@babel/plugin-transform-runtime": "^7.6.2",
"@babel/preset-env": "^7.7.1",
"colors": "^1.4.0",
"diff": "^4.0.0",
"colors": "^1.4.0"
"eslint": "^6.6.0",
"eslint-config-airbnb-base": "^14.0.0",
"jasmine": "^3.5.0",
"rollup": "^1.26.3",
"rollup-plugin-babel": "^4.3.3",
"standard": "^14.0.0"
},
"keywords": [
"regexp",
"replace",
"union",
"alternation"
],
"files": [
"dist"
],
"author": "Martin Cizek <[email protected]>",
"license": "MIT",
"bugs": {
Expand Down
16 changes: 16 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import babel from 'rollup-plugin-babel';
import pkg from './package.json';

export default [
{
input: 'src/UnionReplacer.js',
plugins: [
babel({ exclude: 'node_modules/**' }),
],
output: [
{ file: pkg.main, format: 'cjs' },
{ file: pkg.module, format: 'es' },
{ file: pkg.browser, name: 'UnionReplacer', format: 'umd' },
],
},
];
32 changes: 15 additions & 17 deletions src/UnionReplacer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class UnionReplacerRule {
constructor(pattern, replacement) {
if (pattern.constructor !== RegExp) {
throw new TypeError(`Replacement pattern ${pattern} is not a RegExp.`)
throw new TypeError(`Replacement pattern ${pattern} is not a RegExp.`);
}
this.pattern = pattern;
if (typeof replacement === 'function') {
Expand All @@ -22,16 +22,15 @@ class UnionReplacerRule {
captureCount++;
} else if (backref) {
if (+backref > captureCount) {
throw new SyntaxError(`Octal or backreference to undefined capture group ${backref} in ${pattern}`)
throw new SyntaxError(`Octal or backreference to undefined capture group ${backref} in ${this.pattern}`);
}
// renumber backreference
return `\\${+backref + captureNum}`
return `\\${+backref + captureNum}`;
}
return match;
}
);
});
this.captureNum = captureNum;
this.capturePatternStr = `(${patternStr})`
this.capturePatternStr = `(${patternStr})`;
this.captureCount = captureCount + 1;
}

Expand Down Expand Up @@ -60,35 +59,34 @@ class UnionReplacerRule {
return namedCaptures[namedCapture];
}
return m;
}
);
});
}
}

const unionReplacerCountCaptureGroups = rules =>
rules.reduce((num, rule) => num + rule.captureCount, 0);
const unionReplacerCountCaptureGroups = (rules) => rules.reduce((num, rule) => num
+ rule.captureCount, 0);

class UnionReplacer {
constructor(replaces) {
this.rules = [];
this.compiled = false;
if (replaces) {
replaces.forEach(replace => this.addReplacement(replace[0], replace[1]));
replaces.forEach((replace) => this.addReplacement(replace[0], replace[1]));
}
}

addReplacement(pattern, replacement) {
if (this.compiled) {
throw new Error('Dynamic rule changes not yet supported.');
}
let rule = new UnionReplacerRule(pattern, replacement);
const rule = new UnionReplacerRule(pattern, replacement);
rule.compile(unionReplacerCountCaptureGroups(this.rules) + 1);
this.rules.push(rule)
this.rules.push(rule);
}

compile() {
this.totalCaptureGroups = unionReplacerCountCaptureGroups(this.rules);
const regexpStr = this.rules.map(rule => rule.capturePatternStr).join('|');
const regexpStr = this.rules.map((rule) => rule.capturePatternStr).join('|');
this.regexp = new RegExp(regexpStr, 'gm');
this.compiled = true;
}
Expand All @@ -98,13 +96,13 @@ class UnionReplacer {
this.compile();
}
return string.replace(this.regexp, (...args) => {
const rule = this.rules.find(rule => typeof args[rule.captureNum] !== 'undefined');
const rule = this.rules.find((item) => typeof args[item.captureNum] !== 'undefined');
const newargs = args
.slice(rule.captureNum, rule.captureNum + rule.captureCount)
.concat(args.slice(1 + this.totalCaptureGroups));
return rule.replacementFn(...newargs)
return rule.replacementFn(...newargs);
});
}
}

module.exports = UnionReplacer;
export default UnionReplacer;
8 changes: 8 additions & 0 deletions test/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"env": {
"jasmine": true
},
"rules": {
"import/no-unresolved": "off"
}
}
9 changes: 9 additions & 0 deletions test/jasmine.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"spec_dir": "test",
"spec_files": [
"**/*[sS]pec.js",
"!**/*nospec.js"
],
"stopSpecOnExpectationFailure": false,
"random": false
}
90 changes: 45 additions & 45 deletions test/readme-test.js
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
const fs = require('fs');
const UnionReplacer = require("../src/UnionReplacer")
require('colors');
const Diff = require('diff');
// const fs = require('fs');
// const UnionReplacer = require("../src/UnionReplacer")
// require('colors');
// const Diff = require('diff');

const readme = fs.readFileSync(__dirname + '/../README.md', 'utf8');
var docContext = {
asserted: false,
actual: null,
UnionReplacer: UnionReplacer
};
const docBlockRe = /^(([`~])\2{2,})[ \t]*(.*?)\s+([\s\S]*?)(?:^\1\2*[ \t]*$|(?![\s\S]))/gm;
const testify = new UnionReplacer([
[/^(?:[ \t\w{}.]+=)?\s*\brequire\b.*$/, '// $&'],
[/^[ \t]*(var|const|let)\b/, '/* $1 */'],
[/^[ \t]*console\.log\((.*?)\)[;\s]*(?![\s\S])/, 'actual = $1; asserted = true;']
]);
// const readme = fs.readFileSync(__dirname + '/../README.md', 'utf8');
// var docContext = {
// asserted: false,
// actual: null,
// UnionReplacer: UnionReplacer
// };
// const docBlockRe = /^(([`~])\2{2,})[ \t]*(.*?)\s+([\s\S]*?)(?:^\1\2*[ \t]*$|(?![\s\S]))/gm;
// const testify = new UnionReplacer([
// [/^(?:[ \t\w{}.]+=)?\s*\brequire\b.*$/, '// $&'],
// [/^[ \t]*(var|const|let)\b/, '/* $1 */'],
// [/^[ \t]*console\.log\((.*?)\)[;\s]*(?![\s\S])/, 'actual = $1; asserted = true;']
// ]);

let m, total = 0, failed = 0, passed = 0;
while ((m = docBlockRe.exec(readme)) !== null) {
let infoStr = m[3];
let code = m[4].trim().replace(/\r?\n/, '\n');
let asserted = docContext.asserted;
docContext.asserted = false;
// let m, total = 0, failed = 0, passed = 0;
// while ((m = docBlockRe.exec(readme)) !== null) {
// let infoStr = m[3];
// let code = m[4].trim().replace(/\r?\n/, '\n');
// let asserted = docContext.asserted;
// docContext.asserted = false;

if (/^.*no unionreplacer/i.test(code)) {
continue;
}
// if (/^.*no unionreplacer/i.test(code)) {
// continue;
// }

if (infoStr === 'js') {
code = testify.replace(code);
(new Function(`with (this) {\n ${code}\n}`)).call(docContext);
} else if (infoStr === '' && asserted) {
total++;
if (code === docContext.actual) {
passed++;
} else {
failed++;
console.log('Assertion failed:');
let diff = Diff.diffWordsWithSpace(code, docContext.actual);
diff.forEach((part) => {
let color = part.added ? 'green' : part.removed ? 'red' : 'grey';
process.stderr.write(part.value[color]);
});
process.stderr.write('\n');
}
}
}
// if (infoStr === 'js') {
// code = testify.replace(code);
// (new Function(`with (this) {\n ${code}\n}`)).call(docContext);
// } else if (infoStr === '' && asserted) {
// total++;
// if (code === docContext.actual) {
// passed++;
// } else {
// failed++;
// console.log('Assertion failed:');
// let diff = Diff.diffWordsWithSpace(code, docContext.actual);
// diff.forEach((part) => {
// let color = part.added ? 'green' : part.removed ? 'red' : 'grey';
// process.stderr.write(part.value[color]);
// });
// process.stderr.write('\n');
// }
// }
// }

console.log(`Passed ${passed} of ${total} tests.`);
// console.log(`Passed ${passed} of ${total} tests.`);
93 changes: 0 additions & 93 deletions test/union-replacer-test.js

This file was deleted.

Loading

0 comments on commit faebc2c

Please sign in to comment.