Useful Code Snippets & Commands

Some of the contents in this page are not originally from me, every function or command that I found it very useful for me or something that I create by myself, I will put it in here.


JavaScript: Check Vowel in a Sentence
function vowels(str) {
    const matchesVowel = str.match(/[aiueo]/gi);
    return matchesVowel?.length || 0;
}

#javascript

JavaScript: Reverse Integer
function reverseInt(num) {
  if (num < 0) return -reverseIntOpt(-num);
  const reversed = num.toString()
                      .split('')
                      .reverse()
                      .join('');

  return Number(reversed);
}

#javascript

JavaScript: Get Random Item From an Array
const items = [1, 2, 3, 4, 5, 6, 7, 8, 9];

function getRandomItem(arr) {
  return arr[Math.floor(Math.random() * (arr.length))];
}

getRandomItem(items);

#javascript

JavaScript: Get Random Number
function getRandomNumber(minVal, maxVal) {
    return Math.floor(Math.random() * (maxVal - minVal + 1)) + minVal;
}
getRandomNumber(5, 99);

#javascript

JavaScript: Intersection of Arrays
const arr1 = ['a','b','c','d','e'];
const arr2 = ['b','e'];

function intersectionArray(a, b) {
  return a.filter(Set.prototype.has, new Set(b));
}

intersectionArray(arr1, arr2); // ["b", "e"]

#javascript

JavaScript: Union of Arrays
const arr1 = ['a','b','c'];
const arr2 = ['a','c','b','d','e'];

const unionArr = (a, b) => Array.from(new Set([...a, ...b]));

unionArr(arr1, arr2); // ["a", "b", "c", "d", "e"]

#javascript

JavaScript: Remove Specific Object Key from Array of Objects
const allUser = [
  {
    id: 1,
    name: 'John',
    password: '1234'
  },
  {
    id: 2,
    name: 'Doe',
    password: 'abcd'
  }
]

// exclude `password` field
const users = allUser.map(({ password, ...rest }) => rest);
return users;

#javascript

Shell: Docker Stop All Running Containers
docker stop $(docker ps -q)

#shell

Shell: rsync Local Files to Server
rsync -arvz -e 'ssh -p 22' \
source_dir/{dir1,dir2} \
username@ip_address:/destination_dir

#shell

Shell: SSH Tunnel Server Services to Local
  • Host docker IP address: 172.17.0.1
  • Server local IP address: 127.0.0.1
  • Server private IP address: 192.168.10.111
# direct
ssh username@public_ip -L 3306:server_local_ip:3306 -L 27017:server_local_ip:27017

# to docker
ssh username@public_ip -L docker_host_ip:3306:server_local_ip:3306 -L docker_host_ip:27017:server_local_ip:27017

# with key
ssh -i ~/key.pem username@public_ip -L docker_host_ip:3306:server_private_ip:3306

#shell

JavaScript: Chunk an Array
function chunkItems(arr, chunkSize = 4) {
  const chunks = Array.from({ length: Math.ceil(arr.length / chunkSize) }, (_, i) => {
    const start = chunkSize * i;
    return arr.slice(start, start + chunkSize);
  });
}

#javascript

JavaScript: Get Date
const date = new Date();

// change `default` to a specific TimeZone
const day = date.toLocaleString('default', { weekday: 'long' });
const month = date.toLocaleString('default', { month: 'short' });
const year = date.toLocaleString('default', { year: 'numeric' });

#javascript

JavaScript: Object Properties
const user = {id: 1, name: 'John Doe'};
const contact = {phone: 4722880, address: 'Akihabara'}

Object.keys(user); // getting the keys => ["id", "name"]
Object.values(user); // getting the values => [1, "John Doe"]
Object.assign(user, contact); // extend objects => {address: "Akihabara", id: 1, name: "John Doe", phone: 4722880}

#javascript



© Repodev - Muh Ibnu Habil Hanafi