Skip to content

Commit

Permalink
refactor: remove any type in isUndefined. (#207)
Browse files Browse the repository at this point in the history
* fix: remove any type

* feat: add isDefined function

* feat: add throwIf function

* test: add isDefined test

* test: add throwIf test

* fix: format import path

* feat: add isNull function

* fix: isNull return type

* test: add new util function's type-check test case

* refactor: refactoring util function logic

* fix: rollback to original isEmpty logic

* fix: delete non-related code
  • Loading branch information
rojiwon123 authored Jun 2, 2023
1 parent f5a7904 commit dba6bc5
Show file tree
Hide file tree
Showing 49 changed files with 98 additions and 57 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import isArray from "./isArray";
import isBoolean from "./isBoolean";
import isEmpty from "./isEmpty";
import isNil from "./isNil";
import isNull from "./isNull";
import isNumber from "./isNumber";
import isObject from "./isObject";
import isString from "./isString";
Expand Down Expand Up @@ -92,6 +93,7 @@ export {
isObject,
isString,
isUndefined,
isNull,
join,
juxt,
last,
Expand Down
11 changes: 4 additions & 7 deletions src/isNil.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import isNull from "./isNull";
import isUndefined from "./isUndefined";

type Nullable<T> = T extends null | undefined ? T : never;

/**
Expand All @@ -11,12 +14,6 @@ type Nullable<T> = T extends null | undefined ? T : never;
* isNil(null); // true
* ```
*/
function isNil<T>(a: T): a is Nullable<T> {
if (a === undefined || a === null) {
return true;
}

return false;
}
const isNil = <T>(a: T): a is Nullable<T> => isUndefined(a) || isNull(a);

export default isNil;
14 changes: 14 additions & 0 deletions src/isNull.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Checks if the given value is `null`.
*
* @example
* ```ts
* isNull(1); // false
* isNull('1'); // false
* isNull(undefined); // false
* isNull(null); // true
* ```
*/
const isNull = <T>(input: T | null): input is null => input === null;

export default isNull;
6 changes: 2 additions & 4 deletions src/isUndefined.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/**
* Returns true if `a` is a undefined.
* Checks if the given value is `undefined`.
*
* @example
* ```ts
* isUndefined(undefined); // true
* isUndefined(2); // false
* ```
*/
function isUndefined(a: any): a is undefined {
return typeof a === "undefined";
}
const isUndefined = <T>(a: T | undefined): a is undefined => a === undefined;

export default isUndefined;
2 changes: 1 addition & 1 deletion test/apply.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { apply, pipe, range, toArray } from "../src/index";
import { apply, pipe, range, toArray } from "../src";

describe("apply", function () {
it("should apply given list to the function", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/consume.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { consume, peek, pipe, toAsync } from "../src/index";
import { consume, peek, pipe, toAsync } from "../src";

describe("consume", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/countBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { countBy, filter, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { countBy, filter, pipe, toAsync } from "../src/index";

type Obj = {
category: "clothes" | "pants" | "shoes";
Expand Down
2 changes: 1 addition & 1 deletion test/each.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { each, map, pipe, range, toAsync } from "../src/index";
import { each, map, pipe, range, toAsync } from "../src";

describe("each", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/every.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { every, filter, map, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { every, filter, map, pipe, toAsync } from "../src/index";

describe("every", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/evolve.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { evolve, pipe } from "../src/index";
import { evolve, pipe } from "../src";

describe("evolve", function () {
const add1 = (a: number) => a + 1;
Expand Down
2 changes: 1 addition & 1 deletion test/find.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, find, map, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { filter, find, map, pipe, toAsync } from "../src/index";
import type Arrow from "../src/types/Arrow";

describe("find", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/findIndex.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, findIndex, map, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { filter, findIndex, map, pipe, toAsync } from "../src/index";
import type Arrow from "../src/types/Arrow";

describe("findIndex", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/fromEntries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { fromEntries, toAsync } from "../src/index";
import { fromEntries, toAsync } from "../src";

//an object from string keyed-value pairs.
describe("fromEntries", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/groupBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, groupBy, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { filter, groupBy, pipe, toAsync } from "../src/index";

type Obj = {
category: "clothes" | "pants" | "shoes";
Expand Down
2 changes: 1 addition & 1 deletion test/gt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, gt, pipe, toArray } from "../src/index";
import { filter, gt, pipe, toArray } from "../src";

describe("gt(greater then)", function () {
describe("currying", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/gte.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, gte, pipe, toArray } from "../src/index";
import { filter, gte, pipe, toArray } from "../src";

describe("gte(greater then or equal to)", function () {
describe("currying", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/head.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, head, map, pipe, toAsync } from "../src";
import { range } from "../src/Lazy";
import { filter, head, map, pipe, toAsync } from "../src/index";

describe("head", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/includes.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, includes, map, pipe, toAsync } from "../src/index";
import { filter, includes, map, pipe, toAsync } from "../src";

describe("includes", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/indexBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, indexBy, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { filter, indexBy, pipe, toAsync } from "../src/index";

type Obj = {
category: "clothes" | "pants" | "shoes";
Expand Down
2 changes: 1 addition & 1 deletion test/isArray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isArray } from "../src/index";
import { isArray } from "../src";

describe("isArray", function () {
it.each([undefined, null, true, 1, "a", Symbol("a"), () => null])(
Expand Down
2 changes: 1 addition & 1 deletion test/isBoolean.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isBoolean } from "../src/index";
import { isBoolean } from "../src";

describe("isBoolean", function () {
it.each([undefined, null, 1, "1", Symbol("a"), () => null])(
Expand Down
6 changes: 2 additions & 4 deletions test/isEmpty.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isEmpty from "../src/isEmpty";
import { isEmpty } from "../src";

describe("isEmpty", function () {
const testParameters = [
Expand All @@ -22,8 +22,6 @@ describe("isEmpty", function () {

it.each(testParameters)(
"should Return `true` if the given value is empty value(%s)",
function (input, expected) {
expect(isEmpty(input)).toEqual(expected);
},
(input, expected) => expect(isEmpty(input)).toEqual(expected),
);
});
2 changes: 1 addition & 1 deletion test/isNil.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNil } from "../src/index";
import { isNil } from "../src";

describe("isNil", function () {
it("should check if given value is `null` or `undefined`", function () {
Expand Down
16 changes: 16 additions & 0 deletions test/isNull.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { isNull } from "../src";

describe("isNull", function () {
it.each([2, true, undefined, {}, [], Symbol("a"), "a"])(
"given non null then should be false",
function (a) {
const result = isNull(a);
expect(result).toEqual(false);
},
);

it("given null then should be true", function () {
const result = isNull(null);
expect(result).toEqual(true);
});
});
2 changes: 1 addition & 1 deletion test/isNumber.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber } from "../src/index";
import { isNumber } from "../src";

describe("isNumber", function () {
it.each([undefined, null, true, "1", Symbol("a"), () => null])(
Expand Down
2 changes: 1 addition & 1 deletion test/isString.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString } from "../src/index";
import { isString } from "../src";

describe("isString", function () {
it.each([undefined, null, true, 1, Symbol("a"), () => null])(
Expand Down
2 changes: 1 addition & 1 deletion test/isUndefined.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import isUndefined from "../src/isUndefined";
import { isUndefined } from "../src";

describe("isUndefined", function () {
it.each([2, true, null, {}, [], Symbol("a"), "a"])(
Expand Down
2 changes: 1 addition & 1 deletion test/join.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, join, map, pipe, toAsync } from "../src";
import { asyncEmpty, empty } from "../src/_internal/utils";
import { filter, join, map, pipe, toAsync } from "../src/index";

describe("join", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/last.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, last, map, pipe, range, toAsync } from "../src/index";
import { filter, last, map, pipe, range, toAsync } from "../src";

describe("last", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/lt.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, lt, pipe, toArray } from "../src/index";
import { filter, lt, pipe, toArray } from "../src";

describe("lt(less then)", function () {
describe("currying", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/lte.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, lte, pipe, toArray } from "../src/index";
import { filter, lte, pipe, toArray } from "../src";

describe("lte(less then or equal to)", function () {
describe("currying", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/noop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { noop } from "../src/index";
import { noop } from "../src";

describe("noop", function () {
it("should return `undefined`", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/nth.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, map, nth, pipe, toAsync } from "../src/index";
import { filter, map, nth, pipe, toAsync } from "../src";

describe("nth", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/omit.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omit, pipe, toAsync } from "../src/index";
import { omit, pipe, toAsync } from "../src";

describe("omit", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/omitBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { omitBy, pipe } from "../src/index";
import { omitBy, pipe } from "../src";

describe("omitBy", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/partition.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { map, partition, pipe, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { map, partition, pipe, toAsync } from "../src/index";

describe("partition", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/pick.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pick, pipe, toAsync } from "../src/index";
import { pick, pipe, toAsync } from "../src";

describe("pick", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/pickBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pickBy, pipe } from "../src/index";
import { pickBy, pipe } from "../src";

describe("pickBy", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/pipe.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
reduce,
toArray,
toAsync,
} from "../src/index";
} from "../src";

describe("pipe", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/pipe1.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe1 } from "../src/index";
import { pipe1 } from "../src";

const add10 = (a: number) => a + 10;
const add10Async = async (a: number) => a + 10;
Expand Down
2 changes: 1 addition & 1 deletion test/prop.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe, prop } from "../src/index";
import { pipe, prop } from "../src";

describe("prop", () => {
it("should return the value for the given object property", () => {
Expand Down
2 changes: 1 addition & 1 deletion test/props.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { pipe, props, toArray } from "../src/index";
import { pipe, props, toArray } from "../src";

describe("props", () => {
const obj = { a: "v1", b: "v2", c: "v3", d: "v4", e: "v5", f: "v6" };
Expand Down
2 changes: 1 addition & 1 deletion test/reduce.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, map, pipe, range, reduce, toAsync } from "../src/index";
import { filter, map, pipe, range, reduce, toAsync } from "../src";

const addNumber = (a: number, b: number) => a + b;
const addNumberAsync = async (a: number, b: number) => a + b;
Expand Down
2 changes: 1 addition & 1 deletion test/some.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { filter, map, pipe, some, toAsync } from "../src";
import { AsyncFunctionException } from "../src/_internal/error";
import { filter, map, pipe, some, toAsync } from "../src/index";

describe("some", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/sort.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, pipe, sort, toAsync } from "../src/index";
import { filter, pipe, sort, toAsync } from "../src";

describe("sort", function () {
const sortFn = (a: number | string, b: number | string) => {
Expand Down
2 changes: 1 addition & 1 deletion test/sortBy.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { filter, identity, pipe, sortBy, toAsync } from "../src/index";
import { filter, identity, pipe, sortBy, toAsync } from "../src";

describe("sortBy", function () {
describe("sync", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/tap.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { delay, map, pipe, range, tap, toArray, toAsync } from "../src/index";
import { delay, map, pipe, range, tap, toArray, toAsync } from "../src";
import { callFuncAfterTime } from "./utils";

describe("tap", function () {
Expand Down
2 changes: 1 addition & 1 deletion test/toArray.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { range, toArray, toAsync } from "../src/index";
import { range, toArray, toAsync } from "../src";

const identityP = <T>(a: T) => Promise.resolve(a);

Expand Down
16 changes: 16 additions & 0 deletions type-check/isNull.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { filter, isNull, pipe } from "../src";
import * as Test from "../src/types/Test";

const { checks, check } = Test;

const res1 = isNull(undefined);
const res2 = isNull(null);
const res3 = isNull(3);
const res4 = pipe([1, null, 2], filter(isNull));

checks([
check<typeof res1, boolean, Test.Pass>(),
check<typeof res2, boolean, Test.Pass>(),
check<typeof res3, boolean, Test.Pass>(),
check<typeof res4, IterableIterator<null>, Test.Pass>(),
]);

0 comments on commit dba6bc5

Please sign in to comment.