1. Writing files
/** * Write to File * @param {string} filePath file path * @param {string} data file contents * @return {none}*/functionWriteFile (filepath,data) {varDIR =Path.dirname (FilePath); varParts =Dir.split (PATH.SEP); Parts.reduce (function(Previous, current) {varPredir =Path.join (previous,current); if(!Fs.existssync (Predir)) {Fs.mkdirsync (Predir)}returnPredir}); Fs.writefilesync (Filepath,data);}
2. Folder replication
/** * Copy folder and file inside * @param {string} src source file path * @param {string} dest placement Path * @return {bool}*/functioncopy (src, dest) {if(isdirectory (src)) {if(!Fs.existssync (dest)) {Fs.mkdirsync (dest); } varFiles =Fs.readdirsync (SRC); if(Files) {Files.foreach (function(filename) {copy (Path.join (src, filename), path.join (dest, FileName)})} } Else{fs.writefilesync (dest, ReadFile (SRC)); } return true;}
3. Array de-weight
/* * * Array to weight * @param {array} arr * @return {array} */function arrayunique (arr) {var result = []; for (var i = 0; i < arr.length; i++) { if (Result.indexof (arr[i]) = =-1) { Result.push (Arr[i]); } } return result;}
4. Deep Clone objects
/** * Clone Object * @param {object} obj * @return {object}*/functionClone (obj) {//simple data type returns directly if(obj = =NULL) { returnobj; } if(typeofObj! = ' object ') { returnobj; } //Copying Complex objects varresult =NewObj.constructor (); for(varPropinchobj) {Result[prop]=Clone (Obj[prop]); } returnresult;};
A few common node methods