UNPKG

@iimm/shared

Version:

shared utils on browser and react env

66 lines (64 loc) 2.4 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/cjs/encrypt/index.ts var encrypt_exports = {}; __export(encrypt_exports, { byteDecoder: () => byteDecoder, byteEncoder: () => byteEncoder }); module.exports = __toCommonJS(encrypt_exports); var byteEncoder = (salt = "", EncodeURIComponent = true) => { const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0)); const byteHex = (n) => ("0" + Number(n).toString(16)).slice(-2); const applySaltToChar = (code) => ( // eslint-disable-next-line no-bitwise textToChars(salt).reduce((a, b) => a ^ b, code) ); return (content) => { let txt = typeof content === "string" ? content : JSON.stringify(content); if (EncodeURIComponent) { txt = encodeURIComponent(txt); } return txt.split("").map(textToChars).map(applySaltToChar).map(byteHex).join(""); }; }; var byteDecoder = (salt = "", EncodeURIComponent = true) => { const textToChars = (text) => text.split("").map((c) => c.charCodeAt(0)); const applySaltToChar = (code) => ( // eslint-disable-next-line no-bitwise textToChars(salt).reduce((a, b) => a ^ b, code) ); return (encodedContent) => { const text = encodedContent.match(/.{1,2}/g).map((hex) => parseInt(hex, 16)).map(applySaltToChar).map((charCode) => String.fromCharCode(charCode)).join(""); let res = text; if (EncodeURIComponent) { res = decodeURIComponent(text); } try { const obj = JSON.parse(res); return obj; } catch (error) { return res; } }; }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { byteDecoder, byteEncoder });