Skip to content

Commit

Permalink
Merge 6a7d32c into a60a8b9
Browse files Browse the repository at this point in the history
  • Loading branch information
DarrenPaulWright committed Mar 11, 2019
2 parents a60a8b9 + 6a7d32c commit 2281285
Show file tree
Hide file tree
Showing 32 changed files with 395 additions and 78 deletions.
13 changes: 11 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html)

## [0.4.0] - 2019-3-11
### Added
- Float type (check, enforcer, and method)

### Changed
- Integer coercion is now more strict
- Switching dependencies back to object-agent

## [0.3.3] - 2019-2-15
### Changed
- Switching dependencies to deep-equal instead of object-agent
Expand All @@ -27,7 +35,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.2.3] - 2019-01-30
### Changed
- Exporting the check functions as a object
- Exporting the check functions as an object
- Exporting individual enforcers and methods

## [0.2.2] - 2019-01-24
Expand Down Expand Up @@ -111,7 +119,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Initial enforcers, methods, and documentation

[0.3.2]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.3.2...c0.3.3
[0.4.0]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.3.3...c0.4.0
[0.3.3]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.3.2...c0.3.3
[0.3.2]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.3.1...c0.3.2
[0.3.1]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.3.0...c0.3.1
[0.3.0]: https://github.com/DarrenPaulWright/type-enforcer/compare/v0.2.4...c0.3.0
Expand Down
23 changes: 20 additions & 3 deletions docs/checks.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ A type enforcement library for javascript
<dt><a href="#isElement">isElement(value)</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value is a DOM element</p>
</dd>
<dt><a href="#isFloat">isFloat(value, [coerce])</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value is a finite float</p>
</dd>
<dt><a href="#isFunction">isFunction(value)</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value is a function</p>
</dd>
<dt><a href="#isInstanceOf">isInstanceOf(object, constructor)</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value is an instance of a constructor. Fixes issues with native instanceOf and primitives Boolean, Number, and String (see example).</p>
</dd>
<dt><a href="#isInteger">isInteger(value, [coerce])</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value is an integer</p>
<dd><p>Check if a value is a finite integer</p>
</dd>
<dt><a href="#isJson">isJson(value)</a> ⇒ <code>Boolean</code></dt>
<dd><p>Check if a value can be parsed as JSON</p>
Expand Down Expand Up @@ -161,6 +164,20 @@ Check if a value is a DOM element

**Example**
``` javascriptimport { isElement } from 'type-enforcer';isElement(document.createElement('div'));// => true```
<a name="isFloat"></a>

## isFloat(value, [coerce]) ⇒ <code>Boolean</code>
Check if a value is a finite float

**Kind**: global function

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>\*</code> | | |
| [coerce] | <code>Boolean</code> | <code>false</code> | If true then see if the value can be coerced into a float |

**Example**
``` javascriptimport { isFloat } from 'type-enforcer';isFloat(3.14159);// => trueisFloat('3.14159');// => falseisFloat('3.14159', true);// => true```
<a name="isFunction"></a>

## isFunction(value) ⇒ <code>Boolean</code>
Expand Down Expand Up @@ -191,7 +208,7 @@ Check if a value is an instance of a constructor. Fixes issues with native insta
<a name="isInteger"></a>

## isInteger(value, [coerce]) ⇒ <code>Boolean</code>
Check if a value is an integer
Check if a value is a finite integer

**Kind**: global function

Expand All @@ -201,7 +218,7 @@ Check if a value is an integer
| [coerce] | <code>Boolean</code> | <code>false</code> | If true then see if the value can be coerced into an Integer |

**Example**
``` javascriptimport { isInteger } from 'type-enforcer';isInteger(42);// => trueisInteger('42');// => falseisInteger('42', true);// => true```
``` javascriptimport { isInteger } from 'type-enforcer';isInteger(42);// => trueisInteger('42');// => falseisInteger('42', true);// => trueisInteger('42.5', true);// => false```
<a name="isJson"></a>

