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

sleepydogs

v1.0.7

Published

---

Downloads

22

Readme

sleepydogs


Dynamic JSON Badge NPM Unpacked Size (with version) NPM Collaborators GitHub last commit

Sleepy patterns for Javascript

What is this package?

This package is a collection of modules that offer opinionated abstractions over common patterns encountered in node.js development and web development.

Installation


npm install sleepydogs

pnpm add sleepydogs

yarn add sleepydogs

Usage

sleepydogs module collection is largely going to be divided into two sections: Functional Programming paradigms and Object Oriented Programming paradigms. These will be denoted as fp and oop.

fp: guard

Intended Usage: Reduce boilerplate around exception handling.

import { readFileSync } from 'node:fs';
import { guard } from 'sleepydogs';

/** Wrap a function that might fail in safewrap */
const safeReadFile = guard(readFileSync);

/**
 * data contains file contents if it exists
 * error contains any errors thrown during execution
 **/
const { data, error } = safeReadFile('hello.txt');

fp: logFactory

Intended Usage: Simple, very tiny color console logger. Proxy to console object.

import { Log } from 'sleepydogs';

const logger = Log.factory({
    service: 'my-service',
    level: 'info'
});

oop: Attempt

Intended Usage: Reduce boilerplate around exception handling void | Promise<void> functions

import { Attempt } from 'sleepydogs';

/** Sync example */

function writesFile() { /** Might throw an error */ }

const $writeFileAttempt = new Attempt(writesFile); /** writesFile not called yet */
$writeFileAttempt.runSync(); /** writesFile called here */

/** Advanced Configuration */

function writesFile2() {}
function handleWritesFile2Err() {} /** Handling an error */
const invokeImmediate = true; /** Invoke the callback on instance creation, so there's no need to call runSync */
const retryNum = 5; /** Set up retry logic up to N attempts */

const $writeFileAttempt2 = new Attempt(writesFile2, handleWritesFile2Err, invokeImmediate, retryNum);

/** Peek into the state of the callback */
const { state: cbState } = $writeFileAttempt2;

/** Async Example */

async function readEndpoint () {}

const $readEndpointAttempt = new Attempt(readEndpoint);
await $readEndpointAttempt.run();

oop: Option

Intended Usage: Intended Usage: Reduce boilerplate around functions that may or may not throw an error in the process of returning a value, or simply may or may not return a value. Has opt-in caching. Leans on the Rust concept of Options.

import { Option, OptionCache } from 'sleepydogs';

function fetchesDataSync() {}

const $fetchesDataSyncOption = new Option(fetchDataSync);
const { data, error } = $fetchesDataSyncOption.resolveSync();

// ... 

const cache = new OptionCache();
const cacheConfig = {
    optionCache: cache,
    indexingKey: 'mef-1',
    stale: 3600 // Time in ms before flushing value
};

const myExpensiveFn = () => { /** ... */ };

const $myExpensiveFnOption = new Option(myExpensiveFn, cacheConfig);

const { data, error } = $myExpensiveFnOption.resolveSync(); /** Cached value set here */
const _ = $myExpensiveFnOption.resolveSync(); /** Cached value used here */

/** Reusing a cache across options */

const cacheConfig2 = {
    optionCache: cache,
    indexingKey: 'mef-2',
};

function myExpensiveFn2() {}
const $mefOption2 = new Option(myExpensiveFn2, cacheConfig2);

/** Getting the value through match */

function anotherFn() {}
new Option(anotherFn)
    .resolveSync()
    .match(
        (value) => { /** Handle value */},
        (err) => { /** Handle error */ }
    );

oop: Signal

Intended Usage: Simple Signal Based State Management, see TC39 A Proposal To Add Signals To Javascript

import { Signal } from 'sleepydogs';

const stateSignal = new Signal.State(22);
const computedIsEvenSignal = new Signal.Computed(() => (stateSignal.get() % 2) === 0);

oop: LazySingleton

Intended Usage: Lazy singleton class instantiation and management

import { LazySingleton } from 'sleepydogs';

class MySingletonClass { /** Class Implementation */ }
const MySingletonClassProvider = LazySingleton(MySingletonClass); /** Class has not been instantiated yet */
const instanceA = MySingletonClassProvider.getInstance(); /** Class is instantiated here */
const instanceB = MySingletonClassProvider.getInstance(); /** Singleton instance used here */

console.log(Object.is(instanceA, instanceB)); // true