DocSpace-buildtools/web/ASC.Web.Common/src/utils/index.js

53 lines
1.2 KiB
JavaScript
Raw Normal View History

import { LANGUAGE } from "../constants";
import sjcl from "sjcl";
export const toUrlParams = (obj, skipNull) => {
let str = "";
for (var key in obj) {
if (skipNull && !obj[key]) continue;
if (str !== "") {
str += "&";
}
str += key + "=" + encodeURIComponent(obj[key]);
}
return str;
};
export function getObjectByLocation(location) {
if (!location.search || !location.search.length) return null;
const searchUrl = location.search.substring(1);
const object = JSON.parse(
'{"' +
decodeURIComponent(searchUrl)
.replace(/"/g, '\\"')
.replace(/&/g, '","')
.replace(/=/g, '":"') +
'"}'
);
return object;
}
export function changeLanguage(i18n) {
const currentLng = localStorage.getItem(LANGUAGE);
return currentLng
? i18n.language !== currentLng
? i18n.changeLanguage(currentLng)
: Promise.resolve((...args) => i18n.t(...args))
: i18n.changeLanguage("en");
}
export function createHashPassword(password, settings) {
const { size, iterations, salt } = settings;
let bits = sjcl.misc.pbkdf2(password, salt, iterations);
bits = bits.slice(0, size / 32);
const hash = sjcl.codec.hex.fromBits(bits);
return hash;
}