Skip to content

Instantly share code, notes, and snippets.

@HarryStevens
Last active May 16, 2017 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HarryStevens/3a1a3e8d14918e78a6bb52d3cdffe0c9 to your computer and use it in GitHub Desktop.
Save HarryStevens/3a1a3e8d14918e78a6bb52d3cdffe0c9 to your computer and use it in GitHub Desktop.
Node Utility Functions
// Get all directories in another directory
// Requires "fs"
function getDirectories(path) {
return fs.readdirSync(path).filter(function (file) {
return fs.statSync(path + "/" + file).isDirectory();
});
}
// List all files in a directory
// Requires "fs"
var dir = "data";
// Unsynchronized
fs.readdir(dir, function(err, files) {
files.forEach(function(file) {
console.log(file);
});
});
// Synchronized
fs.readdirSync(testFolder).forEach(function(file){
console.log(file);
});
// Slugify a string
function slugify(text) {
return text.toString().toLowerCase()
.replace(/\s+/g, '-') // Replace spaces with -
.replace(/[^\w\-]+/g, '') // Remove all non-word chars
.replace(/\-\-+/g, '-') // Replace multiple - with single -
.replace(/^-+/, '') // Trim - from start of text
.replace(/-+$/, ''); // Trim - from end of text
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment