@purescraps/purehtml 0.1.0

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.
Files changed (62) hide show
  1. package/.eslintrc.json +21 -0
  2. package/.prettierrc +3 -0
  3. package/README.md +75 -0
  4. package/config.yaml +0 -0
  5. package/dist/examples/simple.d.ts +2 -0
  6. package/dist/examples/simple.js +7586 -0
  7. package/dist/examples/simple.js.map +1 -0
  8. package/dist/src/index.d.ts +24 -0
  9. package/dist/src/index.js +7586 -0
  10. package/dist/src/index.js.map +1 -0
  11. package/examples/simple.html +26 -0
  12. package/examples/simple.ts +12 -0
  13. package/examples/simple.yaml +20 -0
  14. package/jest.config.ts +9 -0
  15. package/package.json +36 -0
  16. package/sample.html +0 -0
  17. package/src/config/config.ts +12 -0
  18. package/src/config/factory.ts +142 -0
  19. package/src/config/index.ts +6 -0
  20. package/src/config/plain-config.ts +19 -0
  21. package/src/config/types/array.test.ts +103 -0
  22. package/src/config/types/array.ts +61 -0
  23. package/src/config/types/constant.ts +15 -0
  24. package/src/config/types/object.test.ts +99 -0
  25. package/src/config/types/object.ts +65 -0
  26. package/src/config/types/primitive.ts +63 -0
  27. package/src/config/types/union.ts +36 -0
  28. package/src/config/types/with-selector.test.ts +32 -0
  29. package/src/config/types/with-selector.ts +26 -0
  30. package/src/config/validator.ts +14 -0
  31. package/src/config-schema.json +144 -0
  32. package/src/config-schema.test.ts +187 -0
  33. package/src/core/argument-types.ts +21 -0
  34. package/src/core/complex-types.ts +4 -0
  35. package/src/core/primitive-types.ts +5 -0
  36. package/src/core/property.ts +3 -0
  37. package/src/core/transformer.ts +21 -0
  38. package/src/errors/invalid-parse-input-error.ts +7 -0
  39. package/src/index.ts +26 -0
  40. package/src/jest.d.ts +5 -0
  41. package/src/setup-jest-matchers.ts +26 -0
  42. package/src/transformers/attr.ts +49 -0
  43. package/src/transformers/exists.ts +20 -0
  44. package/src/transformers/factory.test.ts +17 -0
  45. package/src/transformers/factory.ts +54 -0
  46. package/src/transformers/html.ts +20 -0
  47. package/src/transformers/index.ts +9 -0
  48. package/src/transformers/length.ts +28 -0
  49. package/src/transformers/match-count.ts +20 -0
  50. package/src/transformers/number.ts +30 -0
  51. package/src/transformers/resolve.ts +29 -0
  52. package/src/transformers/transformers.ts +27 -0
  53. package/src/transformers/trim.ts +32 -0
  54. package/src/transformers/url-query-param.ts +65 -0
  55. package/tests/config/constant.test.ts +23 -0
  56. package/tests/extract.test.ts +21 -0
  57. package/tests/fixtures/basic.html +16 -0
  58. package/tests/transformers/attr.test.ts +146 -0
  59. package/tests/transformers/html.test.ts +25 -0
  60. package/tests/transformers/url-query-param.test.ts +85 -0
  61. package/tsconfig.json +10 -0
  62. package/tsup.config.ts +12 -0
