swc/bundler/tests/.cache/untrusted/5a2add619c27643036195154842eef5c45ee8512.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

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;