qsu 1.3.1 → 1.3.3
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 +1 -1
- package/README.md +1 -1
- package/dist/index.d.ts +10 -1
- package/dist/index.js +1 -1
- package/package.json +13 -13
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2021-
|
|
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-
|
|
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,10 @@ 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 objFindItemRecursiveByKey(obj: AnyValueObject | any[], searchKey: string, searchValue: any, childKey: string): AnyValueObject | null;
|
|
41
|
+
static objToPrettyStr(obj: AnyValueObject): string;
|
|
42
|
+
static objToArray(obj: AnyValueObject, recursive?: boolean): any[];
|
|
36
43
|
static trim(str: string, removeAllSpace?: boolean): string;
|
|
37
44
|
static removeSpecialChar(str: string, exceptionCharacters?: string): string;
|
|
38
45
|
static replaceBetween(str: string, startChar: string, endChar: string, replaceWith?: string): string;
|
|
@@ -55,8 +62,10 @@ export default class Qsu {
|
|
|
55
62
|
static sha256(str: string): string;
|
|
56
63
|
static encodeBase64(str: string): string;
|
|
57
64
|
static decodeBase64(encodedStr: string): string;
|
|
65
|
+
static strToNumberHash(str: string): number;
|
|
58
66
|
static strToAscii(str: string): number[];
|
|
59
67
|
static strUnique(str: string): string;
|
|
68
|
+
static urlJoin(...args: any[]): string;
|
|
60
69
|
static isObject(data: any): boolean;
|
|
61
70
|
static isEqual(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
62
71
|
static isEqualStrict(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
@@ -75,4 +84,4 @@ export default class Qsu {
|
|
|
75
84
|
static secToTime(seconds?: number, onlyHour?: boolean, separator?: string): string;
|
|
76
85
|
}
|
|
77
86
|
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;
|
|
87
|
+
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, objFindItemRecursiveByKey: typeof Qsu.objFindItemRecursiveByKey, objToArray: typeof Qsu.objToArray, 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, urlJoin: typeof Qsu.urlJoin, 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 n,createCipheriv as a,createDecipheriv as s,createHash as i}from"crypto";export default class o{static sleep(t){return new Promise((e=>{setTimeout(e,t)}))}static funcTimes(t,e){const r=[];for(let n=0;n<t;n+=1)r[n]="function"==typeof e?e.call():e;return r}static debounce(t,e){let r;return(...n)=>{clearTimeout(r),r=setTimeout((()=>{t.apply(this,n)}),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,n=!e||t>=e?null:t;return Math.floor(Math.random()*(n?r-n+1:r+1))+(n||0)}static sum(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let r=0;for(let t=0,n=e.length;t<n;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,n=e.length;t<n;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,n=e.length;t<n;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,n=e.length;t<n;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,n=r.getMonth()+1,a=r.getDate(),s=[`${n<10?"0":""}${n}`,`${a<10?"0":""}${a}`];return e?s.unshift(r.getFullYear().toString()):s.push(r.getFullYear().toString()),s.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,n=t.getDate();return`${t.getFullYear()}${e}${r<10?`0${r}`:r}${e}${n<10?`0${n}`:n}`}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),n=[];let a=t.getFullYear(),s=t.getMonth()+1,i=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)&&(a+=1,s=1,i=1);const t=l(a,s,i);o.isValidDate(t)?(i+=1,n.push(t),c=t):(s+=1,i=1,c=l(a,s,i))}return n}static arrShuffle(t){if(1===t.length)return t[0];const e=t;for(let r=t.length-1;r>0;r-=1){const n=Math.floor(Math.random()*(r+1));[e[r],e[n]]=[t[n],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 n=t.length;if(n<=e||n<=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=[],n=t.length;for(let a=0;a<n;a+=1)"object"!=typeof t[a]?r.push(t[a]):o.is2dArray(t[a])?r.push(...e(t[a])):r.push(...t[a]);return r};return e(t)}static arrRepeat(t,e){if(!t||e<1||"object"!=typeof t)return[];const r=o.isObject(t),n=[];for(let a=0,s=e;a<s;a+=1)r?n.push(t):n.push(...t);return n}static arrCount(t){const e={};for(let r=0;r<t.length;r+=1){const n=t[r];e[n]=(e[n]||0)+1}return e}static sortByObjectKey(t,e,r=!1,n=!1){if(n){const n=new Intl.Collator([],{numeric:!0}),a=t.sort(((t,r)=>n.compare(t[e],r[e])));return r?a.reverse():a}return t.sort(((t,n)=>r?t[e]>n[e]?-1:t[e]<n[e]?1:0:t[e]<n[e]?-1:t[e]>n[e]?1:0))}static sortNumeric(t,e=!1){const r=new Intl.Collator([],{numeric:!0}),n=t.sort(((t,e)=>r.compare(t,e)));return e?n.reverse():n}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 objFindItemRecursiveByKey(t,e,r,n){const a=t=>{let s;s="object"==typeof t&&Array.isArray(t)?t:[t];for(let t=0,i=s.length;t<i;t+=1){if(s[t][e]===r)return s[t];if(!o.isEmpty(s[t][n])){const e=a(s[t][n]);if(e)return e}}return null};return a(t)}static objToPrettyStr(t){return JSON.stringify(t,null,"\t")}static objToArray(t,e=!1){const r=t=>{const n=[],a=Object.keys(t).length;for(let s=0;s<a;s+=1){const a=Object.keys(t)[s];e&&"object"==typeof t[a]?n.push([a,r(t[a])]):n.push([a,t[a]])}return n};return r(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,n=""){if(!t)return"";const a=/[.*+?^${}()|[\]\\]/g,s=a.test(e)?`\\${e}`:e,i=a.test(r)?`\\${r}`:r;return t.replace(new RegExp(`${s}.*?${i}`,"g"),n)}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||".",n=t.split(r);let a,s="";for(let t=0,e=n.length;t<e;t+=1){a=[...n[t]];for(let e=0,r=a.length;e<r;e+=1)if(/[a-zA-Z]/.test(n[t][e])){a[e]=n[t][e].toUpperCase();break}s+=`${a.join("")}${t<e-1?r:""}`}return s}static capitalizeEachWords(t,e){if(!t)return"";const r=t.trim().toLowerCase().split(" ");for(let t=0,n=r.length;t<n;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,n=t.indexOf(e);for(;n>-1;)r+=1,n=t.indexOf(e,n+=e.length);return r}static strShuffle(t){return t?[...t].sort((()=>Math.random()-.5)).join(""):""}static strRandom(t,e){const r=`abcdefghijklmnopqrstuvwxyz0123456789${e}`,n=r.length;let a,s="";for(let e=0;e<t;e+=1)a=r.charAt(Math.floor(Math.random()*n)),a=Math.random()<.5?a.toUpperCase():a,s+=a;return s}static strBlindRandom(t,e,r="*"){if(!t)return"";let n=t,a=0,s=0,i=0;const c=n.length;for(;a<e&&i<c;)s=o.numRandom(0,c),/[a-zA-Z가-힣]/.test(n.substring(s,s+1))&&(n=`${n.substring(0,s+1)}${r}${n.substring(s+2)}`,a+=1),i+=1;return n}static truncate(t,e,r=""){if(!t)return"";let n=t;return t.length>e&&(n=t.substring(0,e)+r),n}static truncateExpect(t,e,r="."){if(!t)return"";const n=t.slice(-1)===r,a=t.split(r),s=a.length;let i="",o=0;for(let t=0;t<s&&o<e;t+=1)i+=`${a[t]}${t!==s-1||n?r:""}`,o+=a[t].length+r.length;return i}static split(t,...e){if(!t)return[];const r=e.length>0&&"object"==typeof e[0]?e[0]:e,n=r.length;let a="",s="";for(let t=0;t<n;t+=1){const e=r[t];e.length>1?s+=`${s.length<1?"":"|"}${e.replace(/\\/g,"\\\\").replace(/\[/g,"\\[").replace(/]/g,"\\]").replace(/\?/g,"\\?").replace(/\./g,"\\.").replace(/\{/g,"\\{").replace(/}/g,"\\}").replace(/\+/g,"\\+")}`:a+="-"===e||"["===e||"]"===e?`\\${e}`:e}return a.length<1&&s.length<1?[t]:(a.length>0&&(a=`[${a}]`,s.length>0&&(s=`|${s}`)),t.split(new RegExp(`${a}${s}+`,"gi")))}static encrypt(t,e,r="aes-256-cbc",s=16,i=!1){if(!t||t.length<1)return"";const o=n(s),c=a(r,e,o);let l=c.update(t);l=Buffer.concat([l,c.final()]);const u=i?"base64":"hex";return`${o.toString(u)}:${l.toString(u)}`}static decrypt(t,e,r="aes-256-cbc",n=!1){if(!t||t.length<1)return"";const a=n?"base64":"hex",i=t.split(":"),o=s(r,e,Buffer.from(i.shift(),a));let c=o.update(Buffer.from(i.join(":"),a));return c=Buffer.concat([c,o.final()]),c.toString()}static md5(t){return i("md5").update(t).digest("hex")}static sha1(t){return i("sha1").update(t).digest("hex")}static sha256(t){return i("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 urlJoin(...t){if(!t)return"";const e=t.length;let r="",n=0;for(let a=0;a<e;a+=1)null!==t[a]&&void 0!==t[a]&&(0===n||t[a].startsWith("/")||t[a].startsWith("?")||t[a].startsWith("&")?r+=t[a]:r+=`/${t[a]}`,n+=1);return r.replace(/\/$/g,"")}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,n=r.length;for(let e=0;e<n;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,n=r.length;for(let e=0;e<n;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 n=0,a=e.length;n<a;n+=1)if(r){if(t===e[n])return!0}else if(-1!==t.indexOf(e[n]))return!0;return!1}static is2dArray(t){return t.filter(Array.isArray).length>0}static between(t,e,r=!1){const n=Math.min.apply(Math,[t[0],t[1]]),a=Math.max.apply(Math,[t[0],t[1]]);return r?e>=n&&e<=a:e>n&&e<a}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(n,a=!1){return n?-1===n.indexOf("/")?a?r.basename(n):r.basename(n,e(n)):a?t(n):t(n,e(n)):""}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 n=Math.floor(t%1e3/100);let a=Math.floor(t/1e3%60),s=Math.floor(t/6e4%60),i=Math.floor(t/36e5);return i=i<10?`0${i}`:i,s=s<10?`0${s}`:s,a=a<10?`0${a}`:a,`${i}${r}${s}${r}${a}${e?`.${n}`:""}`}static secToTime(t=0,e=!1,r=":"){let n=Math.floor(t%60),a=Math.floor(t/60%60),s=Math.floor(t/3600);return s=s<10?`0${s}`:s,a=a<10?`0${a}`:a,n=n<10?`0${n}`:n,e?s.toString():`${s}${r}${a}${r}${n}`}}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,objFindItemRecursiveByKey:objFindItemRecursiveByKey,objToArray:objToArray,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,urlJoin:urlJoin,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.
|
|
3
|
+
"version": "1.3.3",
|
|
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.
|
|
60
|
-
"@types/node": "^20.
|
|
61
|
-
"@typescript-eslint/eslint-plugin": "^
|
|
62
|
-
"@typescript-eslint/parser": "^
|
|
59
|
+
"@types/mocha": "^10.0.6",
|
|
60
|
+
"@types/node": "^20.11.21",
|
|
61
|
+
"@typescript-eslint/eslint-plugin": "^7.1.0",
|
|
62
|
+
"@typescript-eslint/parser": "^7.1.0",
|
|
63
63
|
"dayjs": "^1.11.10",
|
|
64
|
-
"eslint": "^8.
|
|
64
|
+
"eslint": "^8.57.0",
|
|
65
65
|
"eslint-config-airbnb": "^19.0.4",
|
|
66
|
-
"eslint-config-prettier": "^9.
|
|
67
|
-
"eslint-plugin-import": "^2.29.
|
|
68
|
-
"mocha": "^10.
|
|
69
|
-
"prettier": "^3.
|
|
70
|
-
"terser": "^5.
|
|
71
|
-
"ts-node": "^10.9.
|
|
72
|
-
"typescript": "^5.
|
|
66
|
+
"eslint-config-prettier": "^9.1.0",
|
|
67
|
+
"eslint-plugin-import": "^2.29.1",
|
|
68
|
+
"mocha": "^10.3.0",
|
|
69
|
+
"prettier": "^3.2.5",
|
|
70
|
+
"terser": "^5.28.1",
|
|
71
|
+
"ts-node": "^10.9.2",
|
|
72
|
+
"typescript": "^5.3.3"
|
|
73
73
|
}
|
|
74
74
|
}
|