@purescraps/purehtml 0.7.0 → 0.8.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.
@@ -1,3 +1,6 @@
1
+ import { CheerioAPI, Cheerio } from 'cheerio';
2
+ import { AnyNode } from 'domhandler';
3
+
1
4
  /**
2
5
  * PureHTML backend class
3
6
  */
@@ -18,18 +21,19 @@ declare abstract class PureHTMLNode {
18
21
  abstract text(): string;
19
22
  }
20
23
 
24
+ type CheerioRoot = CheerioAPI;
21
25
  declare class PureHTMLCheerioBackend extends PureHTMLBackend {
22
26
  load(html: string | Buffer): PureHTMLCheerioDocument;
23
27
  }
24
28
  declare class PureHTMLCheerioDocument extends PureHTMLDocument {
25
29
  #private;
26
- constructor(root: cheerio.Root);
30
+ constructor(root: CheerioRoot);
27
31
  $(selector: string): PureHTMLCheerioNode[];
28
32
  root(): PureHTMLCheerioNode;
29
33
  }
30
34
  declare class PureHTMLCheerioNode extends PureHTMLNode {
31
35
  #private;
32
- constructor($: cheerio.Root, el: cheerio.Cheerio);
36
+ constructor($: CheerioRoot, el: Cheerio<AnyNode>);
33
37
  attr(name: string): string | null;
34
38
  attr(): PureHTMLNodeAttributes;
35
39
  find(selector: string): PureHTMLCheerioNode[];
@@ -59,8 +63,8 @@ declare class ConfigFactory {
59
63
  private static detectExpectedType;
60
64
  }
61
65
 
62
- declare const cheerio$1: PureHTMLCheerioBackend;
66
+ declare const cheerio: PureHTMLCheerioBackend;
63
67
 
64
68
  declare function extract<T = unknown>(backend: PureHTMLBackend, $: PureHTMLDocument | string, config: Config<T>, url: string): T;
65
69
 
66
- export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio$1 as cheerio, extract };
70
+ export { Config, ConfigFactory, PureHTMLBackend, PureHTMLDocument, PureHTMLNode, type PureHTMLNodeAttributes, cheerio, extract };
package/dist/src/index.js CHANGED
@@ -6472,7 +6472,7 @@ var PureHTMLCheerioNode = class _PureHTMLCheerioNode extends PureHTMLNode {
6472
6472
  if (name) {
6473
6473
  return this.#el.attr(name) ?? null;
6474
6474
  }
6475
- return this.#el.attr();
6475
+ return this.#el.attr() ?? {};
6476
6476
  }
6477
6477
  find(selector) {
6478
6478
  return this.#el.find(selector).toArray().map((el) => new _PureHTMLCheerioNode(this.#$, this.#$(el)));
@@ -6524,6 +6524,9 @@ var ConfigWithSelector = class extends Config {
6524
6524
  }
6525
6525
  return matches;
6526
6526
  }
6527
+ toString() {
6528
+ return `ConfigWithSelector(${this.selector})`;
6529
+ }
6527
6530
  };
6528
6531
 
6529
6532
  // src/config/types/primitive.ts
@@ -6589,6 +6592,7 @@ var ArrayConfig = class _ArrayConfig extends ConfigWithSelector {
6589
6592
  alreadyMatched: params.elementAlreadyMatched ?? false,
6590
6593
  includeRoot: false
6591
6594
  });
6595
+ if (!$el) return null;
6592
6596
  const matches = Array.isArray($el) ? $el : [$el];
6593
6597
  const conf = this.items || PrimitiveValueConfig.generate(null, this.transform);
6594
6598
  const extractOpts = { ...params };
@@ -6764,6 +6768,35 @@ var NumberTransformer = class extends Transformer {
6764
6768
  }
6765
6769
  };
6766
6770
 
6771
+ // src/transformers/remove-last-path-section.ts
6772
+ var RemoveLastPathSection = class _RemoveLastPathSection extends Transformer {
6773
+ static transformerName = "removeLastPathSection";
6774
+ static getName() {
6775
+ return _RemoveLastPathSection.transformerName;
6776
+ }
6777
+ inputType() {
6778
+ return STRING;
6779
+ }
6780
+ outputType() {
6781
+ return STRING;
6782
+ }
6783
+ transform({ val }) {
6784
+ if (val === null) return null;
6785
+ if (typeof val !== "string") {
6786
+ throw new Error(
6787
+ `${_RemoveLastPathSection.transformerName}: expects string. Got ${JSON.stringify(
6788
+ val
6789
+ )}`
6790
+ );
6791
+ }
6792
+ const url = new URL(val);
6793
+ const sections = url.pathname.split("/");
6794
+ sections.pop();
6795
+ url.pathname = sections.join("/");
6796
+ return url.toString();
6797
+ }
6798
+ };
6799
+
6767
6800
  // src/transformers/remove-url-query-param.ts
