From 7f6daf4bd11a958a807b794bec4dd17f6fb01580 Mon Sep 17 00:00:00 2001 From: nirs Date: Wed, 6 Dec 2017 16:50:27 +0200 Subject: [PATCH] Adding an option to delete by prefix. Tests added too. --- README.md | 15 +++++++++++---- find-remove.js | 5 +++++ tests/basics.js | 31 +++++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index ab79c61..8f44265 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,13 @@ var result = findRemoveSync('/dist', {dir: 'CVS'}) var result = findRemoveSync('/tmp', {age: {seconds: 3600}, extensions: '.jpg', limit: 100}) ``` -### 7. apply filter options only for two levels inside the /temp directory for all tmp files +### 7. delete all files with prefix 'filenamestartswith' + +```javascript +var result = findRemoveSync('/tmp', {prefix: 'filenamestartswith'}) +``` + +### 8. apply filter options only for two levels inside the /temp directory for all tmp files ```javascript var result = findRemoveSync('/tmp', {maxLevel: 2, extensions: '.tmp'}) @@ -77,7 +83,7 @@ but not `/tmp/level1/level2/level3/b.tmp` why the heck do we have this `maxLevel` option? because of performance. if you care about deep subfolders, apply that option to get a speed boost. -### 8. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?) +### 9. delete everything recursively (hey, who needs that when you can use nodejs' fs.unlink?) ```javascript var result = findRemoveSync(rootDirectory, {dir: "*", files: "*.*"}) @@ -95,11 +101,12 @@ __arguments__ * options - currently those properties are supported: * `files` - can be a string or an array of files you want to delete within `dir`. * `dir` - can be a string or an array of directories you want to delete within `dir`. - * `extensions` - this too, can be a string or an array of file extenstions you want to delete within `dir`. + * `extensions` - this too, can be a string or an array of file extentions you want to delete within `dir`. * `ignore` - useful to exclude some files. again, can be a string or an array of file names you do NOT want to delete within `dir` * `age.seconds` - can be any float number. findRemoveSync then compares it with the file stats and deletes those with creation times older than `age.seconds` * `limit` - can be any integer number. Will limit the number of files to be deleted at single operation to be `limit` - * `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees. + * `prefix` - can be any string. Will delete any files that start with `prefix`. + * `maxLevel` - advanced: limits filtering to a certain level. useful for performance. recommended for crawling huge directory trees. * `test` - advanced: set to true for a test run, meaning it does not delete anything but returns a JSON of files/directories it would have deleted. useful for testing. as a precaution, nothing happens when there are no options. diff --git a/find-remove.js b/find-remove.js index fdd3824..5fcee00 100644 --- a/find-remove.js +++ b/find-remove.js @@ -84,6 +84,7 @@ function doDeleteFile(currentFile, options) { var extensions = (options && options.extensions) ? options.extensions : null var files = (options && options.files) ? options.files : null + var prefix = (options && options.prefix) ? options.prefix: null var dir = (options && options.dir) ? options.dir : null var ignore = (options && options.ignore) ? options.ignore : null @@ -112,6 +113,10 @@ function doDeleteFile(currentFile, options) { } } + if (!doDelete && prefix) { + doDelete = basename.indexOf(prefix) === 0 + } + if (doDelete && hasLimit(options)) { doDelete = !isOverTheLimit(options) } diff --git a/tests/basics.js b/tests/basics.js index 1e555ec..3979a9e 100644 --- a/tests/basics.js +++ b/tests/basics.js @@ -717,7 +717,7 @@ module.exports = testCase({ 'findRemoveSync(files older than .0005 sec with limit of 2)': function(t) { var result = findRemoveSync(rootDirectory, {files: "*.*", age: {seconds: 0.0005}, limit: 2}) - t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files older than .0005 sec) returned 2 entries (out of 11).') + t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files older than .0005 sec with limit of 2) returned 2 entries (out of 11).') t.done() }, @@ -725,7 +725,34 @@ module.exports = testCase({ 'findRemoveSync(files and dirs older than .0005 sec with limit of 5)': function(t) { var result = findRemoveSync(rootDirectory, {files: "*.*", dir: "*", age: {seconds: 0.0005}, limit: 5}) - t.strictEqual(Object.keys(result).length, 5, 'findRemoveSync(files older than .0005 sec) returned 5 entries (out of 19).') + t.strictEqual(Object.keys(result).length, 5, 'findRemoveSync(files and dirs older than .0005 sec with limit of 5) returned 5 entries (out of 19).') + + t.done() + } + + }), + + 'TC 6: prefix checks': testCase({ + + setUp: function(cb) { + createFakeDirectoryTree(cb) + }, + tearDown: function(cb) { + destroyFakeDirectoryTree(cb) + }, + + 'findRemoveSync(files with exiting prefix "someth")': function(t) { + var result = findRemoveSync(rootDirectory, {prefix: "someth"}) + + t.strictEqual(Object.keys(result).length, 2, 'findRemoveSync(files with prefix "someth") returned 2 entries (out of 11).') + + t.done() + }, + + 'findRemoveSync(files with non-existing prefix "ssssssssssssssssssssssssss" - too many chars)': function(t) { + var result = findRemoveSync(rootDirectory, {prefix: "ssssssssssssssssssssssssss"}) + + t.strictEqual(Object.keys(result).length, 0, 'findRemoveSync(files with non-existing prefix "ssssssssssssssssssssssssss"- too many chars) returned 0 entries (out of 11).') t.done() }