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

@celleb/js-utils

v1.2.0

Published

Typescript/Javascript utils

Downloads

9

Readme

js-utils

Typescript/Javascript utilities for arrays, objects, collections and more.

Getting started

npm i @celleb/js-utils --save

Available utils

arrays

Utils for arrays

Usage:

import arrays from '@celleb/js-utils/arrays';
// or
import { arrays } from '@celleb/js-utils';

uniqueFilter

A filter to return unique values in an array

Example:

import { uniqueFilter } from '@celleb/js-utils/arrays';
const cities = ['Windhoek', 'Bloemfontein', 'Windhoek'].filter(uniqueFilter); // ['Windhoek', 'Bloemfontein']

filterUnique

Returns an array of unique values

Example:

import { filterUnique } from '@celleb/js-utils/arrays';
const cities = filterUnique(['Windhoek', 'Bloemfontein', 'Windhoek']); // ['Windhoek', 'Bloemfontein']

stringSorter

Sort function to sort strings in alphabetical order

import { stringSorter } from '@celleb/js-utils/arrays';
['a', 'b', '1', '5'].sort(stringSorter)) // ['1', '5', 'a', 'b'];

sortStrings

Sort function to sort strings in alphabetical order

import { sortStrings } from '@celleb/js-utils/arrays';
sortStrings(['a', 'b', '1', '5']); // ['1', '5', 'a', 'b'];

sortByReference

Sort an array base on the order of the reference array.

import { sortByReference } from '@celleb/js-utils/arrays';
const order = ['faceTime', 'name'];
const input = ['name', 'lastName', 'faceTime', 'taxi'];
sortByReference(input, order)); // ['faceTime', 'name', 'lastName', 'taxi']

coll

Utils for collections. A collection is an array of objects.

Usage:

import coll from '@celleb/js-utils/coll';
// or
import { coll } from '@celleb/js-utils';

uniqueFilterByKey

Filters for unique items in a collection by the specified key

Example

import { uniqueFilterByKey } from '@celleb/js-utils/coll';
const coll = [
 { id: 1, name: 'Jonas' },
 { id: 1, name: 'Tomanga' },
];
coll.filter(uniqueFilterByKey('id')); // [{id: 1, name: 'Jonas'}]

filterByUniqueKey

Returns a collection of items filtered by unique key

Example

import { filterByUniqueKey } from '@celleb/js-utils/coll';
const coll = [
 { id: 1, name: 'Jonas' },
 { id: 1, name: 'Tomanga' },
];
filterByUniqueKey(coll, 'id'); // [{id: 1, name: 'Jonas'}]

updateColl

Adds or update items of a collection based on the specified key

Example

import { updateColl } from '@celleb/js-utils/coll';
const coll = [
 { id: 1, name: 'Jonas' },
 { id: 2, name: 'Tomanga' },
];
updateColl('id', coll, [{ id: '2', name: 'Manga' }]); // [{id: 1, name: 'Jonas'}, {id: 2, name: 'Manga'}]

obj

Utils for objects.

Usage:

import obj from '@celleb/js-utils/obj';
// or
import { obj } from '@celleb/js-utils';

omit

Returns an object with the specified keys omitted

Parameters

The first parameter is the object, and the subsequent parameters are the keys to be omitted. There's an autocomplete for keys in typescript.

Example

import { omit } from '@celleb/js-utils/obj';
const data = { 1: 'one', id: 'one', city: 'Opuwo', country: 'Namibia' };
omit(data, 1, 'id'); // {city: 'Opuwo', country: 'Namibia'}

omitExtra

Same as omit but also accounts for symbol keys

Example

import { omitExtra } from '@celleb/js-utils/obj';
const ID = Symbol('id');
const data = { [ID]: 'one', id: 'one', city: 'Opuwo', country: 'Namibia' };
omitExtra(data, ID, 'id'); // {city: 'Opuwo', country: 'Namibia'}

shallowTransform

Transforms the keys of the input object to the values of the given dictionary.

Example

import { shallowTransform } from '@celleb/js-utils/obj';
const input = { town: 'Windhoek', state: 'Namibia' };
const dictionary = { town: 'city', state: 'country' };
shallowTransform(input, dictionary); // {city: 'Windhoek', country: 'Namibia'}

swapKeysAndValues

Swaps keys with values in a shallow dictionary.

Example

import { swapKeysAndValues } from '@celleb/js-utils/obj';
const dictionary = { town: 'city', state: 'country' };
swapKeysAndValues(dictionary); // {city: 'town', country: 'state'}

shallowToDeepTransform

Transforms the input keys to those in a shallow dictionary

Example

import { shallowToDeepTransform } from '@celleb/js-utils/obj';
const input = { town: 'Windhoek', state: 'Namibia', cood: { lat: 1, lon: 2 } };
const dictionary = { town: 'city', state: 'country', 'cood.lat': 'point.x', 'cood.lon': 'point.y' };
shallowToDeepTransform(input, dictionary); // {city: 'Windhoek', country: 'Namibia', point: {x: 1, y: 2} }

utils

Additional utils

Usage:

import utils from '@celleb/js-utils/utils';
// or
import { utils } from '@celleb/js-utils';

clone

Creates and returns a copy of the input

Example

import { clone } from '@celleb/js-utils/obj';
clone({ id: 3 }); // {id: 3}

compare

Returns true when the stringify value of two items are identical.

Example

import { compare } from '@celleb/js-utils/obj';
const obj1 = { one: 1 };
const obj2 = { one: 1 };
obj1 === obj2; // false
compare(obj1, obj2); // true