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

@bscotch/utility

v7.2.0

Published

Bscotch Utilities: Methods for common Node.js needs.

Downloads

485

Readme

Bscotch Utilities

Utility and helper methods and types for common programming problems in Node.js.

Installation

Requirements

  • Node.js v16+

Installation

From npm:

npm install @bscotch/utility

Features

The following listings show a subset of the utilities found in this package.

Strings

import {
  capitalize,
  decodeFromBase64,
  encodeToBase64,
  decodeFromBase64JsonString,
  encodeToBase64JsonString,
  explode,
  nodent,
  oneline,
  undent,
} from '@bscotch/utility';

Paths

import { toPosixPath, sortedPaths, parentPaths } from '@bscotch/utility';

/*
 * [
 *  'hello',
 *  'h/another',
 *  'hello/world',
 *  'hello/world/goodbye'
 * ];
 */

// Get all paths leading to a target path
parentPaths('/hello/world/foo/bar.txt'); // =>
/*
 * [
 *  '/',
 *  '/hello',
 *  '/hello/world',
 *  '/hello/world/foo',
 *  '/hello/world/foo/bar.txt'
 * ]
 *
 */

Files

import {
  listPathsSync,
  listFoldersSync,
  listFilesSync,
  listFilesByExtensionSync,
  removeEmptyDirsSync,
} from '@bscotch/utility';

const recursive = true;

listPathsSync('.', recursive); // => paths to all files and folders in cwd
listFoldersSync('.', recursive); // => the subset of paths that are folders
listFilesSync('.', recursive); // => the subset of paths that are files
listFilesByExtensionSync('.', 'txt', recursive); // => the subset of files that end with '.txt'
listFilesByExtensionSync('.', ['txt', 'md'], recursive); // => the subset of files that end with '.txt' or '.md'
removeEmptyDirsSync('.'); // Remove all empty directories (recursively)

Waits

import {
  waitForMillis,
  waitForSeconds,
  waitForTick,
} from '@bscotch/utility';

async myAsynFunction(){
  // Wait for 1 second
  await waitForMillis(1000);
  // Wait for 1 second
  await waitForSeconds(1);
  // Wait until next tick
  await waitForTick();
}

Objects

import {
  isPlainObject,
  isPlainObjectOrArray,
  asObjectIfArray,
  flattenObjectPaths,
  objectPaths,
  getValueAtPath,
  setValueAtPath,
  objectPathsFromWildcardPath,
  transformValueByPath,
} from '@bscotch/utility';

asObjectIfArray(['hello']); // return {'0':'hello'}
const testObject = {
  hello: 'world',
  nested: {
    layer: 1,
    array: [4, 6, 7],
  },
};
flattenObjectPaths(testObject); // returns:
/**
 * {
 *  'hello':'world',
 *  'nested.layer': 1,
 *  'nested.array.0': 4,
 *  'nested.array.1': 6,
 *  'nested.array.2': 7,
 * }
 */
objectPaths(testObject); // returns keys from flattenObjectPaths(testObject)
getValueAtPath(testObject, 'nested.array.2'); // returns 7
setValueAtPath(testObject, 'new.0.field', 10); // adds 'new' field to set to [{field:10}]
objectPathsFromWildcardPath('nested.*', testObject); // returns:
// ['nested.layer','nested.array']
objectPathsFromWildcardPath('nested.array.*', testObject); // returns:
// ['nested.array.0','nested.array.1','nested.array.2]
transformValueByPath(testObject, 'nested.array.*', (n) => ++n); // Increments all array values by 1

Crypto

import {
  md5,
  sha1,
  sha256,
  createHash,
  encrypt,
  decrypt,
} from '@bscotch/utility';

let hash = md5('hello world'); // hex hash
hash = sha256('hello world', 'base64'); // Base64 hash
hash = createHash('sha1', 'hello world');

const key = '00000000000000000000000000000000';
const encrypted = encrypt('Hello World', key);
const sourceAsBuffer = decrypt(encrypted, key);

Dates

import {
  dateSort,
  dateSortDescending,
  dateDifferenceMillis,
  dateDifferenceSeconds,
  dateDifferenceMinutes,
  dateDifferenceHours,
  dateDifferenceDays,
  dateIsGreaterThan,
  dateIsInTheFuture,
  dateIsInThePast,
  dateIsLessThan,
  dateIsOlderThanMillisAgo,
  dateIsOlderThanSecondsAgo,
  dateIsOlderThanMinutesAgo,
  dateIsOlderThanHoursAgo,
  dateIsOlderThanDaysAgo,
  isDate,
  isValidDate,
  assertIsValidDate
} from '@bscotch/utility';

Arrays

import {
  arrayWrapped,
  arrayUnwrapped,
  arrayIsIncreasing,
  arraySortNumeric,
  arraySortNumericDescending,
} from '@bscotch/utility';

arrayIsIncreasing([-10, 99, 1111]); // => true

arrayWrapped('hello'); // => ["hello"]
arrayWrapped(['hello']); // => ["hello"]
arrayWrapped(undefined); // => []

arrayUnwrapped('hello'); // => "hello"
arrayUnwrapped(['hello']); // => "hello"
arrayUnwrapped(['hello', 'goodbye']); // => "hello"