@plq/faker
Version:
A set of classes for mocking known data types such as browser history, browser download list, persona, domain name, Jira project, GitHub repository, etc.
55 lines • 2.59 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
const faker_1 = require("@faker-js/faker");
const array_functions_1 = require("@plq/array-functions");
const base_extended_1 = require("./base-extended");
const DEFAULT_LIMIT = 100;
class MockBookmarks extends base_extended_1.BaseItemsMock {
constructor(query) {
super(query);
this.reset();
}
createMockItems() {
const limit = this.query.limit || DEFAULT_LIMIT;
const rootItemsCount = faker_1.faker.number.int({ min: 1, max: limit });
const childrenItemsCount = limit - rootItemsCount;
const minCount = Math.min(rootItemsCount, childrenItemsCount);
const rootItemsChildrenCount = faker_1.faker.helpers.shuffle((0, array_functions_1.createBalancedArray)(minCount, childrenItemsCount));
return faker_1.faker.helpers.multiple(() => this.createMockItem(), { count: rootItemsCount }).map((item, index) => {
return {
...item,
children: item.children && rootItemsChildrenCount[index] ? faker_1.faker.helpers.multiple(() => this.createMockItem(item.id), { count: rootItemsChildrenCount[index] }).map((child, index) => ({
...child,
index,
})) : undefined,
};
});
}
createMockItem(parentId) {
return {
id: faker_1.faker.string.uuid(),
parentId,
url: parentId ? this.query.url || faker_1.faker.internet.url() : undefined,
title: this.query.title || faker_1.faker.lorem.words(),
dateAdded: faker_1.faker.date.recent().getTime(),
dateGroupModified: faker_1.faker.date.recent().getTime(),
syncing: faker_1.faker.datatype.boolean(),
children: parentId ? undefined : [],
};
}
getRootItem() {
const itemsWithChildren = this.items.filter(item => item.children && item.children.length > 0);
// If no items have children, create one by adding a child to an existing item
if (itemsWithChildren.length === 0) {
const rootItem = faker_1.faker.helpers.arrayElement(this.items);
if (!rootItem.children) {
rootItem.children = [];
}
rootItem.children.push(this.createMockItem(rootItem.id));
return rootItem;
}
return faker_1.faker.helpers.arrayElement(itemsWithChildren);
}
}
exports.default = MockBookmarks;
//# sourceMappingURL=bookmarks.js.map