@gik/tools-checker

A minimal type-checker for JavaScript purists. Part of our [tools suite](https://github.com/gikmx/tools).

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@gik/tools-checker
0.0.116 years ago6 years agoMinified + gzip package size for @gik/tools-checker in KB

Readme

A minimal type-checker for JavaScript purists. Part of our tools suite.
Contributors
Supported platforms

Table of contents

  • checker A minimal type-checker for JavaScript purists.
- is Determine if given value really belongs to the corresponding type.
- **[objectEmpty](#checker.is.objectEmpty)** `member` Determine if an element is an object and has no keys
- **[string](#checker.is.string)** `member` Determines if `value` is really a string.
- **[number](#checker.is.number)** `member` Determines if `value` is really a number.
- **[array](#checker.is.array)** `member` Determines if `value` is really an array.
- **[function](#checker.is.function)** `member` Determines if `value` is really a function.
- **[regexp](#checker.is.regexp)** `member` Determines if `value` is really a regexp.
- **[boolean](#checker.is.boolean)** `member` Determines if `value` is really a boolean.
- **[object](#checker.is.object)** `member` Determines if `value` is really an object.
-
props function Validates properties of given object.
checker
A minimal type-checker for JavaScript purists.
Members

▲ Top

is

Determine if given value really belongs to the corresponding type.
Members

▲ Top

objectEmpty

static property of checker.is

Determine if an element is an object and has no keys
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#Object">Object</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean
- Whether the object is empty or not.
▲ Top

string

static property of checker.is

Determines if value is really a string.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is string or not.
▲ Top

number

static property of checker.is

Determines if value is really a number.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is number or not.
▲ Top

array

static property of checker.is

Determines if value is really an array.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is array or not.
▲ Top

function

static property of checker.is

Determines if value is really a function.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is function or not.
▲ Top

regexp

static property of checker.is

Determines if value is really a regexp.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is regexp or not.
▲ Top

boolean

static property of checker.is

Determines if value is really a boolean.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is boolean or not.
▲ Top

object

static property of checker.is

Determines if value is really an object.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>value</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value you need to check.</td>
</tr>
Returns
boolean - Wheter value is object or not.
▲ Top

props

static method of checker

Validates properties of given object.
Parameters
<tr>
    <td style="white-space: nowrap;">
        <code>subject</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#Object">Object</a>
    </td>
    <td>The object value whose properties will be inspected.</td>
</tr><tr>
    <td style="white-space: nowrap;">
        <code>defmap</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#Object">Object</a>
    </td>
    <td>An definition object map, describing each of the prop' types.</td>
</tr><tr>
    <td style="white-space: nowrap;">
        <code>defmap.prop</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#Object">Object</a> | 
            <a href="#string">string</a>
    </td>
    <td>The name of a corresponding subject's property.
If a string is used, it will be converted to: { type: <string used>, required:true }
</tr><tr>
    <td style="white-space: nowrap;">
        <code>[defmap.type]</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#string">string</a>
    </td>
    <td>Determines the type the prop should have,
all methods on is() are asupported. Default any
</tr><tr>
    <td style="white-space: nowrap;">
        <code>[defmap.required]</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#bool">bool</a>
    </td>
    <td>Whether the prop can be omitted.</td>
</tr><tr>
    <td style="white-space: nowrap;">
        <code>[defmap.default]</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#*">*</a>
    </td>
    <td>The value to use if prop is omitted
(cannot be used along required).
</tr><tr>
    <td style="white-space: nowrap;">
        <code>[defmap.map]</code>
    </td>
    <td style="white-space: nowrap;">
            <a href="#function">function</a>
    </td>
    <td>A function that will receive subject's
prop value and expects you to return acomputed value for it
</tr>
Returns
Object - The validated subject extended with default values (when applies).
Throws
  • CheckerPropParamError when invalid parameters are passed.
  • CheckerPropDefError when a type definition is invalid.
  • CheckerPropDefTypeError when a type defintiion is not supported.
  • CheckerPropReqError when a required property is not found.
  • CheckerPropTypeError when a property does not match the defintion.
Example
const subject = { a: 1, b: 'hello' z: undefined };
const result = props(subject, {
    a: { type:'number', required:true },
    b: 'string',
    c: { default: new Date() },
    d: { required: false, default: null, map: value => [value, true] },
})
// result:
// { a: 1, b: 'hello', c: '1981-06-23 10:06:08', d: [null, true], z: undefined }

▲ Top
Types
Members

▲ Top

CheckerPropParamError

static typedef of Types

A definition prop was sent, but it was invalid.
▲ Top

CheckerPropDefError

static typedef of Types

The specified type is not a supported primitive.
▲ Top

CheckerPropDefTypeError

static typedef of Types

A required property was not found in subject.
▲ Top

CheckerPropReqError

static typedef of Types

A property didn't have the correct type.
▲ Top
---