-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ls
110 lines (99 loc) · 3.08 KB
/
index.ls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
require! {
co
unthenify
}
export add_noerr = (callback) ->
return (cb) ->
callback (res) ->
cb(null, res)
export yfy = (f) ->
return (...functionArguments) ->
self = this
return new Promise (resolve, reject) ->
callbackFunction = (...args) ->
return resolve(args[0])
functionArguments.push(callbackFunction)
f.apply(self, functionArguments)
export yfy_node = (f) ->
return (...functionArguments) ->
self = this
return new Promise (resolve, reject) ->
callbackFunction = (...args) ->
err = args[0]
if err
return reject(err)
return resolve(args[1])
functionArguments.push(callbackFunction)
f.apply(self, functionArguments)
export yfy_multi = (f) ->
return (...functionArguments) ->
self = this
return new Promise (resolve, reject) ->
callbackFunction = (...args) ->
return resolve(args)
functionArguments.push(callbackFunction)
f.apply(self, functionArguments)
export yfy_multi_node = (f) ->
return (...functionArguments) ->
self = this
return new Promise (resolve, reject) ->
callbackFunction = (...args) ->
err = args[0]
if err
return reject(err)
return resolve(args.slice(1))
functionArguments.push(callbackFunction)
f.apply(self, functionArguments)
export cfy = (f, options) ->
num_args = f.length
if options?
if options.varargs?
return cfy_varargs(f)
if options.num_args?
num_args = options.num_args
wrapped = co.wrap(f)
return (...args, callback) ->
if args.length == num_args and typeof(callback) == 'function'
# the callback was passed, call the function immediately
return wrapped.bind(this)(...args).then(callback)
else
# return a promise
return wrapped.bind(this)(...args, callback)
cfy_varargs = (f) ->
wrapped = co.wrap(f)
return (...args, callback) ->
if typeof(callback) == 'function'
# the callback was passed, call the function immediately
return wrapped.bind(this)(...args).then(callback)
else
# return a promise
return wrapped.bind(this)(...args, callback)
export cfy_node = (f, options) ->
num_args = f.length
if options?
if options.varargs?
return cfy_varargs_node(f)
if options.num_args?
num_args = options.num_args
wrapped = co.wrap(f)
wrapped_cb = unthenify(wrapped)
return (...args, callback) ->
if args.length == num_args and typeof(callback) == 'function'
# the callback was passed, call the function immediately
return wrapped_cb.bind(this)(...args, callback)
else
# return a promise
return wrapped.bind(this)(...args, callback)
cfy_varargs_node = (f) ->
wrapped = co.wrap(f)
wrapped_cb = unthenify(wrapped)
return (...args, callback) ->
if typeof(callback) == 'function'
# the callback was passed, call the function immediately
return wrapped_cb.bind(this)(...args, callback)
else
# return a promise
return wrapped.bind(this)(...args, callback)
for k,v of module.exports
cfy[k] = v
module.exports = cfy