From 29bc8b9e17d4c5e89120d520438cbae27b3e8b02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabio=20Jos=C3=A9?= Date: Sun, 4 Aug 2019 09:35:19 -0300 Subject: [PATCH] isBase64 and clone functions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Fabio José --- lib/utils/fun.js | 11 ++++++++++- test/fun_tests.js | 14 ++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/lib/utils/fun.js b/lib/utils/fun.js index 5534889..82d4aca 100644 --- a/lib/utils/fun.js +++ b/lib/utils/fun.js @@ -25,6 +25,13 @@ const equalsOrThrow = (v1, v2, t) => ? true : (() => {throw t;})()); +const isBase64 = (value) => + Buffer.from(value, "base64").toString("base64") === value; + + +const clone = (o) => + JSON.parse(JSON.stringify(o)); + module.exports = { isString, isStringOrThrow, @@ -34,5 +41,7 @@ module.exports = { isDefinedOrThrow, isStringOrObjectOrThrow, - equalsOrThrow + equalsOrThrow, + isBase64, + clone }; diff --git a/test/fun_tests.js b/test/fun_tests.js index aa4fdb9..d3700f0 100644 --- a/test/fun_tests.js +++ b/test/fun_tests.js @@ -29,4 +29,18 @@ describe("Functional approach", () => { .equals(true); }); }); + + describe("isBase64", () => { + it("should return false when is not base64 string", () => { + let actual = fun.isBase64("non base 64"); + + expect(actual).to.equal(false); + }); + + it("should return true when is a base64 string", () => { + let actual = fun.isBase64("Y2xvdWRldmVudHMK"); + + expect(actual).to.equal(true); + }); + }); });