Created
March 20, 2012 15:24
-
-
Save bmc/2136966 to your computer and use it in GitHub Desktop.
This should be standard in CoffeeScript and Javascript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Get a printable version of all properties in an object. Ignores function | |
| # properties. DO NOT MAKE THIS A PROPERTY OF Object! | |
| window.objectToString = (obj) -> | |
| t = typeof obj | |
| if (t is "number") or (t is "boolean") | |
| "#{obj}" | |
| else if window.isArray(obj) | |
| window._arrayToString(obj) | |
| else if t is "string" | |
| '"' + obj + '"' | |
| else if t is "function" | |
| null | |
| else if t is "object" | |
| "#{window._propertiesToString(obj)}" | |
| else | |
| toString.call(obj) | |
| window._propertiesToString = (obj) -> | |
| a = [] | |
| for own k, v of obj | |
| if v? | |
| s = window.objectToString(v) | |
| a.push '"' + k + '": ' + s if s? | |
| "{#{a.join(', ')}}" | |
| window._arrayToString = (a) -> | |
| r = [] | |
| for i in a | |
| if typeof i is 'string' | |
| r.push '"' + i + '"' | |
| else if 'function' is typeof i | |
| # skip | |
| else | |
| r.push i | |
| if r.length is 0 then '[]' else "[" + r.join(", ") + "]" | |
| # Clone of underscore's isArray function, so we don't have to depend on | |
| # underscore.js | |
| window.isArray = Array.isArray || (obj) -> toString.call(obj) == '[object Array]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment