@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,63 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,32 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
}
|
package/src/config/validator.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,21 +0,0 @@
|
|
|
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
|
-
}
|
package/src/core/property.ts
DELETED
package/src/core/transformer.ts
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
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
|
-
});
|
package/src/transformers/attr.ts
DELETED
|
@@ -1,49 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,17 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,54 +0,0 @@
|
|
|
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
|
-
}
|
package/src/transformers/html.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
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
|
-
}
|