@purescraps/purehtml 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +8 -2
- package/.eslintrc.json +0 -21
- package/.prettierrc +0 -3
- package/config.yaml +0 -0
- package/examples/simple.html +0 -26
- package/examples/simple.ts +0 -12
- package/examples/simple.yaml +0 -20
- package/jest.config.ts +0 -9
- package/sample.html +0 -0
- package/src/config/config.ts +0 -12
- package/src/config/factory.ts +0 -142
- package/src/config/index.ts +0 -6
- package/src/config/plain-config.ts +0 -19
- package/src/config/types/array.test.ts +0 -103
- package/src/config/types/array.ts +0 -61
- package/src/config/types/constant.ts +0 -15
- package/src/config/types/object.test.ts +0 -99
- package/src/config/types/object.ts +0 -65
- package/src/config/types/primitive.ts +0 -63
- package/src/config/types/union.ts +0 -36
- package/src/config/types/with-selector.test.ts +0 -32
- package/src/config/types/with-selector.ts +0 -26
- package/src/config/validator.ts +0 -14
- package/src/config-schema.test.ts +0 -187
- package/src/core/argument-types.ts +0 -21
- package/src/core/complex-types.ts +0 -4
- package/src/core/primitive-types.ts +0 -5
- package/src/core/property.ts +0 -3
- package/src/core/transformer.ts +0 -21
- package/src/errors/invalid-parse-input-error.ts +0 -7
- package/src/index.ts +0 -26
- package/src/jest.d.ts +0 -5
- package/src/setup-jest-matchers.ts +0 -26
- package/src/transformers/attr.ts +0 -49
- package/src/transformers/exists.ts +0 -20
- package/src/transformers/factory.test.ts +0 -17
- package/src/transformers/factory.ts +0 -54
- package/src/transformers/html.ts +0 -20
- package/src/transformers/index.ts +0 -9
- package/src/transformers/length.ts +0 -28
- package/src/transformers/match-count.ts +0 -20
- package/src/transformers/number.ts +0 -30
- package/src/transformers/resolve.ts +0 -29
- package/src/transformers/transformers.ts +0 -27
- package/src/transformers/trim.ts +0 -32
- package/src/transformers/url-query-param.ts +0 -65
- package/tests/config/constant.test.ts +0 -23
- package/tests/extract.test.ts +0 -21
- package/tests/fixtures/basic.html +0 -16
- package/tests/transformers/attr.test.ts +0 -146
- package/tests/transformers/html.test.ts +0 -25
- package/tests/transformers/url-query-param.test.ts +0 -85
- package/tsconfig.json +0 -10
- package/tsup.config.ts +0 -12
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { NUMBER, PrimitiveTypes, STRING } from '../core/primitive-types';
|
|
2
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
3
|
-
import { InvalidParseInputError } from '../errors/invalid-parse-input-error';
|
|
4
|
-
|
|
5
|
-
export default class Length extends Transformer {
|
|
6
|
-
static getName(): string {
|
|
7
|
-
return 'length';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
inputType(): PrimitiveTypes {
|
|
11
|
-
return STRING;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
outputType(): PrimitiveTypes {
|
|
15
|
-
return NUMBER;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
transform({ property, val }: TransformParams) {
|
|
19
|
-
if (typeof val === 'string' || Array.isArray(val)) {
|
|
20
|
-
return val.length;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
throw new InvalidParseInputError(
|
|
24
|
-
property,
|
|
25
|
-
`Length.transform: invalid value type: ${typeof val}`
|
|
26
|
-
);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
import { NUMBER, PrimitiveTypes, STRING } from '../core/primitive-types';
|
|
2
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
3
|
-
|
|
4
|
-
export default class MatchCount extends Transformer {
|
|
5
|
-
static getName(): string {
|
|
6
|
-
return 'match-count';
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
inputType(): PrimitiveTypes {
|
|
10
|
-
return STRING;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
outputType(): PrimitiveTypes {
|
|
14
|
-
return NUMBER;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
transform({ $el }: TransformParams) {
|
|
18
|
-
return $el.length;
|
|
19
|
-
}
|
|
20
|
-
}
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
import { NUMBER, PrimitiveTypes, STRING } from '../core/primitive-types';
|
|
2
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
3
|
-
import { InvalidParseInputError } from '../errors/invalid-parse-input-error';
|
|
4
|
-
|
|
5
|
-
export default class NumberTransformer extends Transformer {
|
|
6
|
-
static getName(): string {
|
|
7
|
-
return 'number';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
inputType(): PrimitiveTypes {
|
|
11
|
-
return STRING;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
outputType(): PrimitiveTypes {
|
|
15
|
-
return NUMBER;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
transform({ property, val }: TransformParams) {
|
|
19
|
-
if (typeof val === 'string') {
|
|
20
|
-
return Number(val);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
throw new InvalidParseInputError(
|
|
24
|
-
property,
|
|
25
|
-
`NumberTransformer.transform: invalid value type: ${typeof val}. value=${JSON.stringify(
|
|
26
|
-
val
|
|
27
|
-
)}`
|
|
28
|
-
);
|
|
29
|
-
}
|
|
30
|
-
}
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { URL } from 'url';
|
|
2
|
-
import { PrimitiveTypes, STRING } from '../core/primitive-types';
|
|
3
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
4
|
-
import { InvalidParseInputError } from '../errors/invalid-parse-input-error';
|
|
5
|
-
|
|
6
|
-
export default class ResolveTransformer extends Transformer {
|
|
7
|
-
static getName(): string {
|
|
8
|
-
return 'resolve';
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
inputType(): PrimitiveTypes {
|
|
12
|
-
return STRING;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
outputType(): PrimitiveTypes {
|
|
16
|
-
return STRING;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
transform({ property, val, url }: TransformParams) {
|
|
20
|
-
if (typeof val === 'string') {
|
|
21
|
-
return new URL(val, url).toString();
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
throw new InvalidParseInputError(
|
|
25
|
-
property,
|
|
26
|
-
`ResolveTransformer.transform: invalid value type: ${typeof val}`
|
|
27
|
-
);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import AttributeTransformer from './attr';
|
|
2
|
-
import ExistsTransformer from './exists';
|
|
3
|
-
import HTMLTransformer from './html';
|
|
4
|
-
import LengthTransformer from './length';
|
|
5
|
-
import MatchCountTransformer from './match-count';
|
|
6
|
-
import NumberTransformer from './number';
|
|
7
|
-
import ResolveTransformer from './resolve';
|
|
8
|
-
import TrimTransformer from './trim';
|
|
9
|
-
import UrlQueryParamTransformer from './url-query-param';
|
|
10
|
-
|
|
11
|
-
export class Transformers {
|
|
12
|
-
static readonly transformers = [
|
|
13
|
-
AttributeTransformer,
|
|
14
|
-
ExistsTransformer,
|
|
15
|
-
HTMLTransformer,
|
|
16
|
-
LengthTransformer,
|
|
17
|
-
MatchCountTransformer,
|
|
18
|
-
NumberTransformer,
|
|
19
|
-
ResolveTransformer,
|
|
20
|
-
TrimTransformer,
|
|
21
|
-
UrlQueryParamTransformer,
|
|
22
|
-
];
|
|
23
|
-
|
|
24
|
-
static getByName(name: string) {
|
|
25
|
-
return this.transformers.find((tr) => tr.getName() === name);
|
|
26
|
-
}
|
|
27
|
-
}
|
package/src/transformers/trim.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
import { PrimitiveTypes, STRING } from '../core/primitive-types';
|
|
2
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
3
|
-
import { InvalidParseInputError } from '../errors/invalid-parse-input-error';
|
|
4
|
-
|
|
5
|
-
export default class Trim extends Transformer {
|
|
6
|
-
static getName(): string {
|
|
7
|
-
return 'trim';
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
inputType(): PrimitiveTypes {
|
|
11
|
-
return STRING;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
outputType(): PrimitiveTypes {
|
|
15
|
-
return STRING;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
transform({ property, val }: TransformParams) {
|
|
19
|
-
if (val === null) {
|
|
20
|
-
return null;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (typeof val === 'string') {
|
|
24
|
-
return val.trim();
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
throw new InvalidParseInputError(
|
|
28
|
-
property,
|
|
29
|
-
`Trim: invalid value type: ${typeof val}`
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
}
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
import { URL } from 'url';
|
|
2
|
-
import { STRING } from '../core/primitive-types';
|
|
3
|
-
import { TransformParams, Transformer } from '../core/transformer';
|
|
4
|
-
|
|
5
|
-
export default class UrlQueryParamTransformer extends Transformer {
|
|
6
|
-
constructor(private readonly args: string[]) {
|
|
7
|
-
super();
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
static getName(): string {
|
|
11
|
-
return 'urlQueryParam';
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
inputType(): typeof STRING {
|
|
15
|
-
return STRING;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
outputType(): typeof STRING {
|
|
19
|
-
return STRING;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
transform({
|
|
23
|
-
val,
|
|
24
|
-
}: TransformParams): string | null | Record<string, string | null> {
|
|
25
|
-
const { args } = this;
|
|
26
|
-
|
|
27
|
-
if (typeof val !== 'string') {
|
|
28
|
-
throw new Error(
|
|
29
|
-
`${UrlQueryParamTransformer.getName()}: expected string. Got ${JSON.stringify(
|
|
30
|
-
val
|
|
31
|
-
)}`
|
|
32
|
-
);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
const url = new URL(val);
|
|
36
|
-
|
|
37
|
-
// return all the query params
|
|
38
|
-
if (args.length === 0) {
|
|
39
|
-
return Object.fromEntries(url.searchParams.entries());
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (args.length === 1) {
|
|
43
|
-
return url.searchParams.get(args[0]!);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
return args.reduce((acc, arg) => {
|
|
47
|
-
acc[arg] = url.searchParams.get(arg);
|
|
48
|
-
|
|
49
|
-
return acc;
|
|
50
|
-
}, {} as Record<string, string | null>);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
private static getAttrValue(
|
|
54
|
-
$el: cheerio.Cheerio,
|
|
55
|
-
attr: string
|
|
56
|
-
): string | null {
|
|
57
|
-
const val = $el.attr(attr);
|
|
58
|
-
|
|
59
|
-
if (val === undefined) {
|
|
60
|
-
return null;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return val;
|
|
64
|
-
}
|
|
65
|
-
}
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
import { load } from 'cheerio';
|
|
2
|
-
import { ConfigFactory } from '../../src';
|
|
3
|
-
import { rootProp } from '../../src/core/property';
|
|
4
|
-
|
|
5
|
-
const yaml = `
|
|
6
|
-
constant: foo
|
|
7
|
-
`;
|
|
8
|
-
|
|
9
|
-
describe('ConstantConfig', () => {
|
|
10
|
-
it('Basic', () => {
|
|
11
|
-
const $ = load('');
|
|
12
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
13
|
-
const result = conf.extract({
|
|
14
|
-
$,
|
|
15
|
-
$el: $.root(),
|
|
16
|
-
property: rootProp,
|
|
17
|
-
url: 'https://example.com',
|
|
18
|
-
});
|
|
19
|
-
const expected = 'foo';
|
|
20
|
-
|
|
21
|
-
expect(result).toBe(expected);
|
|
22
|
-
});
|
|
23
|
-
});
|
package/tests/extract.test.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { load } from 'cheerio';
|
|
2
|
-
import { ConfigFactory, extract } from '../src';
|
|
3
|
-
|
|
4
|
-
const SAMPLE = `<div>
|
|
5
|
-
some
|
|
6
|
-
<span>nested</span>
|
|
7
|
-
<p>elements</p>
|
|
8
|
-
</div>`;
|
|
9
|
-
|
|
10
|
-
describe('Extract', () => {
|
|
11
|
-
// The extract() must start from the $.root() so we'll get
|
|
12
|
-
// the outer-most element at beginning
|
|
13
|
-
it('SelectRootElement', () => {
|
|
14
|
-
const $ = load(SAMPLE);
|
|
15
|
-
const config = ConfigFactory.fromYAML('{}');
|
|
16
|
-
const result = extract($, config, 'https://example.com');
|
|
17
|
-
const expected = $.root().text();
|
|
18
|
-
|
|
19
|
-
expect(result).toBe(expected);
|
|
20
|
-
});
|
|
21
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
<!DOCTYPE html>
|
|
2
|
-
<html lang="en">
|
|
3
|
-
<head>
|
|
4
|
-
<meta charset="UTF-8">
|
|
5
|
-
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
|
6
|
-
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
|
-
<title>Document</title>
|
|
8
|
-
</head>
|
|
9
|
-
<body>
|
|
10
|
-
<div class="foo">
|
|
11
|
-
<div class="bar">
|
|
12
|
-
baz
|
|
13
|
-
</div>
|
|
14
|
-
</div>
|
|
15
|
-
</body>
|
|
16
|
-
</html>
|
|
@@ -1,146 +0,0 @@
|
|
|
1
|
-
import { load } from 'cheerio';
|
|
2
|
-
import { ConfigFactory } from '../../src';
|
|
3
|
-
import { ExtractParams } from '../../src/config';
|
|
4
|
-
import { rootProp } from '../../src/core/property';
|
|
5
|
-
|
|
6
|
-
const SAMPLE = `
|
|
7
|
-
<div id="root">
|
|
8
|
-
<no-attributes>
|
|
9
|
-
no attributes defined
|
|
10
|
-
</no-attributes>
|
|
11
|
-
|
|
12
|
-
<span id="one-attr">
|
|
13
|
-
only id attribute is defined
|
|
14
|
-
</span>
|
|
15
|
-
|
|
16
|
-
<multiple-attributes foo="bar" baz="abc" other="attribute value">
|
|
17
|
-
multiple attributes defined
|
|
18
|
-
</multiple-attributes>
|
|
19
|
-
|
|
20
|
-
<list>
|
|
21
|
-
<item a="a1" b="b1" c="c1" />
|
|
22
|
-
<item a="a2" b="b2" c="c2" />
|
|
23
|
-
<item a="a3" b="b3" c="c3" />
|
|
24
|
-
<item a="a4" b="b4" c="c4" />
|
|
25
|
-
</list>
|
|
26
|
-
|
|
27
|
-
<nested-list>
|
|
28
|
-
<nested-item>
|
|
29
|
-
<a a="a1">foo</a>
|
|
30
|
-
</nested-item>
|
|
31
|
-
|
|
32
|
-
<nested-item>
|
|
33
|
-
<a a="a2">bar</a>
|
|
34
|
-
</nested-item>
|
|
35
|
-
|
|
36
|
-
<nested-item>
|
|
37
|
-
<a a="a3">baz</a>
|
|
38
|
-
</nested-item>
|
|
39
|
-
</nested-list>
|
|
40
|
-
</div>
|
|
41
|
-
`;
|
|
42
|
-
|
|
43
|
-
describe('AttrTransformer', () => {
|
|
44
|
-
let $: cheerio.Root, $el: cheerio.Cheerio;
|
|
45
|
-
let extractParams: ExtractParams;
|
|
46
|
-
|
|
47
|
-
beforeAll(() => {
|
|
48
|
-
$ = load(SAMPLE);
|
|
49
|
-
$el = $.root();
|
|
50
|
-
extractParams = {
|
|
51
|
-
$,
|
|
52
|
-
$el,
|
|
53
|
-
url: 'https://example.com',
|
|
54
|
-
property: rootProp,
|
|
55
|
-
};
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('ReturnAttributeValue', () => {
|
|
59
|
-
const yaml = `
|
|
60
|
-
selector: '#root'
|
|
61
|
-
transform: attr(id)`;
|
|
62
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
63
|
-
const result = conf.extract(extractParams);
|
|
64
|
-
const expected = 'root';
|
|
65
|
-
|
|
66
|
-
expect(result).toBe(expected);
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('ReturnNullWhenAttributeIsNotDefined', () => {
|
|
70
|
-
const yaml = `
|
|
71
|
-
selector: '#root'
|
|
72
|
-
transform: attr(non-existing-attribute)`;
|
|
73
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
74
|
-
const result = conf.extract(extractParams);
|
|
75
|
-
const expected = null;
|
|
76
|
-
|
|
77
|
-
expect(result).toBe(expected);
|
|
78
|
-
});
|
|
79
|
-
|
|
80
|
-
it('ReturnAllAttributesWhenNoAttrNameGiven', () => {
|
|
81
|
-
const yaml = `
|
|
82
|
-
selector: multiple-attributes
|
|
83
|
-
transform: attr()`;
|
|
84
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
85
|
-
const result = conf.extract(extractParams);
|
|
86
|
-
const expected = {
|
|
87
|
-
foo: 'bar',
|
|
88
|
-
baz: 'abc',
|
|
89
|
-
other: 'attribute value',
|
|
90
|
-
};
|
|
91
|
-
|
|
92
|
-
expect(result).toMatchObject(expected);
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it('ReturnEmptyObjectWhenNoAttrDefined', () => {
|
|
96
|
-
const yaml = `
|
|
97
|
-
selector: no-attributes
|
|
98
|
-
transform: attr()`;
|
|
99
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
100
|
-
const result = conf.extract(extractParams);
|
|
101
|
-
const expected = {};
|
|
102
|
-
|
|
103
|
-
expect(result).toMatchObject(expected);
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it('SelectMultipleAttributes', () => {
|
|
107
|
-
const yaml = `
|
|
108
|
-
selector: multiple-attributes
|
|
109
|
-
transform: attr(foo, baz)`;
|
|
110
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
111
|
-
const result = conf.extract(extractParams);
|
|
112
|
-
const expected = {
|
|
113
|
-
foo: 'bar',
|
|
114
|
-
baz: 'abc',
|
|
115
|
-
};
|
|
116
|
-
|
|
117
|
-
expect(result).toMatchObject(expected);
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should select multiple attributes for array elems', () => {
|
|
121
|
-
const yaml = `
|
|
122
|
-
selector: list item
|
|
123
|
-
items: { transform: 'attr(a, b)' }`;
|
|
124
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
125
|
-
const result = conf.extract(extractParams);
|
|
126
|
-
const expected = [
|
|
127
|
-
{ a: 'a1', b: 'b1' },
|
|
128
|
-
{ a: 'a2', b: 'b2' },
|
|
129
|
-
{ a: 'a3', b: 'b3' },
|
|
130
|
-
{ a: 'a4', b: 'b4' },
|
|
131
|
-
];
|
|
132
|
-
|
|
133
|
-
expect(result).toStrictEqual(expected);
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('nested list', async () => {
|
|
137
|
-
const yaml = `
|
|
138
|
-
selector: nested-list nested-item
|
|
139
|
-
items: { selector: 'a[a]', transform: attr(a) }`;
|
|
140
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
141
|
-
const result = conf.extract(extractParams);
|
|
142
|
-
const expected = ['a1', 'a2', 'a3'];
|
|
143
|
-
|
|
144
|
-
expect(result).toStrictEqual(expected);
|
|
145
|
-
});
|
|
146
|
-
});
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { load } from 'cheerio';
|
|
2
|
-
import { readFileSync } from 'fs';
|
|
3
|
-
import { ConfigFactory } from '../../src';
|
|
4
|
-
import { rootProp } from '../../src/core/property';
|
|
5
|
-
|
|
6
|
-
const yaml = `
|
|
7
|
-
selector: .foo
|
|
8
|
-
transform: html
|
|
9
|
-
`;
|
|
10
|
-
|
|
11
|
-
describe('HTML', () => {
|
|
12
|
-
it('CorrectlyExtractHTML', () => {
|
|
13
|
-
const $ = load(readFileSync('tests/fixtures/basic.html').toString());
|
|
14
|
-
const config = ConfigFactory.fromYAML(yaml);
|
|
15
|
-
const result = config.extract({
|
|
16
|
-
$,
|
|
17
|
-
$el: $.root(),
|
|
18
|
-
url: 'https://example.com',
|
|
19
|
-
property: rootProp,
|
|
20
|
-
});
|
|
21
|
-
const expected = $('.foo').html();
|
|
22
|
-
|
|
23
|
-
expect(result).toBe(expected);
|
|
24
|
-
});
|
|
25
|
-
});
|
|
@@ -1,85 +0,0 @@
|
|
|
1
|
-
import { load } from 'cheerio';
|
|
2
|
-
import { ConfigFactory } from '../../src';
|
|
3
|
-
import { ExtractParams } from '../../src/config';
|
|
4
|
-
import { rootProp } from '../../src/core/property';
|
|
5
|
-
|
|
6
|
-
const SAMPLE = `
|
|
7
|
-
<div id="main">
|
|
8
|
-
<a id="link1" href="https://google.com/search?q=Typescript&clientId=abc">Search Typescript on Google</a>
|
|
9
|
-
<a id="linkddg" href="https://duckduckgo.com/search?q=Typescript&from=searchBar">Search Typescript on DuckDuckGo</a>
|
|
10
|
-
</div>
|
|
11
|
-
|
|
12
|
-
<div id="other">
|
|
13
|
-
<a id="otherlink1" href="https://example.com/some/page">URL without any query params.</a>
|
|
14
|
-
</div>
|
|
15
|
-
`;
|
|
16
|
-
|
|
17
|
-
describe('UrlQueryParamTransformer', () => {
|
|
18
|
-
let $: cheerio.Root, $el: cheerio.Cheerio;
|
|
19
|
-
let extractParams: ExtractParams;
|
|
20
|
-
|
|
21
|
-
beforeAll(() => {
|
|
22
|
-
$ = load(SAMPLE);
|
|
23
|
-
$el = $.root();
|
|
24
|
-
extractParams = {
|
|
25
|
-
$,
|
|
26
|
-
$el,
|
|
27
|
-
url: 'https://example.com',
|
|
28
|
-
property: rootProp,
|
|
29
|
-
};
|
|
30
|
-
});
|
|
31
|
-
|
|
32
|
-
it('should return all query params as objects', () => {
|
|
33
|
-
const yaml = `
|
|
34
|
-
selector: '#main a'
|
|
35
|
-
items:
|
|
36
|
-
transform: [attr(href), urlQueryParam]`;
|
|
37
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
38
|
-
const result = conf.extract(extractParams);
|
|
39
|
-
const expected = [
|
|
40
|
-
{ clientId: 'abc', q: 'Typescript' }, // google
|
|
41
|
-
{ from: 'searchBar', q: 'Typescript' }, // duckduckgo
|
|
42
|
-
];
|
|
43
|
-
|
|
44
|
-
expect(result).toStrictEqual(expected);
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
// lookup for the q parameter in the search engine links
|
|
48
|
-
it('should return individual query parameter values', () => {
|
|
49
|
-
const yaml = `
|
|
50
|
-
selector: '#main a'
|
|
51
|
-
items:
|
|
52
|
-
transform: [attr(href), urlQueryParam(q)]`;
|
|
53
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
54
|
-
const result = conf.extract(extractParams);
|
|
55
|
-
const expected = [
|
|
56
|
-
'Typescript', // google
|
|
57
|
-
'Typescript', // duckduckgo
|
|
58
|
-
];
|
|
59
|
-
|
|
60
|
-
expect(result).toStrictEqual(expected);
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// lookup for clientId parameter in duckduckgo link
|
|
64
|
-
it('should return null if the parameter was not found', () => {
|
|
65
|
-
const yaml = `
|
|
66
|
-
selector: '#linkddg'
|
|
67
|
-
transform: [attr(href), urlQueryParam(clientId)]`;
|
|
68
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
69
|
-
const result = conf.extract(extractParams);
|
|
70
|
-
const expected = null;
|
|
71
|
-
|
|
72
|
-
expect(result).toStrictEqual(expected);
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
it('should return an empty object if no query params found', () => {
|
|
76
|
-
const yaml = `
|
|
77
|
-
selector: '#otherlink1'
|
|
78
|
-
transform: [attr(href), urlQueryParam]`;
|
|
79
|
-
const conf = ConfigFactory.fromYAML(yaml);
|
|
80
|
-
const result = conf.extract(extractParams);
|
|
81
|
-
const expected = {};
|
|
82
|
-
|
|
83
|
-
expect(result).toStrictEqual(expected);
|
|
84
|
-
});
|
|
85
|
-
});
|
package/tsconfig.json
DELETED