open-index/open-npm huggingface dataset:
npm package: @srhazi/shape
Description: Composable runtime value checks that play well with TypeScript's type narrowing
Author: Sufian Rhazi
Latest version: 1.4.0

# @srhazi/shape

Composable runtime value checks that play well with TypeScript's type narrowing.

A **shape** is an [**assertion
function**](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) that
narrows a type.

Think of it like a schema checker for `unknown` values.


## API

The `@srhazi/shape` package exports the following types and values:

* `AssertFn<T> = (val: unknown) => val is T` - the type of an **assertion function** 

Given an **assertion function**, you can get the type of it with `AssertFnType<typeof myAssertionFunction>`.

There are some self-describing **assertion functions**:

* `isString: AssertFn<string>`
* `isNumber: AssertFn<string>`
* `isNumber: AssertFn<number>`
* `isBigint: AssertFn<bigint>`
* `isBoolean: AssertFn<boolean>`
* `isSymbol: AssertFn<symbol>`
* `isUndefined: AssertFn<undefined>`
* `isUnknown: AssertFn<unknown>`
* `isNull: AssertFn<null>`
* `isArray: AssertFn<unknown[]>`
* `isFunction: AssertFn<() => unknown>`

And some helpful **assertion function** factories:

* `isExact(value: T): AssertFn<T>` - make a function asserting strict equality (`===`) to a specific value
* `is(value: T): AssertFn<T>` - (alias for `isExact`)
* `isEnum(values...: T[]): AssertFn<T>` - make a function asserting strict equality (`===`) to one of many values

**Assertion functions** can be combined together with:

* `isShape({ foo: isNumber, bar: isString }): AssertFn<{ foo: number, bar: string }>` - make a function asserting an object whose keys match the corresponding assertion functions
* `isEither(isString, isNumber, ...etc)` - make a function asserting one of many assertion functions (via OR)
* `isArrayOf: <T>(shape: AssertFn<T>) => AssertFn<T[]>` - make a function asserting an array whose values all match an assertion function
* `isTuple: <T extends AssertFn<any>[]>(...fns: T) => AssertFn<{ [P in keyof T]: AssertFnType<T[P]>; }>` - make a function asserting a tuple (fixed-length array) whose values each match the assertion functions
* `isRecordOf: <T>(shape: AssertFn<T>) => AssertFn<Record<string, T>>` - make a function asserting an object whose
  values all match an assertion function
* `isRecordWith: <K extends string | number | symbol, V>(isEntry: AssertFn<[K, V]>): AssertFn<Record<K, V>>` - make a function asserting an object whose
  entries all match an assertion function

And there's one hard-to-describe, but easy-to-use **assertion function**:

* `function isTruthy<T>(val: T): val is Exclude<T, 0 | -0 | 0n | "" | null | undefined>` - make a function asserting
  just like how `if` works

And if you want a shape with an optional property, mark the **assertion function** with `optional`:

* `optional<T>(fn: AssertFn<T>): AssertFn<T | undefined>` - make a property optional


## Installation

`npm install --save @srhazi/shape`
npm package: @srhazi/simpledom
Description: A simple typescript-oriented DOM manipulation API
Author: Sufian Rhazi
Latest version: 1.2.0

# simpledom

A tiny, simple, strict, typescript-oriented DOM manipulation API.

* Zero dependencies
* Requires ES6 runtime (for `Array.from()`)
* Library size: 2,064 bytes minified (877 bytes gzip-compressed)
* Works well with `tsc --strict`


## Installation

`npm install @srhazi/simpledom`


## Building

* Note: Minified standalone build depends on [closure-compiler](http://code.google.com/closure/compiler).
* Version `1.2.0` built with
  * typescript version: 2.6.2
  * node version: 4.1.1
  * amdclean version: 2.7.0
  * Closure Compiler version: v20200517 (Built on: 2020-05-18 22:36)


1. `git clone https://github.com/sufianrhazi/simpledom.git`
2. `cd simpledom`
3. `npm install`
4. `s/dist`

## Usage

All functions are named exports with concise names:

```typescript
import * as dom from '@srhazi/simpledom';

// Typesafe document.querySelector()
let form: HTMLFormElement = dom.one(HTMLFormElement, 'form.login');

// Typesafe document.querySelector()
let maybeForm: HTMLFormElement | undefined = dom.ge(HTMLFormElement, 'form.login');

// Typesafe document.querySelectorAll()
let buttons: HTMLButtonElement[] = dom.all(HTMLButtonElement, 'button.primary');

// Typesafe el.querySelector()
let submit: HTMLButtonElement = dom.oneFrom(form, HTMLButtonElement, 'button.submit');

// Typesafe el.querySelector()
let maybeSubmit: HTMLButtonElement | undefined= dom.oneFrom(form, HTMLButtonElement, 'button.submit');

// Typesafe el.querySelectorAll()
let formCheckboxes: HTMLInputElement[] = dom.allFrom(form, HTMLInputElement, 'input[type="checkbox"]');

// Typesafe jQuery.on-lookalike
dom.on(form, 'keypress', HTMLInputElement, 'input[type="text"][name="dotcom-2.0-name"]', (event, el) => {
    // event inferred as KeyboardEvent
    // el inferred as HTMLInputElement
    if ('aeiou'.indexOf(e.key) !== -1) {
        event.preventDefault();
    }
});

// String to Element constructor
let container = dom.build(`
    <ul>
        <li>One</li>
        <li>Two</li>
    </ul>
`);

document.body.appendChild(container);

// String to DocumentFragment constructor
let moreItems = dom.buildMany(`
    <li>Three</li>
    <li>Four</li>
`);

container.appendChild(moreItems);
```