This repository was archived by the owner on Aug 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 605
Expand file tree
/
Copy pathutil.coffee
More file actions
executable file
·91 lines (68 loc) · 2.28 KB
/
util.coffee
File metadata and controls
executable file
·91 lines (68 loc) · 2.28 KB
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
# coffeelint: disable=cyclomatic_complexity
# lib
ObjectId = require './objectid'
convertComplexSelector = (selector, konstructor, type, parents) ->
casted = {}
for own key, node of selector
casted[key] = switch key
when '$in', '$nin', '$all'
((konstructor.castValue i, type) for i in node)
when '$gt', '$gte', '$lt', '$lte', '$ne'
konstructor.castValue node, type
when '$or', '$nor', '$and', '$elemMatch', '$not'
if node instanceof RegExp then node
else castAll node, konstructor, parents
when '$not'
if type is String then node
else castAll node, konstructor, parents
else
node # leaving it untouched; moving on.
return casted
castAll = (selector, konstructor, parents = []) ->
casted = {}
for own key, node of selector
path = parents.concat key
pathName = path.join '.'
nodeType = konstructor.types?[pathName]
nodeType ?= ObjectId if pathName is '_id'
if nodeType? and node? and 'object' is typeof node
stop = no
for op in Object.keys node when /^\$/.test op
casted[key] = convertComplexSelector node, konstructor, nodeType, path
stop = yes
break
continue if stop
casted[key] = switch
when key is '_id' and
'string' is typeof node and
not nodeType? and
not konstructor.dontAutoCastId
new ObjectId node
when nodeType is String and node instanceof RegExp
node
when not parents.length and konstructor.castValue? and nodeType?
konstructor.castValue node, nodeType
else node
return casted
module.exports =
asynchronizeOwnMethods:(ofObject) ->
result = {}
Object.keys(ofObject).forEach (key) ->
if 'function' is typeof fn = ofObject[key]
result[key] = (rest..., callback) ->
callback fn rest...
result
getUnusedKey: do ->
getCandidate = (key, tryCount) -> key + tryCount
(keys, key) ->
candidate = key
tryCount = 0
while candidate in keys
candidate = getCandidate key, tryCount++
candidate
partition: (list, fn) ->
result = [[], []]
result[Number not fn item].push item for item in list
result
convertComplexSelector: convertComplexSelector
castAll: castAll