6768
6801
  var RemoveUrlQueryParam = class _RemoveUrlQueryParam extends Transformer {
6769
6802
  constructor(args) {
@@ -6791,10 +6824,16 @@ var RemoveUrlQueryParam = class _RemoveUrlQueryParam extends Transformer {
6791
6824
  )}`
6792
6825
  );
6793
6826
  }
6794
- if (args.length === 0) return val;
6795
6827
  const url = new URL(val);
6796
- for (const paramName of args) {
6797
- url.searchParams.delete(paramName);
6828
+ if (args.length === 0) {
6829
+ const keys = Array.from(url.searchParams.keys());
6830
+ keys.forEach((key) => {
6831
+ url.searchParams.delete(key);
6832
+ });
6833
+ } else {
6834
+ for (const paramName of args) {
6835
+ url.searchParams.delete(paramName);
6836
+ }
6798
6837
  }
6799
6838
  return url.toString();
6800
6839
  }
@@ -6899,6 +6938,7 @@ var Transformers = class _Transformers {
6899
6938
  JSONTransformer,
6900
6939
  Length,
6901
6940
  NumberTransformer,
6941
+ RemoveLastPathSection,
6902
6942
  RemoveUrlQueryParam,
6903
6943
  ResolveTransformer,
6904
6944
  Trim,
@@ -6997,15 +7037,18 @@ var ObjectConfig = class _ObjectConfig extends ConfigWithSelector {
6997
7037
  }
6998
7038
  const props = this.properties;
6999
7039
  const keys = Object.keys(props);
7000
- return keys.reduce((acc, key) => {
7001
- const config = props[key];
7002
- acc[key] = config.extract({
7003
- ...params,
7004
- $el,
7005
- property: this.makeProperty(params.property, key)
7006
- });
7007
- return acc;
7008
- }, {});
7040
+ return keys.reduce(
7041
+ (acc, key) => {
7042
+ const config = props[key];
7043
+ acc[key] = config.extract({
7044
+ ...params,
7045
+ $el,
7046
+ property: this.makeProperty(params.property, key)
7047
+ });
7048
+ return acc;
7049
+ },
7050
+ {}
7051
+ );
7009
7052
  }
7010
7053
  makeProperty(property, prop) {
7011
7054
  if (property === rootProp) {
@@ -7224,7 +7267,7 @@ var ConfigValidator = class {
7224
7267
  var ConfigFactory = class _ConfigFactory {
7225
7268
  static fromYAML(yaml) {
7226
7269
  const plain = (0, import_yaml.parse)(yaml);
7227
- return this.generate(plain);
7270
+ return _ConfigFactory.generate(plain);
7228
7271
  }
7229
7272
  static generate(plain) {
7230
7273
  const validator = new ConfigValidator(plain);
@@ -7251,24 +7294,28 @@ var ConfigFactory = class _ConfigFactory {
7251
7294
  case "constant":
7252
7295
  return ConstantConfig.generate(constant, selector);
7253
7296
  case "object": {
7254
- let propConfigs = void 0;
7297
+ let propConfigs;
7255
7298
  if (properties) {
7256
- propConfigs = Object.keys(properties).reduce((acc, key) => {
7257
- acc[key] = this.generate(properties[key]);
7258
- return acc;
7259
- }, {});
7299
+ propConfigs = Object.keys(properties).reduce(
7300
+ (acc, key) => {
7301
+ acc[key] = _ConfigFactory.generate(properties[key]);
7302
+ return acc;
7303
+ },
7304
+ {}
7305
+ );
7260
7306
  }
7261
7307
  return ObjectConfig.generate(selector, propConfigs);
7262
7308
  }
7263
7309
  case "array":
7264
7310
  return ArrayConfig.generate(
7265
7311
  selector,
7266
- items && this.generate(items),
7312
+ items && _ConfigFactory.generate(items),
7267
7313
  transform
7268
7314
  );
7269
7315
  case "union":
7270
7316
  return UnionConfig.generate(
7271
- union.map((cfg) => this.generate(cfg))
7317
+ // biome-ignore lint/style/noNonNullAssertion: union will be present here. because our schema already validated the input
7318
+ union.map((cfg) => _ConfigFactory.generate(cfg))
7272
7319
  );
7273
7320
  default:
7274
7321
  return PrimitiveValueConfig.generate(selector, transform);