open-index/open-npm huggingface dataset:
npm package: @jack3898/lazy-object
Description: Lazily loaded objects with properties computed at get-time.
Author: Jack Wright
Latest version: 2.0.2

# Create Lazy Object

A tiny zero-dependency package that lets you define an object literal using a utility to lazily compute properties on access.

The first access to the property is cached, and then reused for subsequent accesses.

## lazy

The first way is to use `lazy`. This method returns a new object with the lazy properties added in an immutable way.

```ts
const lazyObject = lazy({
  test: () => {
    console.log("Doing something expensive...");

    return 42;
  },
});

// Output: 42
console.log(lazyObject.test);
```

You can even merge another object into the lazy object:

```ts
const lazyObject = lazy(
  {
    test: () => {
      console.log("Doing something expensive...");

      return 42;
    },
  },
  { test2: "Not lazy" }
);
```

## assignLazy

The second way is to use `assignLazy`. This method will mutate the object passed into it, unlike `lazy`. It does not return anything.

```ts
const object = {
  test: "hello",
};

assignLazy(object, "test2", () => "there!");

// Output: "there!"
console.log(object.test2);
```

## Type safety

This package is one of the most type-safe packages around lazy property creation.

The `lazy` return type signature is inferred from the return type of all the getter functions.

The `assignLazy` function asserts the type signature of objects passed into it, which means not only does it inject the property at runtime, but at the type level too.

## Note on module type

This package is distributed with ESM syntax only.

I apologise in advance for any inconvenience this may cause.
npm package: @jack3898/match-collection
Description: Run a smart matcher against a collection of items.
Author: Jack Wright
Latest version: 1.0.2

# Match Collection

A tiny zero-dependency package that lets you match whether a list of items meets a very specific condition.

## match()

Using `match()` you can match a collection against a tree of conditions.

Basic usage:

```ts
const matches = match(["hello", "there"]).with(or("hello", "world", "there"));
// Output: true
console.log(matches);
```

More advanced usage with combinators:

```ts
const complexQuery = or(
  and("apple", not("banana")),
  and(
    "date",
    and(
      "fig",
      not("grape"),
      // Warning: this predicate will be called for each item in the collection which may have performance implications
      predicate((item) => item.length > 3, "length greater than 3")
    )
  )
);

const matches = match(["date", "fig", "kiwi"]).with(complexQuery);
// Output: true
console.log(matches);
```

## Primary use case

This package is useful for filtering collections of items based on complex criteria. Using it to determine if someone has permission to perform an action is a common use case like:

> Can user edit document X if they can view it AND (they are the owner OR they are an admin)?

> Can a user access resource X if the feature is enabled AND they belong to group Y OR they have role Z?

## Type safety

This package is extremely type safe. The type signature of the items being matched is preserved throughout the entire matching process.

It's recommended to type your input with `as const` assertions to get the most out of the type safety features.

E.g.:

```ts
const items = ["apple", "banana", "cherry"] as const;

const matches = match(items).with(
  or(
    eq("apple"),
    and("banana", not("cherry"), "fig") // TS error: "fig" is not in items
  )
);
```

## Pretty print query

Each query node has a `toString()` method that pretty-prints the query structure. Useful for logging, debugging, or analyzing queries.

```ts
const query = or<string>(
  and("apple", not("banana")),
  predicate((item) => item.startsWith("ch"), 'starts with "ch"')
);

console.log(query.toString()); // Output: (("apple" and not "banana") or starts with "ch")
```

## Walk the tree

You can walk the query AST using the `walk` helper function for even more advanced use cases.

```ts
import { walk } from "@jack3898/match-collection/helpers";

const query = or("apple", "banana");

walk(query.ast, (node) => {
  console.log(`Visiting node: ${node.toString()}`);
});
```

One use case is determining the scope of a query by collecting values from the query, then using that to optimize any tooling to fetch results to match against later on. E.g. if a match only ever checks a subset of rules, why fetch all possible data to match with?

## Note on module type

This package is distributed with ESM syntax only.

I apologise in advance for any inconvenience this may cause.
npm package: @jack3898/templater
Description: Use type-safe template strings to generate text content.
Author: Jack Wright
Latest version: 1.0.1

# Templater

A tiny zero-dependency package that helps you generate text content using type-safe template strings.

## template``

Basic usage:

```ts
const data = { name: "World" };
const generate = template`Hello, ${"name"}!`;

console.log(generate(data)); // Output: "Hello, World!"
```

## Type safety

This package is extremely type safe. The template strings enforce that all placeholders correspond to keys in the provided data object.

E.g.:

```ts
const generate = template`Hello, ${"name"}!`;

// Error: Property 'name' is missing
const string = generate({});
```

## Indexed placeholders (`$` and numbers)

You can use the exported `$` symbol to mark positional placeholders that read from arrays, or explicit numeric indices:

```ts
import { template, $ } from "@jack3898/templater";

const positional = template`Hello, ${$}! ${$}`;
console.log(positional(["friend", "How are you?"])); // "Hello, friend! How are you?"

const indexed = template`You have ${0} new messages.`;
console.log(indexed(["5"])); // "You have 5 new messages."
```

## Drawbacks

This is not a parser. It only works with simple template strings where placeholders are direct keys in the data object (so this will not work with templates stored in DBs, text files and such). If you need more complex templating, then an alternative solution like Handlebars may be more appropriate.

The main advantage to this tool is code readability and type safety when generating text content directly in code. It can shift the bulk of the core string into another place decluttering your logic, while still being easy to read and maintain.

## Note on module type

This package is distributed with ESM syntax only.

I apologise in advance for any inconvenience this may cause.