Node Utility Functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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