-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
79 lines (64 loc) · 1.6 KB
/
index.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
/*jslint node: true */
"use strict";
var u = require("underscore"),
uuid = require("uuid");
/**
* Random uuid from id module is as secure as we get right now.
*
* @returns
*/
function secureId() {
return uuid.v4().toLowerCase();
}
/**
* Introduction prepares a new entity instance for interop.
*
* @param idgen id generator function. defaults to secureId.
*
* @returns promise function for introducing new entity instances.
*/
function createIntroducer(idgen) {
idgen = idgen || secureId;
function introduceOne(target) {
target.id = idgen();
return target;
}
return function (target) {
return u.isArray(target)
? u.map(target, introduceOne)
: introduceOne(target);
};
}
/**
* Cleaning removes persistence-related keys from an object.
*
* @param first
* @returns {Function}
*/
function createCleaner(keys) {
keys = u.isArray(keys) ? keys : [].slice.call(arguments);
function cleanOne(target) {
return u.omit(target, keys);
}
return function (target) {
return u.isArray(target)
? u.map(target, cleanOne)
: cleanOne(target);
};
}
/*
* create an introducer from an id generator.
*/
exports.createIntroducer = createIntroducer;
/*
* default introducer.
*/
exports.introduce = createIntroducer();
/*
* create a cleaner from a list of keys to remove.
*/
exports.createCleaner = createCleaner;
/*
* cleaner for mongo documents
*/
exports.moclean = createCleaner("_id");