├── dart ├── readme.md └── essentials.dart ├── java ├── readme.md └── Essentials.java ├── php ├── README.md └── essentials.php ├── .gitignore ├── python └── essentials.py ├── typescript └── essentials.ts ├── README.md ├── css └── essentials.css └── javascript ├── readme.md └── essentials.js /dart/readme.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /java/readme.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /php/README.md: -------------------------------------------------------------------------------- 1 | ## PHP 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### VS Code ### 2 | .vscode/ 3 | 4 | ### IntelliJ IDEA ### 5 | .idea 6 | *.iws 7 | *.iml 8 | *.ipr -------------------------------------------------------------------------------- /python/essentials.py: -------------------------------------------------------------------------------- 1 | def param_decor(n): 2 | """ 3 | Sample Parameterized Decorator 4 | """ 5 | def wrapper(func): 6 | 7 | def inner_function(): 8 | func(n) 9 | 10 | return inner_function 11 | 12 | return wrapper 13 | 14 | 15 | """ 16 | Usecase: 17 | @param_decor(d) 18 | def aFunction(): 19 | pass 20 | """ 21 | -------------------------------------------------------------------------------- /typescript/essentials.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Generates random number. 3 | * * @returns {number} 4 | */ 5 | function generateRandomNumber(): Number { 6 | const date: Date = new Date(); 7 | 8 | return parseInt( 9 | date.getFullYear().toString() + 10 | date.getMonth().toString() + 11 | date.getDay().toString() + 12 | date.getHours().toString() + 13 | date.getMinutes().toString() + 14 | date.getSeconds().toString() + 15 | date.getMilliseconds().toString() 16 | ); 17 | } 18 | 19 | -------------------------------------------------------------------------------- /java/Essentials.java: -------------------------------------------------------------------------------- 1 | package Essentials.java; 2 | 3 | import java.io.IOException; 4 | import java.nio.file.Files; 5 | import java.nio.file.Path; 6 | 7 | /** 8 | * FileReader class provides method for reading files in modern Java. 9 | */ 10 | public class FileReader { 11 | public void printLineByLine(String pathToFile) throws IOException { 12 | Files.lines(Path.of(pathToFile)).forEach(System.out::println); 13 | } 14 | } 15 | 16 | /** Usage of FileReader */ 17 | class Main { 18 | public static void main(String[] args) throws IOException { 19 | new FileReader().printLineByLine("Essentials/javascript/essentials.js"); 20 | } 21 | } -------------------------------------------------------------------------------- /php/essentials.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | Navigator.push( 32 | context, MaterialPageRoute(builder: (context) => route))); 33 | } 34 | 35 | //while adding content, call this fxn 36 | addPadding(double topValue) { 37 | return Padding(padding: EdgeInsets.only(top: topValue)); 38 | } 39 | 40 | //Pass the title, and message to showAlertDialog and alert Box is yours 41 | 42 | showAlertDialog(BuildContext context, String title, String message) { 43 | // set up the button 44 | Widget okButton = FlatButton( 45 | child: Text("OK"), 46 | onPressed: () => Navigator.pop(context), 47 | ); 48 | 49 | // set up the AlertDialog 50 | AlertDialog alert = AlertDialog( 51 | title: Text(title), 52 | content: Text(message), 53 | actions: [ 54 | okButton, 55 | ], 56 | ); 57 | 58 | // show the dialog 59 | showDialog( 60 | context: context, 61 | builder: (BuildContext context) { 62 | return alert; 63 | }, 64 | ); 65 | } 66 | 67 | //pass list of string in order to validate whether the given field is empty or not 68 | // useful else than checking field one by one. 69 | validateFields(List fields) { 70 | for (int i = 0; i < fields.length; i++) { 71 | if (fields[i].isEmpty) return false; 72 | } 73 | 74 | return true; 75 | } 76 | 77 | String removeAllHtmlTags(String htmlText) { 78 | RegExp exp = RegExp(r"<[^>]*>", multiLine: true, caseSensitive: true); 79 | 80 | return htmlText.replaceAll(exp, ''); 81 | } 82 | 83 | showToast(String message) { 84 | return Fluttertoast.showToast( 85 | msg: message, 86 | toastLength: Toast.LENGTH_SHORT, 87 | gravity: ToastGravity.BOTTOM, 88 | timeInSecForIosWeb: 1, 89 | backgroundColor: PRIMARY_COLOR, 90 | textColor: Colors.white, 91 | fontSize: 16.0); 92 | } 93 | 94 | //convert R,G,B Color to Flutter Material Color 95 | 96 | //usuage : 97 | // MaterialColor PRIMARY_COLOR = MaterialColor(0xFF997D90, convertToMaterialColor(153, 125, 144)); 98 | convertToMaterialColor(int r, int g, int b) { 99 | Map color = { 100 | 50: Color.fromRGBO(r, g, b, .1), 101 | 100: Color.fromRGBO(r, g, b, .2), 102 | 200: Color.fromRGBO(r, g, b, .3), 103 | 300: Color.fromRGBO(r, g, b, .4), 104 | 400: Color.fromRGBO(r, g, b, .5), 105 | 500: Color.fromRGBO(r, g, b, .6), 106 | 600: Color.fromRGBO(r, g, b, .7), 107 | 700: Color.fromRGBO(r, g, b, .8), 108 | 800: Color.fromRGBO(r, g, b, .9), 109 | 900: Color.fromRGBO(r, g, b, 1), 110 | }; 111 | 112 | return color; 113 | } 114 | -------------------------------------------------------------------------------- /javascript/essentials.js: -------------------------------------------------------------------------------- 1 | /* 2 | Essentials.js is simple js code which contain all basic and essential javascript 3 | functions for the website development. 4 | AUTHOR : Saroj Dahal / @isarojdahal 5 | VERSION: 1.0.0 6 | */ 7 | 8 | function convertDateToSQLFormat(paramDate) { 9 | // if date is not given, converts current date to SQL Format. 10 | // else the given data is convert to SQL format. 11 | if (paramDate != null) 12 | return paramDate.toISOString().slice(0, 19).replace("T", " "); 13 | return new Date().toISOString().slice(0, 19).replace("T", " "); 14 | } 15 | 16 | function copyIt(theField) { 17 | var selectedText = document.selection; 18 | if (selectedText.type == "Text") { 19 | var newRange = selectedText.createRange(); 20 | theField.focus(); 21 | theField.value = newRange.text; 22 | } else alert("select a text in the page and then press this button"); 23 | } 24 | 25 | //Function for cookies 26 | 27 | function setCookie(key, value, expiry) { 28 | var expires = new Date(); 29 | expires.setTime(expires.getTime() + expiry * 24 * 60 * 60 * 1000); 30 | document.cookie = key + "=" + value + ";expires=" + expires.toUTCString(); 31 | } 32 | 33 | function getCookie(key) { 34 | var keyValue = document.cookie.match("(^|;) ?" + key + "=([^;]*)(;|$)"); 35 | return keyValue ? keyValue[2] : null; 36 | } 37 | 38 | function eraseCookie(key) { 39 | var keyValue = getCookie(key); 40 | setCookie(key, keyValue, "-1"); 41 | } 42 | 43 | //Network Functions 44 | 45 | function isOnline() { 46 | //Checks Internet Connection 47 | 48 | return window.navigator.onLine ? true : false; 49 | } 50 | 51 | //Gets Vistors information ( like country,network,long-lat 52 | function lookupInfo() { 53 | var xhttp = new XMLHttpRequest(); 54 | xhttp.open("GET", "http://ip-api.com/json", true); 55 | 56 | xhttp.onload = function () { 57 | if (this.readyState == 4 && this.status == 200) { 58 | var data = JSON.parse(xhttp.responseText); 59 | // console.log(data["country"]); // Gets Country Name 60 | } 61 | }; 62 | 63 | xhttp.send(); 64 | } 65 | 66 | //Returns Formatted date 67 | 68 | function showDate() { 69 | var d = new Date(); 70 | var curr_date = d.getDate(); 71 | var curr_month = d.getMonth() + 1; //months are zero based 72 | var curr_year = d.getFullYear(); 73 | return curr_date + "-" + curr_month + "-" + curr_year; 74 | } 75 | 76 | //Add Bookmark 77 | 78 | function addBookmark(url, siteName) { 79 | window.external.AddFavorite(url, siteName); 80 | } 81 | 82 | //Redirect to given URL with certain delay 83 | 84 | function redirectTo(url, seconds) { 85 | setTimeout(`window.location.href ='${url}'`, seconds * 1000); 86 | } 87 | 88 | //Playing With URL (Might be needed sometimes when URL needs to be used and played with using javascript) 89 | 90 | //Remove a particular parameter from a URL 91 | 92 | function removeURLParameter(url, parameter) { 93 | url = url.split("#")[0]; 94 | var urlparts = url.split("?"); 95 | if (urlparts.length >= 2) { 96 | var prefix = encodeURIComponent(parameter) + "="; 97 | var pars = urlparts[1].split(/[&;]/g); 98 | for (var i = pars.length; i-- > 0; ) { 99 | if (pars[i].lastIndexOf(prefix, 0) !== -1) { 100 | pars.splice(i, 1); 101 | } 102 | } 103 | url = urlparts[0] + "?" + pars.join("&"); 104 | return url; 105 | } else { 106 | return url; 107 | } 108 | } 109 | 110 | //Add a particular or a list of parameters to a URL 111 | 112 | function addParams(url, data) { 113 | if (!$.isEmptyObject(data)) { 114 | url += (url.indexOf("?") >= 0 ? "&" : "?") + $.param(data); 115 | } 116 | 117 | return url; 118 | } 119 | 120 | //Get a value of parameter from URL 121 | 122 | function getUrlParameter(sParam) { 123 | var sPageURL = window.location.search.substring(1), 124 | sURLVariables = sPageURL.split("&"), 125 | sParameterName, 126 | i; 127 | 128 | for (i = 0; i < sURLVariables.length; i++) { 129 | sParameterName = sURLVariables[i].split("="); 130 | 131 | if (sParameterName[0] === sParam) { 132 | return sParameterName[1] === undefined 133 | ? true 134 | : decodeURIComponent(sParameterName[1]); 135 | } 136 | } 137 | } 138 | 139 | /** 140 | * function to check the object is empty 141 | * @param {object} obj - Required object to check 142 | */ 143 | function isObjectEmpty(obj = {}) { 144 | return Object.keys(obj).length === 0 145 | } 146 | 147 | // Author:Pramesh Karki(@PrameshKarki) 148 | // www.karkipramesh.com.np 149 | 150 | // Method to automatic scroll to top 151 | const goToTop=()=>window.scrollTo(0,0); 152 | 153 | // Method to reverse string 154 | const reverseString=str=>str.split('').reverse().join(''); 155 | 156 | // Method to get Selected Text 157 | const getSelectedString=()=>window.getSelection().toString(); 158 | 159 | // Method to detect dark mode 160 | const isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches 161 | 162 | // Method to remove duplicate from array 163 | const removeDuplicates = (arr) => [...new Set(arr)]; 164 | 165 | // Method to find difference between two dates 166 | const diffBetweenTwoDates=(date1, date2) => Math.ceil(Math.abs(date1.getTime() - date2.getTime()) / 86400000); 167 | 168 | // Method to copy to the clipboard 169 | const copyToClipboard = (text) => navigator.clipboard.writeText(text); 170 | 171 | // Author:Mishan Khatri(@Mishankhatri) 172 | // www.mishankhatri.com.np 173 | 174 | /** 175 | * Zips any number of indexable iterables arrays/string. 176 | * It will always zip() the smallest iterable until it is exhausted. 177 | * @param {...Array} arrays 178 | * @param {...String} strings 179 | * @returns { Array } 180 | * usecase: 181 | * zip('abc','efg') --> [['a','e'],['b','f'],['c','g']] 182 | * zip([1,2,3,4],[5,6,7,8],['a','b','c']) --> [[1,5,'a'],[2,6,'b'],[3,7,'c']] 183 | */ 184 | function zip(...args){ 185 | let argsLen = args.length 186 | if (argsLen < 2 && args !== null){ 187 | console.error(`Arguments Error:zip() takes at least two arguments of indexable iterables,given ${argsLen}.`) 188 | return 189 | } 190 | let zippedArr = [] 191 | let minLen = args[0].length 192 | args.forEach((e)=>{ 193 | minLen = Math.min(minLen,e.length) 194 | }) 195 | for (let i = 0; i < minLen ; i++){ 196 | let temp=[]; 197 | for ( j = 0 ; j < argsLen;j++){ 198 | temp.push(args[j][i]) 199 | } 200 | zippedArr.push(temp) 201 | } 202 | return zippedArr 203 | } --------------------------------------------------------------------------------