Skip to content

Commit

Permalink
Merge pull request #1 from cuxio/feature/initialization
Browse files Browse the repository at this point in the history
Initialization of the project
  • Loading branch information
kmlwlkwk authored Sep 30, 2020
2 parents 08420e8 + be7980b commit 130ef82
Show file tree
Hide file tree
Showing 18 changed files with 9,311 additions and 2 deletions.
14 changes: 14 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
max_line_length = 80
trim_trailing_whitespace = true

[*.md]
max_line_length = 0
trim_trailing_whitespace = false
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* text=auto eol=lf
32 changes: 32 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: CI
on: [push]
jobs:
build:
name: Build, lint, and test on Node ${{ matrix.node }} and ${{ matrix.os }}

runs-on: ${{ matrix.os }}
strategy:
matrix:
node: ['10.x', '12.x', '14.x']
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- name: Checkout repo
uses: actions/checkout@v2

- name: Use Node ${{ matrix.node }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node }}

- name: Install deps and build (with cache)
uses: bahmutov/npm-install@v1

- name: Lint
run: yarn lint

- name: Test
run: yarn test --ci --coverage --maxWorkers=2

- name: Build
run: yarn build
12 changes: 12 additions & 0 deletions .github/workflows/size.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: size
on: [pull_request]
jobs:
size:
runs-on: ubuntu-latest
env:
CI_JOB_NUMBER: 1
steps:
- uses: actions/checkout@v1
- uses: andresz1/size-limit-action@v1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
*.log
.DS_Store
node_modules
dist
.idea
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 cux.io

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# referrer
Micro package for specifying a referrer
# Referrer

Micro library created to parse and return proper referrer parameters.

## Installation

```
yarn add @cux/referrer
```

## Usage

```
import { getReferrer } from '@cux/referrer';
getReferrer('https://google.com/');
```

You should receive the following object:
```
{
type: ReferrerType;
name: ReferrerName | string;
}
```

## Types of referrers

- Paid Traffic (Google, Facebook, Bing. Yahoo campaigns)
- Search (Google, Bing, Yahoo, Baidu, Yandex)
- Social (Facebook, Twitter, Instagram, Youtube, Linkedin)
- Organic (direct link)
- Other
55 changes: 55 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"name": "@cux/referrer",
"author": "cux.io",
"version": "1.0.0",
"license": "MIT",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"files": [
"dist",
"src"
],
"engines": {
"node": ">=10"
},
"scripts": {
"start": "tsdx watch",
"build": "tsdx build",
"test": "tsdx test",
"lint": "tsdx lint",
"prepare": "tsdx build",
"size": "size-limit",
"analyze": "size-limit --why"
},
"peerDependencies": {},
"husky": {
"hooks": {
"pre-commit": "tsdx lint"
}
},
"prettier": {
"printWidth": 80,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
},
"module": "dist/referrer.esm.js",
"size-limit": [
{
"path": "dist/referrer.cjs.production.min.js",
"limit": "10 KB"
},
{
"path": "dist/referrer.esm.js",
"limit": "10 KB"
}
],
"devDependencies": {
"@size-limit/preset-small-lib": "^4.6.0",
"husky": "^4.3.0",
"size-limit": "^4.6.0",
"tsdx": "^0.14.0",
"tslib": "^2.0.1",
"typescript": "^4.0.3"
}
}
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Referrer } from './shared/referrer.interface';
import { parseReferrer } from './shared/referrer.helpers';

/**
* @param {string} referrer
* @returns {Referrer}
*/
export const getReferrer = (referrer: string): Referrer => {
return parseReferrer(referrer);
};
9 changes: 9 additions & 0 deletions src/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { paidTrafficMatchers } from './paid-traffic.matchers';
import { searchMatchers } from './search.matchers';
import { socialMatchers } from './social.matchers';

export const referrerMatchers = [
...paidTrafficMatchers,
...searchMatchers,
...socialMatchers,
];
33 changes: 33 additions & 0 deletions src/matchers/paid-traffic.matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
ReferrerMatcher,
ReferrerName,
ReferrerType,
} from '../shared/referrer.interface';

