generated from gapitio/utility-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from gapitio/feature/calculate-null-and-bool-o…
…ptions feat: add options to calculate bool and/or null
- Loading branch information
Showing
5 changed files
with
248 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Types, getType } from "./getType"; | ||
|
||
describe("getType", () => { | ||
it("returns Null", () => { | ||
expect(getType(null)).toEqual(Types.Null); | ||
}); | ||
it("returns Undefined", () => { | ||
expect(getType(undefined)).toEqual(Types.Undefined); | ||
}); | ||
it("returns Boolean", () => { | ||
expect(getType(false)).toEqual(Types.Boolean); | ||
expect(getType(true)).toEqual(Types.Boolean); | ||
}); | ||
it("returns String", () => { | ||
expect(getType("test")).toEqual(Types.String); | ||
expect(getType("10")).toEqual(Types.String); | ||
expect(getType("undefined")).toEqual(Types.String); | ||
expect(getType("null")).toEqual(Types.String); | ||
}); | ||
it("returns Number", () => { | ||
expect(getType(0)).toEqual(Types.Number); | ||
expect(getType(1)).toEqual(Types.Number); | ||
expect(getType(1.5)).toEqual(Types.Number); | ||
expect(getType(1000)).toEqual(Types.Number); | ||
expect(getType(-1000)).toEqual(Types.Number); | ||
expect(getType(Infinity)).toEqual(Types.Number); | ||
expect(getType(-Infinity)).toEqual(Types.Number); | ||
}); | ||
it("returns Array", () => { | ||
expect(getType([])).toEqual(Types.Array); | ||
expect(getType([1, 2, 3, 4])).toEqual(Types.Array); | ||
expect(getType(Array(5))).toEqual(Types.Array); | ||
expect(getType(Array(5).fill(null))).toEqual(Types.Array); | ||
expect(getType([{ a: 1 }])).toEqual(Types.Array); | ||
}); | ||
it("returns Object", () => { | ||
expect(getType({})).toEqual(Types.Object); | ||
expect(getType({ a: 2 })).toEqual(Types.Object); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Checks the type of value and returns it as a string | ||
* This works with all types (null, undefined, object, ETC) | ||
* | ||
* @param value - Value to check the type of | ||
* | ||
* @returns the type of the value as a string | ||
*/ | ||
export function getType(value: unknown): string { | ||
return Object.prototype.toString.call(value).slice(8, -1); | ||
} | ||
|
||
export const enum Types { | ||
Null = "Null", | ||
Undefined = "Undefined", | ||
Boolean = "Boolean", | ||
String = "String", | ||
Number = "Number", | ||
Array = "Array", | ||
Object = "Object", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters