Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve isEmpty #34

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ export * from './utils/has-defined';
export * from './utils/is-arguments';
export * from './utils/is-array';
export * from './utils/is-array-of-strings';
export * from './utils/is-boolean';
export * from './utils/is-empty';
export * from './utils/is-enum-value';
export * from './utils/is-map-with-values-of-type';
export * from './utils/is-number';
export * from './utils/is-object';
export * from './utils/is-promise';
export * from './utils/is-promise-like';
export * from './utils/is-set';
export * from './utils/is-string';
export * from './utils/is-undefined';
export * from './utils/is-null';
Expand Down
2 changes: 1 addition & 1 deletion src/utils/is-arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ import { getTagString } from './get-tag-string';
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
* @returns `true` if `o` is a function's array-like `arguments` variable
*/
export function isArguments(o: any): boolean {
export function isArguments(o: any): o is IArguments {
return getTagString(o) === '[object Arguments]';
}
3 changes: 3 additions & 0 deletions src/utils/is-boolean.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function isBoolean(o: unknown): o is boolean {
return o === true || o === false;
}
35 changes: 32 additions & 3 deletions src/utils/is-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,32 @@ import { isArray } from './is-array';
import { isString } from './is-string';
import { isArguments } from './is-arguments';
import { isUndefined } from './is-undefined';
import { isNull } from './is-null';
import { isBoolean } from './is-boolean';
import { isNumber } from './is-number';
import { isSet } from './is-set';
import { isObject } from './is-object';


interface IEmptyArguments extends IArguments {
length: 0;
}

interface IEmptyObj {
[s: string]: never;
}

type IEmptyTypes = (
null |
undefined |
boolean |
number |
never[] |
'' |
IEmptyArguments |
Set<never> |
IEmptyObj
);

/**
* Checks if `o` is an empty object. An object is "empty" if it:
Expand All @@ -12,12 +38,15 @@ import { isUndefined } from './is-undefined';
*
* @returns `true` if `o` is empty
*/
export function isEmpty(o: any): boolean {
if (o === null || isUndefined(o)) {
export function isEmpty(o: unknown): o is IEmptyTypes {
if (isNull(o) || isUndefined(o) || isBoolean(o) || isNumber(o)) {
return true;
}
if (isArray(o) || isString(o) || isArguments(o)) {
return o.length === 0;
}
return Object.keys(o).length === 0;
if (isSet(o)) {
return o.size === 0;
}
return isObject(o) && Object.keys(o).length === 0;
}
8 changes: 8 additions & 0 deletions src/utils/is-set.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Type guard for `Set`s.
*
* @returns `true` if `o` is a `Set`, regardless of the types that it contains
*/
export function isSet(o: unknown): o is Set<unknown> {
return o instanceof Set;
}
16 changes: 16 additions & 0 deletions tests/utils/is-boolean.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { expect } from 'chai';
import * as t from '../../src/index';


describe('isBoolean', () => {

it('correctly classifies bools', () => {
expect(t.isBoolean(true)).to.strictlyEqual(true);
expect(t.isBoolean(false)).to.strictlyEqual(true);
expect(t.isBoolean(null)).to.strictlyEqual(false);
expect(t.isBoolean(0)).to.strictlyEqual(false);
expect(t.isBoolean('')).to.strictlyEqual(false);
expect(t.isBoolean({})).to.strictlyEqual(false);
});

});
2 changes: 2 additions & 0 deletions tests/utils/is-empty.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ describe('isEmpty', () => {
let o;

expect(t.isEmpty([])).to.strictlyEqual(true);
expect(t.isEmpty(new Set([]))).to.strictlyEqual(true);
// eslint-disable-next-line
expect(t.isEmpty(new Array())).to.strictlyEqual(true);
expect(t.isEmpty({})).to.strictlyEqual(true);
Expand Down Expand Up @@ -42,6 +43,7 @@ describe('isEmpty', () => {
expect(t.isEmpty({ length: 0 })).to.strictlyEqual(false);
expect(t.isEmpty([ 1 ])).to.strictlyEqual(false);
expect(t.isEmpty(new Array(10))).to.strictlyEqual(false);
expect(t.isEmpty(new Set([ 1 ]))).to.strictlyEqual(false);
});

});
30 changes: 30 additions & 0 deletions tests/utils/is-set.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect } from 'chai';
import * as t from '../../src/index';


describe('isSet', () => {

it('correctly classifies sets', () => {
expect(t.isSet(new Set([]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 'a', 'b', 'c' ]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 4 ]))).to.strictlyEqual(true);
expect(t.isSet(new Set([ 'a', 'b', 'c', 4 ]))).to.strictlyEqual(true);
// eslint-disable-next-line
expect(t.isSet(new Set())).to.strictlyEqual(true);
});

it('correctly classifies non-sets', () => {
expect(t.isSet([])).to.strictlyEqual(false);
expect(t.isSet({})).to.strictlyEqual(false);
expect(t.isSet(4)).to.strictlyEqual(false);
expect(t.isSet('')).to.strictlyEqual(false);
expect(t.isSet('a')).to.strictlyEqual(false);
expect(t.isSet(true)).to.strictlyEqual(false);
expect(t.isSet(undefined)).to.strictlyEqual(false);
expect(t.isSet(null)).to.strictlyEqual(false);
expect(t.isSet({ length: 0 })).to.strictlyEqual(false);
// eslint-disable-next-line no-empty-function
expect(t.isSet(() => {})).to.strictlyEqual(false);
});

});
Loading