crypto.createHash('sha1').update(str).digest('latin1'); // raw_output
crypto.createHash('sha1').update(str).digest('base64'); // сразу base64
async function sha1(str) {
const buf = Uint8Array.from(unescape(encodeURIComponent(str)), c=>c.charCodeAt(0)).buffer;
const digest = await crypto.subtle.digest('SHA-1', buf);
const raw = String.fromCharCode.apply(null, new Uint8Array(digest));
return raw; // 20-символьная строка
// или
return btoa(raw); // base64
}
return str.replace(/^(\d{4})(\d+)(\d{4})$/, (m, p1, p2, p3) => p1 + '*'.repeat(p2.length) + p3);
The third argument can be used to create virtual mocks – mocks of modules that don't exist anywhere in the system:
jest.mock( '../moduleName', () => { /* * Custom implementation of a module that doesn't exist in JS, * like a generated module or a native module in react-native. */ }, {virtual: true}, );