@tmlmobilidade/strings 20251202.1817.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a secure random number between 0 and 1.
3
+ * This function uses the Web Crypto API so it is suitable
4
+ * for cryptographic purposes.
5
+ * @returns A random number between 0 and 1
6
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
7
+ */
8
+ export declare function generateRandomNumber(): number;
@@ -0,0 +1,14 @@
1
+ /* * */
2
+ import crypto from 'crypto';
3
+ /**
4
+ * Generates a secure random number between 0 and 1.
5
+ * This function uses the Web Crypto API so it is suitable
6
+ * for cryptographic purposes.
7
+ * @returns A random number between 0 and 1
8
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/Crypto/getRandomValues
9
+ */
10
+ export function generateRandomNumber() {
11
+ const array = new Uint32Array(1);
12
+ crypto.getRandomValues(array);
13
+ return array[0] / 2 ** 32;
14
+ }
@@ -0,0 +1,12 @@
1
+ interface GenerateRandomStringProps {
2
+ length?: number;
3
+ type?: 'alphabetic' | 'alphanumeric' | 'numeric';
4
+ }
5
+ /**
6
+ * Creates a random string of a given length and type.
7
+ * @param length The length of the string to generate. Defaults to `6`.
8
+ * @param type The type of characters to include in the string. Defaults to `alphanumeric`.
9
+ * @returns A random string of the specified length.
10
+ */
11
+ export declare function generateRandomString({ length, type }?: GenerateRandomStringProps): string;
12
+ export {};
@@ -0,0 +1,32 @@
1
+ /* * */
2
+ import { generateRandomNumber } from './generate-random-number.js';
3
+ /**
4
+ * Creates a random string of a given length and type.
5
+ * @param length The length of the string to generate. Defaults to `6`.
6
+ * @param type The type of characters to include in the string. Defaults to `alphanumeric`.
7
+ * @returns A random string of the specified length.
8
+ */
9
+ export function generateRandomString({ length = 6, type = 'alphanumeric' } = {}) {
10
+ //
11
+ const numericSet = '0123456789';
12
+ const alphabeticSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
13
+ const alphanumericSet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
14
+ let allowedCharacters;
15
+ switch (type) {
16
+ case 'alphabetic':
17
+ allowedCharacters = alphabeticSet;
18
+ break;
19
+ case 'numeric':
20
+ allowedCharacters = numericSet;
21
+ break;
22
+ default:
23
+ allowedCharacters = alphanumericSet;
24
+ break;
25
+ }
26
+ let result = '';
27
+ for (let i = 0; i < length; i++) {
28
+ result += allowedCharacters.charAt(Math.floor(generateRandomNumber() * allowedCharacters.length));
29
+ }
30
+ return result;
31
+ //
32
+ }
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generates a secure random token.
3
+ * This function uses the Node.js crypto module to generate
4
+ * a random token suitable for cryptographic purposes.
5
+ * @returns A random token as a hexadecimal string.
6
+ * @see https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
7
+ */
8
+ export declare function generateRandomToken(): string;
@@ -0,0 +1,12 @@
1
+ /* * */
2
+ import crypto from 'crypto';
3
+ /**
4
+ * Generates a secure random token.
5
+ * This function uses the Node.js crypto module to generate
6
+ * a random token suitable for cryptographic purposes.
7
+ * @returns A random token as a hexadecimal string.
8
+ * @see https://nodejs.org/api/crypto.html#crypto_crypto_randombytes_size_callback
9
+ */
10
+ export function generateRandomToken() {
11
+ return crypto.randomBytes(32).toString('hex');
12
+ }
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Utility function that returns a humanized version of a string.
3
+ * @param value The string to humanize. Ex: `ThisIsAString`
4
+ * @returns The humanized string. Ex: `This Is A String`
5
+ */
6
+ export declare function humanize(value: string): string;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Utility function that returns a humanized version of a string.
3
+ * @param value The string to humanize. Ex: `ThisIsAString`
4
+ * @returns The humanized string. Ex: `This Is A String`
5
+ */
6
+ export function humanize(value) {
7
+ const str = value
8
+ .replace(/([a-z\d])([A-Z]+)/g, '$1 $2')
9
+ .replace(/\W|_/g, ' ')
10
+ .trim()
11
+ .toLowerCase();
12
+ return `${str.charAt(0).toUpperCase()}${str.slice(1)}`;
13
+ }
@@ -0,0 +1,7 @@
1
+ export * from './generate-random-number.js';
2
+ export * from './generate-random-string.js';
3
+ export * from './generate-random-token.js';
4
+ export * from './humanize.js';
5
+ export * from './normalize.js';
6
+ export * from './truncate.js';
7
+ export * from './validation.js';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ export * from './generate-random-number.js';
2
+ export * from './generate-random-string.js';
3
+ export * from './generate-random-token.js';
4
+ export * from './humanize.js';
5
+ export * from './normalize.js';
6
+ export * from './truncate.js';
7
+ export * from './validation.js';
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Normalizes a string by converting it to lowercase, removing diacritics,
3
+ * and replacing accented characters with their non-accented equivalents.
4
+ * This is useful for case-insensitive and accent-insensitive string comparisons.
5
+ * @param value The string to normalize. Ex: `Café`
6
+ * @returns The normalized string. Ex: `cafe`
7
+ */
8
+ export declare function normalizeString(value: string): string;
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Normalizes a string by converting it to lowercase, removing diacritics,
3
+ * and replacing accented characters with their non-accented equivalents.
4
+ * This is useful for case-insensitive and accent-insensitive string comparisons.
5
+ * @param value The string to normalize. Ex: `Café`
6
+ * @returns The normalized string. Ex: `cafe`
7
+ */
8
+ export function normalizeString(value) {
9
+ // Return an empty string if the value is invalid
10
+ if (typeof value !== 'string')
11
+ return '';
12
+ // Normalize the string
13
+ return value
14
+ .toLowerCase()
15
+ .normalize('NFD')
16
+ .replace(/[\u0300-\u036f]/g, '');
17
+ }
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Truncates a string to a specified length and adds ellipsis if necessary.
3
+ * @param str The string to truncate.
4
+ * @param length The maximum length of the truncated string.
5
+ * @returns The truncated string with ellipsis if it exceeds the specified length, or the original string if it does not.
6
+ */
7
+ export declare const truncate: (str: null | string, length: number) => null | string;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Truncates a string to a specified length and adds ellipsis if necessary.
3
+ * @param str The string to truncate.
4
+ * @param length The maximum length of the truncated string.
5
+ * @returns The truncated string with ellipsis if it exceeds the specified length, or the original string if it does not.
6
+ */
7
+ export const truncate = (str, length) => {
8
+ if (!str || str.length <= length)
9
+ return str;
10
+ return `${str.slice(0, length - 3)}...`;
11
+ };
@@ -0,0 +1,2 @@
1
+ export declare function isEmail(email: string): boolean;
2
+ export declare function isUrl(url: string): boolean;
@@ -0,0 +1,9 @@
1
+ /* * */
2
+ import { z } from 'zod';
3
+ /* * */
4
+ export function isEmail(email) {
5
+ return z.string().email().safeParse(email).success;
6
+ }
7
+ export function isUrl(url) {
8
+ return z.string().url().safeParse(url).success;
9
+ }
package/package.json ADDED
@@ -0,0 +1,48 @@
1
+ {
2
+ "name": "@tmlmobilidade/strings",
3
+ "version": "20251202.1817.5",
4
+ "author": {
5
+ "email": "iso@tmlmobilidade.pt",
6
+ "name": "TML-ISO"
7
+ },
8
+ "license": "AGPL-3.0-or-later",
9
+ "homepage": "https://github.com/tmlmobilidade/go#readme",
10
+ "bugs": {
11
+ "url": "https://github.com/tmlmobilidade/go/issues"
12
+ },
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "git+https://github.com/tmlmobilidade/go.git"
16
+ },
17
+ "keywords": [
18
+ "public transit",
19
+ "tml",
20
+ "transportes metropolitanos de lisboa",
21
+ "go"
22
+ ],
23
+ "publishConfig": {
24
+ "access": "public"
25
+ },
26
+ "type": "module",
27
+ "files": [
28
+ "dist"
29
+ ],
30
+ "main": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "scripts": {
33
+ "build": "tsc && resolve-tspaths",
34
+ "lint": "eslint ./src/ && tsc --noEmit",
35
+ "lint:fix": "eslint ./src/ --fix",
36
+ "watch": "tsc-watch --onSuccess 'resolve-tspaths'"
37
+ },
38
+ "dependencies": {
39
+ "zod": "3.25.76"
40
+ },
41
+ "devDependencies": {
42
+ "@tmlmobilidade/tsconfig": "*",
43
+ "@types/node": "24.10.1",
44
+ "resolve-tspaths": "0.8.23",
45
+ "tsc-watch": "7.2.0",
46
+ "typescript": "5.9.3"
47
+ }
48
+ }