web-utils-kit 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- import{decodeError,encodeError,extractMessage}from"error-message-utils";import{ERRORS}from"../shared/errors.js";import{isObjectValid}from"../validations/index.js";import{FILE_SIZE_THRESHOLD,FILE_SIZE_UNITS,DATE_TEMPLATE_CONFIGS,ONE_DAY_IN_MILLISECONDS,ONE_HOUR_IN_MILLISECONDS,ONE_MINUTE_IN_MILLISECONDS,ONE_MONTH_IN_MILLISECONDS,ONE_SECOND_IN_MILLISECONDS,ONE_WEEK_IN_MILLISECONDS,ONE_YEAR_IN_MILLISECONDS}from"./consts.js";import{canJSONBeSerialized,validateJSONSerializationResult,canJSONBeDeserialized,validateJSONDeserializationResult}from"./validations.js";import{buildNumberFormatConfig,getDateInstance,parseTimeString,sortJSONObjectKeys}from"./utils.js";const prettifyNumber=(e,r)=>{const t=buildNumberFormatConfig(r),i=e.toLocaleString(void 0,{minimumFractionDigits:t.minimumFractionDigits,maximumFractionDigits:t.maximumFractionDigits});return`${t.prefix}${i}${t.suffix}`},prettifyDate=(e,r)=>getDateInstance(e).toLocaleString(void 0,DATE_TEMPLATE_CONFIGS[r]),prettifyFileSize=(e,r=2)=>{if("number"==typeof e&&e>0){let t=e;if(Math.abs(e)<FILE_SIZE_THRESHOLD)return`${e} B`;let i=-1;const a=10**r;do{t/=FILE_SIZE_THRESHOLD,i+=1}while(Math.round(Math.abs(t)*a)/a>=FILE_SIZE_THRESHOLD&&i<FILE_SIZE_UNITS.length-1);return`${t.toFixed(r)} ${FILE_SIZE_UNITS[i]}`}return"0 B"},prettifyBadgeCount=(e,r=9)=>{if(0!==e)return e>=r?`${r}+`:String(e)},capitalizeFirst=e=>e.length>0?`${e[0].toUpperCase()}${e.slice(1)}`:"",toTitleCase=e=>e.replace(/\w\S*/g,(e=>e.charAt(0).toUpperCase()+e.substring(1).toLowerCase())),toSlug=e=>{const r=e.normalize("NFKD").replace(/[\u0300-\u036f]/g,"").toLowerCase().trim().replace(/['’]/g,"").replace(/[^a-z0-9]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"");if(0===r.length)throw new Error(encodeError(`Failed to slugify the string '${e}': the resulting slug is empty.`,ERRORS.UNABLE_TO_SLUGIFY_STRING));return r},truncateText=(e,r)=>e.length<=r?e:`${e.slice(0,r-3)}...`,maskMiddle=(e,r,t="...")=>e.length<=2*r?e:`${e.slice(0,r)}${t}${e.slice(-r)}`,applySubstitutions=(e,r={})=>e.replace(/{{(.*?)}}/g,((e,t)=>t in r?String(r[t]):e)),toMS=e=>{const{value:r,unit:t}=parseTimeString(e);switch(t){case"years":case"year":return r*ONE_YEAR_IN_MILLISECONDS;case"months":case"month":return r*ONE_MONTH_IN_MILLISECONDS;case"weeks":case"week":return r*ONE_WEEK_IN_MILLISECONDS;case"days":case"day":return r*ONE_DAY_IN_MILLISECONDS;case"hours":case"hour":return r*ONE_HOUR_IN_MILLISECONDS;case"minutes":case"minute":return r*ONE_MINUTE_IN_MILLISECONDS;case"seconds":case"second":return r*ONE_SECOND_IN_MILLISECONDS;case"milliseconds":case"millisecond":return r;default:throw new Error(encodeError(`The unit provided to the toMS function is invalid: "${t}". Received: ${e}`,ERRORS.INVALID_TIME_STRING))}},stringifyJSON=e=>{try{canJSONBeSerialized(e);const r=JSON.stringify(e);return validateJSONSerializationResult(e,r),r}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_SERIALIZE_JSON)throw new Error(encodeError(`Failed to stringify the JSON value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_SERIALIZE_JSON));throw r}},stringifyJSONDeterministically=e=>{canJSONBeSerialized(e);try{return stringifyJSON(sortJSONObjectKeys(e))}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_SERIALIZE_JSON)throw new Error(encodeError(`Failed to stringify the JSON value deterministically '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_SERIALIZE_JSON));throw r}},parseJSON=e=>{canJSONBeDeserialized(e);try{const r=JSON.parse(e);return validateJSONDeserializationResult(e,r),r}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_DESERIALIZE_JSON)throw new Error(encodeError(`Failed to parse the JSON value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_DESERIALIZE_JSON));throw r}},createDeepClone=e=>{try{return parseJSON(stringifyJSON(e))}catch(r){throw new Error(encodeError(`Failed to create a deep clone of the value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_CREATE_DEEP_CLONE))}},pruneJSON=e=>{if(null==e)return null;if(Array.isArray(e)){const r=e.map((e=>pruneJSON(e))).filter((e=>null!=e&&(Array.isArray(e)?e.length>0:!isObjectValid(e,!0)||Object.keys(e).length>0)));return r.length>0?r:null}if(isObjectValid(e,!0)){const r={};return Object.entries(e).forEach((([e,t])=>{const i=pruneJSON(t);null!=i&&(Array.isArray(i)&&0===i.length||isObjectValid(i,!0)&&!isObjectValid(i)||(r[e]=i))})),Object.keys(r).length>0?r:null}return e};export{prettifyNumber,prettifyDate,prettifyFileSize,prettifyBadgeCount,capitalizeFirst,toTitleCase,toSlug,truncateText,maskMiddle,applySubstitutions,toMS,stringifyJSON,stringifyJSONDeterministically,parseJSON,createDeepClone,pruneJSON};
1
+ import{decodeError,encodeError,extractMessage}from"error-message-utils";import{ERRORS}from"../shared/errors.js";import{isObjectValid}from"../validations/index.js";import{FILE_SIZE_THRESHOLD,FILE_SIZE_UNITS,DATE_TEMPLATE_CONFIGS,ONE_DAY_IN_MILLISECONDS,ONE_HOUR_IN_MILLISECONDS,ONE_MINUTE_IN_MILLISECONDS,ONE_MONTH_IN_MILLISECONDS,ONE_SECOND_IN_MILLISECONDS,ONE_WEEK_IN_MILLISECONDS,ONE_YEAR_IN_MILLISECONDS}from"./consts.js";import{canJSONBeSerialized,validateJSONSerializationResult,canJSONBeDeserialized,validateJSONDeserializationResult}from"./validations.js";import{buildNumberFormatConfig,getDateInstance,parseTimeString,sortJSONObjectKeys}from"./utils.js";const prettifyNumber=(e,r)=>{const t=buildNumberFormatConfig(r),i=e.toLocaleString(void 0,{minimumFractionDigits:t.minimumFractionDigits,maximumFractionDigits:t.maximumFractionDigits});return`${t.prefix}${i}${t.suffix}`},prettifyDate=(e,r)=>getDateInstance(e).toLocaleString(void 0,DATE_TEMPLATE_CONFIGS[r]),prettifyFileSize=(e,r=2)=>{if("number"==typeof e&&e>0){let t=e;if(Math.abs(e)<FILE_SIZE_THRESHOLD)return`${e} B`;let i=-1;const a=10**r;do{t/=FILE_SIZE_THRESHOLD,i+=1}while(Math.round(Math.abs(t)*a)/a>=FILE_SIZE_THRESHOLD&&i<FILE_SIZE_UNITS.length-1);return`${t.toFixed(r)} ${FILE_SIZE_UNITS[i]}`}return"0 B"},prettifyBadgeCount=(e,r=9)=>{if(0!==e)return e>=r?`${r}+`:String(e)},capitalizeFirst=e=>e.length>0?`${e[0].toUpperCase()}${e.slice(1)}`:"",toTitleCase=e=>e.replace(/\w\S*/g,(e=>e.charAt(0).toUpperCase()+e.substring(1).toLowerCase())),toSlug=e=>{const r=e.normalize("NFKD").replace(/[\u0300-\u036f]/g,"").toLowerCase().trim().replace(/['’]/g,"").replace(/[^a-z0-9]+/g,"-").replace(/-+/g,"-").replace(/^-|-$/g,"");if(0===r.length)throw new Error(encodeError(`Failed to slugify the string '${e}': the resulting slug is empty.`,ERRORS.UNABLE_TO_SLUGIFY_STRING));return r},truncateText=(e,r)=>e.length<=r?e:`${e.slice(0,r-3)}...`,maskMiddle=(e,r,t="...")=>e.length<=2*r?e:`${e.slice(0,r)}${t}${e.slice(-r)}`,applySubstitutions=(e,r={})=>e.replace(/{{(.*?)}}/g,((e,t)=>t in r?String(r[t]):e)),toMS=e=>{const{value:r,unit:t}=parseTimeString(e);switch(t){case"years":case"year":return r*ONE_YEAR_IN_MILLISECONDS;case"months":case"month":return r*ONE_MONTH_IN_MILLISECONDS;case"weeks":case"week":return r*ONE_WEEK_IN_MILLISECONDS;case"days":case"day":return r*ONE_DAY_IN_MILLISECONDS;case"hours":case"hour":return r*ONE_HOUR_IN_MILLISECONDS;case"minutes":case"minute":return r*ONE_MINUTE_IN_MILLISECONDS;case"seconds":case"second":return r*ONE_SECOND_IN_MILLISECONDS;case"milliseconds":case"millisecond":return r;default:throw new Error(encodeError(`The unit provided to the toMS function is invalid: "${t}". Received: ${e}`,ERRORS.INVALID_TIME_STRING))}},stringifyJSON=e=>{canJSONBeSerialized(e);try{const r=JSON.stringify(e);return validateJSONSerializationResult(e,r),r}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_SERIALIZE_JSON)throw new Error(encodeError(`Failed to stringify the JSON value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_SERIALIZE_JSON));throw r}},stringifyJSONDeterministically=e=>{canJSONBeSerialized(e);try{return stringifyJSON(sortJSONObjectKeys(e))}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_SERIALIZE_JSON)throw new Error(encodeError(`Failed to stringify the JSON value deterministically '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_SERIALIZE_JSON));throw r}},parseJSON=e=>{canJSONBeDeserialized(e);try{const r=JSON.parse(e);return validateJSONDeserializationResult(e,r),r}catch(r){if(decodeError(r).code!==ERRORS.UNABLE_TO_DESERIALIZE_JSON)throw new Error(encodeError(`Failed to parse the JSON value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_DESERIALIZE_JSON));throw r}},createDeepClone=e=>{try{return parseJSON(stringifyJSON(e))}catch(r){throw new Error(encodeError(`Failed to create a deep clone of the value '${e}': ${extractMessage(r)}`,ERRORS.UNABLE_TO_CREATE_DEEP_CLONE))}},pruneJSON=e=>{if(null==e)return null;if(Array.isArray(e)){const r=e.map((e=>pruneJSON(e))).filter((e=>null!=e&&(Array.isArray(e)?e.length>0:!isObjectValid(e,!0)||Object.keys(e).length>0)));return r.length>0?r:null}if(isObjectValid(e,!0)){const r={};return Object.entries(e).forEach((([e,t])=>{const i=pruneJSON(t);null!=i&&(Array.isArray(i)&&0===i.length||isObjectValid(i,!0)&&!isObjectValid(i)||(r[e]=i))})),Object.keys(r).length>0?r:null}return e};export{prettifyNumber,prettifyDate,prettifyFileSize,prettifyBadgeCount,capitalizeFirst,toTitleCase,toSlug,truncateText,maskMiddle,applySubstitutions,toMS,stringifyJSON,stringifyJSONDeterministically,parseJSON,createDeepClone,pruneJSON};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "web-utils-kit",
3
- "version": "1.1.0",
3
+ "version": "1.1.1",
4
4
  "description": "The web-utils-kit package provides a collection of well-tested and thoroughly documented utility functions for various web development needs. Each function adheres to a strict coding style and best practices to ensure consistency and maintainability.",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",