-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathassertions.coffee
84 lines (65 loc) · 3.01 KB
/
assertions.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
_ = window?._ ? self?._ ? global?._ ? require 'lodash-node'
mori = window?.mori ? self?.mori ? global?.mori ? require 'mori'
class ArityError extends Error
constructor: (args...) ->
@name = 'ArityError'
@message = if args.length is 3
"Expected #{args[0]}..#{args[1]} args, got #{args[2]}"
else args[0]
@stack = (new Error()).stack
class ArgTypeError extends Error
constructor: (message) ->
@name = 'ArgTypeError'
@message = message
@stack = (new Error()).stack
firstFailure = (args, testFn) ->
_.find args, (arg) -> not testFn(arg)
assertions =
numbers: (args...) ->
unexpectedArg = firstFailure(_.flatten(args), (arg) -> typeof arg is 'number')
if unexpectedArg isnt undefined
throw new ArgTypeError "#{unexpectedArg} is not a number"
integers: (args...) ->
unexpectedArg = firstFailure(_.flatten(args), (arg) -> typeof arg is 'number' and arg % 1 is 0)
if unexpectedArg isnt undefined
throw new ArgTypeError "#{unexpectedArg} is not a integer"
associativeOrSet: (args...) ->
if unexpectedArg = firstFailure(args, (arg) -> mori.is_associative(arg) or mori.is_set(arg))
throw new ArgTypeError "#{unexpectedArg} is not a set or an associative collection"
associative: (args...) ->
if unexpectedArg = firstFailure(args, (arg) -> mori.is_associative(arg))
throw new ArgTypeError "#{unexpectedArg} is not an associative collection"
map: (args...) ->
if unexpectedArg = firstFailure(args, (arg) -> mori.is_map(arg))
throw new ArgTypeError "#{unexpectedArg} is not a map"
collection: (args...) ->
unexpectedArg = firstFailure args, (arg) -> mori.is_seqable(arg) or _.isString(arg) or _.isArray(arg)
if unexpectedArg
throw new ArgTypeError "#{unexpectedArg} is not a collection"
sequential: (args...) ->
unexpectedArg = firstFailure args, (arg) -> mori.is_sequential(arg) or _.isString(arg) or _.isArray(arg)
if unexpectedArg
throw new ArgTypeError "#{unexpectedArg} is not a sequential collection"
stack: (args...) ->
unexpectedArg = firstFailure args, (arg) -> mori.is_vector(arg) or mori.is_list(arg)
if unexpectedArg
throw new ArgTypeError "#{unexpectedArg} does not support stack operations"
type_custom: (checkFn) ->
throw new ArgTypeError msg if msg = checkFn()
function: (args...) ->
unexpectedArg = firstFailure args, (arg) ->
typeof arg is 'function' or mori.is_vector(arg) or mori.is_map(arg) or
mori.is_set(arg) or mori.is_keyword(arg)
if unexpectedArg
throw new ArgTypeError "#{unexpectedArg} is not a function"
arity: (expected_min, expected_max, actual) ->
if arguments.length is 2
actual = expected_max
expected_max = expected_min
unless expected_min <= actual <= expected_max
throw new ArityError expected_min, expected_max, actual
arity_custom: (args, checkFn) ->
throw new ArityError msg if msg = checkFn args
module.exports = assertions
self.closerAssertions = assertions if self?
window.closerAssertions = assertions if window?