Skip to content

Commit

Permalink
refactor: use regex in entities.js
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Aug 14, 2020
1 parent d8154dc commit 107e6dd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
2 changes: 1 addition & 1 deletion bin/shared/utils.js
@@ -1,7 +1,7 @@
module.exports = {
stringifyEntities(entities) {
return JSON.stringify(entities, (key, value) =>
value instanceof RegExp ? value.source : value
value instanceof RegExp ? `REGEXP:${value.source}` : value
)
},
}
12 changes: 6 additions & 6 deletions data/entities.js
Expand Up @@ -75,7 +75,7 @@ module.exports = [
products: [
{
name: 'Facebook Messenger Customer Chat',
urlPatterns: ['.*connect\\.facebook\\.net/.*/sdk/xfbml\\.customerchat\\.js'],
urlPatterns: [/connect\.facebook\.net\/.*\/sdk\/xfbml\.customerchat\.js/],
facades: [
{
name: 'React Live Chat Loader',
Expand Down Expand Up @@ -217,7 +217,7 @@ module.exports = [
products: [
{
name: 'YouTube Embedded Player',
urlPatterns: ['.*\\.youtube\\.com/embed/.*'],
urlPatterns: ['youtube.com/embed/'],
facades: [
{
name: 'Lite YouTube',
Expand Down Expand Up @@ -566,7 +566,7 @@ module.exports = [
products: [
{
name: 'Help Scout Beacon',
urlPatterns: ['.*beacon-v2\\.helpscout\\.net'],
urlPatterns: ['beacon-v2.helpscout.net'],
facades: [
{
name: 'React Live Chat Loader',
Expand Down Expand Up @@ -700,7 +700,7 @@ module.exports = [
products: [
{
name: 'Vimeo Embedded Player',
urlPatterns: ['.*player\\.vimeo\\.com/video/.*'],
urlPatterns: ['player.vimeo.com/video/'],
facades: [
{
name: 'Lite Vimeo',
Expand Down Expand Up @@ -827,7 +827,7 @@ module.exports = [
products: [
{
name: 'Drift Live Chat',
urlPatterns: ['.*js\\.driftt\\.com/include/.*/.*\\.js'],
urlPatterns: [/js\.driftt\.com\/include\/.*\/.*\.js/],
facades: [
{
name: 'React Live Chat Loader',
Expand Down Expand Up @@ -1495,7 +1495,7 @@ module.exports = [
products: [
{
name: 'Intercom Widget',
urlPatterns: ['.*widget\\.intercom\\.io.*', '.*js\\.intercomcdn\\.com/shim\\.latest\\.js'],
urlPatterns: ['widget.intercom.io', 'js.intercomcdn.com/shim.latest.js'],
facades: [
{
name: 'React Live Chat Loader',
Expand Down
23 changes: 18 additions & 5 deletions lib/create-entity-finder-api.js
Expand Up @@ -32,19 +32,32 @@ function getProductInDataset(entityByDomain, entityByRootDomain, originOrURL) {
const entity = getEntityInDataset(entityByDomain, entityByRootDomain, originOrURL)
const products = entity && entity.products
if (!products) return undefined
if (typeof originOrURL !== 'string') return undefined

for (const product of products) {
for (const pattern of product.urlPatterns) {
const regex = new RegExp(pattern)
if (regex.test(originOrURL)) {
return product
}
if (pattern instanceof RegExp && pattern.test(originOrURL)) return product
if (typeof pattern === 'string' && originOrURL.includes(pattern)) return product
}
}
return undefined
}

function cloneEntities(entities) {
return entities.map(entity => ({
...entity,
products: (entity.products || []).map(product => ({
facades: [],
...product,
urlPatterns: product.urlPatterns.map(s =>
s.startsWith('REGEXP:') ? new RegExp(s.slice('REGEXP:'.length)) : s
),
})),
}))
}

function createAPIFromDataset(entities_) {
const entities = entities_.map(e => ({...e}))
const entities = cloneEntities(entities_)
const entityByDomain = new Map()
const entityByRootDomain = new Map()

Expand Down
26 changes: 22 additions & 4 deletions lib/index.test.js
Expand Up @@ -93,7 +93,7 @@ describe('getEntity', () => {
],
"name": "Facebook Messenger Customer Chat",
"urlPatterns": Array [
".*connect\\\\.facebook\\\\.net/.*/sdk/xfbml\\\\.customerchat\\\\.js",
/connect\\\\\\.facebook\\\\\\.net\\\\/\\.\\*\\\\/sdk\\\\/xfbml\\\\\\.customerchat\\\\\\.js/,
],
},
],
Expand Down Expand Up @@ -121,6 +121,7 @@ describe('getEntity', () => {
],
"homepage": "https://fonts.adobe.com/",
"name": "Adobe TypeKit",
"products": Array [],
"totalExecutionTime": 1722921,
"totalOccurrences": 18274,
}
Expand Down Expand Up @@ -149,18 +150,35 @@ describe('getEntity', () => {

describe('getProduct', () => {
it('works on basic url', () => {
expect(getProduct('https://www.youtube.com/embed/alGcULGtiv8')).toEqual({
expect(getProduct('https://www.youtube.com/embed/alGcULGtiv8')).toMatchObject({
name: 'YouTube Embedded Player',
facades: [
{
name: 'Lite YouTube',
repo: 'https://github.com/paulirish/lite-youtube-embed',
},
],
name: 'YouTube Embedded Player',
urlPatterns: ['.*\\.youtube\\.com/embed/.*'],
})
})

it('works on regex based', () => {
expect(
getProduct('https://connect.facebook.net/en_US/sdk/xfbml.customerchat.js')
).toMatchObject({
name: 'Facebook Messenger Customer Chat',
facades: [
{
name: 'React Live Chat Loader',
repo: 'https://github.com/calibreapp/react-live-chat-loader',
},
],
})
})

it('returns undefined when product does not match', () => {
expect(getProduct('https://js.connect.facebook.net/lib.js')).toEqual(undefined)
})

it('returns undefined with no products', () => {
expect(getProduct('https://unknown.typekit.net/fonts.css')).toEqual(undefined)
})
Expand Down

0 comments on commit 107e6dd

Please sign in to comment.