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

prhone-tools

v1.5.3

Published

Universal JavaScript Tools

Downloads

21

Readme

prhone-tools

npm version Build Status license changelog prhone

Universal JavaScript Tools.

Install

$ npm install --save prhone-tools

merge-collections

Merge two array of objects deeply by identifier.

API

mergeCollections(array1, array2 [, options])

  • Array array1 - Source collection.
  • Array array2 - Collection to merge.
  • Object options - Optional options.
    • String id - The identifier. Default: 'id'.
    • Boolean shallow - Shallow extend. Default: false.
  • Returns a new collection with the merged items.

Example

const mergeCollections = require('prhone-tools/merge-collections');

const col1 = [
  { myId: 'x1', v: 'a' },
  { myId: 'x2', v: 'b' },
  { myId: 'x3', v: 'c' }
];
const col2 = [
  { myId: 'x1', v: 'x' },
  { myId: 'x2', v: 'y' },
  { myId: 'x4', v: 'd' }
];
const actual = mergeCollections(col1, col2, { id: 'myId' });
const expected = [
  { myId: 'x1', v: 'x' },
  { myId: 'x2', v: 'y' },
  { myId: 'x3', v: 'c' },
  { myId: 'x4', v: 'd' }
];
expect(actual).to.eql(expected);

throttle

Returns a function that will be called once in an interval of time right away when it is called but will not be called more than once per interval no matter how many times you call it.

API

throttle(fn [, opts])

  • Function fn - Function to throttle.
  • Object opts - Optional options.
    • Number interval - Optional time in milliseconds to regulate. Default 1000.
    • Function gate - Optional function to validate the throttle. If it returns true, we prevent the throttle and function will be called as normal.
  • Returns a function to use as a throttle.

Example

A function that will feed you only once every 1 second. In total it will feed you three times within 2 seconds (at 0s, 1s and 2s).

const throttle = require('prhone-tools/throttle');

function feed () {
  // eat something healthy... really
}
const feedRegulated = throttle(feed);

feedRegulated();  // (CALLED) ok, eat an apple
feedRegulated();  // you have just eaten, I'll do it 1s later
feedRegulated();  // nope, you have to wait...

setTimeout(() => {
  // (CALLED) ok, 1s passed, eat an orange
  feedRegulated();  // again, you have just eaten, wait 1s more
  feedRegulated();  // wait wait, you'll get fat

  setTimeout(() => {
    // (CALLED) ok, another 1s passed, eat pizz... no no, eat a banana
  }, 1000);
}, 1000);

serial-async

Execute serial asynchronous identified tasks based on promises.

API

Promise serialAsync(name, fn)

  • String name - Identifier of the task.
  • Function fn - Task to execute in serial. It should return a promise to indicate when it finishes to execute the next task in queue.
  • Returns a promise when the task (resolved or rejected) is completed.

Example

Suppose we have a task to get data from database or write some files but we need to execute the task only once at the same time and we call the task many times simultaneously.

It should execute in order so:

const serialAsync = require('prhone-tools/serial-async');

function updateDatabase () {
  return serialAsync('taskName', function () {
    return somethingThatReturnsPromise().
      then(function () {
        // More async stuff...
      }).
      then(function () {
        // Still more async stuff...
      });
  });
}

updateDatabase(); // this one will be called right away
updateDatabase(); // this one will have to wait until current one ends
updateDatabase(); // this one is also in queue

move-in-array

Move an item by position in a list of positionated items.

API

Array moveInArray(list, opts)

  • Array list - Current list of items.
  • Object opts - Options.
    • String id - Element identifier to move.
    • Number to - Position to move element.
    • String key - Optional element identifier key in the list. Default 'id'.
    • String positionKey - Optional element position identifier key in the list. Default 'position'.
  • Returns a new list with the updated items. If no update was made the same list is returned.

Example

Move element by id 'i1' from position 1 to 3 where the id key is '_id' and the position key is 'pos'.

const moveInArray = require('prhone-tools/move-in-array');

const list = [
  { _id: 'i0', pos: 0 },
  { _id: 'i1', pos: 1 },
  { _id: 'i2', pos: 2 },
  { _id: 'i3', pos: 3 },
  { _id: 'i4', pos: 4 }
];
const actual = moveInArray(list, {
  id: 'i1',
  to: 3,
  key: '_id',
  positionKey: 'pos'
});
const expected = [
  { _id: 'i0', pos: 0 },
  { _id: 'i1', pos: 3 },  // moved down
  { _id: 'i2', pos: 1 },  // moved up
  { _id: 'i3', pos: 2 },  // moved up
  { _id: 'i4', pos: 4 }
];
expect(actual).to.eql(expected);
expect(actual).to.not.equal(list);