| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152 |
- import crypto from 'crypto'
- export default class StringUtils {
- /**
- * Normalise une chaine de caractères en retirant la casse et les caractères spéciaux, à des fins de recherche
- * par exemple
- * @param s
- */
- public static normalize(s: string): string {
- return s
- .toLowerCase()
- .replace(/[éèẽëê]/g, 'e')
- .replace(/[ç]/g, 'c')
- .replace(/[îïĩ]/g, 'i')
- .replace(/[àã]/g, 'a')
- .replace(/[öôõ]/g, 'o')
- .replace(/[ûüũ]/g, 'u')
- .replace(/[-]/g, ' ')
- .trim()
- }
- /**
- * Convertit le paramètre d'entrée en entier
- * A la différence de parseInt, cette méthode accepte aussi les nombres.
- * @param s
- */
- public static parseInt(s: string | number) {
- return typeof s === 'number' ? s : parseInt(s)
- }
- /**
- * Hash une chaine de caractères avec l'algorithme demandé
- *
- * @param input
- * @param algorithm
- */
- public static async hash(input: string, algorithm: string = 'SHA-256') {
- const textAsBuffer = new TextEncoder().encode(input)
- const isNode =
- typeof process !== 'undefined' &&
- process.versions != null &&
- process.versions.node != null
- const cryptoLib = isNode ? crypto : window.crypto
- const hashBuffer = await cryptoLib.subtle.digest(algorithm, textAsBuffer)
- const hashArray = Array.from(new Uint8Array(hashBuffer))
- return hashArray.map((item) => item.toString(16).padStart(2, '0')).join('')
- }
- }
|