UNPKG

1.02 kBtext/coffeescriptView Raw
1
2exports.clone = clone = (obj) ->
3 if not obj? or typeof obj isnt 'object'
4 return obj
5
6 if obj instanceof Date
7 return new Date(obj.getTime())
8
9 if obj instanceof RegExp
10 flags = ''
11 flags += 'g' if obj.global?
12 flags += 'i' if obj.ignoreCase?
13 flags += 'm' if obj.multiline?
14 flags += 'y' if obj.sticky?
15 return new RegExp(obj.source, flags)
16
17 newInstance = new obj.constructor()
18
19 for key of obj
20 newInstance[key] = clone obj[key]
21
22 return newInstance
23
24exports.readGraph = (filepath, callback) ->
25 path = require 'path'
26 fs = require 'fs'
27 fbp = require 'fbp'
28
29 ext = path.extname filepath
30 fs.readFile filepath, { encoding: 'utf-8' }, (err, contents) ->
31 return callback err if err
32 try
33 if ext == '.fbp'
34 graph = fbp.parse contents
35 else
36 graph = JSON.parse contents
37 catch e
38 return callback e
39 return callback null, graph
40
41
42# Note: relies on convention
43exports.queueName = (role, port) ->
44 return "#{role}.#{port.toUpperCase()}"