Skip to content

Instantly share code, notes, and snippets.

@cortfritz
Created July 26, 2012 19:44
Show Gist options
  • Save cortfritz/3184083 to your computer and use it in GitHub Desktop.
Save cortfritz/3184083 to your computer and use it in GitHub Desktop.
Recursively display contents of JSON object using bootstrap and jade
mixin prettyDate(uglyDate)
daysAgo = ((((new Date() - uglyDate) / 1000) / 60) / 60) / 24
hours = uglyDate.getHours()
minutes = uglyDate.getMinutes()
ampm = hours >= 12 ? 'pm' : 'am'
hours = hours % 12
hours = hours ? hours : 12
minutes = minutes < 10 ? '0'+minutes : minutes
strTime = hours + ':' + minutes + ' ' + ampm
if daysAgo < 1
| Today at #{strTime}
else if daysAgo <= 7
weekDays = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
day = uglyDate.getDay()
#{weekDays[day + 1]} at #{strTime}
else
#{uglyDate.getMonth() + 1}/#{uglyDate.getDate()}/#{uglyDate.getFullYear()}
mixin showJson(myJsonObject, level)
if typeof level == 'undefined'
- var level = 0
- level++
.well(style='background-color:white')
.badge(style='width:1em;text-align:center;background-color:#cccccc').pull-center #{level}
unless typeof myJsonObject == 'undefined'
each value, key in myJsonObject
if value instanceof Array
p #{key}: (Array[#{value.length}])
if value.length == 0
| &nbsp;(empty)
else
.well(style='background-color:white')
each aValue, aIndex in value
p [#{aIndex}]:
if typeof aValue == 'undefined'
| &nbsp;(empty)
else if aValue instanceof Date
mixin prettyDate(aValue)
else if aValue instanceof Object
| (Object)
mixin showJson(aValue, level)
else
#{aValue}
else if value instanceof Date
p #{key}:&nbsp;
mixin prettyDate(value)
else if value instanceof Object
p #{key}:
if typeof value == 'undefined' || value.length == 0
| &nbsp;(empty)
else
mixin showJson(value, level)
else
p #{key}: #{value}
@cortfritz
Copy link
Author

Update

Improved the layout, removed the 'title' param and made the 'level' param default to zero.

@cortfritz
Copy link
Author

Update

I wasn't handling dates correctly. Wrote a date prettifier (do you know of a better one out there with globalized display???) and updated logic to correctly handle dates.

@cortfritz
Copy link
Author

Update

Simplified the layout. Added an array length at the array header.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment