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

@vgerbot/lazy

v1.0.7

Published

A library for defining lazily evaluated properties and variables powered by TypeScript.

Downloads

13

Readme

@vgerbot/lazy NPM

Test Codacy Badge Codacy Badge code style: prettier

A library for defining lazily evaluated properties and variables powered by TypeScript.

Motivation

@vgerbot/lazy is a useful tool for managing lazy evaluation properties. It defers expensive property(or a variable) initialization until needed and avoids unnecessary repeated evaluation. This library supports adding reset tester for lazy properties, once the reset tester detects a change, the property will be re-evaluated and initialized the next time the property is accessed, which simplifies a lot of work

Install

npm i @vgerbot/lazy

Usage

@lazyMember decorator

Requires the compilerOptions.experimentalDecorators in tsconfig.json to be set to true

import { lazyMember } from '@vgerbot/lazy';

function getFriends(id: string, params: Record<string, string | number> = {}): Promise<User[]> {
    const url = new URL('http://api.domain.com/friends');
    url.searchParams.append('userId', id);
    for(const key in params) {
        url.searchParams.append(key, params[key]);
    }
    console.log('fetch: ',url);
    return fetch(url)
        .then(resp => {
            return resp.json()
        })
        .then(data => userListOf(data));
}

class User {
    @lazyMember<User, 'friends'>(user => getFriends(user.id))
    friends!: Promise<User[]>;

    @lazyMember<User, 'littleBrothers'>({
        evaluate: user => getFriends(user.id, {maxAge: user.age}),
        resetBy: ['age']
    })
    littleBrothers!: Promise<User[]>;

    constructor(readonly id: string, age: number) {
    }
}

With the lazy properties ready, we can now use it:

const user = new User('a5cba8f', 18)

user.friends.then(() => {
})
// console output: "fetch:  http://api.domain.com/friends?userId=a5cba8f"

user.friends.then(() => {
}) // nothing output

user.littleBrothers.then(() => {
})
// console output: "fetch:  http://api.domain.com/friends?userId=a5cba8f&maxAge=18"

user.littleBrothers.then(() => {
})
// nothing is output

user.age = 19;
// The `age` property has changed, so the `littleBrothers` property will also be reset.

user.littleBrothers.then(() => {
})
// console output: "fetch:  http://api.domain.com/friends?userId=a5cba8f&maxAge=<b>19</b>"

lazyMemberOfClass

If you don't like or can't use decorators, you can do this:

import { lazyMemberOfClass } from '@vgerbot/lazy';

class User {
    friends!: Promise<User[]>;

    littleBrothers!: Promise<User[]>;

    constructor(readonly id: string, age: number) {
    }
}

lazyMemberOfClass(User, 'friends', (user) => getFriends(user.id));
lazyMemberOfClass(User, 'littleBrothers', {
    evaluate: user => getFriends(user.id, {maxAge: user.age}),
    resetBy: ['age']
});

The rest is the same as the appeal '@lazyMember', so I won't repeat it here.

lazyProp

Define lazily evaluated property on an object:

import { lazyProp } from '@vgerbot/lazy';

const giraffe = {} as { rainbow: string };

lazyProp(object, 'rainbow', () => expansiveComputation());

console.log(giraffe.rainbow);

lazyVal

Create a lazily evaluated value:

import { lazyVal } from '@vgerbot/lazy';

const value = lazyVal(() => expensiveComputation());

button.onclick = () => {
    doSomthing(value.get());
};

For more usage, see the unit test code.

License

License under the MIT Licensed (MIT)