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

@auaust/primitive-kit

v0.32.2

Published

A helper class for each primitive types, providing some useful methods.

Downloads

156

Readme

AUAUST LIBRARIES — JavaScript — PrimitiveKit

This repository is made public to open the codebase to contributors. When contributing to this repo, you agree to assign your copyrights to AUAUST.

PrimitiveKit

PrimitiveKit provides a robust and intuitive set of classes for handling JavaScript primitives. Each class extends its respective JavaScript primitive type (A extends Array, B extends Boolean, N extends Number…) and offers a suite of static methods to simplify and enhance the manipulation and processing of these types.

Overview

// Some methods are simply aliases for native methods.
S.toUpperCase("hello world"); // 'HELLO WORLD'

// Some methods are aliases for native methods with stronger typing.
O.keys({ a: 1, b: 2 }); // ['a', 'b'] instead of PropertyKey[]

// Some methods simplify common operations.
B.from("false"); // false
B.from("False"); // false
B.from("0"); // false

O.clone({ a: 1, b: 2 }); // { a: 1, b: 2 }, but not the same object (and is deeply cloned!)

// Some methods simplifies common operations, with type safety!
const obj = {
  foo: {
    bar: {
      baz: 1,
    },
    meep: [
      {
        deep: [2],
      },
    ],
  },
};

O.deepGet(obj, "foo", "baz", "baz"); // 1 typed as number
O.deepGet(obj, "foo", "not", "found"); // undefined typed as unknown, because it may exist but not be inferred correctly
O.deepGet(obj, "foo", "meep", 0, "deep", 0); // 2 typed as number

// … and more!

Key Features

  • Type Safety – Strongly typed. Always. No more any!
  • Intuitive – Simplifies common operations, namespaced under classes that make sense.
  • Robust – Mostly based on native methods, adding a layer of type safety and convenience.
  • No Dependencies – No external dependencies. Just pure JavaScript. (Or TypeScript, if you prefer.)
  • Isomorphic – No usage of special APIs. Only primitives.

Installation

yarn add @auaust/primitive-kit

or if you prefer Bun:

bun add @auaust/primitive-kit

Usage

Each class works as a namespace for a primitive type, named after the first letter of the primitive type.

ArrayA BooleanB NumberN ObjectO StringS

B (Boolean)

import { B } from "@auaust/primitive-kit";

// Easy conversion from any value to a boolean, and a little bit more input-aware than Boolean()
B.from("false"); // false
B.from("False"); // false
B.from("0"); // false

B.from("true"); // true
B.from("True"); // true
B.from("1"); // true

B.from(new Boolean(false)); // false
B.from({
  valueOf() {
    return false;
  },
}); // false

// Type-checking
B.is(false); // true
B.is(new Boolean(false)); // true
B.is(0); // false

// … or stricter type-checking
B.isStrict(false); // true
B.isStrict(true); // true
B.isStrict(new Boolean(false)); // false

// Loose comparison
B.equals(false, 0); // true
B.equals(false, "false"); // true
B.equals(false, true); // false

// Many logical operations
B.and(true, true); // true
B.or(true, false); // true

B.xor(true, true); // false
B.xor(true, false); // true
// … and not(), nand(), nor(), xnor()

N (Number)

import { N } from "@auaust/primitive-kit";

// Easy conversion from any value to a number
N.from("1"); // 1
N.from("1.1"); // 1.1
N.from("foo123foo"); // 123
N.from(""); // 0
N.from(null); // 0

// Loose type-checking
N.is(1); // true
N.is(new Number(1)); // true
N.is("1"); // true
N.is(Infinity); // true
N.is(NaN); // false

// … or stricter type-checking -> only primitive and finite numbers
N.isStrict(1); // true
N.isStrict(new Number(1)); // false
N.isStrict("1"); // false
N.isStrict(Infinity); // false

// All native number methods are available
// N.isFinite(), N.isInteger(), N.isNaN(), N.isSafeInteger(), N.parseFloat(), N.parseInt(), N.toExponential(), N.toFixed(), N.toPrecision(), …

// And some custom helpers
N.isPositive(1); // true
N.isNegative(-1); // true
N.randInt(1, 10); // 5 (or 1, 2, 3, 4, 6, 7, 8, 9, 10)
N.randFloat(), N.clamp(), N.isOdd(), N.isEven(), N.isBetween(), N.min(), N.max(), …

Many other methods and classes are available. Please refer to the source code for more information, or for more advanced usage.

Browser Support

PrimitiveKit is compatible with all modern browsers. Since it doesn't use any advanced APIs, it should work on older browsers as well, but it hasn't been tested. If it doesn't work on your browser but you think it should, please feel free to contribute!

Sponsor

This library is a project by us, AUAUST. We sponsor ourselves!