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 🙏

© 2024 – Pkg Stats / Ryan Hefner

@trosckey/scraper-utils

v0.5.1

Published

helpful tools for process text

Downloads

7

Readme

scraper-utils

helpful tools for scraping

ci npm publish

Table of contents

Installation

using npm

npm i @trosckey/scraper-utils

using yarn

yarn add @trosckey/scraper-utils

clearHtml

removes html tags, codes and line breaks(\n, \t, etc)

import { clearHtml } from '@trosckey/scraper-utils'
clearHtml('<div>Hello, &#61;World!\n</div>') // "Hello, World!"

date

parsing date formats, returns Date object
or null if cannot parse date

parseRuDate

parse ru date format

import { parseRuDate } from '@trosckey/scraper-utils'
parseRuDate('01 ноя 2020').toDateString() // "Sun Nov 01 2020"
parseRuDate('28 Декабря 2016').toDateString() // "Wed Dec 28 2016"

removeDoubleSpaces

replaces double spaces by single

import { removeDoubleSpaces } from '@trosckey/scraper-utils'
removeDoubleSpaces('Hello    ,     World    !   ') // 'Hello , World ! '

retryOnError

tries to execute the function until it runs out of
attempts (default 3). returns promise with resolved value
from executed function or null if all attempts failed.

retryOnError(
  // function to execute
  func: Function,
  {
    // The number of times to retry, 3 by default
    retries?: number,
    // func if you just want to log all failed tries
    logError?: (error: Error) => Promise<any> | any
    // executes on every error
    onError?: ({
      // error from executed function
      error: Error,
      // returns `true` if it was last try
      isFinalTry: boolean,
    }) => Promise<any> | any
  }
)

examples:

import { retryOnError } from '@trosckey/scraper-utils'

await retryOnError(() => (
  throw new Error("erorr >.>"),
), {
  tries: 2,
  logError: console.error
})

// ...

await retryOnError(() => throw new Error("erorr >.>"))

// ...

const data = await retryOnError(async () => {
  const response = await fetch('https://example.com/')
  return response.text()
}, {
  tries: 5,
  onError: ({ error, isFinalTry }) => {
    if (isFinalTry) {
      console.error("Cannot download page :(", error)
    }
  }
})

console.log(data) // '<!doctype html><html><head>...' or null

sleep

simple sleep function, takes a number (milliseconds)

import { sleep } from '@trosckey/scraper-utils'
await sleep(5000)

sliceTextByWords

slices text by words

import { sliceTextByWords } from '@trosckey/scraper-utils'
sliceTextByWords(
  `
    Lorem Ipsum: It was popularised in the 1960s
    Ipsum passages: and more recently with desktop
    publishing: software like Aldus PageMaker including versions of.
  `,
  [
    'Lorem Ipsum:',
    'Ipsum passages:',
    'publishing:'
  ]
)
/**
 * {
 *   'Lorem Ipsum:': ' It was popularised in the 1960s',
 *   'Ipsum passages:': ' and more recently with desktop',
 *   'publishing:': ' software like Aldus PageMaker including versions of.'
 * }

treeSearch

search node in tree, returns found node or null

import { treeSearch } from '@trosckey/scraper-utils'
const tree = {
  id: 1,
  children: [
    { id: 2 },
    {
      id: 3,
      children: [
        { id: 4 }
      ]
    }
  ]
}

treeSearch(
  tree,
  'children',
  node => node.id === 4
) // { id: 4 }

treeSearch(
  tree,
  'children',
  node => node.id === 5
) // null