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

@rickard.antonsson/ts-utility

v1.0.4

Published

Simple utility functions

Downloads

13

Readme

ts-utility

Simple utility functions

Array functions

simpleSort

Sorts an array using supplied functions to get values to sort by. Takes functions returning either strings or numbers.

Somewhat fast. Speed tests using 58mb json (100k entries) sorted by name length and then name descending took around 300ms

Example test case:

it('should sort pokemons on first letter of name and then by weight, descending ', () => {
  const result = simpleSort(
    json.pokemon,
    asc(i => i.name[0]),
    desc(i => Number.parseFloat(i.weight))
  );

  // Below would give the same result.
  // asc|desc functions above actually just returns this type of tuple.
  const result = simpleSort(
    json.pokemon,
    [i => i.name[0], 'asc'],
    [i => Number.parseFloat(i.weight), 'desc']
  );

  expect(result.slice(0, 10).map(i => `${i.name} ${i.weight}`)).toEqual([
    'Arcanine 155.0 kg',
    'Arbok 65.0 kg',
    'Aerodactyl 59.0 kg',
    'Articuno 55.4 kg',
    'Alakazam 48.0 kg',
    'Abra 19.5 kg',
    'Blastoise 85.5 kg',
    'Butterfree 32.0 kg',
    'Beedrill 29.5 kg',
    'Bulbasaur 6.9 kg',
  ]);
});

For more examples see tests

groupBy

A small implemententation of groupBy

type specificGroupNames = 'first' | 'second' | 'third';
const itemsWithSpecificGroups = items as Array<{
  id: number;
  group: specificGroupNames;
}>;

const result = groupBy(itemsWithSpecificGroups, item => item.group);
// result now contains the grouped arrays.
// typescript error occurs if you try to access result.noneexisting since its not in the group names

Other

Deep Partial Set

As the name implies, sets an objects and subobjects properties. Optionally use a function to modify current value.

it('should add 10 gold, 10 silver and 10 copper to persons pouch', () => {
  const person = {
    personal: {
      firstName: 'Test',
      lastName: 'Testsson',
    },
    items: {
      pouch: {color: 'blue', contents: {gold: 10, silver: 20, copper: 30}},
    },
  };

  const result = deepPartialSet(person, {
    items: {
      pouch: {
        contents: {
          gold: i => i + 10,
          silver: i => i + 10,
          copper: i => i + 10,
        },
      },
    },
  });

  expect(result.items.pouch.contents).toEqual({
    gold: 20,
    silver: 30,
    copper: 40,
  });
  // Keep unchanged objects
  expect(result.personal).toBe(person.personal);
  // New objects for the complete tree where a child has a change
  expect(result).not.toBe(person);
  expect(result.items).not.toBe(person.items);
  expect(result.items.pouch).not.toBe(person.items.pouch);
  expect(result.items.pouch.contents).not.toBe(person.items.pouch.contents);
});