This repository has been archived by the owner on Oct 18, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.coffee
95 lines (89 loc) · 2.25 KB
/
index.coffee
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
library = require './library'
ql = (query, def) ->
return ql.use() if arguments.length is 0
res =
query: -> query
clone: (deep) ->
return ql query, def unless deep
newquery = JSON.parse JSON.stringify query
ql newquery, def
for source in def
for name, _ of source.params
do (name) ->
res[name] = (params) ->
query = ql.params name, params, query
res
for name, _ of source.unary
do (name) ->
res[name] = ->
query = ql.unary name, query
res
for name, _ of source.binary
do (name) ->
res[name] = (right) ->
query = ql.binary name, query, right
res
for name, _ of source.trinary
do (name) ->
res[name] = (left, right) ->
query = ql.trinary name, query, left, right
res
res
ql.params = (name, params, source) ->
res =
__q: name
__p: params
__s: source
if !res.__p?
delete res.__p
res
ql.unary = (name, source) ->
__q: name
__s: source
ql.binary = (name, left, right) ->
__q: name
__l: left
__r: right
ql.trinary = (name, params, left, right) ->
__q: name
__p: params
__l: left
__r: right
applyglobals = (res, def) ->
for name, _ of def.params
do (name) -> res[name] = (params, source) ->
ql.params name, params, source
for name, _ of def.unary
do (name) -> res[name] = (source) ->
ql.unary name, source
for name, _ of def.binary
do (name) -> res[name] = (left, right) ->
ql.binary name, left, right
for name, _ of def.trinary
do (name) -> res[name] = (params, left, right) ->
ql.trinary name, params, left, right
# attach methods against a query in progress
ql.use = (def) ->
res = (query) ->
ql query, res.providers
for name, fn of ql
res[name] = fn
res.providers = []
res.use = (def) ->
if typeof(def) is 'string'
if !library[def]?
throw new Error "#{def} not found in library"
def = library[def]
if def instanceof Array
res.use d for d in def
return res
if def?
applyglobals res, def
res.providers.push def
res
res
# attach methods against ql directly
res.use library.builtin
res.use def
applyglobals ql, library.builtin
module.exports = ql