This commit is contained in:
Benjamin Toby 2024-12-12 06:43:07 +01:00
parent e250dfc105
commit 4750131758
7 changed files with 66 additions and 1 deletions

4
index.d.ts vendored
View File

@ -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 };

View File

@ -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,
},
};

2
package-shared/utils/numberfy.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
declare function _exports(num: any, decimals?: number): number;
export = _exports;

View File

@ -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;
}
};

2
package-shared/utils/slugify.d.ts vendored Normal file
View File

@ -0,0 +1,2 @@
declare function _exports(str: string): string;
export = _exports;

View File

@ -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 "";
}
};

View File

@ -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": {