diff --git a/lib/globals/intex.ts b/lib/globals/intex.ts new file mode 100644 index 00000000..98a12ce1 --- /dev/null +++ b/lib/globals/intex.ts @@ -0,0 +1,3 @@ +/** @namespace Global */ + +import "./isBrowser"; diff --git a/lib/globals/isBrowser/index.ts b/lib/globals/isBrowser/index.ts new file mode 100644 index 00000000..f2dff584 --- /dev/null +++ b/lib/globals/isBrowser/index.ts @@ -0,0 +1,21 @@ +import * as method from "./method"; + +declare global { + namespace NodeJS { + interface Global { + isBrowser(): boolean; + } + } + + const isBrowser: () => boolean; +} + +/** + * Determines if the current runtime environment is a browser, + * so that front-end modules can run on the server (Node) without throwing errors + * @memberof Global + * @returns {boolean} + * @example + * isBrowser(); // false + */ +global.isBrowser = method; diff --git a/lib/globals/isBrowser/method.ts b/lib/globals/isBrowser/method.ts new file mode 100644 index 00000000..eaa231b6 --- /dev/null +++ b/lib/globals/isBrowser/method.ts @@ -0,0 +1,5 @@ +import * as contains from "../../array/contains/method"; + +const method = () => !contains([typeof window, typeof document], "undefined"); + +export = method; diff --git a/lib/globals/isBrowser/test.ts b/lib/globals/isBrowser/test.ts new file mode 100644 index 00000000..356e6b3e --- /dev/null +++ b/lib/globals/isBrowser/test.ts @@ -0,0 +1,13 @@ +import "./index"; + +beforeAll(() => { + // Jest creates virtual ones ! + delete (global as any).window; + delete (global as any).document; +}); + +describe("Global.isBrowser", () => { + test("isBrowser() returns false", () => { + expect(isBrowser()).toBe(false); + }); +}); diff --git a/lib/globals/methods.ts b/lib/globals/methods.ts new file mode 100644 index 00000000..6fb1c288 --- /dev/null +++ b/lib/globals/methods.ts @@ -0,0 +1,5 @@ +import * as isBrowser from "./isBrowser/method"; + +export { + isBrowser, +}; diff --git a/lib/index.ts b/lib/index.ts index 7baa8616..ca6b701a 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -2,6 +2,7 @@ import "./array"; import "./boolean"; import "./date"; import "./function"; +import "./globals"; import "./math"; import "./number"; import "./object"; diff --git a/lib/methods.ts b/lib/methods.ts index b0929c3e..c07a9e8d 100644 --- a/lib/methods.ts +++ b/lib/methods.ts @@ -2,6 +2,7 @@ import * as array from "./array/methods"; import * as boolean from "./boolean/methods"; import * as date from "./date/methods"; import * as func from "./function/methods"; +import * as globals from "./globals/methods"; import * as math from "./math/methods"; import * as number from "./number/methods"; import * as object from "./object/methods"; @@ -12,6 +13,7 @@ export { boolean, date, func as function, + globals, math, number, object, diff --git a/lib/object/size/method.ts b/lib/object/size/method.ts index 3f401e5a..d4833dd5 100644 --- a/lib/object/size/method.ts +++ b/lib/object/size/method.ts @@ -1,3 +1,3 @@ -const method = (obj: { [key: string]: any }): number => Object.keys(obj).length; +const method = (obj: { [key: string]: any }): number => obj.size || obj.length || Object.keys(obj).length; export = method; diff --git a/lib/string/base64/method.ts b/lib/string/base64/method.ts index 789da91c..8345b62d 100644 --- a/lib/string/base64/method.ts +++ b/lib/string/base64/method.ts @@ -1,3 +1,3 @@ -const method = (str: string) => new Buffer(str).toString("base64"); +const method = (str: string) => Buffer.from(str).toString("base64"); export = method; diff --git a/lib/string/base64Decode/method.ts b/lib/string/base64Decode/method.ts index f61f7e67..727fa0b4 100644 --- a/lib/string/base64Decode/method.ts +++ b/lib/string/base64Decode/method.ts @@ -1,3 +1,3 @@ -const method = (str: string) => new Buffer(str, "base64").toString(); +const method = (str: string) => Buffer.from(str, "base64").toString(); export = method;