mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 16:46:12 +01:00
refactor: remove dupe code
This commit is contained in:
@@ -8,22 +8,10 @@ module.exports = function (module) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value = Array.isArray(value) ? value : [value];
|
value = Array.isArray(value) ? value : [value];
|
||||||
value = value.map(helpers.valueToString);
|
|
||||||
value.reverse();
|
value.reverse();
|
||||||
const exists = await module.isObjectField(key, 'array');
|
const exists = await module.isObjectField(key, 'array');
|
||||||
if (exists) {
|
if (exists) {
|
||||||
await module.client.collection('objects').updateOne({
|
await listPush(key, value, { $position: 0 });
|
||||||
_key: key,
|
|
||||||
}, {
|
|
||||||
$push: {
|
|
||||||
array: {
|
|
||||||
$each: value,
|
|
||||||
$position: 0,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}, {
|
|
||||||
upsert: true,
|
|
||||||
});
|
|
||||||
} else {
|
} else {
|
||||||
await module.listAppend(key, value);
|
await module.listAppend(key, value);
|
||||||
}
|
}
|
||||||
@@ -34,19 +22,24 @@ module.exports = function (module) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
value = Array.isArray(value) ? value : [value];
|
value = Array.isArray(value) ? value : [value];
|
||||||
value = value.map(helpers.valueToString);
|
await listPush(key, value);
|
||||||
|
};
|
||||||
|
|
||||||
|
async function listPush(key, values, position) {
|
||||||
|
values = values.map(helpers.valueToString);
|
||||||
await module.client.collection('objects').updateOne({
|
await module.client.collection('objects').updateOne({
|
||||||
_key: key,
|
_key: key,
|
||||||
}, {
|
}, {
|
||||||
$push: {
|
$push: {
|
||||||
array: {
|
array: {
|
||||||
$each: value,
|
$each: values,
|
||||||
|
...(position || {}),
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
upsert: true,
|
upsert: true,
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
module.listRemoveLast = async function (key) {
|
module.listRemoveLast = async function (key) {
|
||||||
if (!key) {
|
if (!key) {
|
||||||
|
|||||||
Reference in New Issue
Block a user