Skip to content

Commit

Permalink
Added contributing, added tests and alias support
Browse files Browse the repository at this point in the history
  • Loading branch information
dylmye committed Mar 16, 2022
1 parent 9ed9a33 commit 4849ee8
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 20 deletions.
18 changes: 18 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
Thanks for considering contributing to this project!

If you wish to contribute on behalf of a service provider and want your PAC/STAC codes to be recognised, please take the following steps:

0. 'Fork' (make a copy of) this project and 'clone' (download) your copy of the project to your computer. Run the `yarn` command in the folder to install all dependencies.
0. Duplicate one of the existing objects in `src/metadata.json` and add in your own information.
0. When you are happy with your changes, run `yarn test` to make sure everything still works. Everything should pass.
0. 'Commit' and push (upload) your changes, then create [a pull request](https://docs.github.com/en/pull-requests/collaborating-with-pull-requests/proposing-changes-to-your-work-with-pull-requests/creating-a-pull-request) from your branch to ours.

If you are considering making any other type of change, please create an issue first. Once it's been validated, follow the same process as above for submitting your proposed change. Please make sure your editor is using the `.editorconfig` file when contributing changes.

## Removing your network from this project

No confidential or copyrighted materials are included in the source code of this project. All details are sourced from public knowledge.

If your company has any issues with their details being included in this project, let's be constructive - please create an issue, or reach out through [my linked Twitter profile](https://github.com/dylmye/).

<3
4 changes: 4 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ Instructions for using client-side with unpkg etc coming soon...
* Run `yarn build` before commiting changes, and `yarn schema` if you've made a difference to any types in src/types.ts.

### Contributing

Please read CONTRIBUTING.md :)

## Example usage

