Streamline array operation handling

This commit is contained in:
arggh
2022-12-05 23:26:20 +02:00
parent 5a165057d8
commit 855c02447b

View File

@@ -36,7 +36,7 @@ function join(prefix, key) {
return prefix ? `${prefix}.${key}` : key;
}
const arrayOperatorKeyRegex = /^(a|u\d+)$/;
const arrayOperatorKeyRegex = /^(a|[su]\d+)$/;
function isArrayOperatorKey(field) {
return arrayOperatorKeyRegex.test(field);
@@ -46,12 +46,6 @@ function isArrayOperator(operator) {
return operator.a === true && Object.keys(operator).every(isArrayOperatorKey);
}
const arrayUpdateRegex = /^(u\d+)$/;
function isArrayUpdate(operator) {
return arrayUpdateRegex.test(operator);
}
function flattenObjectInto(target, source, prefix) {
if (Array.isArray(source) || typeof source !== 'object' || source === null) {
target[prefix] = source;
@@ -91,15 +85,6 @@ function convertOplogDiff(oplogEntry, diff, prefix) {
Object.entries(value).forEach(([key, value]) => {
oplogEntry.$set[join(prefix, key)] = value;
});
} else if (isArrayUpdate(diffKey)) {
const positionKey = join(prefix, diffKey.slice(1));
if (value === null) {
oplogEntry.$unset ??= {};
oplogEntry.$unset[positionKey] = true;
} else {
oplogEntry.$set ??= {};
oplogEntry.$set[positionKey] = value;
}
} else {
// Handle s-fields.
const key = diffKey.slice(1);
@@ -111,7 +96,9 @@ function convertOplogDiff(oplogEntry, diff, prefix) {
}
const positionKey = join(join(prefix, key), position.slice(1));
if (value === null) {
if (position[0] === 's') {
convertOplogDiff(oplogEntry, value, positionKey);
} else if (value === null) {
oplogEntry.$unset ??= {};
oplogEntry.$unset[positionKey] = true;
} else {