Last active
February 13, 2019 04:07
-
-
Save chaoticbit/5ade0af7390e9b71ed2c8cc3e8121fb6 to your computer and use it in GitHub Desktop.
JS Utils
This file contains hidden or 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
/** | |
Method to remove directory and files recursively & | |
check if user wants to delete the directory or just the files in it. | |
**/ | |
const fs = require('fs'); | |
const path = require('path'); | |
function rmDir(dir, rmSelf = true) { | |
let files; | |
try { | |
files = fs.readdirSync(dir); | |
} catch (e) { | |
console.log('!Oops, directory not exist.'); | |
return; | |
} | |
if (files.length > 0) { | |
files.forEach(function(file, index) { | |
let newDir = path.join(dir, file); | |
if (fs.statSync(newDir).isDirectory()) { | |
rmDir(newDir); | |
} else { | |
fs.unlinkSync(newDir); | |
} | |
}); | |
} | |
if (rmSelf) { | |
// check if user want to delete the directory or just the files in this directory | |
fs.rmdirSync(dir); | |
} | |
}; |
This file contains hidden or 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
/* | |
To UpperCamelCase(string) | |
input: toUpperCamelCase('action-button-material'); | |
returns 'ActionButtonMaterial' | |
*/ | |
function toUpperCamelCase(str) { | |
return str | |
.replace(/[a-z]/, function(p) { return p[0].toUpperCase()}) | |
.replace(/-([a-z])/g, function (p) { return p[1].toUpperCase()}); | |
} | |
/* | |
extractFileName(string, boolean) | |
1. input: extractFileName('/src/components/action-button/action-buton.js'); | |
returns 'action-buton' | |
2. input: extractFileName('/src/components/action-button/action-buton.js', true); | |
returns ['action-buton','js'] | |
*/ | |
function extractFileName(file, ext=false) { | |
let fileParts = file.substr(file.lastIndexOf('/') + 1).split('.'); | |
return ext ? fileParts : fileParts.shift(); | |
} | |
/* | |
deep equality | |
*/ | |
const deepEquality = (value1, value2) => { | |
return value1 === value2 | |
? true | |
: value !== null && | |
value2 !== null && | |
(typeof value1 === 'object' && typeof value2 === 'object') | |
? deepEquality( | |
Object.keys(value1) | |
.map(value => value1[value] === value2[value]) | |
.reduce((acc, cur) => acc && cur), | |
Object.keys(value2) | |
.map(value => value2[value] === value1[value]) | |
.reduce((acc, cur) => acc && cur) | |
) | |
: false; | |
}; | |
//console.log([1,2,3] === [1,2,3]); false | |
//console.log(deepEquality[1,2,3],[1,2,3])); true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment