/
AppSrv.Helper.js
114 lines (96 loc) · 3.25 KB
/
AppSrv.Helper.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
/*
Copyright 2017 apHarmony
This file is part of jsHarmony.
jsHarmony is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
jsHarmony is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this package. If not, see <http://www.gnu.org/licenses/>.
*/
var _ = require('lodash');
var ejsext = require('./lib/ejsext.js');
module.exports = exports = {};
exports.getFieldNames = function (req, fields, perm, fcond) {
return _.map(exports.getFields(req, fields, perm, fcond), 'name');
};
exports.getFields = function (req, fields, perm, fcond) {
var rslt = [];
_.each(fields, function (field) {
if (ejsext.hasFieldAction(req, field, perm)) {
if (('type' in field) && (field.type == 'file')) return;
if (!('type' in field)) return;
if(fcond && (!fcond(field))) return;
rslt.push(field);
}
});
return rslt;
};
exports.getFieldNamesWithProp = function (fields, prop) {
return _.map(exports.getFieldsWithProp(fields, prop), 'name');
};
exports.getFieldsWithProp = function (fields, prop) {
var rslt = [];
_.each(fields, function (field) {
if ((prop in field) && (field[prop])) {
if (('type' in field) && (field.type == 'file')) return;
rslt.push(field);
}
});
return rslt;
};
exports.getFieldsByName = function (fields, fieldnames, fcond) {
var rslt = [];
if(!fieldnames) return rslt;
var fieldnames_missing = fieldnames.slice();
for(var i=0;i<fields.length;i++){
var field = fields[i];
if (_.includes(fieldnames, field.name)){
for(var j=0;j<fieldnames_missing.length;j++){
if(fieldnames_missing[j]==field.name){
fieldnames_missing.splice(j,1);
j--;
}
}
if(fcond && !fcond(field)) continue;
rslt.push(field);
}
}
if(fieldnames_missing.length > 0){ this.jsh.Log.warning('Fields not found: ' + fieldnames_missing.join(', ')); }
return rslt;
};
//Static function
exports.getFieldByName = function (fields, fieldname) {
for (var i = 0; i < fields.length; i++) {
if (fields[i].name == fieldname) return fields[i];
}
return;
};
exports.getKeyNames = function (fields) {
return _.map(exports.getKeys(fields), 'name');
};
exports.getKeys = function (fields) {
return _.filter(fields, function (field) {
if (field.key) return true;
return false;
});
};
exports.getEncryptedFields = function (req, fields, perm) {
var rslt = [];
_.each(fields, function (field) {
if (('type' in field) && ((field.type == 'encascii') || (field.type == 'hash')) && ejsext.hasFieldAction(req, field, perm)) rslt.push(field);
});
return rslt;
};
exports.getFileFieldNames = function (req, fields, perm) {
var rslt = [];
_.each(fields, function (field) {
if (('type' in field) && (field.type == 'file') && ejsext.hasFieldAction(req, field, perm)) rslt.push(field.name);
});
return rslt;
};
return module.exports;