mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 22:57:21 +01:00
Ported refPush and added tests
This commit is contained in:
@@ -7,13 +7,14 @@ import uuid from './uuid';
|
|||||||
no problem. To maintain sorting, we use these helper functions:
|
no problem. To maintain sorting, we use these helper functions:
|
||||||
*/
|
*/
|
||||||
|
|
||||||
module.exports = function(reflist, item, prune) {
|
module.exports = function refPush (reflist, item) {
|
||||||
if (prune == null) {
|
|
||||||
prune = 0;
|
|
||||||
}
|
|
||||||
item.sort = _.isEmpty(reflist) ? 0 : _.max(reflist, 'sort').sort + 1;
|
item.sort = _.isEmpty(reflist) ? 0 : _.max(reflist, 'sort').sort + 1;
|
||||||
|
|
||||||
if (!(item.id && !reflist[item.id])) {
|
if (!(item.id && !reflist[item.id])) {
|
||||||
item.id = uuid();
|
item.id = uuid();
|
||||||
}
|
}
|
||||||
return reflist[item.id] = item;
|
|
||||||
|
reflist[item.id] = item;
|
||||||
|
|
||||||
|
return reflist[item.id];
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -69,7 +69,6 @@ const COMMON_FILES = [
|
|||||||
'!./common/script/libs/planGemLimits.js',
|
'!./common/script/libs/planGemLimits.js',
|
||||||
'!./common/script/libs/preenHistory.js',
|
'!./common/script/libs/preenHistory.js',
|
||||||
'!./common/script/libs/preenTodos.js',
|
'!./common/script/libs/preenTodos.js',
|
||||||
'!./common/script/libs/refPush.js',
|
|
||||||
'!./common/script/libs/removeWhitespace.js',
|
'!./common/script/libs/removeWhitespace.js',
|
||||||
'!./common/script/libs/silver.js',
|
'!./common/script/libs/silver.js',
|
||||||
'!./common/script/libs/splitWhitespace.js',
|
'!./common/script/libs/splitWhitespace.js',
|
||||||
|
|||||||
53
test/common/libs/refPush.js
Normal file
53
test/common/libs/refPush.js
Normal file
@@ -0,0 +1,53 @@
|
|||||||
|
import shared from '../../../common';
|
||||||
|
import { v4 as generateUUID } from 'uuid';
|
||||||
|
|
||||||
|
describe('refPush', () => {
|
||||||
|
it('it hashes one object into another by its id', () => {
|
||||||
|
let referenceObject = {};
|
||||||
|
let objectToHash = {
|
||||||
|
a: 1,
|
||||||
|
id: generateUUID(),
|
||||||
|
};
|
||||||
|
|
||||||
|
shared.refPush(referenceObject, objectToHash);
|
||||||
|
|
||||||
|
expect(referenceObject[objectToHash.id].a).to.equal(objectToHash.a);
|
||||||
|
expect(referenceObject[objectToHash.id].id).to.equal(objectToHash.id);
|
||||||
|
expect(referenceObject[objectToHash.id].sort).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it hashes one object into another by a uuid when object does not have an id', () => {
|
||||||
|
let referenceObject = {};
|
||||||
|
let objectToHash = {
|
||||||
|
a: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
shared.refPush(referenceObject, objectToHash);
|
||||||
|
|
||||||
|
let hashedObject = _.find(referenceObject, (hashedItem) => {
|
||||||
|
return objectToHash.a === hashedItem.a;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(hashedObject.a).to.equal(objectToHash.a);
|
||||||
|
expect(hashedObject.id).to.equal(objectToHash.id);
|
||||||
|
expect(hashedObject.sort).to.equal(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('it hashes one object into another by a id and gives it the highest sort value', () => {
|
||||||
|
let referenceObject = {};
|
||||||
|
referenceObject[generateUUID()] = { b: 2, sort: 1 };
|
||||||
|
let objectToHash = {
|
||||||
|
a: 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
shared.refPush(referenceObject, objectToHash);
|
||||||
|
|
||||||
|
let hashedObject = _.find(referenceObject, (hashedItem) => {
|
||||||
|
return objectToHash.a === hashedItem.a;
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(hashedObject.a).to.equal(objectToHash.a);
|
||||||
|
expect(hashedObject.id).to.equal(objectToHash.id);
|
||||||
|
expect(hashedObject.sort).to.equal(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user