Skip to content

Instantly share code, notes, and snippets.

@iamsjn
Last active June 26, 2025 20:10
Show Gist options
  • Save iamsjn/ccb15b3800970efec9a184e1c4f14a9f to your computer and use it in GitHub Desktop.
Save iamsjn/ccb15b3800970efec9a184e1c4f14a9f to your computer and use it in GitHub Desktop.
Handy But Hazy Programming Tasks: Java, Python, JavaScript
## ๐Ÿ” 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 => ({'&': '&amp;', '<': '&lt;', '>': '&gt;', '"': '&quot;', "'": '&#39;'}[c]));
}
```
**Python**
```py
import html
html.escape("<div>") # '&lt;div&gt;'
html.unescape("&lt;div&gt;") # '<div>'
```
**Java**
```java
import org.apache.commons.text.StringEscapeUtils;
StringEscapeUtils.escapeHtml4("<div>");
StringEscapeUtils.unescapeHtml4("&lt;div&gt;");
```
---
## ๐Ÿ“ 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