Initial commit

This commit is contained in:
Arnaud Nelissen
2021-07-16 10:18:13 +02:00
commit 3af7ddab06
5894 changed files with 590836 additions and 0 deletions

View File

@@ -0,0 +1,18 @@
'use strict';
/*!
* ignore
*/
module.exports = function isDefiningProjection(val) {
if (val == null) {
// `undefined` or `null` become exclusive projections
return true;
}
if (typeof val === 'object') {
// Only cases where a value does **not** define whether the whole projection
// is inclusive or exclusive are `$meta` and `$slice`.
return !('$meta' in val) && !('$slice' in val);
}
return true;
};

View File

@@ -0,0 +1,32 @@
'use strict';
const isDefiningProjection = require('./isDefiningProjection');
/*!
* ignore
*/
module.exports = function isExclusive(projection) {
if (projection == null) {
return null;
}
const keys = Object.keys(projection);
let ki = keys.length;
let exclude = null;
if (ki === 1 && keys[0] === '_id') {
exclude = !!projection[keys[ki]];
} else {
while (ki--) {
// Does this projection explicitly define inclusion/exclusion?
// Explicitly avoid `$meta` and `$slice`
if (keys[ki] !== '_id' && isDefiningProjection(projection[keys[ki]])) {
exclude = !projection[keys[ki]];
break;
}
}
}
return exclude;
};

View File

@@ -0,0 +1,34 @@
'use strict';
const isDefiningProjection = require('./isDefiningProjection');
/*!
* ignore
*/
module.exports = function isInclusive(projection) {
if (projection == null) {
return false;
}
const props = Object.keys(projection);
const numProps = props.length;
if (numProps === 0) {
return false;
}
for (let i = 0; i < numProps; ++i) {
const prop = props[i];
// Plus paths can't define the projection (see gh-7050)
if (prop.startsWith('+')) {
continue;
}
// If field is truthy (1, true, etc.) and not an object, then this
// projection must be inclusive. If object, assume its $meta, $slice, etc.
if (isDefiningProjection(projection[prop]) && !!projection[prop]) {
return true;
}
}
return false;
};

View File

@@ -0,0 +1,35 @@
'use strict';
const isDefiningProjection = require('./isDefiningProjection');
/*!
* Determines if `path` is excluded by `projection`
*
* @param {Object} projection
* @param {string} path
* @return {Boolean}
*/
module.exports = function isPathExcluded(projection, path) {
if (path === '_id') {
return projection._id === 0;
}
const paths = Object.keys(projection);
let type = null;
for (const _path of paths) {
if (isDefiningProjection(projection[_path])) {
type = projection[path] === 1 ? 'inclusive' : 'exclusive';
break;
}
}
if (type === 'inclusive') {
return projection[path] !== 1;
}
if (type === 'exclusive') {
return projection[path] === 0;
}
return false;
};

View File

@@ -0,0 +1,28 @@
'use strict';
/*!
* ignore
*/
module.exports = function isPathSelectedInclusive(fields, path) {
const chunks = path.split('.');
let cur = '';
let j;
let keys;
let numKeys;
for (let i = 0; i < chunks.length; ++i) {
cur += cur.length ? '.' : '' + chunks[i];
if (fields[cur]) {
keys = Object.keys(fields);
numKeys = keys.length;
for (j = 0; j < numKeys; ++j) {
if (keys[i].indexOf(cur + '.') === 0 && keys[i].indexOf(path) !== 0) {
continue;
}
}
return true;
}
}
return false;
};

View File

@@ -0,0 +1,33 @@
'use strict';
/**
* Convert a string or array into a projection object, retaining all
* `-` and `+` paths.
*/
module.exports = function parseProjection(v, retainMinusPaths) {
const type = typeof v;
if (type === 'string') {
v = v.split(/\s+/);
}
if (!Array.isArray(v) && Object.prototype.toString.call(v) !== '[object Arguments]') {
return v;
}
const len = v.length;
const ret = {};
for (let i = 0; i < len; ++i) {
let field = v[i];
if (!field) {
continue;
}
const include = '-' == field[0] ? 0 : 1;
if (!retainMinusPaths && include === 0) {
field = field.substring(1);
}
ret[field] = include;
}
return ret;
};