Skip to content

Instantly share code, notes, and snippets.

View 9point6's full-sized avatar
🤔
Who actually reads this?

John Sanderson 9point6

🤔
Who actually reads this?
View GitHub Profile
@9point6
9point6 / formatnum.coffee
Created October 16, 2013 09:28
Formats a number with commas, and can shorten large numbers when surpassing a million. Implemented on number prototype,
Number.prototype.formatNum = ( decimal = true, millionify = 1 ) ->
if @ > 999999 && millionify
"#{( Math.round( @ / 100000 ) / 10 ).formatNum decimal, millionify - 1}m"
else
[n,d] = "#{@}".split '.'
reg = /(\d+)(\d{3})/
while reg.test n
n = n.replace reg, '$1,$2'
n + if d? and decimal then ( if not decimal then ".#{d.substr( 0 , decimal )}" else ".#{d}" ) else ''
@9point6
9point6 / ipsort.coffee
Last active September 3, 2021 13:34
JavaScript & CoffeeScript function for sorting IP addresses
ipSort = ( ipAddressArray ) ->
ipAddressArray.sort ( a, b ) ->
a = a.split '.'
b = b.split '.'
for i of a
if ( a[i] = parseInt a[i] ) < ( b[i] = parseInt b[i] )
return -1
else if a[i] > b[i]
return 1
return 0