## isJson(value) ⇒ <code>Boolean</code>
Expand Down
22 changes: 20 additions & 2 deletions docs/enforce.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Utility functions for enforcing data types. ## Usage ``` javascript import { en
* [.dockPoint(value, alt, [coerce])](#enforce.dockPoint) ⇒ <code>DockPoint</code>
* [.element(value, alt)](#enforce.element) ⇒ <code>Element</code>
* [.enum(value, enumerable, alt)](#enforce.enum) ⇒ <code>String</code>
* [.float(value, alt, [coerce], [minValue], [maxValue])](#enforce.float) ⇒ <code>int</code>
* [.function(value, alt)](#enforce.function) ⇒ <code>function</code>
* [.instance(value, constructor, alt)](#enforce.instance) ⇒ <code>Object</code>
* [.integer(value, alt, [coerce], [minValue], [maxValue])](#enforce.integer) ⇒ <code>int</code>
Expand Down Expand Up @@ -138,6 +139,23 @@ Enforce that a value exists in the provided [Enum](docs/Enum.md)

**Example**
``` javascriptimport { enforce, Enum } from 'type-enforcer';const values = new Enum({ a: 'item a', b: 'item b'});enforce.enum(values.a, values, values.b);// => 'item a'enforce.enum(values.c, values, values.b);// => 'item b'```
<a name="enforce.float"></a>

### enforce.float(value, alt, [coerce], [minValue], [maxValue]) ⇒ <code>int</code>
Enforce that a value is a finite float. Uses [isFloat](docs/checks.md#isFloat).

**Kind**: static method of [<code>enforce</code>](#enforce)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| value | <code>\*</code> | | |
| alt | <code>int</code> | | Returned if the value is not the correct type |
| [coerce] | <code>Boolean</code> | <code>false</code> | If true then coerce the value when possible |
| [minValue] | <code>int</code> | | |
| [maxValue] | <code>int</code> | | |

**Example**
``` javascriptimport { enforce } from 'type-enforcer';enforce.float(3.14159, 13.2);// => 3.14159enforce.float('3.14159', 13.2);// => 13.2enforce.float('3.14159', 13.2, true);// => 3.14159```
<a name="enforce.function"></a>

### enforce.function(value, alt) ⇒ <code>function</code>
Expand Down Expand Up @@ -170,7 +188,7 @@ Enforce that a value is an instance of a constructor. Uses [isInstanceOf](docs/c
<a name="enforce.integer"></a>

### enforce.integer(value, alt, [coerce], [minValue], [maxValue]) ⇒ <code>int</code>
Enforce that a value is an integer. Uses [isInteger](docs/checks.md#isInteger).
Enforce that a value is a finite integer. Uses [isInteger](docs/checks.md#isInteger).

**Kind**: static method of [<code>enforce</code>](#enforce)

Expand Down Expand Up @@ -200,7 +218,7 @@ Enforce that a value is a number (excluding NaN). Uses [isNumber](docs/checks.md
| [maxValue] | <code>Number</code> | <code>Infinity</code> | |

**Example**
``` javascriptimport { enforce } from 'type-enforcer';enforce.number(3.14159, 13.2);// => 3.14159enforce.number('3.14159', 13.2);// => 13.2enforce.number('3.14159', 13.2, true);// => 3.14159```
``` javascriptimport { enforce } from 'type-enforcer';enforce.number(3.14159, 13.2);// => 3.14159enforce.number('3.14159', 13.2);// => 13.2enforce.number('3.14159', 13.2, true);// => 3.14159enforce.number(Infinity, 13.2, true);// => Infinity```
<a name="enforce.object"></a>

### enforce.object(value, alt, [coerce]) ⇒ <code>Object</code>
Expand Down
19 changes: 18 additions & 1 deletion docs/method.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ Enforce data types and remove common boilerplate code on class methods. ## Usag
* [.dockPoint([options])](#method.dockPoint) ⇒ <code>function</code>
* [.element([options])](#method.element) ⇒ <code>function</code>
* [.enum([options])](#method.enum) ⇒ <code>function</code>
* [.float([options])](#method.float) ⇒ <code>function</code>
* [.function([options])](#method.function) ⇒ <code>function</code>
* [.instance([options])](#method.instance) ⇒ <code>function</code>
* [.int([options])](#method.int) ⇒ <code>function</code>
Expand Down Expand Up @@ -162,6 +163,22 @@ Builds a chainable method for getting/setting an enumerable value in an [Enum](d
| [options.enforce] | <code>function</code> | <code>enforce.enum</code> | |
| options.enum | <code>Enum</code> | | An enum to restrict the values to. |

<a name="method.float"></a>

### method.float([options]) ⇒ <code>function</code>
Builds a chainable method for getting/setting a float

**Kind**: static method of [<code>method</code>](#method)
**Extends**: [<code>any</code>](#method.any)

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| [options] | <code>Object</code> | | Same as [any](#method.any) with the following differences: |
| [options.enforce] | <code>function</code> | <code>enforce.float</code> | |
| [options.coerce] | <code>Boolean</code> | <code>false</code> | If true then coerce the value when possible |
| [options.min] | <code>Number</code> | | Passed to enforce.float |
| [options.max] | <code>Number</code> | | Passed to enforce.float |

<a name="method.function"></a>

### method.function([options]) ⇒ <code>function</code>
Expand Down Expand Up @@ -278,7 +295,7 @@ Builds a chainable method that implements a [Queue](docs/Queue.md)
| Param | Type | Description |
| --- | --- | --- |
| [options] | <code>Object</code> | |
| [options.set] | <code>function</code> | Called after a new callback is added to the queue. Provides a reference to the queue, sets the context to the methods constructor. |
| [options.set] | <code>function</code> | Called after a new callback is added to the queue. Provides a reference to the queue, the new ID for the callback, the callback, and sets the context to the methods constructor. |

<a name="method.regExp"></a>

Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "type-enforcer",
"version": "0.3.3",
"version": "0.4.0",
"description": "Type enforcement library for javascript",
"main": "src/index.js",
"scripts": {
Expand Down Expand Up @@ -40,7 +40,7 @@
},
"homepage": "https://github.com/DarrenPaulWright/type-enforcer#readme",
"dependencies": {
"deep-equal": "^1.0.1"
"object-agent": "^0.3.1"
},
"devDependencies": {
"@babel/core": "^7.2.2",
Expand Down
2 changes: 2 additions & 0 deletions src/checks/is.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import isCssSize from './types/isCssSize';
import isDate from './types/isDate';
import isDockPoint from './types/isDockPoint';
import isElement from './types/isElement';
import isFloat from './types/isFloat';
import isFunction from './types/isFunction';
import isInstanceOf from './types/isInstanceOf';
import isInteger from './types/isInteger';
Expand Down Expand Up @@ -37,6 +38,7 @@ export default {
date: isDate,
dockPoint: isDockPoint,
element: isElement,
float: isFloat,
function: isFunction,
instanceOf: isInstanceOf,
integer: isInteger,
Expand Down
2 changes: 1 addition & 1 deletion src/checks/types/isArray.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ import isJson from './isJson';
*
* @returns {Boolean}
*/
export default buildCheckWithCoerce((item) => Array.isArray(item), (value) => isJson(value) && Array.isArray(JSON.parse(value)));
export default buildCheckWithCoerce(Array.isArray, (value) => isJson(value) && Array.isArray(JSON.parse(value)));
33 changes: 33 additions & 0 deletions src/checks/types/isFloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { buildCheckWithCoerce } from './checks';
import isNumber from './isNumber';

const isFinite = (item) => item !== Infinity && item !== -Infinity;

/**
* Check if a value is a finite float
*
* @example
* ``` javascript
* import { isFloat } from 'type-enforcer';
*
* isFloat(3.14159);
* // => true
*
* isFloat('3.14159');
* // => false
*
* isFloat('3.14159', true);
* // => true
* ```
*
* @function isFloat
*
* @arg {*} value
* @arg {Boolean} [coerce=false] - If true then see if the value can be coerced into a float
*
* @returns {Boolean}
*/
export default buildCheckWithCoerce((item) => isNumber(item) && item !== Infinity && item !== -Infinity, (value) => {
const parsed = parseFloat(value);
return !isNaN(value) && parsed !== Infinity && parsed !== -Infinity;
});
10 changes: 8 additions & 2 deletions src/checks/types/isInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { buildCheckWithCoerce } from './checks';
import isNumber from './isNumber';

/**
* Check if a value is an integer
* Check if a value is a finite integer
*
* @example
* ``` javascript
Expand All @@ -16,6 +16,9 @@ import isNumber from './isNumber';
*
* isInteger('42', true);
* // => true
*
* isInteger('42.5', true);
* // => false
* ```
*
* @function isInteger
Expand All @@ -25,4 +28,7 @@ import isNumber from './isNumber';
*
* @returns {Boolean}
*/
export default buildCheckWithCoerce((item) => Number.isInteger(item) || isNumber(item) && Number.isInteger(Number.parseFloat(item)), (value) => !isNaN(value) && !isNaN(parseInt(value)));
export default buildCheckWithCoerce((item) => Number.isInteger(item) || isNumber(item) && Number.isInteger(Number.parseFloat(item)), (value) => {
const parsed = parseFloat(value);
return !isNaN(value) && parsed === (parsed | 0);
});
4 changes: 1 addition & 3 deletions src/checks/types/isObject.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { buildCheckWithCoerce } from './checks';
import isJson from './isJson';

// const isPlainObject = (item) => item && typeof item === 'object' && item instanceof Object;
// const isPlainObject = (item) => isInstanceOf(item, Object);
const isPlainObject = (item) => item && typeof item === 'object' && toString.call(item) === '[object Object]' && item.constructor === Object;
const isPlainObject = (item) => item && item.constructor === Object;

/**
* Check if a value is a plain object
Expand Down
2 changes: 2 additions & 0 deletions src/enforcer/enforce.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import enforceDate from './types/enforceDate';
import enforceDockPoint from './types/enforceDockPoint';
import enforceElement from './types/enforceElement';
import enforceEnum from './types/enforceEnum';
import enforceFloat from './types/enforceFloat';
import enforceFunction from './types/enforceFunction';
import enforceInstance from './types/enforceInstance';
import enforceInteger from './types/enforceInteger';
Expand Down Expand Up @@ -38,6 +39,7 @@ export default {
dockPoint: enforceDockPoint,
element: enforceElement,
enum: enforceEnum,
float: enforceFloat,
function: enforceFunction,
instance: enforceInstance,
integer: enforceInteger,
Expand Down
31 changes: 31 additions & 0 deletions src/enforcer/types/enforceFloat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import isFloat from '../../checks/types/isFloat';
import { numericEnforcer } from './enforcer';

/**
* Enforce that a value is a finite float. Uses [isFloat](docs/checks.md#isFloat).
*
* @example
* ``` javascript
* import { enforce } from 'type-enforcer';
*
* enforce.float(3.14159, 13.2);
* // => 3.14159
*
* enforce.float('3.14159', 13.2);
* // => 13.2
*
* enforce.float('3.14159', 13.2, true);
* // => 3.14159
* ```
*
* @function enforce.float
*
* @arg {*} value
* @arg {int} alt - Returned if the value is not the correct type
* @arg {Boolean} [coerce=false] - If true then coerce the value when possible
* @arg {int} [minValue]
* @arg {int} [maxValue]
*
* @returns {int}
*/
export default numericEnforcer(isFloat, Number);
4 changes: 2 additions & 2 deletions src/enforcer/types/enforceInteger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import isInteger from '../../checks/types/isInteger';
import { numericEnforcer } from './enforcer';

/**
* Enforce that a value is an integer. Uses [isInteger](docs/checks.md#isInteger).
* Enforce that a value is a finite integer. Uses [isInteger](docs/checks.md#isInteger).
*
* @example
* ``` javascript
Expand All @@ -28,4 +28,4 @@ import { numericEnforcer } from './enforcer';
*
* @returns {int}
*/
export default numericEnforcer(isInteger, Number);
export default numericEnforcer(isInteger, (item) => item - 0);
3 changes: 3 additions & 0 deletions src/enforcer/types/enforceNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { numericEnforcer } from './enforcer';
*
* enforce.number('3.14159', 13.2, true);
* // => 3.14159
*
* enforce.number(Infinity, 13.2, true);
* // => Infinity
* ```
*
* @function enforce.number
Expand Down
Loading

0 comments on commit 2281285

Please sign in to comment.