mentie 0.3.1 → 0.3.2
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.
- package/modules/text.js +29 -0
- package/package.json +1 -1
package/modules/text.js
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import { random_number_between } from "./numbers"
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Truncates a given text to a specified length and appends a suffix if necessary.
|
|
3
5
|
*
|
|
@@ -38,3 +40,30 @@ export const capitalise = string => {
|
|
|
38
40
|
if( !string ) return ''
|
|
39
41
|
return `${ string?.charAt( 0 ).toUpperCase() }${ string?.slice( 1, string?.length ) }`
|
|
40
42
|
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Generates a random letter from the allowed characters.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} [allowed_chars] - A string of characters to choose from. If not provided, defaults to the lowercase English alphabet.
|
|
49
|
+
* @returns {string} A randomly selected character from the allowed characters.
|
|
50
|
+
*/
|
|
51
|
+
export const random_letter = ( allowed_chars, capitals=true ) => {
|
|
52
|
+
|
|
53
|
+
const letters = 'abcdefghijklmnopqrstuvwxyz'
|
|
54
|
+
let chars = allowed_chars || letters
|
|
55
|
+
if( capitals ) chars += letters.toUpperCase()
|
|
56
|
+
|
|
57
|
+
return chars.charAt( Math.floor( Math.random() * chars.length ) )
|
|
58
|
+
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export const random_string_of_length = ( length, allowed_chars, numbers=true ) => {
|
|
62
|
+
|
|
63
|
+
return Array.from( { length }, () => {
|
|
64
|
+
// If numbers are allowed, add a 50% chance of generating a number
|
|
65
|
+
if( numbers && Math.random() > 0.5 ) return `${ random_number_between( 9, 0 ) }`
|
|
66
|
+
return random_letter( allowed_chars )
|
|
67
|
+
} ).join( '' )
|
|
68
|
+
|
|
69
|
+
}
|