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

@ack_inc/utils

v2.0.1

Published

A collection of utility functions I find myself needing across multiple projects

Downloads

9

Readme

node-utils

A collection of utility functions I find myself needing across multiple projects

Functions can be required individually like so: const fn = require('@ack_inc/utils/lib/<fn-name-in-kebab-case>')

Docs

generateRandomString(nChars, constraints)

Generates a string of length nChars that satisfies constraints

Use case: password or temporary token generation

Parameters

| Name | Type | Required? | Description | Default | Comments | | ----------- | ------ | :-------: | ----------------------------------------------------------------- | ------- | ----------------------- | | nChars | number | Y | The length of the string to generate | | Must be a whole number. | | constraints | object | N | Object specifying what characters can be in the generated string. | {} | |

constraints may have the following keys:

| Key | Type | Effect | | --------- | ------- | --------------------------------------- | | lowercase | Boolean | If true, include lowercase characters | | uppercase | Boolean | If true, include uppercase chracters | | digits | Boolean | If true, include digits | | symbols | Boolean | If true, include symbols |

If constraints has none of the above keys, it is assumed that all characters are allowed

Look in lib/generate-random-string.js for the full list of symbols used

Return value

| Type | Description | | ------ | ----------------- | | string | The random string |

Usage

const { generateRandomString: rsg } = require('@ack_inc/utils');
console.log(rsg(<anything but a whole number>)); //=> throws TypeError
console.log(rsg(0)); //=> ""
console.log(rsg(5)); //=> "3`8aE"
console.log(rsg(5, { lowercase: true })); //=> "fewjk"
console.log(rsg(5, { lowercase: true, uppercase: true, digits: true, symbols: true })); //=> "%q31G"

groupByMultiKeys(records, getKeys)

Group an array of records into an array of sub-arrays of records. Records go into the same sub-array ("group") if any of the keys generated by getKeys are === equal

Use case: you have an array of records (of people, say); the records are from multiple sources, and you want to merge records (for belonging to the same person) if any of several different "identifiers" (email, phone number, social media handle, etc.) match

Parameters

| Name | Type | Required? | Description | Default | Comments | | ------- | -------------- | :-------: | ----------------------------------------- | ------- | ---------------------------------------------------------------------------------------------------------------------------------- | | records | Array<object> | Y | Array of records to group | | A TypeError is thrown if records is not an array with at least one element | | getKeys | Function | Y | Function to generate keys for each record | | Should take a record as the sole param, and return a single key or array of keys; signature is getKeys(record: object): Array<T> |

Return value

| Type | Description | | ---------------------- | ------------------------------------------------------------------------------------------- | | Array<Array<object>> | Each element of the returned array is a group of records for which at least one key matched |

It may not be the case that there is a common key for all the records in a group. Further, there may be two records in a group that have no keys in common. For example, the following three records will be in the same group, through A and C do not have a common key, because they each have a key in common with another record in the group:

  • Record A with keys ['a', 'b']
  • Record B with keys ['b', 'c']
  • Record C with keys ['c', 'd']

Usage

See the tests in lib/group-by-multi-keys.test.js for usage examples


formatMobileNumber(mobileNumber, formatString)

Format a mobile number (must include country code) in different ways

Parameters

| Name | Type | Required? | Description | Default | Comments | | ------------ | ------ | :-------: | -------------------------------------------------------------------- | ------- | ------------------------------------------------------------------ | | mobileNumber | string | Y | The mobile number to be formatted | | An error is thrown if mobileNumber does not contain country code | | formatString | string | N | String describing how the provided mobile number should be formatted | "+CASR" | |

In formatString, only the following characters will be transformed:

| Character | Transformed into | | --------- | --------------------------------------------- | | C | country code | | A | area code (first 3 digits after country code) | | S | subscriber number (next 3 digits) | | R | remaining digits |

Any other characters will appear as-is in the returned string

Return value

| Type | Description | | ------ | --------------------------- | | string | The formatted mobile number |

Usage

const { formatMobileNumber: format } = require('@ack_inc/utils');
console.log(format(<anything but a mobile number with country code>)); //=> throws Error

const mobile = "+1 123 456 7890";
expect(format(mobile)).toEqual("+11234567890");
expect(format(mobile, "+CASR")).toEqual("+11234567890");
expect(format(mobile, "+C A S R")).toEqual("+1 123 456 7890");
expect(format(mobile, "+C (A) S-R")).toEqual("+1 (123) 456-7890");

buildArr(n, generatorFn)

Format a mobile number (must include country code) in different ways

Parameters

| Name | Type | Required? | Description | Default | Comments | | ----------- | -------- | :-------: | ---------------------------------------------------------------------- | ------- | -------- | | n | number | Y | The number of elements in the created array | | | | generatorFn | function | N | Function that controls the values of the elements in the created array | x => x | |

Return value

| Type | Description | | ----- | --------------- | | array | The built array |

Usage

const { buildArr } = require('@ack_inc/utils');

expect(buildArr(0)).toEqual([]);
expect(buildArr(2)).toEqual([0, 1]);
expect(buildArr(2, idx => idx + 1)).toEqual([1, 2]);

groupBy(records, groupFn)

Group records according to groupFn

Parameters

| Name | Type | Required? | Description | Default | Comments | | ------- | --------------- | :-------: | --------------------------------------------------------------------- | --------------- | -------- | | records | Array<record> | Y | The records to be grouped | | | | groupFn | function | N | Function that determines whether two records belong in the same group | (a, b) => false | |

A record is any object with an id key

Return value

| Type | Description | | ------------------------ | ------------------------------------------------- | | Array<Array<record>> | Each sub-array in the returned array is one group |

Usage

  • See group-by.test.js

To Do

  • Automated documentation generation

  • Improve generateRandomString

    • Constraint: whitelisted chars
    • Constraint: blacklisted chars
    • Constraint: symbols that can be part of a url-component (See SO answer)