### React
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 43 additions & 3 deletions lib/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "../lib/metadata.schema.json",
"version": "1.1",
"version": "1.2",
"serviceProviders": [
{
"id": "BTC",
Expand Down Expand Up @@ -82,11 +82,51 @@
"instructions": "https://www.vodafone.co.uk/help-and-information/cancel-your-account",
"logo": "https://logo.clearbit.com/vodafone.co.uk",
"lastUpdated": "2022-03-15"
},
{
"id": "LCS",
"fullName": "Three Smarty",
"shortName": "SMARTY",
"website": "https://smarty.co.uk/",
"instructions": "https://help.smarty.co.uk/en/articles/952726",
"logo": "https://logo.clearbit.com/smarty.co.uk",
"lastUpdated": "2022-03-16"
},
{
"id": "TKM",
"fullName": "Talkmobile",
"shortName": "Talkmobile",
"website": "https://talkmobile.co.uk/",
"instructions": "https://talkmobile.co.uk/switching",
"logo": "https://logo.clearbit.com/talkmobile.co.uk",
"lastUpdated": "2022-03-16"
}
],
"aliases": {
"TSL": "VTM",
"VCN": "VUK"
"VCN": "VUK",
"VOD": "VUK",
"H3G": "HTG",
"CEL": "TEL"
},
"dead": []
"dead": [
{
"id": "ORG",
"fullName": "Orange",
"shortName": "Orange",
"website": "https://orange.co.uk/",
"instructions": "https://orange.co.uk/",
"logo": "https://logo.clearbit.com/orange.co.uk",
"lastUpdated": "2022-03-16"
},
{
"id": "ONE",
"fullName": "T-Mobile",
"shortName": "T-Mobile",
"website": "https://t-mobile.co.uk/",
"instructions": "https://t-mobile.co.uk/",
"logo": "https://logo.clearbit.com/telekom.com",
"lastUpdated": "2022-03-16"
}
]
}
3 changes: 2 additions & 1 deletion lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export interface ValidationResult {
/** Descriptions for reasons why parsing a PAC/STAC code failed */
export declare enum ValidationFailureReason {
NO_MATCH = "No authorisation code was found",
INVALID_FORMAT = "Authorisation code is not formatted correctly"
INVALID_FORMAT = "Authorisation code is not formatted correctly",
CEASED_OPERATOR = "The PAC Code provider has ceased operations"
}
/** Shape of metadata.json objects */
export interface ServiceProviderData {
Expand Down
2 changes: 1 addition & 1 deletion lib/types.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/types.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion lib/types.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 20 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FormattedAuthCode,
ServiceProviderData,
} from "./types";
import { serviceProviders } from "./metadata.json";
import { serviceProviders, aliases } from "./metadata.json";

/** A PAC code consists of a three letter SPID and six unique numeric digits */
export const PAC_REGEX = /^([A-Z]{3})([0-9]{6})$/;
Expand Down Expand Up @@ -55,7 +55,10 @@ export function validateAuthCode(
*
* @returns The code split into two, unless it's invalid
*/
export function splitAuthCode(code: string, type: AuthCodeType): FormattedAuthCode | null {
export function splitAuthCode(
code: string,
type: AuthCodeType
): FormattedAuthCode | null {
const split = code.match(/[a-zA-Z]+|[0-9]+/g);

if (!split || split.length < 1) {
Expand All @@ -76,10 +79,22 @@ export function splitAuthCode(code: string, type: AuthCodeType): FormattedAuthCo
}

/**
*
*
* @param id The ID to search for
* @returns The ServiceProviderData object for the given ID
*/
export function getServiceProviderDataById(id: string): ServiceProviderData | undefined {
return (serviceProviders as ServiceProviderData[]).find(x => x.id === id);
export function getServiceProviderDataById(
id: string
): ServiceProviderData | undefined {
const found = (serviceProviders as ServiceProviderData[]).find(
(x) => x.id === id
);
if (!found) {
const alias = (aliases as Record<string, string>)[id];
if (!alias) return undefined;
return (serviceProviders as ServiceProviderData[]).find(
(x) => x.id === alias
);
}
return found;
}
48 changes: 44 additions & 4 deletions src/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "../lib/metadata.schema.json",
"version": "1.1",
"version": "1.2",
"serviceProviders": [
{
"id": "BTC",
Expand Down Expand Up @@ -82,11 +82,51 @@
"instructions": "https://www.vodafone.co.uk/help-and-information/cancel-your-account",
"logo": "https://logo.clearbit.com/vodafone.co.uk",
"lastUpdated": "2022-03-15"
},
{
"id": "LCS",
"fullName": "Three Smarty",
"shortName": "SMARTY",
"website": "https://smarty.co.uk/",
"instructions": "https://help.smarty.co.uk/en/articles/952726",
"logo": "https://logo.clearbit.com/smarty.co.uk",
"lastUpdated": "2022-03-16"
},
{
"id": "TKM",
"fullName": "Talkmobile",
"shortName": "Talkmobile",
"website": "https://talkmobile.co.uk/",
"instructions": "https://talkmobile.co.uk/switching",
"logo": "https://logo.clearbit.com/talkmobile.co.uk",
"lastUpdated": "2022-03-16"
}
],
"aliases": {
"TSL": "VTM",
"VCN": "VUK"
"VCN": "VUK",
"VOD": "VUK",
"H3G": "HTG",
"CEL": "TEL"
},
"dead": []
}
"dead": [
{
"id": "ORG",
"fullName": "Orange",
"shortName": "Orange",
"website": "https://orange.co.uk/",
"instructions": "https://orange.co.uk/",
"logo": "https://logo.clearbit.com/orange.co.uk",
"lastUpdated": "2022-03-16"
},
{
"id": "ONE",
"fullName": "T-Mobile",
"shortName": "T-Mobile",
"website": "https://t-mobile.co.uk/",
"instructions": "https://t-mobile.co.uk/",
"logo": "https://logo.clearbit.com/telekom.com",
"lastUpdated": "2022-03-16"
}
]
}
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface ValidationResult {
export enum ValidationFailureReason {
NO_MATCH = "No authorisation code was found",
INVALID_FORMAT = "Authorisation code is not formatted correctly",
CEASED_OPERATOR = "The PAC Code provider has ceased operations"
}

/** Shape of metadata.json objects */
Expand Down
24 changes: 22 additions & 2 deletions test/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { AuthCodeType, FormattedAuthCode, ValidationFailureReason } from "../src

const validO2PacCode = "TEL000000";
const validUnknownPacCode = "ZZZ111111";
const validAliasedPacCode = "VOD000000";
const validO2StacCode = "000000TEL";
const validUnknownStacCode = "111111ZZZ";
const validAliasedStacCode = "000000VOD";

const invalidLengthPacCode = "TEL0000007";
const invalidLengthStacCode = "111111ZZZJ";
Expand All @@ -15,20 +17,26 @@ const valid = [
validUnknownPacCode,
validO2StacCode,
validUnknownStacCode,
validAliasedPacCode,
validAliasedStacCode
];

const validSplits = {
const validSplits: Record<string, string[]> = {
[validO2PacCode]: ["TEL", "000000"],
[validO2StacCode]: ["TEL", "000000"],
[validUnknownPacCode]: ["ZZZ", "111111"],
[validUnknownStacCode]: ["ZZZ", "111111"],
[validAliasedPacCode]: ["VOD", "000000"],
[validAliasedStacCode]: ["VOD", "000000"],
};

const validTypeMap = {
const validTypeMap: Record<string, AuthCodeType> = {
[validO2PacCode]: "PAC",
[validO2StacCode]: "STAC",
[validUnknownPacCode]: "PAC",
[validUnknownStacCode]: "STAC",
[validAliasedPacCode]: "PAC",
[validAliasedStacCode]: "STAC",
};

const invalid = [invalidLengthPacCode, invalidLengthStacCode];
Expand Down Expand Up @@ -64,6 +72,12 @@ describe("validateAuthCode", () => {
strictEqual<boolean>(res.metadata, undefined);
});
});
describe("valid PAC code with aliased SPID", () => {
const res = validateAuthCode(validAliasedPacCode, "PAC");
it("matches the aliased item correctly", () => {
strictEqual<string>(res.metadata.id, "VUK");
});
});

describe("valid o2 STAC code", () => {
const res = validateAuthCode(validO2StacCode, "STAC");
Expand All @@ -77,6 +91,12 @@ describe("validateAuthCode", () => {
strictEqual<boolean>(res.metadata, undefined);
});
});
describe("valid STAC code with aliased SPID", () => {
const res = validateAuthCode(validAliasedStacCode, "STAC");
it("matches the aliased item correctly", () => {
strictEqual<string>(res.metadata.id, "VUK");
});
});

invalid.forEach((code) => {
describe(`invalid code ${code}`, () => {
Expand Down

0 comments on commit 4849ee8

Please sign in to comment.