Last active
June 26, 2025 20:10
-
-
Save iamsjn/ccb15b3800970efec9a184e1c4f14a9f to your computer and use it in GitHub Desktop.
Handy But Hazy Programming Tasks: Java, Python, JavaScript
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
## ๐ String & Text Manipulation | |
### Slugify a string | |
**JavaScript** | |
```js | |
function slugify(str) { | |
return str.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, ''); | |
} | |
``` | |
**Python** | |
```py | |
import re | |
def slugify(text): | |
return re.sub(r'[^a-z0-9-]', '', re.sub(r'\s+', '-', text.lower())) | |
``` | |
**Java** | |
```java | |
public static String slugify(String input) { | |
return input.toLowerCase().replaceAll("\\s+", "-").replaceAll("[^a-z0-9-]", ""); | |
} | |
``` | |
--- | |
### Escape/Unescape HTML | |
**JavaScript** | |
```js | |
function escapeHTML(str) { | |
return str.replace(/[&<>"']/g, c => ({'&': '&', '<': '<', '>': '>', '"': '"', "'": '''}[c])); | |
} | |
``` | |
**Python** | |
```py | |
import html | |
html.escape("<div>") # '<div>' | |
html.unescape("<div>") # '<div>' | |
``` | |
**Java** | |
```java | |
import org.apache.commons.text.StringEscapeUtils; | |
StringEscapeUtils.escapeHtml4("<div>"); | |
StringEscapeUtils.unescapeHtml4("<div>"); | |
``` | |
--- | |
## ๐ File Operations | |
### Read all files in a directory (non-recursive) | |
**JavaScript (Node.js)** | |
```js | |
const fs = require('fs'); | |
fs.readdirSync('./path').forEach(file => console.log(file)); | |
``` | |
**Python** | |
```py | |
import os | |
for f in os.listdir('./path'): | |
print(f) | |
``` | |
**Java** | |
```java | |
import java.io.File; | |
File folder = new File("./path"); | |
for (File file : folder.listFiles()) { | |
System.out.println(file.getName()); | |
} | |
``` | |
--- | |
## ๐งฎ Date & Time | |
### Get current timestamp | |
**JavaScript** | |
```js | |
Date.now(); | |
``` | |
**Python** | |
```py | |
import time | |
time.time() | |
``` | |
**Java** | |
```java | |
System.currentTimeMillis(); | |
``` | |
--- | |
### Add 5 days to current date | |
**JavaScript** | |
```js | |
let d = new Date(); | |
d.setDate(d.getDate() + 5); | |
``` | |
**Python** | |
```py | |
from datetime import datetime, timedelta | |
print(datetime.now() + timedelta(days=5)) | |
``` | |
**Java** | |
```java | |
import java.time.LocalDate; | |
LocalDate.now().plusDays(5); | |
``` | |
--- | |
## ๐งต Async/Concurrency | |
### Sleep for 1 second | |
**JavaScript** | |
```js | |
await new Promise(res => setTimeout(res, 1000)); | |
``` | |
**Python** | |
```py | |
import time | |
time.sleep(1) | |
``` | |
**Java** | |
```java | |
Thread.sleep(1000); // in milliseconds | |
``` | |
--- | |
## ๐ Security & Hashing | |
### Hash a string (SHA-256) | |
**JavaScript (Node.js)** | |
```js | |
const crypto = require('crypto'); | |
crypto.createHash('sha256').update('hello').digest('hex'); | |
``` | |
**Python** | |
```py | |
import hashlib | |
hashlib.sha256(b'hello').hexdigest() | |
``` | |
**Java** | |
```java | |
import java.security.MessageDigest; | |
MessageDigest md = MessageDigest.getInstance("SHA-256"); | |
byte[] hash = md.digest("hello".getBytes(StandardCharsets.UTF_8)); | |
String hex = DatatypeConverter.printHexBinary(hash); | |
``` | |
--- | |
## ๐ Network | |
### Simple HTTP GET | |
**JavaScript (fetch)** | |
```js | |
fetch('https://example.com').then(res => res.text()).then(console.log); | |
``` | |
**Python** | |
```py | |
import requests | |
response = requests.get('https://example.com') | |
print(response.text) | |
``` | |
**Java** | |
```java | |
import java.net.*; | |
import java.io.*; | |
URL url = new URL("https://example.com"); | |
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream())); | |
String inputLine; | |
while ((inputLine = in.readLine()) != null) | |
System.out.println(inputLine); | |
in.close(); | |
``` |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment