tiny-essentials
Version:
Collection of small, essential scripts designed to be used across various projects. These simple utilities are crafted for speed, ease of use, and versatility.
1 lines • 15.8 kB
JavaScript
(()=>{var e={606:e=>{var t,r,n=e.exports={};function o(){throw new Error("setTimeout has not been defined")}function i(){throw new Error("clearTimeout has not been defined")}function s(e){if(t===setTimeout)return setTimeout(e,0);if((t===o||!t)&&setTimeout)return t=setTimeout,setTimeout(e,0);try{return t(e,0)}catch(r){try{return t.call(null,e,0)}catch(r){return t.call(this,e,0)}}}!function(){try{t="function"==typeof setTimeout?setTimeout:o}catch(e){t=o}try{r="function"==typeof clearTimeout?clearTimeout:i}catch(e){r=i}}();var a,l=[],c=!1,f=-1;function h(){c&&a&&(c=!1,a.length?l=a.concat(l):f=-1,l.length&&u())}function u(){if(!c){var e=s(h);c=!0;for(var t=l.length;t;){for(a=l,l=[];++f<t;)a&&a[f].run();f=-1,t=l.length}a=null,c=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===i||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(e);try{return r(e)}catch(t){try{return r.call(null,e)}catch(t){return r.call(this,e)}}}(e)}}function p(e,t){this.fun=e,this.array=t}function g(){}n.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var r=1;r<arguments.length;r++)t[r-1]=arguments[r];l.push(new p(e,t)),1!==l.length||c||s(u)},p.prototype.run=function(){this.fun.apply(null,this.array)},n.title="browser",n.browser=!0,n.env={},n.argv=[],n.version="",n.versions={},n.on=g,n.addListener=g,n.once=g,n.off=g,n.removeListener=g,n.removeAllListeners=g,n.emit=g,n.prependListener=g,n.prependOnceListener=g,n.listeners=function(e){return[]},n.binding=function(e){throw new Error("process.binding is not supported")},n.cwd=function(){return"/"},n.chdir=function(e){throw new Error("process.chdir is not supported")},n.umask=function(){return 0}},766:()=>{},911:()=>{},975:(e,t,r)=>{"use strict";var n=r(606);function o(e){if("string"!=typeof e)throw new TypeError("Path must be a string. Received "+JSON.stringify(e))}function i(e,t){for(var r,n="",o=0,i=-1,s=0,a=0;a<=e.length;++a){if(a<e.length)r=e.charCodeAt(a);else{if(47===r)break;r=47}if(47===r){if(i===a-1||1===s);else if(i!==a-1&&2===s){if(n.length<2||2!==o||46!==n.charCodeAt(n.length-1)||46!==n.charCodeAt(n.length-2))if(n.length>2){var l=n.lastIndexOf("/");if(l!==n.length-1){-1===l?(n="",o=0):o=(n=n.slice(0,l)).length-1-n.lastIndexOf("/"),i=a,s=0;continue}}else if(2===n.length||1===n.length){n="",o=0,i=a,s=0;continue}t&&(n.length>0?n+="/..":n="..",o=2)}else n.length>0?n+="/"+e.slice(i+1,a):n=e.slice(i+1,a),o=a-i-1;i=a,s=0}else 46===r&&-1!==s?++s:s=-1}return n}var s={resolve:function(){for(var e,t="",r=!1,s=arguments.length-1;s>=-1&&!r;s--){var a;s>=0?a=arguments[s]:(void 0===e&&(e=n.cwd()),a=e),o(a),0!==a.length&&(t=a+"/"+t,r=47===a.charCodeAt(0))}return t=i(t,!r),r?t.length>0?"/"+t:"/":t.length>0?t:"."},normalize:function(e){if(o(e),0===e.length)return".";var t=47===e.charCodeAt(0),r=47===e.charCodeAt(e.length-1);return 0!==(e=i(e,!t)).length||t||(e="."),e.length>0&&r&&(e+="/"),t?"/"+e:e},isAbsolute:function(e){return o(e),e.length>0&&47===e.charCodeAt(0)},join:function(){if(0===arguments.length)return".";for(var e,t=0;t<arguments.length;++t){var r=arguments[t];o(r),r.length>0&&(void 0===e?e=r:e+="/"+r)}return void 0===e?".":s.normalize(e)},relative:function(e,t){if(o(e),o(t),e===t)return"";if((e=s.resolve(e))===(t=s.resolve(t)))return"";for(var r=1;r<e.length&&47===e.charCodeAt(r);++r);for(var n=e.length,i=n-r,a=1;a<t.length&&47===t.charCodeAt(a);++a);for(var l=t.length-a,c=i<l?i:l,f=-1,h=0;h<=c;++h){if(h===c){if(l>c){if(47===t.charCodeAt(a+h))return t.slice(a+h+1);if(0===h)return t.slice(a+h)}else i>c&&(47===e.charCodeAt(r+h)?f=h:0===h&&(f=0));break}var u=e.charCodeAt(r+h);if(u!==t.charCodeAt(a+h))break;47===u&&(f=h)}var p="";for(h=r+f+1;h<=n;++h)h!==n&&47!==e.charCodeAt(h)||(0===p.length?p+="..":p+="/..");return p.length>0?p+t.slice(a+f):(a+=f,47===t.charCodeAt(a)&&++a,t.slice(a))},_makeLong:function(e){return e},dirname:function(e){if(o(e),0===e.length)return".";for(var t=e.charCodeAt(0),r=47===t,n=-1,i=!0,s=e.length-1;s>=1;--s)if(47===(t=e.charCodeAt(s))){if(!i){n=s;break}}else i=!1;return-1===n?r?"/":".":r&&1===n?"//":e.slice(0,n)},basename:function(e,t){if(void 0!==t&&"string"!=typeof t)throw new TypeError('"ext" argument must be a string');o(e);var r,n=0,i=-1,s=!0;if(void 0!==t&&t.length>0&&t.length<=e.length){if(t.length===e.length&&t===e)return"";var a=t.length-1,l=-1;for(r=e.length-1;r>=0;--r){var c=e.charCodeAt(r);if(47===c){if(!s){n=r+1;break}}else-1===l&&(s=!1,l=r+1),a>=0&&(c===t.charCodeAt(a)?-1===--a&&(i=r):(a=-1,i=l))}return n===i?i=l:-1===i&&(i=e.length),e.slice(n,i)}for(r=e.length-1;r>=0;--r)if(47===e.charCodeAt(r)){if(!s){n=r+1;break}}else-1===i&&(s=!1,i=r+1);return-1===i?"":e.slice(n,i)},extname:function(e){o(e);for(var t=-1,r=0,n=-1,i=!0,s=0,a=e.length-1;a>=0;--a){var l=e.charCodeAt(a);if(47!==l)-1===n&&(i=!1,n=a+1),46===l?-1===t?t=a:1!==s&&(s=1):-1!==t&&(s=-1);else if(!i){r=a+1;break}}return-1===t||-1===n||0===s||1===s&&t===n-1&&t===r+1?"":e.slice(t,n)},format:function(e){if(null===e||"object"!=typeof e)throw new TypeError('The "pathObject" argument must be of type Object. Received type '+typeof e);return function(e,t){var r=t.dir||t.root,n=t.base||(t.name||"")+(t.ext||"");return r?r===t.root?r+n:r+"/"+n:n}(0,e)},parse:function(e){o(e);var t={root:"",dir:"",base:"",ext:"",name:""};if(0===e.length)return t;var r,n=e.charCodeAt(0),i=47===n;i?(t.root="/",r=1):r=0;for(var s=-1,a=0,l=-1,c=!0,f=e.length-1,h=0;f>=r;--f)if(47!==(n=e.charCodeAt(f)))-1===l&&(c=!1,l=f+1),46===n?-1===s?s=f:1!==h&&(h=1):-1!==s&&(h=-1);else if(!c){a=f+1;break}return-1===s||-1===l||0===h||1===h&&s===l-1&&s===a+1?-1!==l&&(t.base=t.name=0===a&&i?e.slice(1,l):e.slice(a,l)):(0===a&&i?(t.name=e.slice(1,s),t.base=e.slice(1,l)):(t.name=e.slice(a,s),t.base=e.slice(a,l)),t.ext=e.slice(s,l)),a>0?t.dir=e.slice(0,a-1):i&&(t.dir="/"),t},sep:"/",delimiter:":",win32:null,posix:null};s.posix=s,e.exports=s}},t={};function r(n){var o=t[n];if(void 0!==o)return o.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,r),i.exports}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n={};(()=>{"use strict";r.d(n,{TinyI18:()=>i});var e=r(911),t=r(766),o=r(975);const i=class{static async mergeLocaleFiles({files:r,output:n,spaces:o=0}){if("number"!=typeof o||Number.isNaN(o)||o<0)throw new TypeError('mergeLocaleFiles: "spaces" must be a non-negative number');if(!Array.isArray(r)||0===r.length)throw new TypeError('mergeLocaleFiles: "files" must be a non-empty array of file paths');if("string"!=typeof n||!n)throw new TypeError('mergeLocaleFiles: "output" must be a non-empty string path');const i={};for(const n of r){if("string"!=typeof n||!n)throw new TypeError("mergeLocaleFiles: each file path must be a non-empty string");if(!(0,t.existsSync)(n))throw new Error(`mergeLocaleFiles: file "${n}" does not exist`);const r=await(0,e.readFile)(n,"utf-8");let o;try{o=JSON.parse(r)}catch(e){throw new Error(`mergeLocaleFiles: file "${n}" is not valid JSON`)}Object.assign(i,o)}await(0,e.writeFile)(n,JSON.stringify(i,null,o),"utf-8")}#e;#t;#r=null;#n;#o=null;#i=new Map;#s=new Map;#a=new Map;#l=new Map;get currentLocale(){return this.#r}get defaultLocale(){return this.#t}get mode(){return this.#e}get strict(){return this.#n}get basePath(){return this.#o}get stats(){const e=[];for(const t of this.#i.keys())e.push(this.getStatsForLocale(t));return e}get stringTables(){const e={};for(const[t,r]of this.#i.entries())e[t]=this.#c(r);return e}get patternTables(){const e={};for(const[t,r]of this.#s.entries())e[t]=r.map(e=>({$pattern:new RegExp(e.$pattern.source,e.$pattern.flags),value:this.#c(e.value),elseValue:this.#c(e.elseValue)}));return e}get helpers(){const e={};for(const[t,r]of this.#a.entries())e[t]=r;return e}get regexCache(){const e={};for(const[t,r]of this.#l.entries())e[t]=new RegExp(r.source,r.flags);return e}#c(e){if(null==e||"string"==typeof e||"function"==typeof e||"number"==typeof e)return e;if(e instanceof RegExp)return new RegExp(e.source,e.flags);if(Array.isArray(e))return e.map(e=>this.#c(e));if("object"==typeof e){const t={};for(const[r,n]of Object.entries(e))t[r]=this.#c(n);return t}return e}#f(e){if(void 0!==e&&"string"!=typeof e)throw new TypeError('#resolveOrder: "forceLocale" must be a string if provided');const t=[];return e&&"string"==typeof e&&this.#i.has(e)?t.push(e):this.#r&&t.push(this.#r),t.push(this.#t),t}#h(e,t){if(!Array.isArray(e)||!e.every(e=>"string"==typeof e))throw new TypeError('#resolveExact: "order" must be an array of strings');if("string"!=typeof t||!t)throw new TypeError('#resolveExact: "key" must be a non-empty string');for(const r of e){const e=this.#i.get(r);if(e&&Object.prototype.hasOwnProperty.call(e,t))return e[t]}}#u(e,t){if(!Array.isArray(e)||!e.every(e=>"string"==typeof e))throw new TypeError('#resolveByPattern: "order" must be an array of strings');if("string"!=typeof t||!t)throw new TypeError('#resolveByPattern: "key" must be a non-empty string');for(const r of e){const e=this.#s.get(r)||[];for(const r of e)return r.$pattern.test(t)?r.value:r.elseValue??void 0}}#p(e,t){if(null==e){if(this.#n)throw new TypeError('#materialize: "value" cannot be null or undefined');return""}if("string"==typeof e)return this.#g(e,t);if("function"==typeof e)return e(t??{},this.#y());if(e&&"object"==typeof e&&"string"==typeof e.$fn){const r=this.#a.get(e.$fn);if("function"!=typeof r){if(this.#n)throw new Error(`TinyI18: helper "${e.$fn}" is not registered`);return""}return r({...t??{},args:void 0!==e.args?e.args:void 0},this.#y())}if(this.#n)throw new Error("TinyI18: unsupported entry type");return""}#g(e,t){if("string"!=typeof e)throw new TypeError('#interpolate: "template" must be a string');if(void 0!==t&&(null===t||"object"!=typeof t))throw new TypeError('#interpolate: "params" must be an object if provided');return t&&"object"==typeof t?e.replace(/\{([a-zA-Z0-9_.$-]+)\}/g,(e,r)=>{const n=this.#d(t,r);return null==n?"":String(n)}):e}#d(e,t){if(!e||"object"!=typeof e)throw new TypeError('#dotGet: "obj" must be an object');if("string"!=typeof t||!t)throw new TypeError('#dotGet: "path" must be a non-empty string');const r=t.split(".");let n=e;for(const e of r){if(null==n)return;n=n[e]}return n}#y(){return{has:e=>this.#a.has(e),call:(e,t,r)=>{const n=this.#a.get(e);if("function"!=typeof n)throw new Error(`Helper "${e}" not found`);return n(t,r??{})}}}#b(e,t){if("string"!=typeof e||!e)throw new TypeError('#ingestLocale: "locale" must be a non-empty string');if(!t||"object"!=typeof t)throw new TypeError('#ingestLocale: "raw" must be an object');if("string"!=typeof e||!e)throw new TypeError("#ingestLocale: invalid locale");if(!t||"object"!=typeof t)throw new TypeError('#ingestLocale: "raw" must be an object');const r={...this.#i.get(e)??{}},n=[...this.#s.get(e)??[]],o=(e,t)=>{if(null!=t)if("string"==typeof t||"function"==typeof t||t&&"object"==typeof t&&"string"==typeof t.$fn){if(!e)throw new TypeError("Leaf value requires a key path");r[e]=t}else if(t&&"object"==typeof t){if(t.$pattern instanceof RegExp&&Object.prototype.hasOwnProperty.call(t,"value"))return void n.push({$pattern:t.$pattern,value:t.value,elseValue:t.elseValue});for(const[r,n]of Object.entries(t))o(e?`${e}.${r}`:r,n)}};o("",t),this.#i.set(e,r),this.#s.set(e,n),this.#i.has(this.#t)||(this.#i.set(this.#t,{}),this.#s.set(this.#t,[]))}#w(e){if("string"!=typeof e||!e)throw new TypeError('#unloadLocale: "locale" must be a non-empty string');e!==this.#t&&(this.#i.delete(e),this.#s.delete(e))}async#m(t){if("string"!=typeof t||!t)throw new TypeError('#loadLocaleFromFile: "locale" must be a non-empty string');if("string"!=typeof this.#o||!this.#o)throw new TypeError('#loadLocaleFromFile: "this.#basePath" must be a non-empty string');const r=(0,o.join)(this.#o,`${t}.json`);let n;try{const t=await(0,e.readFile)(r,"utf8");n=JSON.parse(t)}catch(e){if(!(e instanceof Error))return;if(this.#n)throw new Error(`TinyI18: failed to load or parse ${r}: ${e.message}`);return this.#i.set(t,{}),void this.#s.set(t,[])}const i={...this.#i.get(t)??{}},s=[...this.#s.get(t)??[]],a=(e,t)=>{if(null!=t){if(t&&"object"==typeof t&&"string"==typeof t.$pattern&&Object.prototype.hasOwnProperty.call(t,"value")){const e=this.#T(t.$pattern);return void s.push({$pattern:e,value:this.#v(t.value),elseValue:this.#v(t.elseValue)})}if("string"==typeof t||t&&"object"==typeof t&&"string"==typeof t.$fn){if(!e)throw new TypeError("Leaf value requires a key path");i[e]=this.#v(t)}else if(t&&"object"==typeof t)for(const[r,n]of Object.entries(t))a(e?`${e}.${r}`:r,n)}};a("",n),this.#i.set(t,i),this.#s.set(t,s)}#T(e){if("string"!=typeof e||!e)throw new TypeError('#safeRegExp: "src" must be a non-empty string');const t=this.#l.get(e);if(t)return t;try{const t=new RegExp(e);return this.#l.set(e,t),t}catch{if(this.#n)throw new Error(`TinyI18: invalid regex "${e}" in file`);return/^$/}}#v(e){if("string"==typeof e)return e;if(e&&"object"==typeof e&&"string"==typeof e.$fn)return{$fn:String(e.$fn),args:e.args};if(this.#n)throw new Error("TinyI18: unsupported value in file JSON");return""}constructor(e){if(!e||"object"!=typeof e)throw new TypeError("TinyI18: options must be an object");const{mode:t,defaultLocale:r,basePath:n,localResources:o,strict:i=!0}=e;if("local"!==t&&"file"!==t)throw new TypeError('TinyI18: "mode" must be "local" or "file"');if("string"!=typeof r||!r)throw new TypeError('TinyI18: "defaultLocale" must be a non-empty string');if("boolean"!=typeof i)throw new TypeError('TinyI18: "strict" must be a boolean');if("file"===t){if("string"!=typeof n||!n)throw new TypeError('TinyI18: "basePath" is required in file mode');this.#o=n}if(this.#e=t,this.#t=r,this.#n=i,"local"===t&&o&&"object"==typeof o)for(const[e,t]of Object.entries(o))this.#b(e,t);this.#i.has(this.#t)||(this.#i.set(this.#t,{}),this.#s.set(this.#t,[]))}clearRegexCache(){this.#l.clear()}registerHelper(e,t){if("string"!=typeof e||!e)throw new TypeError('registerHelper: "name" must be non-empty string');if("function"!=typeof t)throw new TypeError('registerHelper: "fn" must be a function');this.#a.set(e,t)}unregisterHelper(e){if("string"!=typeof e||!e)throw new TypeError('unregisterHelper: "name" must be non-empty string');return this.#a.delete(e)}loadLocaleLocal(e,t){if("local"!==this.#e)throw new TypeError('loadLocaleLocal is only available in "local" mode');this.#b(e,t)}async setLocale(e){if(null!==e&&("string"!=typeof e||!e))throw new TypeError('setLocale: "locale" must be string or null');const t=this.#r;null!==e&&("file"===this.#e?await this.#m(e):this.#i.has(e)||(this.#i.set(e,{}),this.#s.set(e,[]))),"file"===this.#e&&t&&t!==this.#t&&t!==e&&this.#w(t),this.#r=e}t(e,t=void 0,r=void 0){return this.get(e,t,r)}get(e,t=void 0,r=void 0){if("string"!=typeof e||!e)throw new TypeError('get: "key" must be a non-empty string');const{locale:n}=r||{},o=this.#f(n);let i=this.#h(o,e);if(void 0===i){if(this.#n)throw new Error(`TinyI18: missing translation for key "${e}"`);return e}return this.#p(i,t)}p(e,t){return this.resolveByPattern(e,t)}resolveByPattern(e,t){if("string"!=typeof e||!e)throw new TypeError('get: "key" must be a non-empty string');const{locale:r}=t||{},n=this.#f(r);let o=this.#u(n,e);if(void 0===o){if(this.#n)throw new Error(`TinyI18: missing translation for key "${e}"`);return e}return o}resetToDefaultOnly(){if("file"===this.#e)for(const e of Array.from(this.#i.keys()))e!==this.#t&&this.#w(e);this.#r=null}getStatsForLocale(e){if("string"!=typeof e||!e)throw new TypeError('getStatsForLocale: "locale" must be a non-empty string');if(!this.#i.has(e))throw new Error(`getStatsForLocale: locale "${e}" is not registered`);return{locale:e,strings:Object.keys(this.#i.get(e)??{}).length,patterns:this.#s.get(e)?.length??0,isDefault:e===this.#t,isCurrent:e===this.#r}}}})(),window.TinyI18=n.TinyI18})();