Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@0.11.0 - expose direct string.replace functionality #37

Merged
merged 1 commit into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ A brief description of the options that control file selection and processing:
+ `update` - Does the actual update on the files in place. Use only if you have version control.
+ `includeExts` - {Array} File extensions used to include files in processing. Must include the dot.
+ `excludeDirsRe` - {RegExp} A regular expression used to exclude directories from processing. A match is an exclusion.
+ `targetText` - {String} The string used to identify the line to process.
+ `replacementText` - {String} The string used to replace the `targetText`.
+ `targetText` - {String|RegExp} Used to identify the line to process. First argument to `string.replace`.
+ `replacementText` - {String|Function} Used to replace the `targetText`. Second argument to `string.replace`.
+ `replacementFilter` - {Function} A function to deny a specific replacement otherwise matched by `targetText`. Return true to allow the replacement to proceed as normal, false to deny it. If not supplied, defaults to all matched replacements allowed.
*signature:* **function (filePath, lineText): Boolean**
+ `logger` - {Function} A log function to receive output. Defaults to `console.log`.
Expand Down
11 changes: 6 additions & 5 deletions lib/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import fs from 'fs';
* @private
*/
function processFile (filePath, update) {
let change, found = false;
let change, candidate, found = false;
const foundLines = [];
const lines = fs.readFileSync(filePath, { encoding: 'utf8' }).split('\n');

for (let i = 0; i < lines.length; i++) {
const test = lines[i].indexOf(this.targetText) !== -1 &&
candidate = lines[i].replace(
this.targetText, this.replacementText
);
const test = candidate !== lines[i] &&
this.replacementFilter(filePath, lines[i]);
if (test) {
found = true;
change = lines[i].replace(
this.targetText, this.replacementText
);
change = candidate;
foundLines.push({
found: lines[i].slice(0),
change
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "picklr",
"version": "0.10.0",
"version": "0.11.0",
"description": "A utility to update a single line in multiple files",
"main": "index.js",
"type": "module",
Expand Down
41 changes: 37 additions & 4 deletions tests/unit/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ describe('picklr', () => {
expect(replaceText[0]).to.contain('9').and.not.contain('8');
});

it('should report the proposed update, regexp', () => {
picklr(workit, {
action: 'audit',
targetText: /8+/,
replacementText: '9',
includeExts: ['.txt'],
logger: getAudits
});

expect(foundText.length).to.equal(1);
expect(replaceText.length).to.equal(1);
expect(foundText[0]).to.contain('88888888');
expect(replaceText[0]).to.contain('9').and.not.contain('8');
});

it('should report omitted files', () => {
picklr(workit, {
action: 'audit',
Expand Down Expand Up @@ -333,19 +348,37 @@ describe('picklr', () => {
{
description: 'should update multiple lines if found',
checkLineArgs: [['_app.scss', 1], ['_multi.scss', 4]],
replacementFilter: null
replacementFilter: null,
target: '39393939',
replace: '40404040'
},
{
description: 'should update multiple lines if found, regexp',
checkLineArgs: [['_app.scss', 1], ['_multi.scss', 4]],
replacementFilter: null,
target: /39+/,
replace: '40404040'
},
{
description: 'should filter multiple lines if found',
checkLineArgs: [['_app.scss', 1], ['_multi.scss', 3]],
replacementFilter
replacementFilter,
target: '39393939',
replace: '40404040'
},
{
description: 'should filter multiple lines if found, regexp',
checkLineArgs: [['_app.scss', 1], ['_multi.scss', 3]],
replacementFilter,
target: /39+/,
replace: '40404040'
}
].forEach(args => {
it(args.description, () => {
picklr(update, {
action: 'update',
targetText: '39393939',
replacementText: '40404040',
targetText: args.target,
replacementText: args.replace,
replacementFilter: args.replacementFilter,
includeExts: ['.scss'],
excludeDirsRe: /1|2/,
Expand Down