2016-03-30 19:08:06 +09:00
|
|
|
|
// add support for nested properties
|
2016-06-13 16:05:46 +09:00
|
|
|
|
const deepValue = function(obj, path){
|
2016-03-30 19:08:06 +09:00
|
|
|
|
for (var i=0, path=path.split('.'), len=path.length; i<len; i++){
|
|
|
|
|
obj = obj[path[i]];
|
|
|
|
|
};
|
|
|
|
|
return obj;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// see http://stackoverflow.com/questions/19098797/fastest-way-to-flatten-un-flatten-nested-json-objects
|
2016-06-13 16:05:46 +09:00
|
|
|
|
const flatten = function(data) {
|
2016-03-30 19:08:06 +09:00
|
|
|
|
var result = {};
|
|
|
|
|
function recurse (cur, prop) {
|
|
|
|
|
|
|
|
|
|
if (Object.prototype.toString.call(cur) !== "[object Object]") {
|
|
|
|
|
result[prop] = cur;
|
|
|
|
|
} else if (Array.isArray(cur)) {
|
|
|
|
|
for(var i=0, l=cur.length; i<l; i++)
|
|
|
|
|
recurse(cur[i], prop + "[" + i + "]");
|
|
|
|
|
if (l == 0)
|
|
|
|
|
result[prop] = [];
|
|
|
|
|
} else {
|
|
|
|
|
var isEmpty = true;
|
|
|
|
|
for (var p in cur) {
|
|
|
|
|
isEmpty = false;
|
|
|
|
|
recurse(cur[p], prop ? prop+"."+p : p);
|
|
|
|
|
}
|
|
|
|
|
if (isEmpty && prop)
|
|
|
|
|
result[prop] = {};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
recurse(data, "");
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-13 16:05:46 +09:00
|
|
|
|
/**
|
|
|
|
|
* @method Mongo.Collection.getInsertableFields
|
|
|
|
|
* Get an array of all fields editable by a specific user for a given collection
|
|
|
|
|
* @param {Object} user – the user for which to check field permissions
|
|
|
|
|
*/
|
|
|
|
|
const getInsertableFields = function (schema, user) {
|
|
|
|
|
const fields = _.filter(_.keys(schema), function (fieldName) {
|
|
|
|
|
var field = schema[fieldName];
|
|
|
|
|
return field.insertableIf && field.insertableIf(user);
|
|
|
|
|
});
|
|
|
|
|
return fields;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @method Mongo.Collection.getEditableFields
|
|
|
|
|
* Get an array of all fields editable by a specific user for a given collection
|
|
|
|
|
* @param {Object} user – the user for which to check field permissions
|
|
|
|
|
*/
|
|
|
|
|
const getEditableFields = function (schema, user, document) {
|
|
|
|
|
const fields = _.filter(_.keys(schema), function (fieldName) {
|
|
|
|
|
var field = schema[fieldName];
|
|
|
|
|
return field.editableIf && field.editableIf(user, document);
|
|
|
|
|
});
|
|
|
|
|
return fields;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { flatten, deepValue, getInsertableFields, getEditableFields };
|