typof
v1.0.8
Published
Infer types.
Downloads
949
Maintainers
Readme
Contents
About
Infer types.
Features
- Infers multiple types
- Infers integer and float types
- Infers the type from a string
- There are simple type converters
Installation
You can install it as follows.
# NPM
npm add typof
# PNPM
pnpm add typof
# Yarn
yarn add typof
# Bun
bun add typof
# Deno
deno add typofDocumentation
Tree
Briefly as follows.
Typof
│
├── typof(value)
├── string(value)
├── number(value)
├── integer(value)
├── boolean(value)
├── date(value)
├── object(value)
├── array(value)
├── _null(value)
├── _undefined(value)
│
└── type TypesImport
Briefly as follows.
import { typof } from 'typof';Methods
typof(value)
Infer types.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | --------------------- | | value | Unknown | | Value to infer types. |
returns Types[]
Example:
// Tests are as follows. (Is this an integer?) if (typof(0).includes('integer')) console.log('This is an integer.'); // Index zero always ensures reliable type checking. As the index increases, species depth also increases. if (typof('0.5')[0] === 'string') console.log('This is a string.'); typof('test'); // [ "string" ] typof('0'); // [ "string", "number", "integer" ] typof(0); // [ "number", "integer" ] typof('0.5'); // [ "string", "number", "float" ] typof(0.5); // [ "number", "float" ] typof('true'); // [ "string", "boolean" ] typof(true); // [ "boolean" ] typof('2025-01-01'); // [ "string", "date" ] typof(new Date('2025-01-01')); // [ "object", "date" ] typof('{"key":"value"}'); // [ "string", "object" ] typof({ key: 'value' }); // [ "object" ] typof('["test"]'); // [ "string", "array" ] typof(['test']); // [ "array" ] typof('null'); // [ "string", "null" ] typof(null); // [ "null" ] typof('undefined'); // [ "string", "undefined" ] typof(undefined); // [ "undefined" ]
string(value)
Convert to string.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns String
Example:
string(0.5); // "0.5" string(true); // "true" string(new Date('2025-01-01')); // "2025-01-01T00:00:00.000Z" string({ key: 'value' }); // '{"key":"value"}' string(['test']); // '["test"]' string(null); // "null" string(undefined); // "undefined"
number(value)
Convert to number.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Number
Example:
number('0.5'); // 0.5 number(0.5); // 0.5 number('test'); // NaN
integer(value)
Convert to integer.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Number
Example:
integer('0.5'); // 0 integer(0.5); // 0 integer('test'); // NaN
boolean(value)
Convert to boolean.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Boolean | Value
Example:
boolean('true'); // true boolean(true); // true boolean('test'); // "test"
date(value)
Convert to date.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Date | Value
Example:
date('2025-01-01'); // 2025-01-01T00:00:00.000Z date(new Date('2025-01-01')); // 2025-01-01T00:00:00.000Z date('test'); // "test"
object(value)
Convert to object.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Object | Value
Example:
object('{"key":"value"}'); // { key: "value" } object('["test"]'); // '["test"]'
array(value)
Convert to array.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Unknown[] | Value
Example:
array('["test"]'); // [ "test" ] array('{"key":"value"}'); // '{"key":"value"}'
_null(value)
Convert to null.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Null | Value
Example:
_null('null'); // null _null(null); // null _null('test'); // "test"
_undefined(value)
Convert to undefined.
| Parameter | Type | Default | Description | | --------- | ------- | ------- | ----------------- | | value | Unknown | | Value to convert. |
returns Undefined | Value
Example:
_undefined('undefined'); // undefined _undefined(undefined); // undefined _undefined('test'); // "test"
Types
| Type | | ------- | | Types |
Links
License
MIT License
Copyright (c) 2025 Keift
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
