mirror of
https://github.com/swc-project/swc.git
synced 2024-12-22 21:21:31 +03:00
bbaf619f63
swc_bundler: - [x] Fix wrapped esms. (denoland/deno#9307) - [x] Make test secure.
60 lines
2.0 KiB
TypeScript
60 lines
2.0 KiB
TypeScript
// Loaded from https://raw.githubusercontent.com/denyncrawford/mongo-project.node/master/dist/bundle.js
|
|
|
|
|
|
const recursiveCloneAndFilter = (object, predicate, objectPath = [], knownObjects = []) => {
|
|
if (object && !knownObjects.includes(object) && typeof object === 'object') {
|
|
if ((object.constructor && object.constructor.name === 'Object')) {
|
|
const currentknownObjects = knownObjects.concat([object]);
|
|
const copy = {};
|
|
Object.keys(object).forEach((key) => {
|
|
const value = object[key];
|
|
const currentPath = objectPath.concat(key);
|
|
if (predicate(currentPath, value)) {
|
|
copy[key] = recursiveCloneAndFilter(value, predicate, currentPath, currentknownObjects);
|
|
}
|
|
});
|
|
return copy;
|
|
}
|
|
else if (Array.isArray(object)) {
|
|
const copy = [];
|
|
object.forEach((value, index) => {
|
|
copy[index] = recursiveCloneAndFilter(value, predicate, objectPath, knownObjects);
|
|
});
|
|
return copy;
|
|
}
|
|
return object;
|
|
}
|
|
return object;
|
|
};
|
|
|
|
const arrayStartsWithArray = (a, b) => {
|
|
return (b.filter((element, index) => {
|
|
return a[index] !== b[index];
|
|
}).length === 0);
|
|
};
|
|
|
|
var project = (object, projection) => {
|
|
const defaultPredicate = !Object.values(projection).includes(1);
|
|
return recursiveCloneAndFilter(object, (cpath) => {
|
|
const filteredPaths = Object.keys(projection).filter((ppathString) => {
|
|
const cpathString = cpath.join('.');
|
|
const ppath = ppathString.split('.');
|
|
if (arrayStartsWithArray(ppath, cpath) || arrayStartsWithArray(cpath, ppath)) {
|
|
if (defaultPredicate) {
|
|
return (ppathString === cpathString && !projection[ppathString] === defaultPredicate);
|
|
}
|
|
return (ppathString === cpathString || !projection[ppathString] === defaultPredicate);
|
|
}
|
|
return false;
|
|
});
|
|
if (filteredPaths.length) {
|
|
return !defaultPredicate;
|
|
}
|
|
return defaultPredicate;
|
|
});
|
|
};
|
|
|
|
var src = project;
|
|
|
|
export default src;
|