├── README.md ├── abbrevatedText.js ├── catchAsync.js ├── cleanupMongooseSchema.js ├── des.js ├── flatToNestedObject.js ├── objectDeepFreezing.js └── spaceSplit.js /README.md: -------------------------------------------------------------------------------- 1 | # Javascript-Utility-functions 2 | Javascript Utility functions 3 | -------------------------------------------------------------------------------- /abbrevatedText.js: -------------------------------------------------------------------------------- 1 | const longText = "World Wide Web"; 2 | 3 | const abbrevatedText = longText 4 | .split(" ") 5 | .map((word) => word[0]) 6 | .join(""); 7 | console.log(abbrevatedText); 8 | -------------------------------------------------------------------------------- /catchAsync.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Handle errors in Express middlewares and routes easily. No need of using try/catch blocks in async functions anymore. 3 | * @param {Function} fn Callback function 4 | * @example 5 | * catchAsync(async (req, res) => { 6 | res.status(200).json('Success'); 7 | }) 8 | * @returns {Function} Callback function with the error handled 9 | * @author Bihan Chakraborty 10 | */ 11 | 12 | const catchAsync = (fn) => { 13 | return (req, res, next) => { 14 | fn(req, res, next).catch((err) => next(err)); 15 | }; 16 | }; 17 | 18 | export default catchAsync; 19 | -------------------------------------------------------------------------------- /cleanupMongooseSchema.js: -------------------------------------------------------------------------------- 1 | const cleanupMongooseSchema = (schema) => { 2 | let cleanSchema = {}; 3 | for (const field in schema.paths) { 4 | const { path, instance, options } = schema.paths[field]; 5 | 6 | cleanSchema[path] = { type: instance }; 7 | 8 | if (schema.paths[field].hasOwnProperty("schema")) { 9 | console.log(schema.paths[field].schema); 10 | const nestedSchema = schema.paths[field].schema; 11 | const nestedCleanup = cleanupMongooseSchema(nestedSchema); 12 | cleanSchema[path].schema = nestedCleanup; 13 | } 14 | 15 | const toDestrcture = ["ref", "enum", "default"]; 16 | toDestrcture.forEach((des) => { 17 | if (options.hasOwnProperty(des)) { 18 | cleanSchema[path][des] = options[des]; 19 | } 20 | }); 21 | } 22 | 23 | return cleanSchema; 24 | }; -------------------------------------------------------------------------------- /des.js: -------------------------------------------------------------------------------- 1 | const crypto = require("crypto"); 2 | 3 | String.prototype.getBytes = function () { 4 | var bytes = []; 5 | for (var i = 0; i < this.length; ++i) { 6 | bytes.push(this.charCodeAt(i)); 7 | } 8 | return bytes; 9 | }; 10 | 11 | function decryptURL(encURL) { 12 | const des = crypto.createDecipheriv( 13 | "des-ecb", 14 | Buffer.from("38346591".getBytes()), 15 | "" 16 | ); 17 | 18 | const enc_url = Buffer.from(encURL, "base64"); 19 | let dec_url = des.update(enc_url, "base64", "utf-8"); 20 | dec_url = dec_url.replace("_96", "_320.mp4"); 21 | 22 | return dec_url; 23 | } 24 | 25 | const musicURL = decryptURL( 26 | "ID2ieOjCrwfgWvL5sXl4B1ImC5QfbsDyt9FOynzequ5IvriekLOmemhlOrNuXjaBrWeCHqaPoit2JaIm/ylxaRw7tS9a8Gtq" 27 | ); 28 | 29 | console.log(musicURL); -------------------------------------------------------------------------------- /flatToNestedObject.js: -------------------------------------------------------------------------------- 1 | const flatToNestedObject = (obj) => { 2 | let nestedObj = {}; 3 | for (let key in obj) { 4 | const value = obj[key]; 5 | if (key.indexOf(".") >= 0) { 6 | const nestedKeys = key.split("."); 7 | const parent = nestedKeys.shift(); 8 | const child = nestedKeys.join("."); 9 | 10 | nestedObj[parent] = { ...nestedObj[parent] }; 11 | nestedObj[parent][child] = value; 12 | nestedObj[parent] = flatToNestedObject(nestedObj[parent]); 13 | } else { 14 | if (typeof value === "object") { 15 | nestedObj[key] = flatToNestedObject(value); 16 | } else { 17 | nestedObj[key] = value; 18 | } 19 | } 20 | } 21 | 22 | return nestedObj; 23 | }; -------------------------------------------------------------------------------- /objectDeepFreezing.js: -------------------------------------------------------------------------------- 1 | const deepFreeze = (anObject) => { 2 | Object.entries(anObject).forEach(([name, value]) => { 3 | if (value && typeof value === 'object') { 4 | deepFreeze(value); 5 | } 6 | }); 7 | return Object.freeze(anObject); 8 | }; 9 | 10 | export { deepFreeze }; 11 | -------------------------------------------------------------------------------- /spaceSplit.js: -------------------------------------------------------------------------------- 1 | const spaceSplit = (str, limit = 30, maxSize = 6) => { 2 | let spaceSplitNchars = []; 3 | 4 | str.split(" ").forEach((word, i) => { 5 | if (i === 0) { 6 | spaceSplitNchars.push(word); 7 | } else { 8 | const lastItem = spaceSplitNchars.pop(); 9 | if (lastItem.length + word.length <= limit) { 10 | spaceSplitNchars.push(lastItem + " " + word); 11 | } else { 12 | spaceSplitNchars.push(lastItem); 13 | spaceSplitNchars.push(word); 14 | } 15 | } 16 | }); 17 | 18 | while (spaceSplitNchars.length < maxSize) { 19 | spaceSplitNchars.push(""); 20 | } 21 | 22 | return spaceSplitNchars; 23 | }; 24 | 25 | const address = "Colby Bernard Ap #285-7193 Ullamcorper Avenue Amesbury HI 93373 (302) 259-2375"; 26 | const limit = 30; 27 | const maxSize = 6; 28 | 29 | console.log(spaceSplit(address)); 30 | --------------------------------------------------------------------------------