Skip to content

Commit

Permalink
Add primitive integer types
Browse files Browse the repository at this point in the history
  • Loading branch information
h-sifat committed Oct 11, 2021
1 parent ff2290f commit c40879c
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 25 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Changelog
=========
1.0.2
-----
- Added types **int8**, **uint8**, **int16**, **uint16**, **int32**, and **uint32**
- Added type alias **integer**

1.0.0
-----
- Initial release
48 changes: 30 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Handy-Types
A small library to check variable types. It consists of many utility functions
like `int`, `positive_int`, `array`, `non_empty_array` and many more. Every
**type**(*function*) has their fullname in case you want to write a meaningful
**type**(*function*) has their full name in case you want to write a meaningful
error message. The library is fully tested so it has **100%** test coverage. It
uses the **UMD** module system so it supports every javascript environment.

Expand All @@ -13,60 +13,67 @@ const { types, typeNames } = require("handy-types");

const user = {};

if(!types.ne_object(obj)) // or use the fullname: types.non_empty_object(obj)
if(!types.ne_object(obj)) // or use the long form: types.non_empty_object(obj)
throw new Error(`"user" must be of type "${typeNames.ne_object}"`);
```
All the available types are listed below.

## Number Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
NaN | Not A Number
number | Number
finite_num | Finite Number
positive_number | Positive Number | p_number
negative_number | Negative Number | n_number

## Integer Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
int | Integer
integer | Integer | int
safe_int | Safe Integer
odd | Odd Number
even | Even Number
natural_num | Natural Number
positive_int | Positive Integer | p_int
negative_int | Negative Integer | n_int

## Primitive Integer
Type | Full Name | Range
---- | -------- | -----
int8 | 8 Bit Integer | **-128** to **127**
uint8 | 8 Bit Unsigned Integer | **0** to **255**
int16 | 16 Bit Integer | **-32,768** to **32,767**
uint16 | 16 Bit Unsigned Integer | **0** to **65,535**
int32 | 32 Bit Integer | **-2,147,483,648** to **2,147,483,647**
uint32 | 32 Bit Unsigned Integer | **0** to **4,294,967,295**

## String Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
string | String
empty_string | Empty String | es
non_empty_string | Non-Empty String | ne_string


## Object Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
null | Null
object | Object
empty_object | Empty Object | eo
non_null_object | Non-Null Object | nn_object
non_empty_object | Non-Empty Object | ne_object


## Array Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
array | Array
empty_array | Empty Array | ea
non_empty_array | Non-Empty Array | ne_array


## Global Objects
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
regex | Regular Expression
date | Date
Expand All @@ -75,31 +82,36 @@ map | Map


## Function Type
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
function | Function


## Symbol Type
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
symbol | Symbol


## Boolean Type
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
boolean | Boolean
true | True
false | False
truthy | Truthy
falsy | Falsy

## Constants
Type | Full Name | Alias
---- | -------- | -----
true | True
false | False
undefined | Undefined
null | Null
NaN | Not A Number

## Other Types
Type | Fullname | Alias
Type | Full Name | Alias
---- | -------- | -----
undefined | Undefined
defined | Defined
any | Any

Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "handy-types",
"version": "1.0.1",
"version": "1.1.0",
"description": "A collection of small utility functions to check variable types",
"keywords": [
"types",
Expand Down
40 changes: 37 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ type StringPredicate = TypePredicate<string>;
type ObjectPredicate = TypePredicate<object>;
type NullableObjectPredicate = TypePredicate<object | null>;
type ArrayPredicate = TypePredicate<unknown[]>;
type predicate = (v: any) => boolean;

interface Types {
[key: string]: predicate;
// Number types
number: NumberPredicate;
p_number: NumberPredicate;
positive_number: NumberPredicate;
Expand All @@ -17,6 +16,7 @@ interface Types {
finite_num: NumberPredicate;
natural_num: NumberPredicate;
int: NumberPredicate;
integer: NumberPredicate;
p_int: NumberPredicate;
positive_int: NumberPredicate;
n_int: NumberPredicate;
Expand All @@ -25,6 +25,14 @@ interface Types {
even: (v: number) => boolean;
safe_int: NumberPredicate;

// Byte wise int
int8: NumberPredicate;
uint8: NumberPredicate;
int16: NumberPredicate;
uint16: NumberPredicate;
int32: NumberPredicate;
uint32: NumberPredicate;

// String types
string: StringPredicate;
es: StringPredicate;
Expand Down Expand Up @@ -87,6 +95,7 @@ types.finite_num = (v): v is number => Number.isFinite(v);

// Integer Types ===============
types.int = (v): v is number => Number.isInteger(v);
types.integer = types.int;
types.safe_int = (v): v is number => Number.isSafeInteger(v);
types.p_int = (v): v is number => types.int!(v) && v > -1;
types.positive_int = types.p_int;
Expand All @@ -96,6 +105,19 @@ types.natural_num = (v): v is number => types.int!(v) && v > 0;
types.odd = (v: number) => Math.abs(v % 2) === 1;
types.even = (v: number) => v % 2 === 0;

// byte wise integer
types.int8 = (v: unknown): v is number =>
types.int!(v) && v >= -128 && v <= 127;
types.uint8 = (v: unknown): v is number => types.int!(v) && v >= 0 && v <= 255;
types.int16 = (v: unknown): v is number =>
types.int!(v) && v >= -32768 && v <= 32767;
types.uint16 = (v: unknown): v is number =>
types.int!(v) && v >= 0 && v <= 65535;
types.int32 = (v: unknown): v is number =>
types.int!(v) && v >= -2147483648 && v <= 2147483647;
types.uint32 = (v: unknown): v is number =>
types.int!(v) && v >= 0 && v <= 4294967295;

// String Types ----------------------------------------------------------------
types.string = (v): v is string => typeof v === "string";
types.es = (v): v is string => v === "";
Expand Down Expand Up @@ -160,6 +182,7 @@ let typeNames: TypeNames = {
finite_num: "Finite Number",

// Integer
integer: "Integer",
int: "Integer",
safe_int: "Safe Integer",
p_int: "Positive Integer",
Expand All @@ -170,6 +193,14 @@ let typeNames: TypeNames = {
even: "Even Number",
natural_num: "Natural Number",

// Byte wise integer
int8: "8 Bit Integer",
uint8: "8 Bit Unsigned Integer",
int16: "16 Bit Integer",
uint16: "16 Bit Unsigned Integer",
int32: "32 Bit Integer",
uint32: "32 Bit Unsigned Integer",

// String
string: "String",
es: "Empty String",
Expand Down Expand Up @@ -220,7 +251,10 @@ let typeNames: TypeNames = {
NaN: "NaN",
};

const frozenTypeObject = Object.freeze(types) as Readonly<Types>;
type ReadonlyTypes = Readonly<Types> & {
[key: string]: (v: unknown) => boolean;
};
const frozenTypeObject = Object.freeze(types) as ReadonlyTypes;
typeNames = Object.freeze(typeNames);

export { frozenTypeObject as types };
Expand Down
32 changes: 31 additions & 1 deletion tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,40 @@ const testData = [
validValues: [1, 2, 0, -1, -2, 2.0],
invalidValues: [1.1, 0.11],
},
{
typeName: "int8",
validValues: [-128, 127],
invalidValues: [-129, 128],
},
{
typeName: "uint8",
validValues: [0, 255],
invalidValues: [-1, 256],
},
{
typeName: "int16",
validValues: [-32768, 32767],
invalidValues: [-32769, 32768],
},
{
typeName: "uint16",
validValues: [0, 65535],
invalidValues: [-1, 65536],
},
{
typeName: "int32",
validValues: [-2147483648, 2147483647],
invalidValues: [-2147483649, 2147483648],
},
{
typeName: "uint32",
validValues: [0, 4294967295],
invalidValues: [-1, 4294967296],
},
{
typeName: "safe_int",
validValues: [1, 2, 0, -1, -2, 2.0, 9999999],
invalidValues: [1.1, 0.11, 9999999999999999999],
invalidValues: [1.1, 0.11, 99999999999999999999999999999],
},
{
typeName: "p_int",
Expand Down

0 comments on commit c40879c

Please sign in to comment.