Skip to content

Commit

Permalink
feat: products inherit from entities
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Aug 14, 2020
1 parent 15710c2 commit 46be861
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions data/entities.js
Expand Up @@ -1343,6 +1343,7 @@ module.exports = [
},
{
name: 'SpeedCurve LUX',
company: 'SpeedCurve',
homepage: 'https://speedcurve.com/features/lux/',
categories: ['analytics'],
domains: ['*.speedcurve.com'],
Expand Down
22 changes: 15 additions & 7 deletions lib/create-entity-finder-api.js
Expand Up @@ -44,16 +44,25 @@ function getProductInDataset(entityByDomain, entityByRootDomain, originOrURL) {
}

function cloneEntities(entities) {
return entities.map(entity => ({
...entity,
products: (entity.products || []).map(product => ({
return entities.map(entity_ => {
const entity = {
company: entity_.name,
...entity_,
}

const products = (entity_.products || []).map(product => ({
company: entity.company,
categories: entity.categories,
facades: [],
...product,
urlPatterns: product.urlPatterns.map(s =>
urlPatterns: (product.urlPatterns || []).map(s =>
s.startsWith('REGEXP:') ? new RegExp(s.slice('REGEXP:'.length)) : s
),
})),
}))
}))

entity.products = products
return entity
})
}

function createAPIFromDataset(entities_) {
Expand All @@ -62,7 +71,6 @@ function createAPIFromDataset(entities_) {
const entityByRootDomain = new Map()

for (const entity of entities) {
if (!entity.company) entity.company = entity.name
entity.totalExecutionTime = Number(entity.totalExecutionTime) || 0
entity.totalOccurrences = Number(entity.totalOccurrences) || 0
entity.averageExecutionTime = entity.totalExecutionTime / entity.totalOccurrences
Expand Down
5 changes: 4 additions & 1 deletion lib/index.d.ts
Expand Up @@ -5,7 +5,10 @@ export interface IFacade {

export interface IProduct {
name: string
urlPatterns: string[]
company: string
homepage?: string
categories: string[]
urlPatterns?: string[]
facades?: IFacade[]
}

Expand Down
6 changes: 6 additions & 0 deletions lib/index.test.js
Expand Up @@ -85,6 +85,10 @@ describe('getEntity', () => {
"name": "Facebook",
"products": Array [
Object {
"categories": Array [
"social",
],
"company": "Facebook",
"facades": Array [
Object {
"name": "React Live Chat Loader",
Expand Down Expand Up @@ -152,6 +156,8 @@ describe('getProduct', () => {
it('works on basic url', () => {
expect(getProduct('https://www.youtube.com/embed/alGcULGtiv8')).toMatchObject({
name: 'YouTube Embedded Player',
company: 'YouTube',
categories: ['video'],
facades: [
{
name: 'Lite YouTube',
Expand Down

0 comments on commit 46be861

Please sign in to comment.