npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

xtor

v0.1.0

Published

Declarative HTML data extraction library with schema-based selectors

Readme

xtor

Declarative HTML data extraction library with schema-based selectors.

Features

  • Declarative Schema - Define data structure with simple JSON schemas
  • Cheerio-based - Fast and reliable HTML parsing
  • Loop Accumulation - Built-in support for paginated data extraction
  • Merge Strategies - Automatic data merging with concat/collect/merge strategies
  • Deduplication - Remove duplicates by single or multiple fields
  • TypeScript - Full type safety and IntelliSense support

Installation

npm install xtor
# or
pnpm add xtor
# or
yarn add xtor

Quick Start

Single Extraction

import { Extractor } from 'xtor';

const html = `
  <div class="product">
    <h3>iPhone 15</h3>
    <span class="price">$999</span>
  </div>
`;

const schema = {
  name: 'h3',
  price: '.price'
};

const extractor = new Extractor(schema);
const result = extractor.extract(html);

console.log(result);
// { name: 'iPhone 15', price: '$999' }

Array Extraction

const html = `
  <div class="products">
    <div class="product">
      <h3>iPhone 15</h3>
      <span class="price">$999</span>
    </div>
    <div class="product">
      <h3>MacBook Pro</h3>
      <span class="price">$2499</span>
    </div>
  </div>
`;

const schema = {
  products: ['.product', {
    name: 'h3',
    price: '.price'
  }]
};

const extractor = new Extractor(schema);
const result = extractor.extract(html);

console.log(result);
// {
//   products: [
//     { name: 'iPhone 15', price: '$999' },
//     { name: 'MacBook Pro', price: '$2499' }
//   ]
// }

Loop Accumulation

Perfect for pagination scenarios:

const schema = {
  products: ['.product', {
    id: '@data-id',
    name: 'h3',
    price: '.price'
  }]
};

const extractor = new Extractor(schema, {
  merge: 'concat',      // Concatenate arrays
  unique: 'id'          // Remove duplicates by id
});

const accumulator = extractor.loop();

// Extract from page 1
accumulator.extract(page1Html);

// Extract from page 2
accumulator.extract(page2Html);

// Get final merged result
const result = accumulator.getResult();

Schema Syntax

Basic Selectors

{
  title: 'h1',              // Text content
  image: 'img@src',         // Attribute value
  html: 'div@html',         // Inner HTML
  description: 'p'          // Text content
}

Arrays

// Simple array
{ links: ['a@href'] }

// Object array
{
  products: ['.product', {
    name: 'h3',
    price: '.price'
  }]
}

Nested Objects

{
  author: {
    name: '.author-name',
    avatar: '.author-avatar@src'
  }
}

Current Element

Use empty string to extract current element:

['.product', {
  id: '@data-id',
  text: ''              // Extract current element text
}]

Merge Strategies

concat (default for arrays)

Concatenates arrays from multiple extractions:

// Page 1: ['A', 'B']
// Page 2: ['C', 'D']
// Result: ['A', 'B', 'C', 'D']

collect (default for objects)

Collects objects into an array:

// Page 1: { name: 'Alice' }
// Page 2: { name: 'Bob' }
// Result: [{ name: 'Alice' }, { name: 'Bob' }]

merge

Merges objects using Object.assign:

// Page 1: { name: 'Alice' }
// Page 2: { age: 25 }
// Result: { name: 'Alice', age: 25 }

Deduplication

By Single Field

const extractor = new Extractor(schema, {
  merge: 'concat',
  unique: 'id'              // Keep first by default
});

By Multiple Fields

const extractor = new Extractor(schema, {
  merge: 'concat',
  unique: ['id', 'type']    // Composite key
});

Keep Last

const extractor = new Extractor(schema, {
  merge: 'concat',
  unique: {
    by: 'id',
    keep: 'last'            // Keep last occurrence
  }
});

API Reference

Extractor

class Extractor {
  constructor(schema: XRaySchema, strategy?: LoopStrategy);

  // Single extraction
  extract(html: string): XRayResult | any[];

  // Create accumulator for loop extraction
  loop(): ExtractionAccumulator;
}

ExtractionAccumulator

class ExtractionAccumulator {
  // Extract and accumulate
  extract(html: string): any;

  // Get current result without extraction
  getResult(): any;

  // Get iteration count
  getIterationCount(): number;

  // Reset accumulated state
  reset(): void;
}

Types

interface XRaySchemaObject {
  [key: string]: XRayValue;
}

type XRaySchema = XRaySchemaObject | [string, XRaySchemaObject];

type XRayValue =
  | string                          // Simple selector
  | XRaySchemaObject                // Nested object
  | Array<string>                   // Simple array
  | [string, XRaySchemaObject];     // Object array

interface LoopStrategy {
  merge: 'concat' | 'collect' | 'merge';
  unique?: string | string[] | {
    by: string | string[];
    keep?: 'first' | 'last';
  };
}

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.