@@ -0,0 +1,20 @@
1
+ import { BOOLEAN, PrimitiveTypes, STRING } from '../core/primitive-types';
2
+ import { TransformParams, Transformer } from '../core/transformer';
3
+
4
+ export default class Exists extends Transformer {
5
+ static getName(): string {
6
+ return 'exists';
7
+ }
8
+
9
+ inputType(): PrimitiveTypes {
10
+ return STRING;
11
+ }
12
+
13
+ outputType(): PrimitiveTypes {
14
+ return BOOLEAN;
15
+ }
16
+
17
+ transform({ $el }: TransformParams) {
18
+ return $el.length > 0;
19
+ }
20
+ }
@@ -0,0 +1,17 @@
1
+ import { TRANSFORMER_DEFINITION_REGEX } from './factory';
2
+
3
+ describe('TransformerFactory', () => {
4
+ it('TRANSFORMER_DEFINITION_REGEX', () => {
5
+ const validTransformerDefinitions = [
6
+ 'foo',
7
+ 'foo-ttt_fff',
8
+ 'foo()',
9
+ 'foo(a)',
10
+ 'foo(a, b, c)'
11
+ ];
12
+
13
+ for (const def of validTransformerDefinitions) {
14
+ expect(def).toMatch(new RegExp(TRANSFORMER_DEFINITION_REGEX));
15
+ }
16
+ });
17
+ });
@@ -0,0 +1,54 @@
1
+ import { Transformer } from '../core/transformer';
2
+ import { Transformers } from './transformers';
3
+
4
+ export const TRANSFORMER_DEFINITION_REGEX =
5
+ '^[\\w-]+(\\(([\\w-]+|(([\\w-]+\\s*,\\s*)*[\\w-]+))?\\))?$';
6
+
7
+ const TRANSFORMER_NAME_REGEX = /^([\w-]+[^(])/;
8
+ const TRANSFORMER_ARGUMENT_PARANTHESIS_REGEX = /\(([\w-]+\s*,?\s*)*\)/;
9
+ const TRANSFORMER_ARGUMENT_NAME_REGEX = /[\w-]+/g;
10
+
11
+ export class TransformerFactory {
12
+ static create(transform: string): Transformer {
13
+ const transformerName =
14
+ TransformerFactory.extractTransformerName(transform);
15
+ const args = TransformerFactory.extractTransformerArgs(transform);
16
+ const TransfomerImpl = Transformers.getByName(transformerName);
17
+
18
+ if (!TransfomerImpl) {
19
+ throw new Error(`Transformer with name "${transformerName}" not found.`);
20
+ }
21
+
22
+ return new TransfomerImpl(args);
23
+ }
24
+
25
+ private static extractTransformerName(def: string) {
26
+ const matches = def.match(TRANSFORMER_NAME_REGEX);
27
+
28
+ if (matches === null) {
29
+ throw new Error(`Invalid transformer name: ${def}`);
30
+ }
31
+
32
+ return matches[0];
33
+ }
34
+
35
+ private static extractTransformerArgs(def: string) {
36
+ const argsParanthesisMatch = def.match(
37
+ TRANSFORMER_ARGUMENT_PARANTHESIS_REGEX
38
+ );
39
+
40
+ if (argsParanthesisMatch === null) {
41
+ return [];
42
+ }
43
+
44
+ const argMatches = argsParanthesisMatch[0].match(
45
+ TRANSFORMER_ARGUMENT_NAME_REGEX
46
+ );
47
+
48
+ if (argMatches === null) {
49
+ return [];
50
+ }
51
+
52
+ return argMatches;
53
+ }
54
+ }
@@ -0,0 +1,20 @@
1
+ import { PrimitiveTypes, STRING } from '../core/primitive-types';
2
+ import { TransformParams, Transformer } from '../core/transformer';
3
+
4
+ export default class HTMLTransformer extends Transformer {
5
+ static getName(): string {
6
+ return 'html';
7
+ }
8
+
9
+ inputType(): PrimitiveTypes {
10
+ return STRING;
11
+ }
12
+
13
+ outputType(): PrimitiveTypes {
14
+ return STRING;
15
+ }
16
+
17
+ transform({ $el }: TransformParams) {
18
+ return $el.html();
19
+ }
20
+ }
@@ -0,0 +1,9 @@
1
+ export * from './attr';
2
+ export * from './exists';
3
+ export * from './factory';
4
+ export * from './html';
5
+ export * from './length';
6
+ export * from './match-count';
7
+ export * from './number';
8
+ export * from './resolve';
9
+ export * from './transformers';
@@ -0,0 +1,28 @@
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
+ }
@@ -0,0 +1,20 @@
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
+ }
@@ -0,0 +1,30 @@
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
+ }
@@ -0,0 +1,29 @@
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
+ }
@@ -0,0 +1,27 @@
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
+ }
@@ -0,0 +1,32 @@
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
+ }
@@ -0,0 +1,65 @@
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
+ }
@@ -0,0 +1,23 @@
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
+ });
@@ -0,0 +1,21 @@
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
+ });
@@ -0,0 +1,16 @@
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>
@@ -0,0 +1,146 @@
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
+ });
@@ -0,0 +1,25 @@
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
+ });
@@ -0,0 +1,85 @@
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
+ });