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

@sapphirecode/utilities

v1.8.8

Published

small utility functions to make much needed features easier to work with

Downloads

5

Readme

@sapphirecode/utilities

version: 1.8.x

small utility functions to make much needed features easier to work with

Installation

npm:

npm i --save @sapphirecode/utilities

yarn:

yarn add @sapphirecode/utilities

Usage

const util = require('@sapphirecode/utilities');

cut off decimal places to a specified point

util.truncate_decimal(12.345678, 2);
// returns 12.34

will return null instead of throwing on invalid json

util.try_parse_json('{{foo');

copy an object to prevent modification of the original

const obj = {foo: 'bar'};
const copy = util.copy_object(obj);
copy.foo = 'baz';
console.log(obj.foo); // bar

run a regular expression and get a callback for every result

const data = 'foobarfoo';
const regex = /foo/g;
util.run_regex(regex, data, (res) => {
  console.log(res[0]); // will output 'foo' 2 times
});

check if a variable is null, undefined or NaN

console.log(util.is_nil(parseInt('abc'))); // true
console.log(util.is_nil('foo')); // false
console.log(util.is_nil(42)); // false
console.log(util.is_nil(null)); // true
console.log(util.is_nil(undefined)); // true

filter an array recursively

const to_filter = [
  {name: 'include_foo'},
  {
    name: 'include_bar',
    children: [{name: 'foo'}, {name: 'bar'}],
  },
  {
    name: 'baz',
    children: [{name: 'include_foo'}, {name: 'bar'}],
  },
  {
    name: 'barbaz',
    children: [{name: 'foo'}, {name: 'bar'}],
  },
];
const filter = {
  field: 'name', // the field the filter will apply on
  filter: /^include_.*/iu, // regex filter
};

const result = util.recursive_filter(
  to_filter,
  [filter], // you can specify multiple filters, they will be connected using AND
  'children' // specify which field an objects children are stored in ('children' is default)
);

console.log(JSON.stringify(result, null, 2));
/* output:

[
  { name: 'include_foo' },
  {
    name:     'include_bar',
    children: [
      { name: 'foo' },
      { name: 'bar' }
    ]
  },
  {
    name:     'baz',
    children: [ { name: 'include_foo' } ]
  }
]*/

filters can also apply to multiple fields

const result = util.recursive_filter(
  [{name: 'foo', name_2: 'bar'}],
  [
    {
      filter: /foo bar/iu,
      field: ['name', 'name_2'], // fields will be joined with a space in between
      // {name: 'foo', name_2: 'bar'} will become 'foo bar'
    },
  ]
);

filter groups can be used to connect filters with OR

const result = util.recursive_filter(
  [...],
  [
    {
      filter: /foo bar/iu,
      field: 'name'
    },
    {
      or: [
        { filter: /foo/u, field: 'bar' },
        { filter: /bar/u, field: 'bar' }
      ]
    }
  ]
);

field values can be carried to filter runs on children to filter over the entire tree instead of just one element

  const to_filter = [
    {
      name:     'foo',
      children: [
        { name: 'bar' },
        { name: 'baz' },
        { foo: 'bar' }
      ]
    }
  ];

  const res = util.recursive_filter (
    to_filter,
    [ { field: 'name', filter: /foo bar/ui } ], // values will be separaed by a space
    'children',
    [ 'name' ] // specify which fields should be carried
    // the filters will receive the carried values instead of the plain fields
  );
  
  /* result:
  
  [
    {
      name:     'foo',
      children: [ { name: 'bar' } ]
    }
  ]
  */

custom functions to match items can also be used

const filter = {
  function: (element) => {
    return element.should_match;
  }
}

License

MIT © Timo Hocker [email protected]