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

ugd10a

v0.8.0

Published

Collection of time, object and data utilities

Downloads

10

Readme

UGD10A!

A small bunch of data, time and object utilities placed into public scope for easier reuse.

Runtime data validation

Utility that ensures that incoming data is of required shape a type.

Simple usage example look like this:

import {Validator} from "ugd10a/validator";
// Data shape to validate incoming data against
type OuterData = {
    id: number,
    name: string,
    description?: string
}
// Create validator
const validator = new Validator<OuterData>({
    id: {type: "number"},
    name: {type: "string"},
    description: {type: "string", optional: true},
});

// This will return true
validator.validate({id: 1, name: "name"});

// As well as this
validator.validate({id: 1, name: "name", description: "description"});

// But this will return false
validator.validate({id: "1", name: "name", description: "description"});
// Reason for failure can be checked by validator.lastError property, so:
console.log(validator.lastError);
// Will return:
{
    "field": "id",
    "error": "Type mismatch. Expected: number, actual: string"
}

Configuration options

  • optional: Defines if it's fine for this field to be missing (default = false).

  • exactValue: Check entry for exact value.

  • type: String representing one of built in types: undefined | object | boolean | number | bigint | string | symbol | function or any of custom, array types : array | string[] | number[].
    (Value can contain a single entry or an array of entries.)

  • validator: Entry validator function of form: (value: any, field?: keyof T) => boolean | string where true indicate that value is valid, false is simple denial of value, and string represent reasoned denial that will be included in error message.

  • itemValidator: Array item validator that will be used only in conjunction with array data type.

  • notEmpty: Defines that empty values, such as empty strings or arrays, or undefined should not be accepted as valid ones.

In memory data caching

import {Cached, toMilliseconds} from "ugd10a";

const randomNumberCache = new Cached(
    // Async function that knows where to get new data
    async () => Math.floor(Math.random() * 0xFFFFFF),
    // Time in milliseconds which defines data update interval
    toMilliseconds(10, "seconds")
);
// This will output new random number every 10 seconds.
setInterval(
    async () => console.log(await randomNumberCache.getData()),
    toMilliseconds(1, "seconds")
);

Typical use case for this functionality would be to limit requests to scarce resource, like DB and keep last known value in memory between updates within single entry point.

TODO: More docs object utils, execution queue and time utils?