mirror of
https://github.com/atlanticbiomedical/biomedjs.git
synced 2025-07-02 00:47:26 -04:00
Added node-modules
This commit is contained in:
40
node_modules/piler/lib/serialize.coffee
generated
vendored
Normal file
40
node_modules/piler/lib/serialize.coffee
generated
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
|
||||
# Remove last comma from string
|
||||
removeTrailingComma = (s) ->
|
||||
s.trim().replace(/,$/, "")
|
||||
|
||||
# Map of functions that can convert various Javascript objects to strings.
|
||||
types =
|
||||
|
||||
function: (fn) -> "#{ fn }"
|
||||
string: (s) -> JSON.stringify s # Knows how to correctly serialize strings to String literals
|
||||
number: (n) -> n.toString()
|
||||
boolean: (n) -> n.toString()
|
||||
|
||||
object: (obj) ->
|
||||
|
||||
# typeof reports array as object
|
||||
return this._array obj if Array.isArray obj
|
||||
|
||||
code = "{"
|
||||
code += "\"#{ k }\": #{ codeFrom v }," for k, v of obj
|
||||
removeTrailingComma(code) + "}"
|
||||
|
||||
_array: (array) ->
|
||||
code = "["
|
||||
code += " #{ codeFrom v }," for v in array
|
||||
removeTrailingComma(code) + "]"
|
||||
|
||||
|
||||
# Generates code string from given object. Works for numbers, strings, regexes
|
||||
# and even functions. Does not handle circular references.
|
||||
exports.stringify = codeFrom = (obj) ->
|
||||
types[typeof obj]?(obj)
|
||||
|
||||
|
||||
if require.main is module
|
||||
console.log exports.stringify
|
||||
foo: 1
|
||||
bar:
|
||||
lol: ":D"
|
||||
hah: 2
|
Reference in New Issue
Block a user