
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
prhone-tools
Advanced tools
Universal JavaScript Tools.
$ npm install --save prhone-tools
Merge two array of objects deeply by identifier.
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.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);
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.
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.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);
Execute serial asynchronous identified tasks based on promises.
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.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 an item by position in a list of positionated items.
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'.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);
FAQs
Universal JavaScript Tools
We found that prhone-tools demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.