/
the-thing-is.js
168 lines (136 loc) · 4.21 KB
/
the-thing-is.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// The Thing Is
//
// Detailed Object Descriptions
// ...with a sort of _artistic_ quality of code
//
// Usage:
//
// var whatYouExpect = 'number' -- string (least useful)
// var whatYouExpect = ['present', 'number', {greaterThan:0}] -- array (tightest to write, preferred?)
// var whatYouExpect = ['present', 'integer', {greaterThan:0, lessThan:256}] -- combo v2, maintains order, groups comparisons that are chaotic
// var whatYouExpect = [{present:true}, {number:true}, {greaterThan:0}] -- array of objects (very verbose, reliable order, overkill)
//
// if (the(thing).is(whatYouExpect)) {
// ohYeah()
// }
//
// if (the(thing).isnt(whatYouExpect)) {
// throwSomething()
// }
'use strict'
// Dependencies
var is = require('is-too')
// the -- function you care about
function the (thing) {
the.path = []
the.last = {
thing: thing,
error: []
}
return {
is: begin
}
}
function begin (expected) {
var thing = the.last.thing;
// the(thing).is()
// expected is undefined or null -- so check mere presence of a thing
if ( is.not.present(expected) )
return see('present', thing)
return goDeeper(expected, thing)
}
// will recursively see if the(thing).is(whatYouExpect)
function goDeeper (expected, thing) {
// 'present' -- single boolean check
// the(thing).is('integer') // true/false
// the(thing).is('borkborkbork') // throw
if ( is.string(expected) ) {
return see(expected, thing)
}
// ['present', 'number'] -- array of boolean comparisons
// ['present', 'number', {greaterThan:0}, {lessThanorEqualTo:100}] -- separated objects
// ['present', 'number', {greaterThan:0, lessThanorEqualTo:100}] -- combined object
// { foo: ['present', { bar: ['present'] }] }
// the(thing).is(['present', 'integer'])
if ( is.array(expected) ) {
expected.forEach(function(expected){
return goDeeper(expected, thing)
})
return !the.last.error.length
}
// { foo: ['bar'] } -- dictionary describing complex or deep objects
// the(thing).is({
// name: ['present', 'string'],
// address: {
// street: 'string',
// city: 'string',
// state: 'string',
// zip: 'string'
// }
// })
if ( is.plainObject(expected) ) {
if (is.object(thing)) {
// stash the path to branch the tree
var pathStash = the.path.slice()
Object.keys(expected).forEach(function expectedKeys(key, i) {
var nexPectation, nexThing
if (i > 0)
the.path.pop()
the.path.push(key)
if (!thing.hasOwnProperty(key)) {
return see('present', thing[key]);
}
nexPectation = expected[key]
nexThing = thing[key]
if (is.present(nexPectation)) {
if ( is.array(nexPectation) ) {
nexPectation.forEach(function(nexPected){
return goDeeper(nexPected, nexThing)
})
return !the.last.error.length
}
return goDeeper(nexPectation, nexThing)
}
})
the.path = pathStash.slice();
} else {
Object.keys(expected).forEach(function(key, i, arr){
var standard = expected[key];
return see(key, thing, standard);
})
}
}
return !the.last.error.length;
}
// see if the expected thing meets the standard
function see (expected, thing, standard) {
var err = {},
fail = {},
failPath = the.path.join('.')
if ( is.not.string(expected) || is.not.present(is[expected]) )
throw new TypeError('`' + expected + '` isn\'t a valid comparison method.')
// good to go, so go
if ( is[expected](thing, standard) )
return true
// needs to be stored as an object
if ( is.present(standard) )
fail[expected] = standard // eg. {greaterThan:0}
else
fail = expected // eg. 'present'
// needs to be stored as the value of the path
if (the.path.length)
err[failPath] = [fail] // eg. {'foo.bar': ['present']}
else
err = fail // eg. 'present'
// need to group the fails for a key in the same array
var isExisting = the.last.error.some(function something (last) {
if (is.plainObject(last) && failPath in last) {
last[failPath].push(fail)
return true
}
})
if (!isExisting)
the.last.error.push(err)
return false
}
module.exports = the