Skip to content
This repository has been archived by the owner on Dec 22, 2018. It is now read-only.

Commit

Permalink
Provide path to replace(); undefined value -> empty replacement string
Browse files Browse the repository at this point in the history
Two related changes, plus eslint cleaning, dependencies, and tests.

 - If no `value` key is given, the replacement string is '' rather than
   the 'undefined' provided by JSON.stringify().
 - The `file.path` parameter is given to the replacement function.

Note: this updates the dependency on eslint from 3.x to 4.x.  This is
because [email protected] now requires eslint@^4.
  • Loading branch information
Chris White committed Sep 8, 2018
1 parent 3647570 commit 9bc43ca
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*~
*.swp
*.bak
.DS_Store
npm-debug.log
Expand Down
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ replacer: {
// custom configuration JSON, generate random string, etc.
key: /{#VERSION}/g,
value: 'v1.0.0'
},
{
key: 'remove_me'
// If value is omitted, the replacement is the empty string
}
],
// By default replacer uses String.replace() function.
Expand All @@ -42,7 +46,25 @@ replacer: {
// - str (string) - string to be processed
// - key (any) - key from the dict
// - value (string) - replacement value
replace: (str, key, value) => str.split(key).join(value)
// - path (string) - the path of the file being processed
replace: (str, key, value, path) => str.split(key).join(value)
}
```

For example, to replace `__filename` with the name of the file being
processed, you can use:

```js
replacer: {
dict: [
{
key: /\b__filename\b/,
// No value needed - the custom replacer below supplies it
}
],
replace: (str, key, value, path) => {
return str.split(key).join(`'${path}'`);
}
}
```

Expand All @@ -59,3 +81,8 @@ Or, do manual install:
## License

Licensed under [MIT License](https://github.com/tkesgar/replacer-brunch/blob/master/LICENSE).

## Contributors

* [Ted Kesgar](https://github.com/tkesgar)
* [Chris White](https://github.com/cxw42)
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ class BrunchReplacer {

// Set defaults for config.
if (!this.config.dict) this.config.dict = [];
if (!this.config.replace) this.config.replace = (str, key, value) => str.replace(key, value);
if (!this.config.replace) {
this.config.replace = (str, key, value) => str.replace(key, value);
}

// Stringify non-string values.
for (const entry of this.config.dict) {
const value = entry.value;
entry.value = typeof value === 'string' ? value : JSON.stringify(value);
if (typeof value === 'undefined') {
entry.value = '';
} else {
entry.value = typeof value === 'string' ? value : JSON.stringify(value);
}
}
}

Expand All @@ -26,7 +32,7 @@ class BrunchReplacer {
for (const entry of dict) {
const key = entry.key;
const value = entry.value;
file.data = replace(file.data, key, value);
file.data = replace(file.data, key, value, file.path);
}

return Promise.resolve(file);
Expand Down
4 changes: 2 additions & 2 deletions package.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "replacer-brunch",
"version": "1.0.0",
"version": "1.1.0",
"description": "Adds string replacement support to Brunch.",
"author": "Ted Kesgar (https://tkesgar.com/)",
"homepage": "https://github.com/tkesgar/replacer-brunch",
Expand All @@ -16,7 +16,7 @@
"devDependencies": {
"chai": "^3",
"chai-as-promised": "^6.0.0",
"eslint": "^3.12.2",
"eslint": "4.x",
"eslint-config-brunch": "brunch/eslint-config-brunch",
"mocha": "^3"
},
Expand Down
33 changes: 32 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,18 @@ describe('Plugin', () => {
dict: [{key: '__KEY__', value: '__VALUE__'}],
}}});

it('should replace key with value', () => {
it('should not replace when key is absent', () => {
const file1 = {data: 'test'};
const file2 = {data: 'test'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});

it('should replace key with value', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: '__VALUE__'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});

it('should not replace anything', () => {
const file1 = {data: '__KEY'};
const file2 = {data: '__KEY'};
Expand Down Expand Up @@ -74,6 +80,18 @@ describe('Plugin', () => {

describe('using non-strings as value', () => {

describe('with unspecified replacement value', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g}],
}}});

it('should replace key with empty string', () => {
const file1 = {data: '__KEY__'};
const file2 = {data: ''};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});

describe('with object', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: /__KEY__/g, value: {one: 1, two: 'two'}}],
Expand Down Expand Up @@ -125,4 +143,17 @@ describe('Plugin', () => {
});
});

describe('using custom replace function to add path', () => {
const plugin = new Plugin({plugins: {replacer: {
dict: [{key: '__KEY__', value: '__VALUE__'}],
replace: (str, key, value, path) => str.split(key).join(path),
}}});

it('should replace key with path', () => {
const file1 = {path: 'hello.js', data: 'The quick brown __KEY__ jumps over the lazy __KEY__'};
const file2 = {path: 'hello.js', data: 'The quick brown hello.js jumps over the lazy hello.js'};
return expect(plugin.compile(file1)).to.eventually.deep.equal(file2);
});
});

});

0 comments on commit 9bc43ca

Please sign in to comment.