Skip to content

Commit

Permalink
Added isURLFragmentContained
Browse files Browse the repository at this point in the history
  • Loading branch information
oleersoy committed Sep 13, 2022
1 parent 384190b commit 45e0b4e
Show file tree
Hide file tree
Showing 14 changed files with 684 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.

/doc
# Compiled output
/dist
/tmp
Expand Down
524 changes: 425 additions & 99 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@
"@angular/platform-browser": "^14.0.0",
"@angular/platform-browser-dynamic": "^14.0.0",
"@angular/router": "^14.0.0",
"@types/estree": "^1.0.0",
"gh-pages": "^4.0.0",
"rxjs": "~7.5.0",
"tslib": "^2.3.0",
"typedoc": "^0.23.14",
"zone.js": "~0.11.4"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion projects/fs-validatorts/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@fireflysemantics/validatorts",
"description": "Data Validation and Sanitization",
"version": "13.0.87",
"version": "13.0.89",
"homepage": "https://github.com/fireflysemantics/validatorts",
"keywords": [
"validator",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { merge } from '../util/merge';
import { merge } from '../utilities/merge';
import { MessageFunctionType, Result } from '../types';

export interface NormalizeEmailErrors {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isURLFragmentContained } from './isURLFragmentContained';

let urlA = 'http://example.com/aa/bb/';
let urlD = 'http://example.com/aa/bb#fragment';
let urlE = 'http://example.com/aa/bb?a=1;b=2&c=3';

it(`should be true`, () => {
expect(isURLFragmentContained(urlA, '').value).toBeTruthy();
expect(isURLFragmentContained(urlE, '').value).toBeTruthy();
expect(isURLFragmentContained(urlD, 'fragment').value).toBeTruthy();
});
it(`should be false`, () => {
expect(isURLFragmentContained(urlA, '/').value).toBeFalsy();
expect(isURLFragmentContained(urlA, '/dd').value).toBeFalsy();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { MessageFunctionType, Result } from '../types';
import { isString } from './isString';

export interface IsURLFragmentContainedErrors {
URL_ARGUMENT_NOT_A_STRING: MessageFunctionType;
FRAGMENT_ARGUMENT_NOT_A_STRING: MessageFunctionType;
}

export const IS_URL_FRAGMENT_CONTAINED_ERRORS: IsURLFragmentContainedErrors = {
URL_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The URL argument ${arr![0]} is not a string.`;
},
FRAGMENT_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The fragment argument ${arr![0]} is not a string.`;
},
};

/**
* Checks whether the `url` path
* contains the `fragment` argument.
*
* Note that if the URL fragment does not
* exist, then Javascript considers
* the fragment to be an empty string or
* ''.
*
* So if the fragment argument is also an
* empty string, the validation result will
* be true as an empty string contains and empty
* string.
*
* Also Note that the validation of the url
* argument should be performed before passing
* the `url` argument to this validation.
*
* ### Example
* ```
* expect(isURLPathsEqual(urlA, "/aa/bb/").value).toBeTruthy();
* ```
* @param url The url string
* @param path The path string
*/
export function isURLFragmentContained(
url: string,
fragment: string
): Result<boolean | undefined> {
if (!isString(url).value) {
return new Result(
undefined,
IS_URL_FRAGMENT_CONTAINED_ERRORS.URL_ARGUMENT_NOT_A_STRING,
[url]
);
}
if (!isString(fragment).value) {
return new Result(
undefined,
IS_URL_FRAGMENT_CONTAINED_ERRORS.FRAGMENT_ARGUMENT_NOT_A_STRING,
[fragment]
);
}

const hash = new URL(url).hash;
if (hash) {
return new Result(new URL(url).hash.substring(1).includes(fragment));
}
return new Result(''.includes(fragment));
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { isURLFragmentEqual } from './isURLFragmentEqual';

let urlA = 'http://example.com/aa/bb/';
let urlD = 'http://example.com/aa/bb#fragment';
let urlE = 'http://example.com/aa/bb?a=1;b=2&c=3';

it(`should be true`, () => {
expect(isURLFragmentEqual(urlA, '').value).toBeTruthy();
expect(isURLFragmentEqual(urlE, '').value).toBeTruthy();
expect(isURLFragmentEqual(urlD, 'fragment').value).toBeTruthy();
});
it(`should be false`, () => {
expect(isURLFragmentEqual(urlA, 'cc').value).toBeFalsy();
expect(isURLFragmentEqual(urlD, '').value).toBeFalsy();
});
56 changes: 56 additions & 0 deletions projects/fs-validatorts/src/lib/validators/isURLFragmentEqual.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MessageFunctionType, Result } from '../types';
import { isString } from './isString';

export interface IsURLFragmentEqualErrors {
URL_ARGUMENT_NOT_A_STRING: MessageFunctionType;
FRAGMENT_ARGUMENT_NOT_A_STRING: MessageFunctionType;
}

export const IS_URL_PATH_EQUAL_ERRORS: IsURLFragmentEqualErrors = {
URL_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The URL argument ${arr![0]} is not a string.`;
},
FRAGMENT_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The fragment argument ${arr![0]} is not a string.`;
},
};

/**
* Checks whether the `url` fragment
* is equal to the `fragment` argument.
*
* Note that the validation of the url
* argument should be performed before passing
* the `url` argument to this validation.
*
* ### Example
* ```
* expect(isURLFragmentEqual(urlA, "/aa/bb/").value).toBeTruthy();
* ```
* @param url The url string
* @param fragment The fragment string
*/
export function isURLFragmentEqual(
url: string,
fragment: string
): Result<boolean | undefined> {
if (!isString(url).value) {
return new Result(
undefined,
IS_URL_PATH_EQUAL_ERRORS.URL_ARGUMENT_NOT_A_STRING,
[url]
);
}
if (!isString(fragment).value) {
return new Result(
undefined,
IS_URL_PATH_EQUAL_ERRORS.FRAGMENT_ARGUMENT_NOT_A_STRING,
[fragment]
);
}
const hash = new URL(url).hash;
if (hash) {
return new Result(fragment === new URL(url).hash.substring(1));
}
return new Result(fragment === '');
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { isURLPathContained } from './isURLPathContained';

let urlA = 'http://example.com/aa/bb/';
let urlB = 'http://example.com/';
let urlC = 'http://example.com';
let urlD = 'http://example.com/aa/bb#fragment';
let urlE = 'http://example.com/aa/bb?a=1;b=2&c=3';

it(`should be true`, () => {
expect(isURLPathContained(urlA, '/aa/bb/').value).toBeTruthy();
expect(isURLPathContained(urlA, '/aa/b').value).toBeTruthy();
expect(isURLPathContained(urlD, '/aa/bb').value).toBeTruthy();
expect(isURLPathContained(urlD, '/aa/b').value).toBeTruthy();
expect(isURLPathContained(urlE, '/aa/bb').value).toBeTruthy();
expect(isURLPathContained(urlE, '/aa/b').value).toBeTruthy();
expect(isURLPathContained(urlB, '/').value).toBeTruthy();
expect(isURLPathContained(urlC, '/').value).toBeTruthy();
});
it(`should be false`, () => {
expect(isURLPathContained(urlA, '/aa/bb/cc').value).toBeFalsy();
expect(isURLPathContained(urlA, '/dd').value).toBeFalsy();
});
52 changes: 52 additions & 0 deletions projects/fs-validatorts/src/lib/validators/isURLPathContained.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { MessageFunctionType, Result } from '../types';
import { isString } from './isString';

export interface IsURLPathContainedErrors {
URL_ARGUMENT_NOT_A_STRING: MessageFunctionType;
PATH_ARGUMENT_NOT_A_STRING: MessageFunctionType;
}

export const IS_URL_PATH_CONTAINED_ERRORS: IsURLPathContainedErrors = {
URL_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The URL argument ${arr![0]} is not a string.`;
},
PATH_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The paths argument ${arr![0]} is not a string.`;
},
};

/**
* Checks whether the `url` path
* contains the `path` argument.
*
* Note that the validation of the url
* argument should be performed before passing
* the `url` argument to this validation.
*
* ### Example
* ```
* expect(isURLPathsEqual(urlA, "/aa/bb/").value).toBeTruthy();
* ```
* @param url The url string
* @param path The path string
*/
export function isURLPathContained(
url: string,
path: string
): Result<boolean | undefined> {
if (!isString(url).value) {
return new Result(
undefined,
IS_URL_PATH_CONTAINED_ERRORS.URL_ARGUMENT_NOT_A_STRING,
[url]
);
}
if (!isString(path).value) {
return new Result(
undefined,
IS_URL_PATH_CONTAINED_ERRORS.PATH_ARGUMENT_NOT_A_STRING,
[path]
);
}
return new Result(new URL(url).pathname.includes(path));
}
20 changes: 20 additions & 0 deletions projects/fs-validatorts/src/lib/validators/isURLPathEqual.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { isURLPathEqual } from "./isURLPathEqual";

let urlA = "http://example.com/aa/bb/"
let urlB = "http://example.com/"
let urlC = "http://example.com"
let urlD = 'http://example.com/aa/bb#fragment';
let urlE = 'http://example.com/aa/bb?a=1;b=2&c=3';


it(`should be true`, () => {
expect(isURLPathEqual(urlA, "/aa/bb/").value).toBeTruthy();
expect(isURLPathEqual(urlB, "/").value).toBeTruthy();
expect(isURLPathEqual(urlC, "/").value).toBeTruthy();
expect(isURLPathEqual(urlD, "/aa/bb").value).toBeTruthy();
expect(isURLPathEqual(urlE, "/aa/bb").value).toBeTruthy();
})
it(`should be false`, () => {
expect(isURLPathEqual(urlA, "/aa/bb/cc").value).toBeFalsy();
expect(isURLPathEqual(urlC, "").value).toBeFalsy();
})
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { MessageFunctionType, Result } from '../types';
import { isString } from './isString';

export interface IsURLPathsEqualErrors {
export interface IsURLPathEqualErrors {
URL_ARGUMENT_NOT_A_STRING: MessageFunctionType;
PATH_ARGUMENT_NOT_A_STRING: MessageFunctionType;
}

export const IS_URL_PATH_EQUAL_ERRORS: IsURLPathsEqualErrors = {
export const IS_URL_PATH_EQUAL_ERRORS: IsURLPathEqualErrors = {
URL_ARGUMENT_NOT_A_STRING: (arr?: string[]) => {
return `The URL argument ${arr![0]} is not a string.`;
},
Expand All @@ -25,12 +25,12 @@ export const IS_URL_PATH_EQUAL_ERRORS: IsURLPathsEqualErrors = {
*
* ### Example
* ```
* expect(isURLPathsEqual(urlA, "/aa/bb/").value).toBeTruthy();
* expect(isURLPathEqual(urlA, "/aa/bb/").value).toBeTruthy();
* ```
* @param target The target string
* @param whitelist The white list
* @param url The url string
* @param path The path string
*/
export function isURLPathsEqual(
export function isURLPathEqual(
url: string,
path: string
): Result<boolean | undefined> {
Expand Down
15 changes: 0 additions & 15 deletions projects/fs-validatorts/src/lib/validators/isURLPathsEqual.spec.ts

This file was deleted.

0 comments on commit 45e0b4e

Please sign in to comment.