From 65494a0a919c15630f4d28476893a199938e4140 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 3 Apr 2020 15:27:51 +0200 Subject: [PATCH] Move basicType to lib/helpers Move the basicType function from the config lib to a shared helpers lib. --- lib/config/classes.js | 17 ++--------------- lib/helpers.js | 23 +++++++++++++++++++++++ test/lib/helpers.js | 17 +++++++++++++++++ 3 files changed, 42 insertions(+), 15 deletions(-) create mode 100644 lib/helpers.js create mode 100644 test/lib/helpers.js diff --git a/lib/config/classes.js b/lib/config/classes.js index acbaa212..c43ec6be 100644 --- a/lib/config/classes.js +++ b/lib/config/classes.js @@ -4,6 +4,8 @@ */ "use strict"; +const { basicType } = require("lib/helpers"); + /** * Invalid Value exception @@ -39,21 +41,6 @@ function splitOn(separator, string) { return [string.slice(0, index), string.slice(index + separator.length)]; } -/** - * Return a string describing the type of the value passed - * - * Works the same as typeof, excpet that null and array types get their - * own string. - * - * @param value - value to return the type of. - * @returns {string} basic type of the value passed. - */ -function basicType(value) { - if (value === null) { return "null"; } - if (value instanceof Array) { return "array"; } - return typeof value; -} - /** * Collection of config groups * diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 00000000..afc1f9bf --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,23 @@ +/** + * Collection of small utilites that are useful in multiple places. + * @module + */ + +/** + * Return a string describing the type of the value passed + * + * Works the same as typeof, excpet that null and array types get their + * own string. + * + * @param value - value to return the type of. + * @returns {string} basic type of the value passed. + */ +function basicType(value) { + if (value === null) { return "null"; } + if (value instanceof Array) { return "array"; } + return typeof value; +} + +module.exports = { + basicType, +} diff --git a/test/lib/helpers.js b/test/lib/helpers.js new file mode 100644 index 00000000..e5586bd5 --- /dev/null +++ b/test/lib/helpers.js @@ -0,0 +1,17 @@ +const assert = require("assert").strict; + +const helpers = require("lib/helpers"); + + +describe("lib/helpers", function() { + describe("basicType()", function() { + it("should return the expected values", function() { + assert.equal(helpers.basicType("s"), "string"); + assert.equal(helpers.basicType(null), "null"); + assert.equal(helpers.basicType(undefined), "undefined"); + assert.equal(helpers.basicType([1, 2]), "array"); + assert.equal(helpers.basicType(7), "number"); + assert.equal(helpers.basicType({ a: 1 }), "object"); + }); + }); +});