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 🙏

© 2025 – Pkg Stats / Ryan Hefner

sb-util-ts

v2.13.0

Published

Typescript utility

Readme

util-ts

Lightweight Typescript utility for

  • checking that a variable is a non-empty string, array or object
  • pluralizing english words
  • capitalizing words
  • loading JSON files and package json parameters
  • comparing arrays
  • getting a bool value from a string expression (like env var)
  • making a random number from range
  • ... many more ... just read below:

offers the following functions

  • stringIsEmpty(value: string): boolean - returns true if the given value is not a string or empty
  • capitalize(value: string): string - returns the given string with an upper-cased first character
  • deCapitalize(value: string): string - returns the given string with a lower-cased first character
  • pluralize(value: string): string - returns the given string as plural (like onion -> onions or entity -> entities) with different detection of word endings (like y -> ies)
  • randomString(length: number, chars?: string): string - returns a random string with the given length. Optionally takes a list of chars that the random string is allowed to contain in form of a string.
  • arrayIsEmpty(value: any[]): boolean - returns true is the given array is not an array or empty
  • mapIsEmpty(value: any): boolean - returns true is the given object is not an object or has no properties (meaning equals: {})
  • loadPackageInfo(fpath: string, key?:string, nodejs?: {fs: 'module:fs', path: 'module:path'}): any
    • returns the package.json given in fpath, optionally you can return a specific key's value by setting key
    • ATTENTION: needs fs and path from nodejs either as function parameter or setup like so:
     import * as fs from 'fs';
     import * as path from 'path';
     //import * as util from 'util'; // optional
      
     setupSbUtil({fs, path});
     //setupSbUtil({fs, path, util}); // optional
      
    // ALTERNATIVE
    const version = loadPackageInfo('.', 'version', {fs, path}) as string;
  • loadJSONFromFileSync(filePath: string, fs?: any): any - returns the parsed contents of a json file using the given fs or from setupSbUtil (see above)
  • async loadJSONFromFile(filePath: string, nodejs?: {fs: any, util: any}): Promise<any> - returns the parsed contents of a json file using the given fs or from setupSbUtil (see above)
  • compareArrays(left: any[], right: any[], comp?: ArrayItemSame, fullComp?: ArrayItemSame): ArrayCompareResult - Array comparator for full change detection. You can optionally add a compare - function that identifies ignoring changes (e.g. by a single property such as the id of a db object) and also a full comparison callback that returns true if there are no changes. Both default to comparing via JSON.stringify. Please read the comments in the src file. Types meaning:
    • ArrayCompareResult = { onlyInLeft: any[], changed: any[], same: any[],onlyInRight: any[] }
    • ArrayItemSame = (left: any, right: any) => boolean
  • filterAsync(array: any[], filter: (item: any) => Promise<boolean>): Promise<any[]> - like array filter this function excepts a promised filter function to run async
  • boolFromString(value: string, trim: boolean = true): boolean | undefined - returns true if a string made lower case equals 'yes', 'true' or '1', false if equals 'no', 'false', '0', in all other cases returns undefined
  • randomNumberForRange(min: number, max: number): number - returns a random number in the range between the given min and max parameters (including min and max)
  • sleep(milliseconds: number): Promise<void> - promised sleep function for ussage with await. e.g. await sleep(1500); // continues in next line after 1.5 sek
  • numberOfMatches(value: string, expression: RegExp | string, caseSensitive = false): number - checks how often an expression can be found in a string
  • clone<T = any>(data: T, deep = 1): T - clones an object, array or string including contents by the depth of deep to a new instance
  • prefixObjectKeys<T>(data: T, prefix: string): any - will return a new object by adding the prefix string to every single key. eg { name: 'Jim', age: 12 } plus prefix '_' => { _name: 'Jim', _age: 12 }
  • stripString(value: string, allowedChars: string | string[], caseSensitive = false): string - returns a new string containing all chars that are in value and allowedChars. e.g. stripString('Hello World', 'ael') return 'elll'
  • envVariable(varName: string, defaultValue: any, type: 'string' | 'boolean' | 'int' | 'float' = 'string'): string | boolean | number Returns an env variable or (if not set) the default value in the data type you choose
//// File Access:
/**
* a short wrap for util.promisify(fs.readFile)
* @param path - PathLik | number as in fs.readFile
* @param options - options as in fs.readFile
* @param nodejs - enables you to override fs and util, alternatively see docs for setupSbUtil
  */
  export async function readFileAsync(
  path: any,
  options: { encoding?: null | undefined; flag?: string | undefined; } | string | undefined | null,
  nodejs?: { fs: any, util: any }
  ): Promise<string | Buffer>

/**
* a short wrap for util.promisify(fs.writeFile)
* @param path - PathLik | number as in fs.writeFile
* @param data - as in fs.writeFile
* @param options - WriteFileOptions as in fs.writeFile
* @param nodejs - enables you to override fs and util, alternatively see docs for setupSbUtil
  */
  export async function writeFileAsync(
  path: any,
  data: any,
  options: WriteFileOptions,
  nodejs?: { fs: any, util: any }
  ): Promise<string | Buffer>

/**
* a short wrap for util.promisify(fs.exists) using fs.access
* @param path - PathLik | number as in fs.writeFile
* @param nodejs - enables you to override fs and util, alternatively see docs for setupSbUtil
  */
  export async function fileExists(
  path: any,
  nodejs?: { fs: any, util: any }
  ): Promise<boolean>

For more details:

  • read comments in https://github.com/sittingbool/util-ts/blob/master/src/util.ts
  • read tests in https://github.com/sittingbool/util-ts/blob/master/test/util.spec.ts

Custom types

  • interface IMap<T = string> { [key: string]: T } a key - value map where the key is always a string and the value is string by default but may be changed using the generic. This prevents the use of any for plain JS objects.
  • interface IMapAny { [key: string]: any } a key - value map where the key is always a string and the value is any. This prevents the use of any for plain JS objects, even as a generic.

browser support before version 2.0

  • is given since 1.1.1 (checking if window exists)
  • if you add it to angular 4+ please add this to your package.json to make ng serve possible without error:
"browser": {
    "fs": false,
    "path": false,`
    "util": false
}

Otherwise you will get a warning that fs or path cannot be found. Also the loadPackageInfo function will not be working without fs and path.