mirror of
https://github.com/JonasunderscoreJones/jonas_jones-api.git
synced 2025-10-25 01:59:18 +02:00
some progress
This commit is contained in:
parent
aea93a5527
commit
e3c15bd288
1388 changed files with 306946 additions and 68323 deletions
77
node_modules/mongoose/lib/helpers/projection/applyProjection.js
generated
vendored
Normal file
77
node_modules/mongoose/lib/helpers/projection/applyProjection.js
generated
vendored
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
'use strict';
|
||||
|
||||
const hasIncludedChildren = require('./hasIncludedChildren');
|
||||
const isExclusive = require('./isExclusive');
|
||||
const isInclusive = require('./isInclusive');
|
||||
const isPOJO = require('../../utils').isPOJO;
|
||||
|
||||
module.exports = function applyProjection(doc, projection, _hasIncludedChildren) {
|
||||
if (projection == null) {
|
||||
return doc;
|
||||
}
|
||||
if (doc == null) {
|
||||
return doc;
|
||||
}
|
||||
|
||||
let exclude = null;
|
||||
if (isInclusive(projection)) {
|
||||
exclude = false;
|
||||
} else if (isExclusive(projection)) {
|
||||
exclude = true;
|
||||
}
|
||||
|
||||
if (exclude == null) {
|
||||
return doc;
|
||||
} else if (exclude) {
|
||||
_hasIncludedChildren = _hasIncludedChildren || hasIncludedChildren(projection);
|
||||
return applyExclusiveProjection(doc, projection, _hasIncludedChildren);
|
||||
} else {
|
||||
_hasIncludedChildren = _hasIncludedChildren || hasIncludedChildren(projection);
|
||||
return applyInclusiveProjection(doc, projection, _hasIncludedChildren);
|
||||
}
|
||||
};
|
||||
|
||||
function applyExclusiveProjection(doc, projection, hasIncludedChildren, projectionLimb, prefix) {
|
||||
if (doc == null || typeof doc !== 'object') {
|
||||
return doc;
|
||||
}
|
||||
const ret = { ...doc };
|
||||
projectionLimb = prefix ? (projectionLimb || {}) : projection;
|
||||
|
||||
for (const key of Object.keys(ret)) {
|
||||
const fullPath = prefix ? prefix + '.' + key : key;
|
||||
if (projection.hasOwnProperty(fullPath) || projectionLimb.hasOwnProperty(key)) {
|
||||
if (isPOJO(projection[fullPath]) || isPOJO(projectionLimb[key])) {
|
||||
ret[key] = applyExclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath);
|
||||
} else {
|
||||
delete ret[key];
|
||||
}
|
||||
} else if (hasIncludedChildren[fullPath]) {
|
||||
ret[key] = applyExclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
function applyInclusiveProjection(doc, projection, hasIncludedChildren, projectionLimb, prefix) {
|
||||
if (doc == null || typeof doc !== 'object') {
|
||||
return doc;
|
||||
}
|
||||
const ret = { ...doc };
|
||||
projectionLimb = prefix ? (projectionLimb || {}) : projection;
|
||||
|
||||
for (const key of Object.keys(ret)) {
|
||||
const fullPath = prefix ? prefix + '.' + key : key;
|
||||
if (projection.hasOwnProperty(fullPath) || projectionLimb.hasOwnProperty(key)) {
|
||||
if (isPOJO(projection[fullPath]) || isPOJO(projectionLimb[key])) {
|
||||
ret[key] = applyInclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath);
|
||||
}
|
||||
continue;
|
||||
} else if (hasIncludedChildren[fullPath]) {
|
||||
ret[key] = applyInclusiveProjection(ret[key], projection, hasIncludedChildren, projectionLimb[key], fullPath);
|
||||
} else {
|
||||
delete ret[key];
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue