What is junk?
The 'junk' npm package is a small utility that helps in filtering out system files and other non-essential files from an array of filenames. It is particularly useful when dealing with file listings that may include unwanted files like '.DS_Store' on macOS or 'Thumbs.db' on Windows.
What are junk's main functionalities?
Filter out junk files from a list
This feature allows you to remove common system-generated files from an array of filenames, ensuring that only relevant files are processed or displayed.
const junk = require('junk');
const files = ['file1.txt', '.DS_Store', 'file2.txt', 'Thumbs.db'];
const cleanFiles = files.filter(junk.not);
console.log(cleanFiles); // Output: ['file1.txt', 'file2.txt']
Identify if a file is considered junk
This function checks if a single filename is considered 'junk' by comparing it against a predefined list of junk file names.
const junk = require('junk');
console.log(junk.is('.DS_Store')); // Output: true
console.log(junk.is('file1.txt')); // Output: false
Other packages similar to junk
ignore
Similar to 'junk', the 'ignore' package is used to manage and filter files based on rules specified in a .gitignore file. While 'junk' specifically filters out system and hidden files, 'ignore' provides a more flexible approach allowing users to define custom patterns for what should be ignored.
glob
The 'glob' package provides functionality to match file paths against specified patterns. Although it's not specifically for filtering out junk files, it can be configured to exclude certain patterns similar to junk files, offering a broader file filtering capability compared to 'junk'.
junk
Helps you filter out OS junk files like .DS_Store and Thumbs.db
Getting started
Install: npm install --save junk
Example
var fs = require('fs');
var junk = require('junk');
fs.readdir('path', function (err, files) {
console.log(files);
console.log(files.filter(junk.isnt));
});
Documentation
junk.is(filename)
Returns true if filename
matches any of the junk.rules
.
junk.isnt(filename)
Returns true if filename
doesn't match any of the junk.rules
.
junk.rules
Returns an array of regexes you can match against.
License
MIT License • © Sindre Sorhus