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 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)
@kevin-machship
kevin-machship / script.js
Created June 18, 2025 00:55
Converting a date string with a "T" into a specific format using Luxon
const { DateTime } = luxon
const con = { despatchDateTime: '2022-07-20T00:00:00' } // Example only
const dateInDDMMYYYFormat = DateTime.fromISO(con.despatchDateTime).isValid ? DateTime.fromISO(con.despatchDateTime).toFormat('dd/MM/yyyy') : ''
@kevin-machship
kevin-machship / script.js
Last active July 2, 2025 00:04
Handling multiple dgItems.subDgClasses
const getDgClasses = (item) => {
if (!item.isDgItem || !item.dgItems) return ''
const dgClasses = item.dgItems.map(dg => {
let dgClassDisplay = dg.dgClass
if (dg.hasSubRisk && dg.subDgClasses && dg.subDgClasses.length > 0) {
const subDgClassesString = dg.subDgClasses.map(subDg => subDg.class).join(',')
dgClassDisplay += `/${subDgClassesString}`
}