qsu 1.3.1 → 1.3.2

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.
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  MIT License
2
2
 
3
- Copyright (c) 2021-2023 jooy2 <jooy2.contact@gmail.com> (https://jooy2.com).
3
+ Copyright (c) 2021-2024 jooy2 <jooy2.contact@gmail.com> (https://jooy2.com).
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy
6
6
  of this software and associated documentation files (the "Software"), to deal
package/README.md CHANGED
@@ -21,4 +21,4 @@ You can report issues on [GitHub Issue Tracker](https://github.com/jooy2/qsu/iss
21
21
 
22
22
  ## License
23
23
 
24
- Copyright © 2021-2023 [Jooy2](https://jooy2.com) <[jooy2.contact@gmail.com](mailto:jooy2.contact@gmail.com)> Released under the MIT license.
24
+ Copyright © 2021-2024 [Jooy2](https://jooy2.com) <[jooy2.contact@gmail.com](mailto:jooy2.contact@gmail.com)> Released under the MIT license.
package/dist/index.d.ts CHANGED
@@ -2,6 +2,9 @@ declare type PositiveNumber<N extends number> = number extends N ? N : `${N}` ex
2
2
  declare type NumberValueObject = {
3
3
  [key: string]: number;
4
4
  };
5
+ declare type AnyValueObject = {
6
+ [key: string]: any;
7
+ };
5
8
  export default class Qsu {
6
9
  static sleep<N extends number>(delay: PositiveNumber<N>): Promise<void>;
7
10
  static funcTimes<N extends number>(times: PositiveNumber<N>, iteratee: any): Array<any>;
@@ -33,6 +36,8 @@ export default class Qsu {
33
36
  static arrCount(array: string[] | number[]): NumberValueObject;
34
37
  static sortByObjectKey(array: any[], key: string, descending?: boolean, numerically?: boolean): any[];
35
38
  static sortNumeric(array: string[], descending?: boolean): string[];
39
+ static objToQueryString(obj: AnyValueObject): string;
40
+ static objToPrettyStr(obj: AnyValueObject): string;
36
41
  static trim(str: string, removeAllSpace?: boolean): string;
37
42
  static removeSpecialChar(str: string, exceptionCharacters?: string): string;
38
43
  static replaceBetween(str: string, startChar: string, endChar: string, replaceWith?: string): string;
@@ -55,6 +60,7 @@ export default class Qsu {
55
60
  static sha256(str: string): string;
56
61
  static encodeBase64(str: string): string;
57
62
  static decodeBase64(encodedStr: string): string;
63
+ static strToNumberHash(str: string): number;
58
64
  static strToAscii(str: string): number[];
59
65
  static strUnique(str: string): string;
60
66
  static isObject(data: any): boolean;
@@ -75,4 +81,4 @@ export default class Qsu {
75
81
  static secToTime(seconds?: number, onlyHour?: boolean, separator?: string): string;
76
82
  }
77
83
  export { Qsu };
78
- export declare const sleep: typeof Qsu.sleep, funcTimes: typeof Qsu.funcTimes, debounce: typeof Qsu.debounce, getPlatform: typeof Qsu.getPlatform, objectId: typeof Qsu.objectId, numRandom: typeof Qsu.numRandom, sum: typeof Qsu.sum, mul: typeof Qsu.mul, sub: typeof Qsu.sub, div: typeof Qsu.div, dayDiff: typeof Qsu.dayDiff, today: typeof Qsu.today, isValidDate: typeof Qsu.isValidDate, dateToYYYYMMDD: typeof Qsu.dateToYYYYMMDD, createDateListFromRange: typeof Qsu.createDateListFromRange, arrShuffle: typeof Qsu.arrShuffle, arrWithDefault: typeof Qsu.arrWithDefault, arrUnique: typeof Qsu.arrUnique, arrWithNumber: typeof Qsu.arrWithNumber, arrRepeat: typeof Qsu.arrRepeat, arrCount: typeof Qsu.arrCount, average: typeof Qsu.average, arrMove: typeof Qsu.arrMove, arrTo1dArray: typeof Qsu.arrTo1dArray, sortByObjectKey: typeof Qsu.sortByObjectKey, sortNumeric: typeof Qsu.sortNumeric, trim: typeof Qsu.trim, replaceBetween: typeof Qsu.replaceBetween, removeSpecialChar: typeof Qsu.removeSpecialChar, removeNewLine: typeof Qsu.removeNewLine, capitalizeFirst: typeof Qsu.capitalizeFirst, capitalizeEverySentence: typeof Qsu.capitalizeEverySentence, capitalizeEachWords: typeof Qsu.capitalizeEachWords, strCount: typeof Qsu.strCount, strShuffle: typeof Qsu.strShuffle, strRandom: typeof Qsu.strRandom, strBlindRandom: typeof Qsu.strBlindRandom, truncate: typeof Qsu.truncate, truncateExpect: typeof Qsu.truncateExpect, split: typeof Qsu.split, encrypt: typeof Qsu.encrypt, decrypt: typeof Qsu.decrypt, md5: typeof Qsu.md5, sha1: typeof Qsu.sha1, sha256: typeof Qsu.sha256, encodeBase64: typeof Qsu.encodeBase64, decodeBase64: typeof Qsu.decodeBase64, strUnique: typeof Qsu.strUnique, strToAscii: typeof Qsu.strToAscii, isObject: typeof Qsu.isObject, isEqual: typeof Qsu.isEqual, isEqualStrict: typeof Qsu.isEqualStrict, isEmpty: typeof Qsu.isEmpty, isUrl: typeof Qsu.isUrl, contains: typeof Qsu.contains, is2dArray: typeof Qsu.is2dArray, between: typeof Qsu.between, len: typeof Qsu.len, isEmail: typeof Qsu.isEmail, numberFormat: typeof Qsu.numberFormat, fileName: typeof Qsu.fileName, fileSize: typeof Qsu.fileSize, fileExt: typeof Qsu.fileExt, msToTime: typeof Qsu.msToTime, secToTime: typeof Qsu.secToTime;
84
+ export declare const sleep: typeof Qsu.sleep, funcTimes: typeof Qsu.funcTimes, debounce: typeof Qsu.debounce, getPlatform: typeof Qsu.getPlatform, objectId: typeof Qsu.objectId, numRandom: typeof Qsu.numRandom, sum: typeof Qsu.sum, mul: typeof Qsu.mul, sub: typeof Qsu.sub, div: typeof Qsu.div, dayDiff: typeof Qsu.dayDiff, today: typeof Qsu.today, isValidDate: typeof Qsu.isValidDate, dateToYYYYMMDD: typeof Qsu.dateToYYYYMMDD, createDateListFromRange: typeof Qsu.createDateListFromRange, arrShuffle: typeof Qsu.arrShuffle, arrWithDefault: typeof Qsu.arrWithDefault, arrUnique: typeof Qsu.arrUnique, arrWithNumber: typeof Qsu.arrWithNumber, arrRepeat: typeof Qsu.arrRepeat, arrCount: typeof Qsu.arrCount, average: typeof Qsu.average, arrMove: typeof Qsu.arrMove, arrTo1dArray: typeof Qsu.arrTo1dArray, sortByObjectKey: typeof Qsu.sortByObjectKey, sortNumeric: typeof Qsu.sortNumeric, objToQueryString: typeof Qsu.objToQueryString, objToPrettyStr: typeof Qsu.objToPrettyStr, trim: typeof Qsu.trim, replaceBetween: typeof Qsu.replaceBetween, removeSpecialChar: typeof Qsu.removeSpecialChar, removeNewLine: typeof Qsu.removeNewLine, capitalizeFirst: typeof Qsu.capitalizeFirst, capitalizeEverySentence: typeof Qsu.capitalizeEverySentence, capitalizeEachWords: typeof Qsu.capitalizeEachWords, strCount: typeof Qsu.strCount, strShuffle: typeof Qsu.strShuffle, strRandom: typeof Qsu.strRandom, strBlindRandom: typeof Qsu.strBlindRandom, truncate: typeof Qsu.truncate, truncateExpect: typeof Qsu.truncateExpect, split: typeof Qsu.split, encrypt: typeof Qsu.encrypt, decrypt: typeof Qsu.decrypt, md5: typeof Qsu.md5, sha1: typeof Qsu.sha1, sha256: typeof Qsu.sha256, encodeBase64: typeof Qsu.encodeBase64, decodeBase64: typeof Qsu.decodeBase64, strToNumberHash: typeof Qsu.strToNumberHash, strUnique: typeof Qsu.strUnique, strToAscii: typeof Qsu.strToAscii, isObject: typeof Qsu.isObject, isEqual: typeof Qsu.isEqual, isEqualStrict: typeof Qsu.isEqualStrict, isEmpty: typeof Qsu.isEmpty, isUrl: typeof Qsu.isUrl, contains: typeof Qsu.contains, is2dArray: typeof Qsu.is2dArray, between: typeof Qsu.between, len: typeof Qsu.len, isEmail: typeof Qsu.isEmail, numberFormat: typeof Qsu.numberFormat, fileName: typeof Qsu.fileName, fileSize: typeof Qsu.fileSize, fileExt: typeof Qsu.fileExt, msToTime: typeof Qsu.msToTime, secToTime: typeof Qsu.secToTime;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{basename as t,extname as e,win32 as r}from"path";import{randomBytes as a,createCipheriv as n,createDecipheriv as i,createHash as s}from"crypto";export default class o{static sleep(t){return new Promise((e=>{setTimeout(e,t)}))}static funcTimes(t,e){const r=[];for(let a=0;a<t;a+=1)r[a]="function"==typeof e?e.call():e;return r}static debounce(t,e){let r;return(...a)=>{clearTimeout(r),r=setTimeout((()=>{t.apply(this,a)}),e)}}static getPlatform(){switch(process.platform){case"win32":return"Windows";case"darwin":return"macOS";case"linux":case"aix":case"sunos":case"netbsd":case"openbsd":case"freebsd":case"cygwin":return"Linux";case"android":return"Android";default:return"Unknown"}}static objectId(){return Math.floor(Date.now()/1e3).toString(16)+"x".repeat(16).replace(/x/g,(()=>Math.floor(16*Math.random()).toString(16)))}static numRandom(t,e){if(!t&&!e)return Math.random()>.5?1:0;const r=e||t,a=!e||t>=e?null:t;return Math.floor(Math.random()*(a?r-a+1:r+1))+(a||0)}static sum(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=0;for(let t=0,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r+=e[t]);return r}static mul(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r*=e[t]);return r}static sub(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r-=e[t]);return r}static div(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r/=e[t]);return r}static dayDiff(t,e){const r=e||new Date;return Math.ceil(Math.abs(r.getTime()-t.getTime())/864e5)}static today(t="-",e=!0){const r=new Date,a=r.getMonth()+1,n=r.getDate(),i=[`${a<10?"0":""}${a}`,`${n<10?"0":""}${n}`];return e?i.unshift(r.getFullYear().toString()):i.push(r.getFullYear().toString()),i.join(t)}static isValidDate(t){if(!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(t))throw new Error("The date format must be 'YYYY-MM-DD'");const e=t.split("-");return/^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/.test(`${parseInt(e[2],10)}-${parseInt(e[1],10)}-${parseInt(e[0],10)}`)}static dateToYYYYMMDD(t,e="-"){const r=t.getMonth()+1,a=t.getDate();return`${t.getFullYear()}${e}${r<10?`0${r}`:r}${e}${a<10?`0${a}`:a}`}static createDateListFromRange(t,e){if(!o.isValidDate(o.dateToYYYYMMDD(t))||!o.isValidDate(o.dateToYYYYMMDD(e)))throw new Error("Either the start date or end date is an invalid date.");if(Math.floor((Date.parse(e.toString())-Date.parse(t.toString()))/864e5)<0)throw new Error("The start date is more recent than the end date.");const r=o.dateToYYYYMMDD(e),a=[];let n=t.getFullYear(),i=t.getMonth()+1,s=t.getDate(),c="";const l=(t,e,r)=>`${t}-${e<10?"0":""}${e}-${r<10?"0":""}${r}`;for(;r!==c;){/[0-9]{4}-12-31/g.test(c)&&(n+=1,i=1,s=1);const t=l(n,i,s);o.isValidDate(t)?(s+=1,a.push(t),c=t):(i+=1,s=1,c=l(n,i,s))}return a}static arrShuffle(t){if(1===t.length)return t[0];const e=t;for(let r=t.length-1;r>0;r-=1){const a=Math.floor(Math.random()*(r+1));[e[r],e[a]]=[t[a],t[r]]}return e}static arrWithDefault(t,e=0){return e<1?[]:Array(e).fill(t)}static arrUnique(t){return o.is2dArray(t)?Array.from(new Set(t.map((t=>JSON.stringify(t)))),(t=>JSON.parse(t))):[...new Set(t)]}static arrWithNumber(t,e){if(t>e)throw new Error("`end` is greater than `start`.");return Array.from({length:e-t+1},((e,r)=>r+t))}static average(t){return t.reduce(((t,e)=>t+e),0)/t.length}static arrMove(t,e,r){const a=t.length;if(a<=e||a<=r)throw new Error("Invalid move params");return t.splice(r,0,t.splice(e,1)[0]),t}static arrTo1dArray(t){const e=t=>{const r=[],a=t.length;for(let n=0;n<a;n+=1)"object"!=typeof t[n]?r.push(t[n]):o.is2dArray(t[n])?r.push(...e(t[n])):r.push(...t[n]);return r};return e(t)}static arrRepeat(t,e){if(!t||e<1||"object"!=typeof t)return[];const r=o.isObject(t),a=[];for(let n=0,i=e;n<i;n+=1)r?a.push(t):a.push(...t);return a}static arrCount(t){const e={};for(let r=0;r<t.length;r+=1){const a=t[r];e[a]=(e[a]||0)+1}return e}static sortByObjectKey(t,e,r=!1,a=!1){if(a){const a=new Intl.Collator([],{numeric:!0}),n=t.sort(((t,r)=>a.compare(t[e],r[e])));return r?n.reverse():n}return t.sort(((t,a)=>r?t[e]>a[e]?-1:t[e]<a[e]?1:0:t[e]<a[e]?-1:t[e]>a[e]?1:0))}static sortNumeric(t,e=!1){const r=new Intl.Collator([],{numeric:!0}),a=t.sort(((t,e)=>r.compare(t,e)));return e?a.reverse():a}static trim(t,e=!1){return t.trim().replace(e?/\s+/g:/\s{2,}/g,"")}static removeSpecialChar(t,e){return t?t.replace(new RegExp(`[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ0-9぀-ヿ㐀-䶿一-鿿豈-﫿ヲ-゚${e??""}]`,"gi"),""):""}static replaceBetween(t,e,r,a=""){if(!t)return"";const n=/[.*+?^${}()|[\]\\]/g,i=n.test(e)?`\\${e}`:e,s=n.test(r)?`\\${r}`:r;return t.replace(new RegExp(`${i}.*?${s}`,"g"),a)}static removeNewLine(t,e=""){return t?t.replace(/(\r\n|\n|\r)/gm,e).trim():""}static capitalizeFirst(t){return t?t.charAt(0).toUpperCase()+t.slice(1):""}static capitalizeEverySentence(t,e){if(!t)return"";const r=e||".",a=t.split(r);let n,i="";for(let t=0,e=a.length;t<e;t+=1){n=[...a[t]];for(let e=0,r=n.length;e<r;e+=1)if(/[a-zA-Z]/.test(a[t][e])){n[e]=a[t][e].toUpperCase();break}i+=`${n.join("")}${t<e-1?r:""}`}return i}static capitalizeEachWords(t,e){if(!t)return"";const r=t.trim().toLowerCase().split(" ");for(let t=0,a=r.length;t<a;t+=1)e&&o.contains(r[t],["in","on","the","at","and","or","of","for","to","that","a","by","it","is","as","are","were","was","nor","an"],!0)||(r[t]=o.capitalizeFirst(r[t]));return o.capitalizeFirst(r.join(" "))}static strCount(t,e){if(!t||!e)return 0;let r=0,a=t.indexOf(e);for(;a>-1;)r+=1,a=t.indexOf(e,a+=e.length);return r}static strShuffle(t){return t?[...t].sort((()=>Math.random()-.5)).join(""):""}static strRandom(t,e){const r=`abcdefghijklmnopqrstuvwxyz0123456789${e}`,a=r.length;let n,i="";for(let e=0;e<t;e+=1)n=r.charAt(Math.floor(Math.random()*a)),n=Math.random()<.5?n.toUpperCase():n,i+=n;return i}static strBlindRandom(t,e,r="*"){if(!t)return"";let a=t,n=0,i=0,s=0;const c=a.length;for(;n<e&&s<c;)i=o.numRandom(0,c),/[a-zA-Z가-힣]/.test(a.substring(i,i+1))&&(a=`${a.substring(0,i+1)}${r}${a.substring(i+2)}`,n+=1),s+=1;return a}static truncate(t,e,r=""){if(!t)return"";let a=t;return t.length>e&&(a=t.substring(0,e)+r),a}static truncateExpect(t,e,r="."){if(!t)return"";const a=t.slice(-1)===r,n=t.split(r),i=n.length;let s="",o=0;for(let t=0;t<i&&o<e;t+=1)s+=`${n[t]}${t!==i-1||a?r:""}`,o+=n[t].length+r.length;return s}static split(t,...e){if(!t)return[];const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;let n="",i="";for(let t=0;t<a;t+=1){const e=r[t];e.length>1?i+=`${i.length<1?"":"|"}${e.replace(/\\/g,"\\\\").replace(/\[/g,"\\[").replace(/]/g,"\\]").replace(/\?/g,"\\?").replace(/\./g,"\\.").replace(/\{/g,"\\{").replace(/}/g,"\\}").replace(/\+/g,"\\+")}`:n+="-"===e||"["===e||"]"===e?`\\${e}`:e}return n.length<1&&i.length<1?[t]:(n.length>0&&(n=`[${n}]`,i.length>0&&(i=`|${i}`)),t.split(new RegExp(`${n}${i}+`,"gi")))}static encrypt(t,e,r="aes-256-cbc",i=16,s=!1){if(!t||t.length<1)return"";const o=a(i),c=n(r,e,o);let l=c.update(t);l=Buffer.concat([l,c.final()]);const u=s?"base64":"hex";return`${o.toString(u)}:${l.toString(u)}`}static decrypt(t,e,r="aes-256-cbc",a=!1){if(!t||t.length<1)return"";const n=a?"base64":"hex",s=t.split(":"),o=i(r,e,Buffer.from(s.shift(),n));let c=o.update(Buffer.from(s.join(":"),n));return c=Buffer.concat([c,o.final()]),c.toString()}static md5(t){return s("md5").update(t).digest("hex")}static sha1(t){return s("sha1").update(t).digest("hex")}static sha256(t){return s("sha256").update(t).digest("hex")}static encodeBase64(t){return Buffer.from(t,"utf8").toString("base64")}static decodeBase64(t){return Buffer.from(t,"base64").toString("utf8")}static strToAscii(t){const e=[];for(let r=0;r<t.length;r+=1)e.push(t.charCodeAt(r));return e}static strUnique(t){return t?[...new Set(t)].join(""):""}static isObject(t){return"object"==typeof t&&!Array.isArray(t)&&null!==t}static isEqual(t,...e){const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;for(let e=0;e<a;e+=1)if(r[e]!=t)return!1;return!0}static isEqualStrict(t,...e){const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;for(let e=0;e<a;e+=1)if(r[e]!==t)return!1;return!0}static isEmpty(t){if(!t)return!0;switch(typeof t){case"string":return t.length<1;case"object":return Array.isArray(t)?t.length<1:Object.keys(t).length<1;default:return!1}}static isUrl(t,e=!1,r=!1){if(r&&-1===t.indexOf("."))return!1;try{new URL(`${e&&-1===t.indexOf("://")?"https://":""}${t}`).toString()}catch(t){return!1}return!0}static contains(t,e,r=!1){if("string"==typeof e)return!(t.length<1)&&-1!==t.indexOf(e);for(let a=0,n=e.length;a<n;a+=1)if(r){if(t===e[a])return!0}else if(-1!==t.indexOf(e[a]))return!0;return!1}static is2dArray(t){return t.filter(Array.isArray).length>0}static between(t,e,r=!1){const a=Math.min.apply(Math,[t[0],t[1]]),n=Math.max.apply(Math,[t[0],t[1]]);return r?e>=a&&e<=n:e>a&&e<n}static len(t){if(!t)return 0;switch(typeof t){case"object":return Array.isArray(t)?t.length:Object.keys(t).length;case"number":case"bigint":return t.toString().length;case"boolean":return t?4:5;case"function":return t().length;default:return t.length}}static isEmail(t){return/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(t)}static numberFormat(t){return(new Intl.NumberFormat).format(t)}static fileName(a,n=!1){return a?-1===a.indexOf("/")?n?r.basename(a):r.basename(a,e(a)):n?t(a):t(a,e(a)):""}static fileSize(t,e=2){if(!t||0===t||t<0)return"0 Bytes";const r=Math.floor(Math.log(t)/Math.log(1024));return`${parseFloat((t/1024**r).toFixed(e<0?0:e))} ${["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][r]}`}static fileExt(t){if(-1===t.indexOf("."))return"Unknown";const e=t.trim().toLowerCase().split(".");return e.length>0?e[e.length-1]:"Unknown"}static msToTime(t=0,e=!1,r=":"){const a=Math.floor(t%1e3/100);let n=Math.floor(t/1e3%60),i=Math.floor(t/6e4%60),s=Math.floor(t/36e5);return s=s<10?`0${s}`:s,i=i<10?`0${i}`:i,n=n<10?`0${n}`:n,`${s}${r}${i}${r}${n}${e?`.${a}`:""}`}static secToTime(t=0,e=!1,r=":"){let a=Math.floor(t%60),n=Math.floor(t/60%60),i=Math.floor(t/3600);return i=i<10?`0${i}`:i,n=n<10?`0${n}`:n,a=a<10?`0${a}`:a,e?i.toString():`${i}${r}${n}${r}${a}`}}export{o as Qsu};export const{sleep:sleep,funcTimes:funcTimes,debounce:debounce,getPlatform:getPlatform,objectId:objectId,numRandom:numRandom,sum:sum,mul:mul,sub:sub,div:div,dayDiff:dayDiff,today:today,isValidDate:isValidDate,dateToYYYYMMDD:dateToYYYYMMDD,createDateListFromRange:createDateListFromRange,arrShuffle:arrShuffle,arrWithDefault:arrWithDefault,arrUnique:arrUnique,arrWithNumber:arrWithNumber,arrRepeat:arrRepeat,arrCount:arrCount,average:average,arrMove:arrMove,arrTo1dArray:arrTo1dArray,sortByObjectKey:sortByObjectKey,sortNumeric:sortNumeric,trim:trim,replaceBetween:replaceBetween,removeSpecialChar:removeSpecialChar,removeNewLine:removeNewLine,capitalizeFirst:capitalizeFirst,capitalizeEverySentence:capitalizeEverySentence,capitalizeEachWords:capitalizeEachWords,strCount:strCount,strShuffle:strShuffle,strRandom:strRandom,strBlindRandom:strBlindRandom,truncate:truncate,truncateExpect:truncateExpect,split:split,encrypt:encrypt,decrypt:decrypt,md5:md5,sha1:sha1,sha256:sha256,encodeBase64:encodeBase64,decodeBase64:decodeBase64,strUnique:strUnique,strToAscii:strToAscii,isObject:isObject,isEqual:isEqual,isEqualStrict:isEqualStrict,isEmpty:isEmpty,isUrl:isUrl,contains:contains,is2dArray:is2dArray,between:between,len:len,isEmail:isEmail,numberFormat:numberFormat,fileName:fileName,fileSize:fileSize,fileExt:fileExt,msToTime:msToTime,secToTime:secToTime}=o;
1
+ import{basename as t,extname as e,win32 as r}from"path";import{randomBytes as a,createCipheriv as n,createDecipheriv as i,createHash as s}from"crypto";export default class o{static sleep(t){return new Promise((e=>{setTimeout(e,t)}))}static funcTimes(t,e){const r=[];for(let a=0;a<t;a+=1)r[a]="function"==typeof e?e.call():e;return r}static debounce(t,e){let r;return(...a)=>{clearTimeout(r),r=setTimeout((()=>{t.apply(this,a)}),e)}}static getPlatform(){switch(process.platform){case"win32":return"Windows";case"darwin":return"macOS";case"linux":case"aix":case"sunos":case"netbsd":case"openbsd":case"freebsd":case"cygwin":return"Linux";case"android":return"Android";default:return"Unknown"}}static objectId(){return Math.floor(Date.now()/1e3).toString(16)+"x".repeat(16).replace(/x/g,(()=>Math.floor(16*Math.random()).toString(16)))}static numRandom(t,e){if(!t&&!e)return Math.random()>.5?1:0;const r=e||t,a=!e||t>=e?null:t;return Math.floor(Math.random()*(a?r-a+1:r+1))+(a||0)}static sum(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=0;for(let t=0,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r+=e[t]);return r}static mul(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r*=e[t]);return r}static sub(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r-=e[t]);return r}static div(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=e[0];for(let t=1,a=e.length;t<a;t+=1)"number"==typeof e[t]&&(r/=e[t]);return r}static dayDiff(t,e){const r=e||new Date;return Math.ceil(Math.abs(r.getTime()-t.getTime())/864e5)}static today(t="-",e=!0){const r=new Date,a=r.getMonth()+1,n=r.getDate(),i=[`${a<10?"0":""}${a}`,`${n<10?"0":""}${n}`];return e?i.unshift(r.getFullYear().toString()):i.push(r.getFullYear().toString()),i.join(t)}static isValidDate(t){if(!/^[0-9]{4}-[0-9]{2}-[0-9]{2}$/.test(t))throw new Error("The date format must be 'YYYY-MM-DD'");const e=t.split("-");return/^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/.test(`${parseInt(e[2],10)}-${parseInt(e[1],10)}-${parseInt(e[0],10)}`)}static dateToYYYYMMDD(t,e="-"){const r=t.getMonth()+1,a=t.getDate();return`${t.getFullYear()}${e}${r<10?`0${r}`:r}${e}${a<10?`0${a}`:a}`}static createDateListFromRange(t,e){if(!o.isValidDate(o.dateToYYYYMMDD(t))||!o.isValidDate(o.dateToYYYYMMDD(e)))throw new Error("Either the start date or end date is an invalid date.");if(Math.floor((Date.parse(e.toString())-Date.parse(t.toString()))/864e5)<0)throw new Error("The start date is more recent than the end date.");const r=o.dateToYYYYMMDD(e),a=[];let n=t.getFullYear(),i=t.getMonth()+1,s=t.getDate(),c="";const l=(t,e,r)=>`${t}-${e<10?"0":""}${e}-${r<10?"0":""}${r}`;for(;r!==c;){/[0-9]{4}-12-31/g.test(c)&&(n+=1,i=1,s=1);const t=l(n,i,s);o.isValidDate(t)?(s+=1,a.push(t),c=t):(i+=1,s=1,c=l(n,i,s))}return a}static arrShuffle(t){if(1===t.length)return t[0];const e=t;for(let r=t.length-1;r>0;r-=1){const a=Math.floor(Math.random()*(r+1));[e[r],e[a]]=[t[a],t[r]]}return e}static arrWithDefault(t,e=0){return e<1?[]:Array(e).fill(t)}static arrUnique(t){return o.is2dArray(t)?Array.from(new Set(t.map((t=>JSON.stringify(t)))),(t=>JSON.parse(t))):[...new Set(t)]}static arrWithNumber(t,e){if(t>e)throw new Error("`end` is greater than `start`.");return Array.from({length:e-t+1},((e,r)=>r+t))}static average(t){return t.reduce(((t,e)=>t+e),0)/t.length}static arrMove(t,e,r){const a=t.length;if(a<=e||a<=r)throw new Error("Invalid move params");return t.splice(r,0,t.splice(e,1)[0]),t}static arrTo1dArray(t){const e=t=>{const r=[],a=t.length;for(let n=0;n<a;n+=1)"object"!=typeof t[n]?r.push(t[n]):o.is2dArray(t[n])?r.push(...e(t[n])):r.push(...t[n]);return r};return e(t)}static arrRepeat(t,e){if(!t||e<1||"object"!=typeof t)return[];const r=o.isObject(t),a=[];for(let n=0,i=e;n<i;n+=1)r?a.push(t):a.push(...t);return a}static arrCount(t){const e={};for(let r=0;r<t.length;r+=1){const a=t[r];e[a]=(e[a]||0)+1}return e}static sortByObjectKey(t,e,r=!1,a=!1){if(a){const a=new Intl.Collator([],{numeric:!0}),n=t.sort(((t,r)=>a.compare(t[e],r[e])));return r?n.reverse():n}return t.sort(((t,a)=>r?t[e]>a[e]?-1:t[e]<a[e]?1:0:t[e]<a[e]?-1:t[e]>a[e]?1:0))}static sortNumeric(t,e=!1){const r=new Intl.Collator([],{numeric:!0}),a=t.sort(((t,e)=>r.compare(t,e)));return e?a.reverse():a}static objToQueryString(t){return Object.keys(t).map((e=>{let r=t[e];return"object"==typeof r&&(r=JSON.stringify(r)),`${encodeURIComponent(e)}=${encodeURIComponent(r)}`})).join("&")}static objToPrettyStr(t){return JSON.stringify(t,null,"\t")}static trim(t,e=!1){return t.trim().replace(e?/\s+/g:/\s{2,}/g,"")}static removeSpecialChar(t,e){return t?t.replace(new RegExp(`[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ0-9぀-ヿ㐀-䶿一-鿿豈-﫿ヲ-゚${e??""}]`,"gi"),""):""}static replaceBetween(t,e,r,a=""){if(!t)return"";const n=/[.*+?^${}()|[\]\\]/g,i=n.test(e)?`\\${e}`:e,s=n.test(r)?`\\${r}`:r;return t.replace(new RegExp(`${i}.*?${s}`,"g"),a)}static removeNewLine(t,e=""){return t?t.replace(/(\r\n|\n|\r)/gm,e).trim():""}static capitalizeFirst(t){return t?t.charAt(0).toUpperCase()+t.slice(1):""}static capitalizeEverySentence(t,e){if(!t)return"";const r=e||".",a=t.split(r);let n,i="";for(let t=0,e=a.length;t<e;t+=1){n=[...a[t]];for(let e=0,r=n.length;e<r;e+=1)if(/[a-zA-Z]/.test(a[t][e])){n[e]=a[t][e].toUpperCase();break}i+=`${n.join("")}${t<e-1?r:""}`}return i}static capitalizeEachWords(t,e){if(!t)return"";const r=t.trim().toLowerCase().split(" ");for(let t=0,a=r.length;t<a;t+=1)e&&o.contains(r[t],["in","on","the","at","and","or","of","for","to","that","a","by","it","is","as","are","were","was","nor","an"],!0)||(r[t]=o.capitalizeFirst(r[t]));return o.capitalizeFirst(r.join(" "))}static strCount(t,e){if(!t||!e)return 0;let r=0,a=t.indexOf(e);for(;a>-1;)r+=1,a=t.indexOf(e,a+=e.length);return r}static strShuffle(t){return t?[...t].sort((()=>Math.random()-.5)).join(""):""}static strRandom(t,e){const r=`abcdefghijklmnopqrstuvwxyz0123456789${e}`,a=r.length;let n,i="";for(let e=0;e<t;e+=1)n=r.charAt(Math.floor(Math.random()*a)),n=Math.random()<.5?n.toUpperCase():n,i+=n;return i}static strBlindRandom(t,e,r="*"){if(!t)return"";let a=t,n=0,i=0,s=0;const c=a.length;for(;n<e&&s<c;)i=o.numRandom(0,c),/[a-zA-Z가-힣]/.test(a.substring(i,i+1))&&(a=`${a.substring(0,i+1)}${r}${a.substring(i+2)}`,n+=1),s+=1;return a}static truncate(t,e,r=""){if(!t)return"";let a=t;return t.length>e&&(a=t.substring(0,e)+r),a}static truncateExpect(t,e,r="."){if(!t)return"";const a=t.slice(-1)===r,n=t.split(r),i=n.length;let s="",o=0;for(let t=0;t<i&&o<e;t+=1)s+=`${n[t]}${t!==i-1||a?r:""}`,o+=n[t].length+r.length;return s}static split(t,...e){if(!t)return[];const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;let n="",i="";for(let t=0;t<a;t+=1){const e=r[t];e.length>1?i+=`${i.length<1?"":"|"}${e.replace(/\\/g,"\\\\").replace(/\[/g,"\\[").replace(/]/g,"\\]").replace(/\?/g,"\\?").replace(/\./g,"\\.").replace(/\{/g,"\\{").replace(/}/g,"\\}").replace(/\+/g,"\\+")}`:n+="-"===e||"["===e||"]"===e?`\\${e}`:e}return n.length<1&&i.length<1?[t]:(n.length>0&&(n=`[${n}]`,i.length>0&&(i=`|${i}`)),t.split(new RegExp(`${n}${i}+`,"gi")))}static encrypt(t,e,r="aes-256-cbc",i=16,s=!1){if(!t||t.length<1)return"";const o=a(i),c=n(r,e,o);let l=c.update(t);l=Buffer.concat([l,c.final()]);const u=s?"base64":"hex";return`${o.toString(u)}:${l.toString(u)}`}static decrypt(t,e,r="aes-256-cbc",a=!1){if(!t||t.length<1)return"";const n=a?"base64":"hex",s=t.split(":"),o=i(r,e,Buffer.from(s.shift(),n));let c=o.update(Buffer.from(s.join(":"),n));return c=Buffer.concat([c,o.final()]),c.toString()}static md5(t){return s("md5").update(t).digest("hex")}static sha1(t){return s("sha1").update(t).digest("hex")}static sha256(t){return s("sha256").update(t).digest("hex")}static encodeBase64(t){return Buffer.from(t,"utf8").toString("base64")}static decodeBase64(t){return Buffer.from(t,"base64").toString("utf8")}static strToNumberHash(t){if(!t)return 0;let e=0;for(let r=0;r<t.length;r+=1)e=(e<<5)-e+t.charCodeAt(r),e|=0;return e}static strToAscii(t){const e=[];for(let r=0;r<t.length;r+=1)e.push(t.charCodeAt(r));return e}static strUnique(t){return t?[...new Set(t)].join(""):""}static isObject(t){return"object"==typeof t&&!Array.isArray(t)&&null!==t}static isEqual(t,...e){const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;for(let e=0;e<a;e+=1)if(r[e]!=t)return!1;return!0}static isEqualStrict(t,...e){const r=e.length>0&&"object"==typeof e[0]?e[0]:e,a=r.length;for(let e=0;e<a;e+=1)if(r[e]!==t)return!1;return!0}static isEmpty(t){if(!t)return!0;switch(typeof t){case"string":return t.length<1;case"object":return Array.isArray(t)?t.length<1:Object.keys(t).length<1;default:return!1}}static isUrl(t,e=!1,r=!1){if(r&&-1===t.indexOf("."))return!1;try{new URL(`${e&&-1===t.indexOf("://")?"https://":""}${t}`).toString()}catch(t){return!1}return!0}static contains(t,e,r=!1){if("string"==typeof e)return!(t.length<1)&&-1!==t.indexOf(e);for(let a=0,n=e.length;a<n;a+=1)if(r){if(t===e[a])return!0}else if(-1!==t.indexOf(e[a]))return!0;return!1}static is2dArray(t){return t.filter(Array.isArray).length>0}static between(t,e,r=!1){const a=Math.min.apply(Math,[t[0],t[1]]),n=Math.max.apply(Math,[t[0],t[1]]);return r?e>=a&&e<=n:e>a&&e<n}static len(t){if(!t)return 0;switch(typeof t){case"object":return Array.isArray(t)?t.length:Object.keys(t).length;case"number":case"bigint":return t.toString().length;case"boolean":return t?4:5;case"function":return t().length;default:return t.length}}static isEmail(t){return/^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/.test(t)}static numberFormat(t){return(new Intl.NumberFormat).format(t)}static fileName(a,n=!1){return a?-1===a.indexOf("/")?n?r.basename(a):r.basename(a,e(a)):n?t(a):t(a,e(a)):""}static fileSize(t,e=2){if(!t||0===t||t<0)return"0 Bytes";const r=Math.floor(Math.log(t)/Math.log(1024));return`${parseFloat((t/1024**r).toFixed(e<0?0:e))} ${["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][r]}`}static fileExt(t){if(-1===t.indexOf("."))return"Unknown";const e=t.trim().toLowerCase().split(".");return e.length>0?e[e.length-1]:"Unknown"}static msToTime(t=0,e=!1,r=":"){const a=Math.floor(t%1e3/100);let n=Math.floor(t/1e3%60),i=Math.floor(t/6e4%60),s=Math.floor(t/36e5);return s=s<10?`0${s}`:s,i=i<10?`0${i}`:i,n=n<10?`0${n}`:n,`${s}${r}${i}${r}${n}${e?`.${a}`:""}`}static secToTime(t=0,e=!1,r=":"){let a=Math.floor(t%60),n=Math.floor(t/60%60),i=Math.floor(t/3600);return i=i<10?`0${i}`:i,n=n<10?`0${n}`:n,a=a<10?`0${a}`:a,e?i.toString():`${i}${r}${n}${r}${a}`}}export{o as Qsu};export const{sleep:sleep,funcTimes:funcTimes,debounce:debounce,getPlatform:getPlatform,objectId:objectId,numRandom:numRandom,sum:sum,mul:mul,sub:sub,div:div,dayDiff:dayDiff,today:today,isValidDate:isValidDate,dateToYYYYMMDD:dateToYYYYMMDD,createDateListFromRange:createDateListFromRange,arrShuffle:arrShuffle,arrWithDefault:arrWithDefault,arrUnique:arrUnique,arrWithNumber:arrWithNumber,arrRepeat:arrRepeat,arrCount:arrCount,average:average,arrMove:arrMove,arrTo1dArray:arrTo1dArray,sortByObjectKey:sortByObjectKey,sortNumeric:sortNumeric,objToQueryString:objToQueryString,objToPrettyStr:objToPrettyStr,trim:trim,replaceBetween:replaceBetween,removeSpecialChar:removeSpecialChar,removeNewLine:removeNewLine,capitalizeFirst:capitalizeFirst,capitalizeEverySentence:capitalizeEverySentence,capitalizeEachWords:capitalizeEachWords,strCount:strCount,strShuffle:strShuffle,strRandom:strRandom,strBlindRandom:strBlindRandom,truncate:truncate,truncateExpect:truncateExpect,split:split,encrypt:encrypt,decrypt:decrypt,md5:md5,sha1:sha1,sha256:sha256,encodeBase64:encodeBase64,decodeBase64:decodeBase64,strToNumberHash:strToNumberHash,strUnique:strUnique,strToAscii:strToAscii,isObject:isObject,isEqual:isEqual,isEqualStrict:isEqualStrict,isEmpty:isEmpty,isUrl:isUrl,contains:contains,is2dArray:is2dArray,between:between,len:len,isEmail:isEmail,numberFormat:numberFormat,fileName:fileName,fileSize:fileSize,fileExt:fileExt,msToTime:msToTime,secToTime:secToTime}=o;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "qsu",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "description": "QSU is a lightweight and simple module with a variety of built-in utility functions that you can utilize in your NodeJS projects.",
5
5
  "author": "Jooy2 <jooy2.contact@gmail.com>",
6
6
  "license": "MIT",
@@ -56,19 +56,19 @@
56
56
  "file"
57
57
  ],
58
58
  "devDependencies": {
59
- "@types/mocha": "^10.0.4",
60
- "@types/node": "^20.9.0",
61
- "@typescript-eslint/eslint-plugin": "^6.10.0",
62
- "@typescript-eslint/parser": "^6.10.0",
59
+ "@types/mocha": "^10.0.6",
60
+ "@types/node": "^20.10.5",
61
+ "@typescript-eslint/eslint-plugin": "^6.16.0",
62
+ "@typescript-eslint/parser": "^6.16.0",
63
63
  "dayjs": "^1.11.10",
64
- "eslint": "^8.53.0",
64
+ "eslint": "^8.56.0",
65
65
  "eslint-config-airbnb": "^19.0.4",
66
- "eslint-config-prettier": "^9.0.0",
67
- "eslint-plugin-import": "^2.29.0",
66
+ "eslint-config-prettier": "^9.1.0",
67
+ "eslint-plugin-import": "^2.29.1",
68
68
  "mocha": "^10.2.0",
69
- "prettier": "^3.0.3",
70
- "terser": "^5.24.0",
71
- "ts-node": "^10.9.1",
72
- "typescript": "^5.2.2"
69
+ "prettier": "^3.1.1",
70
+ "terser": "^5.26.0",
71
+ "ts-node": "^10.9.2",
72
+ "typescript": "^5.3.3"
73
73
  }
74
74
  }