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

ap-import

v1.0.1

Published

Easy way to import elements from an array of data

Downloads

4

Readme

AP IMPORT

v1.0.0

apImport

Usage

apImport takes 4 parameters

- items: Array = default array of items

- options: Object = 3 options

  • stopOnFail <default: true> = determine whether to throw an error and stop the importing or to go on and retain the "bad" items in an array
  • stopOnFailErr <default: "defaultErrorHandler"> = set the callback function that will handle the error in case stopOnFail is set to true. This callback function is called using 3 parameters:
// This is the default for stopOnFailErr
const defaultErrorHandler = (err, index, item) => {
  return (
    "Whoops... Something went wrong while importing. Here is the error: \n" +
    err +
    "\n" +
    "This error happened while importing this product: \n" +
    `items[${index}] => ${item}`
  );
};
  • continueOnFailErr <default: 5> = set the callback function that will handle the error in case stopOnFail is set to false. This callback function is called using 1 parameter:
// This is the default for continueOnFailErr
const defaultContinueErrorHandler = (arrayOfItems) => {
  console.log(
    "There were some products that didn't quite make the cut. Here is an array of indexes:",
    arrayOfItems
  );
};

- resolveCallback: Function = callback function with 1 parameter that returns a string(optional) and handles the product importing. This function will need to throw and error to enable the rejectCallback

- rejectCallback: Function = callback function with 2 parameters that returns a string(error message) and handles the product importing failure. This function is called using 2 parameters:

  • err: error message
  • item: the failed item

Examples

    import { apImport } from "ap-import";

    let databaseLMAO = [];

    const items = [30, 10, 20, 30, 40, 50, 60];

    await apImport(
        items,
        {
        stopOnFail: false,
        },
        async (item) => {
        if (item % 3 === 0) {
            databaseLMAO.push(item);
            return true;
        } else {
            // need to throw an error to access the rejectCallback
            throw new Error();
        }
        },
        async (err, _) => {
            return "custom err";
        }
    );

    // There were some products that didn't quite make the cut. Here is an array of indexes: [ 1, 2, 4, 5 ]
    console.log(databaseLMAO);
    // [30, 30, 60]
    databaseLMAO = [];

    try {
    await apImport(
    items,
    {
        stopOnFail: true,
    },
    async (item) => {
        if (item % 3 === 0) {
            databaseLMAO.push(item);
            return true;
        } else {
        // need to throw an error to access the rejectCallback
            throw new Error();
        }
    },
    async (err, _) => {
        return "custom err";
    }
    );
    } catch(err) {
        // try-catch method is done so you can see that the items that din't throw an error were corectly handled
        // without the try-catch block around the function (with stopOnFail: true) the application would have stopped on the error
        console.log(databaseLMAO);
        // [30]
        throw new Error(err);
        /*
        Error: Error: Whoops... Something went wrong while importing. Here is the error:
        custom err
        This error happened while importing this product:
        items[1] => 10
        */
    }