import XRegExp from 'xregexp'; import Config, { MultiLang } from './config'; const escape = (str: string) => { return encodeURIComponent(str).replace(/[!'()*]/g, (c) => { return '%' + c.charCodeAt(0).toString(16); }); } const unescape = (str: string) => { return decodeURIComponent(str); } const htmlspecialchars = (str: string) => { return str.replace(/(<|>|&|'|")/g, (match) => { switch (match) { case '<': return '<'; case '>': return '>'; case '&': return '&'; case '\'': return '''; case '"': return '"'; } return ''; }); } const camelCase = (str: string) => { return str.replace(/[-_](.)/g, (...matches) => { return matches[1].toUpperCase(); }); } const snakeCase = (str: string) => { var camel = camelCase(str); return camel.replace(/[A-Z]/g, (...matches) => { return "_" + matches[0].charAt(0).toLowerCase(); }); } const pascalCase = (str: string) => { var camel = camelCase(str); return camel.charAt(0).toUpperCase() + camel.slice(1); } const base64Encode = (str: string) => { return btoa(unescape(encodeURIComponent(str))); } const base64Decode = (str: string) => { return decodeURIComponent(escape(atob(str))); } const ordinal = (num: number) => { const hundred = num % 100; if (hundred >= 10 && hundred <= 20) { return 'th'; } const ten = num % 10; const suffix = ['st', 'nd', 'rd']; return ten > 0 && ten < 4 ? suffix[ten - 1] : 'th'; } const mlang = (str: string, lang: MultiLang) => { const mlLangs = ['en', 'ja']; // escape brackets inside of let text = str.replace(/(]*)(>)/isg, (whole, m1, m2, m3) => { if (m2.match(/type=["']?(?:text|hidden)["']?/is)) { return m1 + m2.replace(/\[/g, '__ml[ml__') + m3; } return whole; }); // escape brackets inside of text = text.replace(/(]*>)(.*)(<\/textarea>)/isg, (whole, m1, m2, m3) => { return m1 + m2.replace(/\[/g, '__ml[ml__') + m3; }); // simple pattern to strip selected lang_tags const re = new RegExp('\\[/?(?:[^\\]]+\\|)?' + lang + '(?:\\|[^\\]]+)?\\](?:
)?', 'g'); text = text.replace(re, ''); // eliminate description between the other language tags. mlLangs.forEach((mlLang) => { if (mlLang !== lang) { const re = XRegExp('\\[(?:[^/][^\\]]+\\|)?' + mlLang + '(?:\\|[^\\]]+)?\\].*?\\[/(?:[^\\]]+\\|)?' + mlLang + '(?:\\|[^\\]]+)?\\](?:
)?', 'isg'); text = text.replace(re, (whole) => { return whole.match(/<\/table>/) ? whole : ''; }); } }); // unescape brackets inside of text = text.replace(/(]*>)(.*)(<\/textarea>)/isg, (whole, m1, m2, m3) => { return m1 + m2.replace(/__ml\[ml__/g, '[') + m3; }); // unescape brackets inside of text = text.replace(/(]*)(>)/isg, (whole, m1, m2, m3) => { if (m2.match(/type=["']?(?:text|hidden)["']?/is)) { return m1 + m2.replace(/__ml\[ml__/g, '[') + m3; } return whole; }); return text; } const siteTitle = (lang: MultiLang) => { return mlang(Config.SITE_TITLE, lang); } const siteSlogan = (lang: MultiLang) => { return mlang(Config.SITE_SLOGAN, lang); } const Functions = { escape, unescape, htmlspecialchars, camelCase, snakeCase, pascalCase, base64Encode, base64Decode, ordinal, mlang, siteTitle, siteSlogan, }; export default Functions;