export const paidTrafficMatchers: ReferrerMatcher[] = [
{
type: ReferrerType.PAID_TRAFFIC,
name: ReferrerName.GOOGLE,
contains: ['google', 'utm_medium=cpc'],
},
{
type: ReferrerType.PAID_TRAFFIC,
name: ReferrerName.GOOGLE,
contains: ['google', 'gclid'],
},
{
type: ReferrerType.PAID_TRAFFIC,
name: ReferrerName.FACEBOOK,
contains: ['facebook', 'utm_medium=cpc'],
},
{
type: ReferrerType.PAID_TRAFFIC,
name: ReferrerName.BING,
contains: ['bing.com', 'utm_medium=cpc'],
},
{
type: ReferrerType.PAID_TRAFFIC,
name: ReferrerName.YAHOO,
contains: ['search.yahoo', 'utm_medium=cpc'],
},
];
33 changes: 33 additions & 0 deletions src/matchers/search.matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
ReferrerMatcher,
ReferrerName,
ReferrerType,
} from '../shared/referrer.interface';

export const searchMatchers: ReferrerMatcher[] = [
{
type: ReferrerType.SEARCH,
name: ReferrerName.GOOGLE,
contains: ['google'],
},
{
type: ReferrerType.SEARCH,
name: ReferrerName.BING,
contains: ['bing.com'],
},
{
type: ReferrerType.SEARCH,
name: ReferrerName.YAHOO,
contains: ['search.yahoo'],
},
{
type: ReferrerType.SEARCH,
name: ReferrerName.BAIDU,
contains: ['baidu.com'],
},
{
type: ReferrerType.SEARCH,
name: ReferrerName.YANDEX,
contains: ['yandex.com'],
},
];
33 changes: 33 additions & 0 deletions src/matchers/social.matchers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
ReferrerMatcher,
ReferrerName,
ReferrerType,
} from '../shared/referrer.interface';

export const socialMatchers: ReferrerMatcher[] = [
{
type: ReferrerType.SOCIAL,
name: ReferrerName.FACEBOOK,
contains: ['facebook.com'],
},
{
type: ReferrerType.SOCIAL,
name: ReferrerName.TWITTER,
contains: ['twitter.com'],
},
{
type: ReferrerType.SOCIAL,
name: ReferrerName.INSTAGRAM,
contains: ['instagram.com'],
},
{
type: ReferrerType.SOCIAL,
name: ReferrerName.YOUTUBE,
contains: ['youtube.com'],
},
{
type: ReferrerType.SOCIAL,
name: ReferrerName.LINKEDIN,
contains: ['linkedin.com'],
},
];
30 changes: 30 additions & 0 deletions src/shared/referrer.helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Referrer, ReferrerType } from './referrer.interface';
import { referrerMatchers } from '../matchers';

/**
* @param {string} referrer
* @returns {Referrer}
*/
export const parseReferrer = (referrer: string): Referrer => {
if (!referrer) {
return {
type: ReferrerType.ORGANIC,
};
}

const referrerMatch = referrerMatchers.find(matcher => {
return matcher.contains.every(phrase => referrer.indexOf(phrase) > -1);
});

if (referrerMatch) {
return {
type: referrerMatch.type,
name: referrerMatch.name,
};
}

return {
type: ReferrerType.OTHER,
name: referrer,
};
};
29 changes: 29 additions & 0 deletions src/shared/referrer.interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export enum ReferrerType {
PAID_TRAFFIC = 'paidTraffic',
ORGANIC = 'organic',
SEARCH = 'search',
SOCIAL = 'social',
OTHER = 'other',
}

export enum ReferrerName {
GOOGLE = 'google',
FACEBOOK = 'facebook',
BING = 'bing',
YAHOO = 'yahoo',
BAIDU = 'baidu',
YANDEX = 'yandex',
TWITTER = 'twitter',
INSTAGRAM = 'instagram',
YOUTUBE = 'youtube',
LINKEDIN = 'linkedin',
}

export interface Referrer {
type: ReferrerType;
name?: ReferrerName | string;
}

export interface ReferrerMatcher extends Referrer {
contains: string[];
}
13 changes: 13 additions & 0 deletions test/referrer.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { getReferrer } from '../src';
import { ReferrerName, ReferrerType } from '../src/shared/referrer.interface';

const GOOGLE = 'https://www.google.com/';

describe('Getting referrer object', () => {
it('From google search', () => {
expect(getReferrer(GOOGLE)).toEqual({
type: ReferrerType.SEARCH,
name: ReferrerName.GOOGLE,
});
});
});
Loading

0 comments on commit 130ef82

Please sign in to comment.