-
Notifications
You must be signed in to change notification settings - Fork 5
/
helpers.js
139 lines (108 loc) · 3.08 KB
/
helpers.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
'use strict'
const uid = require('nedb/lib/customUtils').uid
const bufferEncoding = 'base64'
const idKey = '_id'
module.exports = { idKey, inputRecord, outputRecord, mapValues, castValue }
// Cast and assign values per field.
function inputRecord (type, record) {
const recordTypes = this.recordTypes
const primaryKey = this.keys.primary
const typeKey = this.keys.type
const isArrayKey = this.keys.isArray
const clone = {}
const fields = recordTypes[type]
// ID business.
const id = record[primaryKey]
clone[idKey] = id ? id : generateId()
for (const field in record) {
if (field === primaryKey) continue
clone[field] = record[field]
}
for (const field of Object.getOwnPropertyNames(fields)) {
const fieldType = fields[field][typeKey]
const fieldIsArray = fields[field][isArrayKey]
if (!(field in record)) {
clone[field] = fieldIsArray ? [] : null
continue
}
// NeDB lacks native support for buffer types.
if (fieldType &&
(fieldType === Buffer || fieldType.prototype.constructor === Buffer) &&
record[field]) {
clone[field] = fieldIsArray ?
record[field].map(toString) : toString(record[field])
continue
}
}
return clone
}
function outputRecord (type, record) {
const recordTypes = this.recordTypes
const primaryKey = this.keys.primary
const typeKey = this.keys.type
const isArrayKey = this.keys.isArray
const denormalizedInverseKey = this.keys.denormalizedInverse
const clone = {}
const fields = recordTypes[type]
// ID business.
clone[primaryKey] = record[idKey]
for (const field in record) {
if (!(field in fields)) continue
const value = record[field]
const fieldType = fields[field][typeKey]
// NeDB lacks native support for buffer types.
if (fieldType &&
(fieldType === Buffer || fieldType.prototype.constructor === Buffer) &&
record[field]) {
clone[field] = fields[field][isArrayKey] ?
value.map(toBuffer) : toBuffer(value)
continue
}
// Do not enumerate denormalized fields.
if (fields[field][denormalizedInverseKey]) {
Object.defineProperty(clone, field, {
configurable: true, writable: true, value
})
continue
}
clone[field] = value
}
return clone
}
// Buffer to string casting, and vice versa.
function toString (buffer) {
return buffer.toString(bufferEncoding)
}
function toBuffer (string) {
return new Buffer(string, bufferEncoding)
}
/**
* Immutable mapping on an object.
*
* @param {Object} object
* @param {Function} map should return the first argument, which is the value
* @return {Object}
*/
function mapValues (object, map) {
return Object.keys(object).reduce((clone, key) =>
Object.assign(clone, { [key]: map(object[key], key) }), {})
}
/**
* Cast non-native types.
*
* @param {*} value
* @return {*}
*/
function castValue (value) {
if (Buffer.isBuffer(value))
return value.toString(bufferEncoding)
return value
}
/**
* Defer to using the uid function provided by NEDB.
*
* @return {String}
*/
function generateId () {
return uid(16)
}