Skip to content

Instantly share code, notes, and snippets.

View kevin-machship's full-sized avatar

kevin-machship

View GitHub Profile
@kevin-machship
kevin-machship / script.js
Created September 2, 2025 07:46
Function to get a random float
const randomFloat = function (min, max, decimalPlaces = 2) {
return Number((Math.random() * (max - min) + min).toFixed(decimalPlaces));
}
const randomNumbersWithDecimal = randomFloat(1, 1000, 2)
// Sample output:
// 816.27
@kevin-machship
kevin-machship / script.js
Created July 31, 2025 03:09
Function for random alphanumeric characters (A-Z, 0-9)
const randomAlphanumeric = function (length) {
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
let output = ''
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * characters.length)
output += characters[randomIndex]
}
return output
@kevin-machship
kevin-machship / script.js
Created July 31, 2025 02:36
Function for random letters
const randomLetters = function (length) {
const letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
let output = ''
for (let i = 0; i < length; i++) {
const randomIndex = Math.floor(Math.random() * letters.length)
output += letters[randomIndex]
}
return output
@kevin-machship
kevin-machship / script.js
Last active September 2, 2025 08:04
Function for random digits (including zeroes)
const randomDigits = function (length) {
let output = ''
for (let i = 0; i < length; i++) {
if (i === 0 && length > 1) { // Exclude 0
output += Math.floor(Math.random() * 9) + 1
} else {
output += Math.floor(Math.random() * 10)
}
}
@kevin-machship
kevin-machship / script.js
Created July 9, 2025 06:16
Query params
// URL encoding with encodeURIComponent
const objectToQueryString = function (obj) {
return Object.entries(obj)
.map(([key, value]) => `${encodeURIComponent(key)}=${encodeURIComponent(value)}`)
.join('&')
}
const input = {
routes: [
{
@kevin-machship
kevin-machship / script.js
Created July 9, 2025 02:21
Simple UUID V4 generator
const uuidv4 = function() {
return 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'.replace(/[x]/g, function(c) {
let r = Math.random() * 16 | 0
let v = c === 'x' ? r : (r & 0x3 | 0x8)
return v.toString(16)
})
}
// Sample output:
@kevin-machship
kevin-machship / script.js
Created July 2, 2025 04:42
Invoice date dd/M/yyyy hh:mm:ss converted to another format using Luxon
const invoice = { invoiceDate: '22/6/2025 12:00:00 am' }
// Use 'MM' (uppercase) for 2-digit month, or 'M' for 1-digit/2-digit month
const dateFormatted = DateTime.fromFormat(invoice.invoiceDate, 'dd/M/yyyy hh:mm:ss a').toFormat('dd/MM/yyyy')
// Sample output:
// 22/06/2025
@kevin-machship
kevin-machship / script.js
Created July 2, 2025 01:35
Function to handle email and phone fallbacks
const getEmail = (input) => {
return (
input?.consignment?.fromAddress?.email ||
input?.userInformation?.email ||
input?.company?.address?.email ||
null
)
}
const getPhone = (input) => {
@kevin-machship
kevin-machship / script.js
Created June 18, 2025 01:04
Truncate text based on length
const truncateText = (str, maxLength) => {
if (str && str.length > maxLength) {
return str.substring(0, maxLength)
}
return str
}
@kevin-machship
kevin-machship / script.js
Created June 18, 2025 01:00
Set time to local timezone using Luxon
const { DateTime } = luxon
const deliveryTimeZoneName = 'Australia/Sydney' // Example only
const utcNow = DateTime.utc() // eg. 2025-06-04T00:51:38.438Z
// Sets the time of the delivery location based on its timezone
// eg. 2025-06-04T10:51:38.438+10:00
const deliveryLocationDateTime = utcNow.setZone(deliveryTimeZoneName)