forked from nickforddev/vue-models
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtype.js
44 lines (43 loc) · 838 Bytes
/
type.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
import { path } from 'ramda'
export default class Type {
constructor(value, key) {
this.value = undefined
if (key) this.key = key
const val = this.getValue(value)
if (val) this.set(val)
return this
}
set(value) {
this.in(value)
return this
}
valueOf() {
return this.out()
}
getValue(value) {
const output = this.key
? path([this.key], value) || value
: value
return output
}
in(value) {
this.value = value
return this
}
out() {
return this.value
}
encode() {
// encode data for mongodb
let output
const value = this.valueOf()
// only use mongo encoding if both mongo key and value are set
if (this.key && value) {
output = {}
output[this.key] = value
} else {
output = value
}
return output
}
}