@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.
- package/.eslintrc.json +21 -0
- package/.prettierrc +3 -0
- package/README.md +75 -0
- package/config.yaml +0 -0
- package/dist/examples/simple.d.ts +2 -0
- package/dist/examples/simple.js +7586 -0
- package/dist/examples/simple.js.map +1 -0
- package/dist/src/index.d.ts +24 -0
- package/dist/src/index.js +7586 -0
- package/dist/src/index.js.map +1 -0
- package/examples/simple.html +26 -0
- package/examples/simple.ts +12 -0
- package/examples/simple.yaml +20 -0
- package/jest.config.ts +9 -0
- package/package.json +36 -0
- package/sample.html +0 -0
- package/src/config/config.ts +12 -0
- package/src/config/factory.ts +142 -0
- package/src/config/index.ts +6 -0
- package/src/config/plain-config.ts +19 -0
- package/src/config/types/array.test.ts +103 -0
- package/src/config/types/array.ts +61 -0
- package/src/config/types/constant.ts +15 -0
- package/src/config/types/object.test.ts +99 -0
- package/src/config/types/object.ts +65 -0
- package/src/config/types/primitive.ts +63 -0
- package/src/config/types/union.ts +36 -0
- package/src/config/types/with-selector.test.ts +32 -0
- package/src/config/types/with-selector.ts +26 -0
- package/src/config/validator.ts +14 -0
- package/src/config-schema.json +144 -0
- package/src/config-schema.test.ts +187 -0
- package/src/core/argument-types.ts +21 -0
- package/src/core/complex-types.ts +4 -0
- package/src/core/primitive-types.ts +5 -0
- package/src/core/property.ts +3 -0
- package/src/core/transformer.ts +21 -0
- package/src/errors/invalid-parse-input-error.ts +7 -0
- package/src/index.ts +26 -0
- package/src/jest.d.ts +5 -0
- package/src/setup-jest-matchers.ts +26 -0
- package/src/transformers/attr.ts +49 -0
- package/src/transformers/exists.ts +20 -0
- package/src/transformers/factory.test.ts +17 -0
- package/src/transformers/factory.ts +54 -0
- package/src/transformers/html.ts +20 -0
- package/src/transformers/index.ts +9 -0
- package/src/transformers/length.ts +28 -0
- package/src/transformers/match-count.ts +20 -0
- package/src/transformers/number.ts +30 -0
- package/src/transformers/resolve.ts +29 -0
- package/src/transformers/transformers.ts +27 -0
- package/src/transformers/trim.ts +32 -0
- package/src/transformers/url-query-param.ts +65 -0
- package/tests/config/constant.test.ts +23 -0
- package/tests/extract.test.ts +21 -0
- package/tests/fixtures/basic.html +16 -0
- package/tests/transformers/attr.test.ts +146 -0
- package/tests/transformers/html.test.ts +25 -0
- package/tests/transformers/url-query-param.test.ts +85 -0
- package/tsconfig.json +10 -0
- package/tsup.config.ts +12 -0
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { TransformParams, Transformer } from '../../core/transformer';
|
|
2
|
+
import ConfigWithSelector, {
|
|
3
|
+
ConfigWithSelectorExtractParams,
|
|
4
|
+
} from './with-selector';
|
|
5
|
+
|
|
6
|
+
export type Transform = Transformer | Transformer[];
|
|
7
|
+
|
|
8
|
+
export class PrimitiveValueConfig extends ConfigWithSelector {
|
|
9
|
+
private transform?: Transform;
|
|
10
|
+
|
|
11
|
+
private constructor() {
|
|
12
|
+
super();
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
extract(params: ConfigWithSelectorExtractParams) {
|
|
16
|
+
let val: unknown = null;
|
|
17
|
+
const $el = this.getSelectorMatches(
|
|
18
|
+
params.$el,
|
|
19
|
+
params.elementAlreadyMatched ?? false
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
if ($el.length > 0) {
|
|
23
|
+
val = $el.text();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (!this.transform) {
|
|
27
|
+
return val;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return this.transformVal(this.transform, { ...params, $el, val });
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static generate(
|
|
34
|
+
selector: ConfigWithSelector['selector'],
|
|
35
|
+
transform?: Transform
|
|
36
|
+
) {
|
|
37
|
+
const conf = new PrimitiveValueConfig();
|
|
38
|
+
|
|
39
|
+
conf.selector = selector;
|
|
40
|
+
conf.transform = transform;
|
|
41
|
+
|
|
42
|
+
return conf;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
protected transformVal(
|
|
46
|
+
transformer: Transform,
|
|
47
|
+
transformParams: TransformParams
|
|
48
|
+
): unknown {
|
|
49
|
+
const transform = transformer;
|
|
50
|
+
|
|
51
|
+
if (transform instanceof Transformer) {
|
|
52
|
+
return transform.transform(transformParams);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return transform.reduce((acc, tr) => {
|
|
56
|
+
if (typeof tr === 'string') {
|
|
57
|
+
return this.transformVal(tr, { ...transformParams, val: acc });
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return tr.transform({ ...transformParams, val: acc });
|
|
61
|
+
}, transformParams.val);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Config, ExtractParams } from '../config';
|
|
2
|
+
import ConfigWithSelector from './with-selector';
|
|
3
|
+
|
|
4
|
+
export default class UnionConfig extends Config {
|
|
5
|
+
private constructor(private readonly configs: Config[]) {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
extract(params: ExtractParams) {
|
|
10
|
+
const $parent = params.$el;
|
|
11
|
+
|
|
12
|
+
for (const config of this.configs) {
|
|
13
|
+
if (config instanceof ConfigWithSelector) {
|
|
14
|
+
const $el = config.getSelectorMatches($parent, false);
|
|
15
|
+
|
|
16
|
+
if ($el.length === 0) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return config.extract({
|
|
21
|
+
...params,
|
|
22
|
+
$el,
|
|
23
|
+
elementAlreadyMatched: true,
|
|
24
|
+
});
|
|
25
|
+
} else {
|
|
26
|
+
return config.extract({ ...params, $el: params.$('') });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
static generate(configs: Config[]) {
|
|
34
|
+
return new UnionConfig(configs);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { load } from 'cheerio';
|
|
2
|
+
import ConfigWithSelector from './with-selector';
|
|
3
|
+
|
|
4
|
+
class FakeWithSelector extends ConfigWithSelector {
|
|
5
|
+
constructor(readonly selector: ConfigWithSelector['selector']) {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
extract() {
|
|
10
|
+
return '';
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const html = `
|
|
15
|
+
<div id="foo">
|
|
16
|
+
<span id="bar">
|
|
17
|
+
baz
|
|
18
|
+
</span>
|
|
19
|
+
</div>
|
|
20
|
+
`;
|
|
21
|
+
|
|
22
|
+
describe('ConfigWithSelector', () => {
|
|
23
|
+
it('ReturnSelf', () => {
|
|
24
|
+
const $ = load(html);
|
|
25
|
+
const $el = $.root();
|
|
26
|
+
const conf = new FakeWithSelector(null);
|
|
27
|
+
const $match = conf.getSelectorMatches($el, false);
|
|
28
|
+
const expected = $el;
|
|
29
|
+
|
|
30
|
+
expect($match).toBe(expected);
|
|
31
|
+
});
|
|
32
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Config, ExtractParams } from '../config';
|
|
2
|
+
|
|
3
|
+
export interface ConfigWithSelectorExtractParams extends ExtractParams {
|
|
4
|
+
elementAlreadyMatched?: boolean;
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export default abstract class ConfigWithSelector extends Config {
|
|
8
|
+
protected selector: string | null = null;
|
|
9
|
+
|
|
10
|
+
abstract extract(params: ConfigWithSelectorExtractParams): unknown;
|
|
11
|
+
|
|
12
|
+
getSelectorMatches(
|
|
13
|
+
$: cheerio.Cheerio,
|
|
14
|
+
alreadyMatched: boolean
|
|
15
|
+
): cheerio.Cheerio {
|
|
16
|
+
if (alreadyMatched) {
|
|
17
|
+
return $;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
if (this.selector === null) {
|
|
21
|
+
return $;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return $.find(this.selector);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import Ajv from 'ajv';
|
|
2
|
+
import * as schema from '../config-schema.json';
|
|
3
|
+
|
|
4
|
+
const ajv = new Ajv();
|
|
5
|
+
|
|
6
|
+
export class ConfigValidator {
|
|
7
|
+
constructor(private readonly plain: unknown) {}
|
|
8
|
+
|
|
9
|
+
validate(): Ajv['errors'] {
|
|
10
|
+
ajv.validate(schema, this.plain);
|
|
11
|
+
|
|
12
|
+
return ajv.errors;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "http://json-schema.org/draft-07/schema",
|
|
3
|
+
"type": "object",
|
|
4
|
+
"required": [],
|
|
5
|
+
"$id": "config-schema.json",
|
|
6
|
+
"oneOf": [
|
|
7
|
+
{
|
|
8
|
+
"$comment": "WithSelector",
|
|
9
|
+
"properties": {
|
|
10
|
+
"selector": {
|
|
11
|
+
"oneOf": [
|
|
12
|
+
{ "type": "string", "minLength": 1 },
|
|
13
|
+
{
|
|
14
|
+
"type": "array",
|
|
15
|
+
"items": {
|
|
16
|
+
"oneOf": [
|
|
17
|
+
{ "type": "string" },
|
|
18
|
+
{
|
|
19
|
+
"type": "object",
|
|
20
|
+
"properties": {
|
|
21
|
+
"selector": { "type": "string" },
|
|
22
|
+
"sampleHTMLs": {
|
|
23
|
+
"type": "array",
|
|
24
|
+
"items": { "type": "string" }
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"required": []
|
|
28
|
+
}
|
|
29
|
+
]
|
|
30
|
+
},
|
|
31
|
+
"minItems": 1
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"type": "object",
|
|
35
|
+
"properties": {
|
|
36
|
+
"selector": { "type": "string" },
|
|
37
|
+
"sampleHTMLs": {
|
|
38
|
+
"type": "array",
|
|
39
|
+
"items": { "type": "string" }
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
"required": []
|
|
43
|
+
}
|
|
44
|
+
]
|
|
45
|
+
},
|
|
46
|
+
"type": {
|
|
47
|
+
"type": "string",
|
|
48
|
+
"enum": ["string", "object", "array", "union"]
|
|
49
|
+
},
|
|
50
|
+
"items": { "$ref": "config-schema.json" },
|
|
51
|
+
"properties": {
|
|
52
|
+
"type": "object",
|
|
53
|
+
"additionalProperties": { "$ref": "config-schema.json" }
|
|
54
|
+
},
|
|
55
|
+
"transform": {
|
|
56
|
+
"oneOf": [
|
|
57
|
+
{
|
|
58
|
+
"type": "string",
|
|
59
|
+
"pattern": "^[\\w-]+(\\(([\\w-]+|(([\\w-]+\\s*,\\s*)*[\\w-]+))?\\))?$"
|
|
60
|
+
},
|
|
61
|
+
{
|
|
62
|
+
"type": "array",
|
|
63
|
+
"items": {
|
|
64
|
+
"type": "string",
|
|
65
|
+
"pattern": "^[\\w-]+(\\(([\\w-]+|(([\\w-]+\\s*,\\s*)*[\\w-]+))?\\))?$"
|
|
66
|
+
},
|
|
67
|
+
"minItems": 1
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
},
|
|
72
|
+
"required": [],
|
|
73
|
+
"additionalProperties": false,
|
|
74
|
+
"allOf": [
|
|
75
|
+
{
|
|
76
|
+
"$comment": "when the \"properties\" present, \"type\" can only be \"object\" or not defined at all",
|
|
77
|
+
"if": { "required": ["properties"] },
|
|
78
|
+
"then": { "properties": { "type": { "const": "object" } } }
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
"$comment": "when the \"type\" is \"object\", then the \"properties\" MUST be defined.",
|
|
82
|
+
"if": {
|
|
83
|
+
"properties": { "type": { "const": "object" } },
|
|
84
|
+
"required": ["type"]
|
|
85
|
+
},
|
|
86
|
+
"then": { "required": ["properties"] }
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"$comment": "objects cannot have transform",
|
|
90
|
+
"if": {
|
|
91
|
+
"anyOf": [
|
|
92
|
+
{
|
|
93
|
+
"properties": { "type": { "const": "object" } },
|
|
94
|
+
"required": ["type"]
|
|
95
|
+
},
|
|
96
|
+
{ "required": ["properties"] }
|
|
97
|
+
]
|
|
98
|
+
},
|
|
99
|
+
"then": {
|
|
100
|
+
"allOf": [
|
|
101
|
+
{ "required": ["properties"] },
|
|
102
|
+
{ "not": { "required": ["transform"] } }
|
|
103
|
+
]
|
|
104
|
+
}
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
"$comment": "when the \"items\" present, \"type\" can only be \"array\" or not defined at all",
|
|
108
|
+
"if": { "required": ["items"] },
|
|
109
|
+
"then": { "properties": { "type": { "const": "array" } } }
|
|
110
|
+
},
|
|
111
|
+
{
|
|
112
|
+
"$comment": "when the \"type\" is \"array\", then the \"properties\" MUST be defined.",
|
|
113
|
+
"if": {
|
|
114
|
+
"properties": { "type": { "const": "array" } },
|
|
115
|
+
"required": ["type"]
|
|
116
|
+
},
|
|
117
|
+
"then": {
|
|
118
|
+
"oneOf": [{ "required": ["items"] }, { "required": ["transform"] }]
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
]
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
"$comment": "Union",
|
|
125
|
+
"type": "object",
|
|
126
|
+
"properties": {
|
|
127
|
+
"union": {
|
|
128
|
+
"type": "array",
|
|
129
|
+
"items": { "$ref": "config-schema.json" },
|
|
130
|
+
"minItems": 1
|
|
131
|
+
}
|
|
132
|
+
},
|
|
133
|
+
"required": ["union"],
|
|
134
|
+
"additionalProperties": false
|
|
135
|
+
},
|
|
136
|
+
{
|
|
137
|
+
"$comment": "Constant",
|
|
138
|
+
"type": "object",
|
|
139
|
+
"properties": { "constant": {} },
|
|
140
|
+
"required": ["constant"],
|
|
141
|
+
"additionalProperties": false
|
|
142
|
+
}
|
|
143
|
+
]
|
|
144
|
+
}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
describe('Base', () => {
|
|
2
|
+
it('AllowEmptyConfigs', () => expect({}).toBeValidConfig());
|
|
3
|
+
it('NoAdditionalProperties', () =>
|
|
4
|
+
expect({ foo: 'bar' }).not.toBeValidConfig()
|
|
5
|
+
);
|
|
6
|
+
});
|
|
7
|
+
|
|
8
|
+
describe('Selector', () => {
|
|
9
|
+
it('CanBeString', () => {
|
|
10
|
+
expect({ selector: '.foo' }).toBeValidConfig();
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('CanBeStringArray', () => {
|
|
14
|
+
expect({ selector: ['.foo'] }).toBeValidConfig();
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('CanBeSelectorWithSampleHTMLs', () => {
|
|
18
|
+
expect({
|
|
19
|
+
selector: {
|
|
20
|
+
selector: 'some selector',
|
|
21
|
+
sampleHTMLs: []
|
|
22
|
+
}
|
|
23
|
+
}).toBeValidConfig();
|
|
24
|
+
|
|
25
|
+
expect({
|
|
26
|
+
selector: {
|
|
27
|
+
selector: 'some selector',
|
|
28
|
+
sampleHTMLs: ['tests/fixtures/sample.html']
|
|
29
|
+
}
|
|
30
|
+
}).toBeValidConfig();
|
|
31
|
+
|
|
32
|
+
expect({
|
|
33
|
+
selector: {
|
|
34
|
+
sampleHTMLs: []
|
|
35
|
+
}
|
|
36
|
+
}).toBeValidConfig();
|
|
37
|
+
|
|
38
|
+
expect({
|
|
39
|
+
selector: {},
|
|
40
|
+
}).toBeValidConfig();
|
|
41
|
+
|
|
42
|
+
expect({
|
|
43
|
+
selector: {
|
|
44
|
+
selector: '.foo'
|
|
45
|
+
},
|
|
46
|
+
}).toBeValidConfig();
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('CanBeFullyEmpty', () => {
|
|
50
|
+
expect({ selector: {} }).toBeValidConfig();
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
it('CannotBeEmpty', () => {
|
|
54
|
+
expect({ selector: '' }).not.toBeValidConfig();
|
|
55
|
+
expect({ selector: [] }).not.toBeValidConfig();
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it('ForbidInvalidValues', () => {
|
|
59
|
+
expect({ selector: 123 }).not.toBeValidConfig();
|
|
60
|
+
expect({ selector: true }).not.toBeValidConfig();
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('String', () => {
|
|
65
|
+
it('Selector', () => expect({ selector: '.foo' }).toBeValidConfig());
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
describe('Object', () => {
|
|
69
|
+
it('ImplyTypeToBeObjectWhenPropertiesPresent', () => {
|
|
70
|
+
expect({
|
|
71
|
+
selector: '.foo',
|
|
72
|
+
properties: { foo: { selector: '.bar' } },
|
|
73
|
+
type: 'object',
|
|
74
|
+
}).toBeValidConfig();
|
|
75
|
+
expect({
|
|
76
|
+
selector: '.foo',
|
|
77
|
+
properties: { foo: { selector: '.bar' } },
|
|
78
|
+
type: 'string',
|
|
79
|
+
}).not.toBeValidConfig();
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('PropertiesMustBeValidConfigs', () => {
|
|
83
|
+
expect({ properties: { x: 123 } }).not.toBeValidConfig();
|
|
84
|
+
expect({
|
|
85
|
+
selector: '.ttt',
|
|
86
|
+
properties: { x: { selector: '.foo' } },
|
|
87
|
+
}).toBeValidConfig();
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('PropertiesMustBeDefinedWhenTypeIsObject', () => {
|
|
91
|
+
expect({ selector: '.bar', type: 'object' }).not.toBeValidConfig();
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('CannotHaveTransform', () => {
|
|
95
|
+
const conf = {
|
|
96
|
+
selector: '.foo',
|
|
97
|
+
properties: {
|
|
98
|
+
x: { selector: '.x' },
|
|
99
|
+
},
|
|
100
|
+
transform: 'length'
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
expect(conf).not.toBeValidConfig();
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
describe('Array', () => {
|
|
108
|
+
it('ImplyTypeToBeArrayWhenItemsPresent', () => {
|
|
109
|
+
expect({
|
|
110
|
+
selector: '.foo',
|
|
111
|
+
items: { selector: '.tt' },
|
|
112
|
+
type: 'array',
|
|
113
|
+
}).toBeValidConfig();
|
|
114
|
+
|
|
115
|
+
expect({
|
|
116
|
+
selector: '.foo',
|
|
117
|
+
items: { selector: '.tt' },
|
|
118
|
+
type: 'string',
|
|
119
|
+
}).not.toBeValidConfig();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('CannotHaveTransformAndItemsTogether', () => {
|
|
123
|
+
const conf = {
|
|
124
|
+
selector: '.foo',
|
|
125
|
+
type: 'array',
|
|
126
|
+
items: {
|
|
127
|
+
selector: '.x',
|
|
128
|
+
},
|
|
129
|
+
transform: 'length'
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
expect(conf).not.toBeValidConfig();
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
describe('Union', () => {
|
|
137
|
+
it('MustNotBeEmpty', () => {
|
|
138
|
+
expect({ union: [] }).not.toBeValidConfig();
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('MustNotContainInvalidConfig', () => {
|
|
142
|
+
expect({ union: [123] }).not.toBeValidConfig();
|
|
143
|
+
expect({ union: [{ selector: 123 }] }).not.toBeValidConfig();
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('MustForbidAnyOtherProps', () => {
|
|
147
|
+
expect({ union: [{ selector: '.foo' }], selector: '.bar' }).not.toBeValidConfig();
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
describe('Transform', () => {
|
|
152
|
+
it('FailOnNonStringValues', () => expect({ selector: '.foo', transform: 123 }).not.toBeValidConfig());
|
|
153
|
+
|
|
154
|
+
it('ValidTransformer', () => {
|
|
155
|
+
expect({ selector: '.foo', transform: 'length' }).toBeValidConfig();
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
it('AllowStringArray', () => {
|
|
159
|
+
expect({ selector: '.foo', transform: ['length', 'trim'] }).toBeValidConfig();
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
it('StringArrayCannotBeEmpty', () => {
|
|
163
|
+
expect({ selector: '.foo', transform: [] }).not.toBeValidConfig();
|
|
164
|
+
});
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
describe('Constant', () => {
|
|
168
|
+
it('String', () => {
|
|
169
|
+
expect({ constant: 'foo' }).toBeValidConfig();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it('Number', () => {
|
|
173
|
+
expect({ constant: 123 }).toBeValidConfig();
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
it('Boolean', () => {
|
|
177
|
+
expect({ constant: true }).toBeValidConfig();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('Object', () => {
|
|
181
|
+
expect({ constant: { foo: 'bar' } }).toBeValidConfig();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('Array', () => {
|
|
185
|
+
expect({ constant: [ 'foo' ] }).toBeValidConfig();
|
|
186
|
+
});
|
|
187
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ARRAY, ComplexTypes, OBJECT } from './complex-types';
|
|
2
|
+
import { BOOLEAN, NUMBER, PrimitiveTypes, STRING } from './primitive-types';
|
|
3
|
+
|
|
4
|
+
export type ArgumentTypes = PrimitiveTypes | ComplexTypes;
|
|
5
|
+
|
|
6
|
+
export function argTypeToStr(typ: ArgumentTypes) {
|
|
7
|
+
switch (typ) {
|
|
8
|
+
case STRING:
|
|
9
|
+
return 'string';
|
|
10
|
+
case NUMBER:
|
|
11
|
+
return 'number';
|
|
12
|
+
case BOOLEAN:
|
|
13
|
+
return 'boolean';
|
|
14
|
+
case OBJECT:
|
|
15
|
+
return 'object';
|
|
16
|
+
case ARRAY:
|
|
17
|
+
return 'array';
|
|
18
|
+
default:
|
|
19
|
+
throw new Error(`Unrecognized argument type: ${typ}`);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { PrimitiveTypes } from './primitive-types';
|
|
2
|
+
import { Property } from './property';
|
|
3
|
+
|
|
4
|
+
export interface TransformParams {
|
|
5
|
+
val: unknown;
|
|
6
|
+
$el: cheerio.Cheerio;
|
|
7
|
+
url: string;
|
|
8
|
+
property: Property;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export abstract class Transformer {
|
|
12
|
+
static getName(): string {
|
|
13
|
+
throw new Error('Not implemented');
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
abstract inputType(): PrimitiveTypes;
|
|
17
|
+
|
|
18
|
+
abstract outputType(): PrimitiveTypes;
|
|
19
|
+
|
|
20
|
+
abstract transform(params: TransformParams): unknown;
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/// <reference types="@types/cheerio/index.d.ts" />
|
|
2
|
+
|
|
3
|
+
import { load } from 'cheerio';
|
|
4
|
+
import { Config } from './config';
|
|
5
|
+
import { rootProp } from './core/property';
|
|
6
|
+
|
|
7
|
+
export { ConfigFactory } from './config';
|
|
8
|
+
|
|
9
|
+
export function extract<T = unknown>(
|
|
10
|
+
$: cheerio.Root | string,
|
|
11
|
+
config: Config<T>,
|
|
12
|
+
url: string
|
|
13
|
+
): T {
|
|
14
|
+
let $root = $;
|
|
15
|
+
|
|
16
|
+
if (typeof $root === 'string') {
|
|
17
|
+
$root = load($root);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return config.extract({
|
|
21
|
+
$: $root,
|
|
22
|
+
$el: $root.root(),
|
|
23
|
+
property: rootProp,
|
|
24
|
+
url,
|
|
25
|
+
});
|
|
26
|
+
}
|
package/src/jest.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import Ajv from 'ajv';
|
|
2
|
+
import * as schema from './config-schema.json';
|
|
3
|
+
|
|
4
|
+
const ajv = new Ajv();
|
|
5
|
+
|
|
6
|
+
expect.extend({
|
|
7
|
+
toBeValidConfig: function (received) {
|
|
8
|
+
const result = ajv.validate(schema, received);
|
|
9
|
+
|
|
10
|
+
return {
|
|
11
|
+
message: () =>
|
|
12
|
+
`Expected ${JSON.stringify(received)} ` +
|
|
13
|
+
(this.isNot
|
|
14
|
+
? 'to be an invalid config. But it threw no validation errors'
|
|
15
|
+
: `to be a valid Config. Got: ${JSON.stringify(ajv.errors)}`),
|
|
16
|
+
pass: result,
|
|
17
|
+
};
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
toExtract() {
|
|
21
|
+
return {
|
|
22
|
+
message: () => '',
|
|
23
|
+
pass: true,
|
|
24
|
+
};
|
|
25
|
+
},
|
|
26
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { STRING } from '../core/primitive-types';
|
|
2
|
+
import { TransformParams, Transformer } from '../core/transformer';
|
|
3
|
+
|
|
4
|
+
export default class AttributeTransformer extends Transformer {
|
|
5
|
+
constructor(private readonly args: string[]) {
|
|
6
|
+
super();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
static getName(): string {
|
|
10
|
+
return 'attr';
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
inputType(): typeof STRING {
|
|
14
|
+
return STRING;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
outputType(): typeof STRING {
|
|
18
|
+
return STRING;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
transform({ $el }: TransformParams): unknown {
|
|
22
|
+
const { args } = this;
|
|
23
|
+
|
|
24
|
+
switch (args.length) {
|
|
25
|
+
case 0:
|
|
26
|
+
return $el.attr();
|
|
27
|
+
case 1:
|
|
28
|
+
return AttributeTransformer.getAttrValue($el, args[0]);
|
|
29
|
+
default:
|
|
30
|
+
return args.reduce((acc, arg) => {
|
|
31
|
+
acc[arg] = AttributeTransformer.getAttrValue($el, arg);
|
|
32
|
+
return acc;
|
|
33
|
+
}, {} as Record<string, unknown>);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
private static getAttrValue(
|
|
38
|
+
$el: cheerio.Cheerio,
|
|
39
|
+
attr: string
|
|
40
|
+
): string | null {
|
|
41
|
+
const val = $el.attr(attr);
|
|
42
|
+
|
|
43
|
+
if (val === undefined) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
return val;
|
|
48
|
+
}
|
|
49
|
+
}
|