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

prototype-fns

v1.3.1

Published

Custom JavaScript prototype functions to perform common operations with more ease and readability.

Downloads

6

Readme

A set of custom JavaScript prototypes that you will learn to love.

This library is adding custom prototype methods to your JavaScript that are easier to use and to understand. You don't need to import helpers everywhere you want to use them - The methods are directly available on the Object/Array.

Node.js CI

Table of Contents

Examples

const people = [
    { name: 'Chris' },
    { name: 'Giuliano' },
    { name: 'Vu' }
];

// Find
people.find(person => person.name === 'Chris'); // Before
people.findBy('name', 'Chris'); // After

// Map
people.map(person => person.name); // Before
people.mapBy('name'); // After

// Sort
people.sort((a, b) => a.prop - b.prop); // Before
people.orderBy('name'); // After

// Last
people[people.length - 1]; // Before
people.last(); // After

Installation

npm install prototype-fns

Usage

Make the custom prototypes available by importing them:

import 'prototype-fns';

You can also import a specific subset of functions, for example if you only need array related ones:

import 'prototype-fns/prototypes/array';

Or even single ones with:

import 'prototype-fns/prototypes/array/filter-by';

Array

allAfter(item)

const people = ['Chris', 'Giuliano', 'Vu'];

people.allAfter('Chris');
// ['Giuliano', 'Vu']

people.allAfter('Vu');
// []

allBefore(item)

const people = ['Chris', 'Giuliano', 'Vu'];

people.allBefore('Vu');
// ['Chris', 'Giuliano']

people.allBefore('Chris');
// []

filterBy(key, value)

const people = [
    { name: 'Chris', isAdmin: true },
    { name: 'Giuliano', isAdmin: false },
    { name: 'Vu', isAdmin: false }
];

people.filterBy('name', 'Chris');
// [
//   { name: 'Chris', isAdmin: true }
// ]

people.filterBy('isAdmin', false);
// [
//   { name: 'Giuliano', isAdmin: false },
//   { name: 'Vu', isAdmin: false },
// ]

people.filterBy('name', 'Tom');
// []

findBy(key, value)

const people = [
    { name: 'Chris' },
    { name: 'Giuliano' },
    { name: 'Vu' }
];

people.findBy('name', 'Giuliano');
// { name: 'Giuliano' }

people.findBy('name', 'Tom');
// undefined

first()

const people = [
    { name: 'Chris' },
    { name: 'Giuliano' },
    { name: 'Vu' }
];

people.first();
// { name: 'Chris' }

[].first();
// undefined

isAny(key, value)

const people = [
    { name: 'Chris', isAdmin: true },
    { name: 'Giuliano', isAdmin: true },
    { name: 'Vu', isAdmin: true }
];

people.isAny('name', 'Giuliano');
// true

people.isAny('isAdmin', false);
// false

isEvery(key, value)

const people = [
    { name: 'Chris', isAdmin: true },
    { name: 'Giuliano', isAdmin: true },
    { name: 'Vu', isAdmin: true }
];

people.isEvery('name', 'Vu');
// false

people.isEvery('isAdmin', true);
// true

last()

const people = [
    { name: 'Chris' },
    { name: 'Giuliano' },
    { name: 'Vu' }
];

people.last();
// { name: 'Vu' }

[].last();
// undefined

mapBy(key)

const people = [
    { name: 'Chris' },
    { name: 'Giuliano' },
    { name: 'Vu' }
];

people.mapBy('name');
// ['Chris', 'Giuliano', 'Vu']

mapProps(prop, ...)

const people = [
    { id: 1, name: 'Chris', token: 'abc' },
    { id: 2, name: 'Giuliano', token: 'def' },
    { id: 3, name: 'Vu', token: 'ghi' },
];

people.mapProps('id', 'name');
// [
//   { id: 1, name: 'Chris' },
//   { id: 2, name: 'Giuliano' },
//   { id: 3, name: 'Vu' }
// ]

mapPropsWithout(prop, ...)

const people = [
    { id: 1, name: 'Chris', token: 'abc' },
    { id: 2, name: 'Giuliano', token: 'def' },
    { id: 3, name: 'Vu', token: 'ghi' },
];

people.mapPropsWithout('token');
// [
//   { id: 1, name: 'Chris' },
//   { id: 2, name: 'Giuliano' },
//   { id: 3, name: 'Vu' }
// ]

oneAfter(item)

const people = ['Chris', 'Giuliano', 'Vu'];

people.oneAfter('Chris');
// 'Giuliano'

people.oneAfter('Vu');
// undefined

oneBefore(item)

const people = ['Chris', 'Giuliano', 'Vu'];

people.oneBefore('Vu');
// 'Giuliano'

people.oneBefore('Chris');
// undefined

order(direction?)

Use order() on flat arrays.

const people = ['Chris', 'Vu', 'Giuliano'];

people.order();
// ['Chris', 'Giuliano', 'Vu']

people.order('desc');
// ['Vu', 'Chris', 'Giuliano']

orderBy(key, direction?)

const people = [
    { name: 'Chris' },
    { name: 'Vu' },
    { name: 'Giuliano' }
];

people.orderBy('name', 'asc');
// [
//   { name: 'Chris' },
//   { name: 'Giuliano' },
//   { name: 'Vu' }
// ]

ℹ️ If you omit the direction, the result will be ordered ascending by default.

people.orderBy('name');
// [
//   { name: 'Chris' },
//   { name: 'Giuliano' },
//   { name: 'Vu' }
// ]

people.orderBy('name', 'desc');
// [
//   { name: 'Vu' },
//   { name: 'Giuliano' },
//   { name: 'Chris' }
// ]

shuffle()

const people = ['Chris', 'Vu', 'Giuliano'];

people.shuffle();
// Possible order: ['Giuliano', 'Chris', 'Vu']

sumBy(key)

const people = [
    { name: 'Chris', score: 1 },
    { name: 'Giuliano', score: 2 },
    { name: 'Vu', score: 3 },
];

people.sumBy('score');
// 6

without(item)

const people = ['Chris', 'Giuliano', 'Vu'];

people.without('Giuliano');
// ['Chris', 'Vu']

Object

isEmpty()

{ name: 'Chris' }.isEmpty();
// false

{}.isEmpty();
// true