0.29.1 • Published 10 days ago

@auaust/primitive-kit v0.29.1

Weekly downloads
-
License
GPL-3.0-only
Repository
-
Last release
10 days ago
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!

0.29.1

10 days ago

0.29.0

23 days ago

0.28.5

1 month ago

0.28.4

1 month ago

0.28.3

1 month ago

0.27.0

1 month ago

0.28.1

1 month ago

0.28.0

1 month ago

0.26.1

1 month ago

0.26.0

1 month ago

0.28.2

1 month ago

0.25.0

1 month ago

0.24.2

1 month ago

0.24.1

1 month ago

0.24.0

1 month ago

0.20.1

1 month ago

0.23.0

1 month ago

0.21.0

1 month ago

0.22.0

1 month ago

0.20.0

2 months ago

0.19.2

2 months ago

0.19.1

2 months ago

0.19.0

2 months ago

0.18.1

2 months ago

0.18.2

2 months ago

0.18.3

2 months ago

0.18.0

3 months ago

0.17.0

3 months ago

0.17.1

3 months ago

0.16.0

3 months ago

0.15.0

3 months ago

0.14.0

3 months ago

0.13.1

3 months ago

0.12.0

4 months ago

0.13.0

4 months ago

0.11.2

4 months ago

0.11.0

4 months ago

0.11.1

4 months ago

0.10.2

4 months ago

0.10.1

4 months ago

0.10.0

4 months ago

0.9.4

4 months ago

0.9.3

5 months ago

0.9.2

5 months ago

0.9.1

5 months ago

0.9.0

5 months ago

0.8.0

5 months ago

0.7.0

5 months ago

0.6.1

5 months ago

0.6.0

5 months ago

0.5.0

5 months ago

0.3.0

5 months ago

0.2.0

6 months ago

0.4.0

5 months ago

0.1.9

6 months ago

0.1.8

6 months ago

0.1.7

6 months ago

0.1.6

6 months ago

0.1.5

6 months ago

0.1.4

6 months ago

0.1.3

6 months ago

0.1.2

6 months ago

0.1.1

6 months ago

0.1.0

6 months ago

0.0.0

6 months ago