UNPKG

3.1 kBPlain TextView Raw
1function isArrayLike(a) {
2 return a && typeof a.length === 'number'
3}
4
5function toStringArray(arr) {
6 return (
7 'array with ' +
8 arr.length +
9 ' items.\n[' +
10 arr.map(toString).join(',') +
11 ']\n'
12 )
13}
14
15function isPrimitive(arg) {
16 return (
17 typeof arg === 'string' ||
18 typeof arg === 'number' ||
19 typeof arg === 'boolean'
20 )
21}
22
23function isError(e) {
24 return e instanceof Error
25}
26
27/*
28 custom JSON.stringify replacer to make sure we do not
29 hide properties that have value "undefined"
30 var o = {
31 foo: 42,
32 bar: undefined
33 }
34
35 standard JSON.stringify returns
36 '{"foo": 42}'
37 this replacer returns
38 '{"foo": 42, "bar": null}'
39*/
40function replacer(key, value) {
41 if (value === undefined) {
42 return null
43 }
44 return value
45}
46
47function toString(arg, k) {
48 if (arg === null) {
49 return 'null'
50 }
51 if (arg === undefined) {
52 return 'undefined'
53 }
54 if (isPrimitive(arg)) {
55 return JSON.stringify(arg)
56 }
57 if (arg instanceof Error) {
58 return arg.name + ' ' + arg.message
59 }
60
61 if (Array.isArray(arg)) {
62 return toStringArray(arg)
63 }
64 if (isArrayLike(arg)) {
65 return toStringArray(Array.prototype.slice.call(arg, 0))
66 }
67 var argString
68 try {
69 argString = JSON.stringify(arg, replacer, 2)
70 } catch (err) {
71 argString =
72 '{ cannot stringify arg ' + k + ', it has type "' + typeof arg + '"'
73 if (typeof arg === 'object') {
74 argString += ' with keys ' + Object.keys(arg).join(', ') + ' }'
75 } else {
76 argString += ' }'
77 }
78 }
79 return argString
80}
81
82function endsWithNewLine(s) {
83 return /\n$/.test(s)
84}
85
86function formMessage(args) {
87 var msg = args.reduce(function(total, arg, k) {
88 if (k && !endsWithNewLine(total)) {
89 total += ' '
90 }
91 if (typeof arg === 'string') {
92 return total + arg
93 }
94 if (typeof arg === 'function') {
95 var fnResult
96 try {
97 fnResult = arg()
98 } catch (err) {
99 // ignore the error
100 fnResult = '[function ' + arg.name + ' threw error!]'
101 }
102 return total + fnResult
103 }
104 var argString = toString(
105 arg,
106 k
107 )
108 return total + argString
109 }, '')
110 return msg
111}
112
113function lazyAssLogic(condition: any, ...args: any[]) {
114 if (isError(condition)) {
115 return condition
116 }
117
118 var fn = typeof condition === 'function' ? condition : null
119
120 if (fn) {
121 condition = fn()
122 }
123 if (!condition) {
124 // var args = [].slice.call(arguments, 1);
125 if (fn) {
126 args.unshift(fn.toString())
127 }
128 return new Error(formMessage(args))
129 }
130}
131
132interface LazyAss {
133 (condition: any, ...args: any[]): void
134 async: (condition: any, ...args: any[]) => void
135}
136
137export const lazyAss: LazyAss = <LazyAss>(
138 function lazyAss(condition: any, ...args: any[]) {
139 var err = lazyAssLogic(condition, ...args)
140 if (err) {
141 throw err
142 }
143 }
144)
145
146export const lazyAssync = function lazyAssync(condition: any, ...args: any[]) {
147 var err = lazyAssLogic(condition, ...args)
148 if (err) {
149 setTimeout(function() {
150 throw err
151 }, 0)
152 }
153}
154
155lazyAss.async = lazyAssync
156
157export default lazyAss