Skip to content

Instantly share code, notes, and snippets.

@HarryStevens HarryStevens/index.js

Last active May 16, 2017
Embed
What would you like to do?
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
You can’t perform that action at this time.