From 4750131758d00dce4f0392d73bb69fca8cf59ca5 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Thu, 12 Dec 2024 06:43:07 +0100 Subject: [PATCH] Updates --- index.d.ts | 4 ++++ index.js | 4 ++++ package-shared/utils/numberfy.d.ts | 2 ++ package-shared/utils/numberfy.js | 26 ++++++++++++++++++++++++++ package-shared/utils/slugify.d.ts | 2 ++ package-shared/utils/slugify.js | 27 +++++++++++++++++++++++++++ package.json | 2 +- 7 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 package-shared/utils/numberfy.d.ts create mode 100644 package-shared/utils/numberfy.js create mode 100644 package-shared/utils/slugify.d.ts create mode 100644 package-shared/utils/slugify.js diff --git a/index.d.ts b/index.d.ts index f409129..db8d1ab 100644 --- a/index.d.ts +++ b/index.d.ts @@ -34,6 +34,8 @@ export namespace sql { import serializeQuery = require("./package-shared/utils/serialize-query"); import serializeCookies = require("./package-shared/utils/serialize-cookies"); import EJSON = require("./package-shared/utils/ejson"); +import numberfy = require("./package-shared/utils/numberfy"); +import slugify = require("./package-shared/utils/slugify"); import uploadImage = require("./utils/upload-image"); import uploadFile = require("./utils/upload-file"); import deleteFile = require("./utils/delete-file"); @@ -73,5 +75,7 @@ export declare namespace utils { export { serializeQuery }; export { serializeCookies }; export { EJSON }; + export { numberfy }; + export { slugify }; } export { get, post, getSchema, datasquirelClient as client }; diff --git a/index.js b/index.js index 7c7cb2f..fcb0615 100644 --- a/index.js +++ b/index.js @@ -34,6 +34,8 @@ const trimSql = require("./package-shared/utils/trim-sql"); const serializeQuery = require("./package-shared/utils/serialize-query"); const serializeCookies = require("./package-shared/utils/serialize-cookies"); const EJSON = require("./package-shared/utils/ejson"); +const numberfy = require("./package-shared/utils/numberfy"); +const slugify = require("./package-shared/utils/slugify"); //////////////////////////////////////// //////////////////////////////////////// @@ -100,6 +102,8 @@ const datasquirel = { serializeQuery, serializeCookies, EJSON: EJSON, + numberfy, + slugify, }, }; diff --git a/package-shared/utils/numberfy.d.ts b/package-shared/utils/numberfy.d.ts new file mode 100644 index 0000000..2b18cb9 --- /dev/null +++ b/package-shared/utils/numberfy.d.ts @@ -0,0 +1,2 @@ +declare function _exports(num: any, decimals?: number): number; +export = _exports; diff --git a/package-shared/utils/numberfy.js b/package-shared/utils/numberfy.js new file mode 100644 index 0000000..79a66eb --- /dev/null +++ b/package-shared/utils/numberfy.js @@ -0,0 +1,26 @@ +// @ts-check + +/** + * # Get Number from any input + * @param {any} num input + * @param {number} [decimals] number of decimals to round to + * @returns {number} number or 0 in case of error + * @example + * numberfy("123") // 123 + * numberfy("123.456") // 123 + * numberfy("123.456", 2) // 123.46 + * numberfy("123.456", 0) // 123 + * numberfy("123.456", 3) // 123.456 + */ +module.exports = function numberfy(num, decimals) { + try { + const numberfiedNum = Number(num); + if (typeof numberfiedNum !== "number") return 0; + if (isNaN(numberfiedNum)) return 0; + if (decimals) return Number(numberfiedNum.toFixed(decimals)); + return Math.round(numberfiedNum); + } catch (/** @type {any} */ error) { + console.log(`Numberfy ERROR: ${error.message}`); + return 0; + } +}; diff --git a/package-shared/utils/slugify.d.ts b/package-shared/utils/slugify.d.ts new file mode 100644 index 0000000..b323843 --- /dev/null +++ b/package-shared/utils/slugify.d.ts @@ -0,0 +1,2 @@ +declare function _exports(str: string): string; +export = _exports; diff --git a/package-shared/utils/slugify.js b/package-shared/utils/slugify.js new file mode 100644 index 0000000..8c5bc15 --- /dev/null +++ b/package-shared/utils/slugify.js @@ -0,0 +1,27 @@ +// @ts-check + +/** + * # Return the slug of a string + * @param {string} str input + * @returns {string} slug or empty string in case of error + * @example + * slugify("Hello World") // "hello-world" + * slugify("Yes!") // "yes" + * slugify("Hello!!! World!") // "hello-world" + */ +module.exports = function slugify(str) { + try { + return String(str) + .trim() + .toLowerCase() + .replace(/ {2,}/g, " ") + .replace(/ /g, "-") + .replace(/[^a-z0-9]/, "-") + .replace(/-{2,}/, "-") + .replace(/^-/, "") + .replace(/-$/, ""); + } catch (/** @type {any} */ error) { + console.log(`Slugify ERROR: ${error.message}`); + return ""; + } +}; diff --git a/package.json b/package.json index e70ac1a..670d368 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@moduletrace/datasquirel", - "version": "2.9.9", + "version": "3.0.0", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": {