Skip to content

Commit

Permalink
Add isUinteger32 function
Browse files Browse the repository at this point in the history
  • Loading branch information
rhian-cs committed May 9, 2022
1 parent e8df0ac commit 7a2166a
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ export { default as isNotFinite } from './isNotFinite';
export { default as isInteger } from './isInteger';
export { default as isInteger32 } from './isInteger32';
export { default as isInt32 } from './isInteger32'; // alias of isInteger32
export { default as isUinteger32 } from './isUinteger32';
export { default as isUint32 } from './isUinteger32'; // alias of isUinteger32
export { default as isNotInteger } from './isNotInteger';
export { default as isBigInt } from './isBigInt';
export { default as isFloat } from './isFloat';
Expand Down
29 changes: 29 additions & 0 deletions src/isUinteger32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { curryN } from 'ramda';

import toUinteger32 from './toUinteger32';

/**
* Checks whether the passed value is an unsigned 32 bit integer.
*
* @func isUinteger32
* @aliases isUint32
* @memberOf RA
* @since {@link https://char0n.github.io/ramda-adjunct/3.2.0|v3.2.0}
* @category Type
* @sig * -> Boolean
* @param {*} val The value to test
* @return {boolean}
* @see {@link RA.toUinteger32|toUinteger32}
* @example
*
* RA.isUinteger32(0); //=> true
* RA.isUinteger32(2 ** 32 - 1); //=> true
*
* RA.isUinteger32(Infinity); //=> false
* RA.isUinteger32(NaN); //=> false
* RA.isUinteger32(-1); //=> false
* RA.isUinteger32(2 ** 32); //=> false
*/
const isUinteger32 = curryN(1, (val) => toUinteger32(val) === val);

export default isUinteger32;
44 changes: 44 additions & 0 deletions test/isUinteger32.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import * as R from 'ramda';
import { assert } from 'chai';

import * as RA from '../src';
import MAX_SAFE_INTEGER from '../src/internal/ponyfills/Number.MAX_SAFE_INTEGER';
import MIN_SAFE_INTEGER from '../src/internal/ponyfills/Number.MIN_SAFE_INTEGER';

describe('isUinteger32', function () {
it('should return true for positive 32 bit integers', function () {
assert.isTrue(RA.isUinteger32(0));
assert.isTrue(RA.isUinteger32(100000));
assert.isTrue(RA.isUinteger32(2 ** 32 - 1));
});

it('should return false for negative 32 bit integers', function () {
assert.isFalse(RA.isUinteger32(-1));
assert.isFalse(RA.isUinteger32(-(2 ** 32 - 1)));
});

it('should return false for 64 bit integers', function () {
assert.isFalse(RA.isUinteger32(2 ** 32));
assert.isFalse(RA.isUinteger32(MIN_SAFE_INTEGER));
assert.isFalse(RA.isUinteger32(MAX_SAFE_INTEGER));
});

it('should return false for non integers', function () {
assert.isFalse(RA.isUinteger32(null));
assert.isFalse(RA.isUinteger32(undefined));
assert.isFalse(RA.isUinteger32(NaN));
assert.isFalse(RA.isUinteger32(Infinity));
assert.isFalse(RA.isUinteger32(-Infinity));
assert.isFalse(RA.isUinteger32(0.1));
assert.isFalse(RA.isUinteger32(Math.E));
assert.isFalse(RA.isUinteger32('10'));
assert.isFalse(RA.isUinteger32(false));
assert.isFalse(RA.isUinteger32([]));
assert.isFalse(RA.isUinteger32({}));
});

it('should support placeholder to specify "gaps"', function () {
const isUinteger32 = RA.isUinteger32(R.__);
assert.isTrue(isUinteger32(0));
});
});
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ declare namespace RamdaAdjunct {
*/
isInteger32(val: any): boolean;

/**
* Checks whether the passed value is an unsigned 32 bit integer number.
*/
isUinteger32(val: any): boolean;
isUint32(val: any): boolean; // alias

/**
* Checks whether the passed value is complement of `integer`.
*/
Expand Down
16 changes: 16 additions & 0 deletions types/test/isUinteger32.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import * as RA from 'ramda-adjunct';

RA.isUinteger32(0); // $ExpectType boolean
RA.isUinteger32(null); // $ExpectType boolean
RA.isUinteger32(true); // $ExpectType boolean
RA.isUinteger32(NaN); // $ExpectType boolean
RA.isUinteger32({}); // $ExpectType boolean
RA.isUinteger32([]); // $ExpectType boolean
RA.isUinteger32(() => {}); // $ExpectType boolean
RA.isUinteger32('string'); // $ExpectType boolean
RA.isUinteger32(1); // $ExpectType boolean
RA.isUinteger32(0.1); // $ExpectType boolean
RA.isUinteger32(Object(0.1)); // $ExpectType boolean
RA.isUinteger32(Infinity); // $ExpectType boolean
RA.isUinteger32(Number.MAX_VALUE); // $ExpectType boolean
RA.isUinteger32(Number.MIN_VALUE); // $ExpectType boolean

0 comments on commit 7a2166a

Please sign in to comment.