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

abstruct

v1.0.0

Published

Abstract structure for JavaScript data validation

Downloads

27

Readme

abstruct

deno land deno doc GitHub release (latest by date) codecov GitHub

test NPM

Abstract structure for JavaScript data validation

Abstruct(not abstract!) provides features for defining data structures. It used for any operation. For example, validation.

Usage

Define the data structure and validate.

import {
  and,
  assert,
  maxCount,
  pattern,
  props,
  string,
  validDate,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
import { assertThrows } from "https://deno.land/std/testing/asserts.ts";

const Id = and(string, pattern(/^\d$/));
const Book = props({
  id: Id,
  title: maxCount(256).expect(`length should be less than or equal to 256`),
  publishAt: validDate,
});

assertThrows(() =>
  assert(Book, {
    id: 0,
    title: "Harry Potter and the Philosopher's Stone",
    publishAt: new Date("1997/6/26"),
  })
);

Validators do only one thing, so they can be combined to make new validator.

import {
  and,
  gte,
  int,
  lte,
  validate,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";

const Int8 = and(
  int,
  gte(-127),
  lte(128),
);
declare const input: number;

const result = validate(Int8, input);

if (result.isOk()) {
  // result.value;
} else {
  // result.value;
}

And narrowing works correctly.

import {
  and,
  assert,
  gte,
  instance,
  lte,
  validDate,
} from "https://deno.land/x/abstruct@$VERSION/mod.ts";
import { Assert, IsExact } from "https://deno.land/std/testing/types.ts";

const ValidDate = and(
  instance(Date),
  validDate,
  gte(new Date("1970/1/1")),
  lte(new Date("2038/1/19")),
);
const input: unknown = null;

assert(ValidDate, input);

type doTest = Assert<IsExact<typeof input, Date>, true>;

Fully customizable messages:

import { int8, string } from "https://deno.land/x/abstruct@$VERSION/mod.ts";

const Int8 = int8.expect("should be int8!!!");
const ID = string.expect(({ input }) =>
  `id should be string, actual ${typeof input}`
);

Philosophy

  1. Composable: All features are composable. Being composable brings the following features:

    • Single responsibility
    • DRY
    • Pay as you go
    • Customizable
    • Tiny
  2. Library first: It can use in library. To fulfill this, special attention has been paid to the following:

    • Universal
    • Customizable
    • Tiny
  3. Type first: Type safety is a matter of course.

Documentation

You have very little to learn.

Example

Few examples of common patterns:

Inspired by

License

Copyright © 2023-present Tomoki Miyauci.

Released under the MIT license