qsu 1.4.2 → 1.6.0
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 +7 -7
- package/dist/array.d.ts +13 -0
- package/dist/array.js +1 -0
- package/dist/crypto.d.ts +9 -0
- package/dist/crypto.js +1 -0
- package/dist/date.d.ts +5 -0
- package/dist/date.js +1 -0
- package/dist/format.d.ts +8 -0
- package/dist/format.js +1 -0
- package/dist/index.d.ts +9 -98
- package/dist/index.js +1 -1
- package/dist/math.d.ts +9 -0
- package/dist/math.js +1 -0
- package/dist/misc.d.ts +4 -0
- package/dist/misc.js +1 -0
- package/dist/object.d.ts +9 -0
- package/dist/object.js +1 -0
- package/dist/string.d.ts +19 -0
- package/dist/string.js +1 -0
- package/dist/types/global.d.ts +13 -0
- package/dist/types/global.js +1 -0
- package/dist/verify.d.ts +11 -0
- package/dist/verify.js +1 -0
- package/package.json +17 -18
- package/logo.webp +0 -0
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2021-2024
|
|
3
|
+
Copyright (c) 2021-2024 CDGet <jooy2.contact@gmail.com> (https://cdget.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
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-

|
|
1
|
+

|
|
2
2
|
|
|
3
|
-
# Qsu: Quick & Simple Utility for
|
|
3
|
+
# Qsu: Quick & Simple Utility for Node.js
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
[](https://github.com/jooy2/qsu/blob/master/LICENSE)   [](https://www.npmjs.com/package/qsu) [](https://www.npmjs.com/package/qsu)    [](https://github.com/jooy2) 
|
|
6
6
|
|
|
7
7
|
**Qsu** is an underscore-based utility library optimized for the **[Node.js](https://nodejs.org)** development environment. It is supported in one module without the need to separately write frequently used methods for each project.
|
|
8
8
|
|
|
9
9
|
- Lightweight and fast!
|
|
10
10
|
- Easy to install and use.
|
|
11
|
-
- 100% optimized for the latest
|
|
11
|
+
- 100% optimized for the latest Node.js and ESM environments.
|
|
12
12
|
- Useful features for websites and web applications
|
|
13
13
|
|
|
14
|
-
## [Documentation (Getting Started & Method Reference)](https://qsu.
|
|
14
|
+
## [Documentation (Getting Started & Method Reference)](https://qsu.cdget.com/getting-started/installation-javascript)
|
|
15
15
|
|
|
16
|
-
Installing and using the package and defining all the utility methods can be found on the documentation page below: https://qsu.
|
|
16
|
+
Installing and using the package and defining all the utility methods can be found on the documentation page below: https://qsu.cdget.com/getting-started/installation-javascript
|
|
17
17
|
|
|
18
18
|
## Contribute
|
|
19
19
|
|
|
@@ -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-2024 [
|
|
24
|
+
Copyright © 2021-2024 [CDGet](https://cdget.com) <[jooy2.contact@gmail.com](mailto:jooy2.contact@gmail.com)> Released under the MIT license.
|
package/dist/array.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { NumberValueObject, PositiveNumber } from './types/global';
|
|
2
|
+
export declare function arrShuffle(array: any[]): any[];
|
|
3
|
+
export declare function arrWithDefault(defaultValue: any, length?: number): any[];
|
|
4
|
+
export declare function arrUnique(array: any[]): any[];
|
|
5
|
+
export declare function arrWithNumber(start: number, end: number): number[];
|
|
6
|
+
export declare function average(array: number[]): number;
|
|
7
|
+
export declare function arrMove<N extends number>(array: any[], from: PositiveNumber<N>, to: PositiveNumber<N>): any[];
|
|
8
|
+
export declare function arrTo1dArray(array: any[]): any[];
|
|
9
|
+
export declare function arrRepeat<N extends number>(array: any, count: PositiveNumber<N>): any[];
|
|
10
|
+
export declare function arrCount(array: string[] | number[]): NumberValueObject;
|
|
11
|
+
export declare function sortByObjectKey(array: any[], key: string, descending?: boolean, numerically?: boolean): any[];
|
|
12
|
+
export declare function sortNumeric(array: string[], descending?: boolean): string[];
|
|
13
|
+
export declare function arrGroupByMaxCount(array: any[], maxLengthPerGroup?: number): any[];
|
package/dist/array.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{is2dArray as r,isObject as t}from"./verify";export function arrShuffle(r){if(1===r.length)return r[0];const t=r;for(let e=r.length-1;e>0;e-=1){const n=Math.floor(Math.random()*(e+1));[t[e],t[n]]=[r[n],r[e]]}return t}export function arrWithDefault(r,t=0){return t<1?[]:Array(t).fill(r)}export function arrUnique(t){return r(t)?Array.from(new Set(t.map((r=>JSON.stringify(r)))),(r=>JSON.parse(r))):[...new Set(t)]}export function arrWithNumber(r,t){if(r>t)throw new Error("`end` is greater than `start`.");return Array.from({length:t-r+1},((t,e)=>e+r))}export function average(r){return r.reduce(((r,t)=>r+t),0)/r.length}export function arrMove(r,t,e){const n=r.length;if(n<=t||n<=e)throw new Error("Invalid move params");return r.splice(e,0,r.splice(t,1)[0]),r}export function arrTo1dArray(t){const e=t=>{const n=[],o=t.length;for(let u=0;u<o;u+=1)"object"!=typeof t[u]?n.push(t[u]):r(t[u])?n.push(...e(t[u])):n.push(...t[u]);return n};return e(t)}export function arrRepeat(r,e){if(!r||e<1||"object"!=typeof r)return[];const n=t(r),o=[];for(let t=0,u=e;t<u;t+=1)n?o.push(r):o.push(...r);return o}export function arrCount(r){const t={};for(let e=0;e<r.length;e+=1){const n=r[e];t[n]=(t[n]||0)+1}return t}export function sortByObjectKey(r,t,e=!1,n=!1){if(n){const n=new Intl.Collator([],{numeric:!0}),o=r.sort(((r,e)=>n.compare(r[t],e[t])));return e?o.reverse():o}return r.sort(((r,n)=>e?r[t]>n[t]?-1:r[t]<n[t]?1:0:r[t]<n[t]?-1:r[t]>n[t]?1:0))}export function sortNumeric(r,t=!1){const e=new Intl.Collator([],{numeric:!0}),n=r.sort(((r,t)=>e.compare(r,t)));return t?n.reverse():n}export function arrGroupByMaxCount(r,t=1){const e=[],n=r.length;let o=[];for(let u=0;u<n;u+=1)o.length===t&&(e.push(o),o=[]),o.push(r[u]);return o.length>0&&e.push(o),e}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function encrypt(str: string, secret: string, algorithm?: string, ivSize?: number, toBase64?: boolean): string;
|
|
2
|
+
export declare function decrypt(str: string, secret: string, algorithm?: string, toBase64?: boolean): string;
|
|
3
|
+
export declare function objectId(): string;
|
|
4
|
+
export declare function md5Hash(str: string): string;
|
|
5
|
+
export declare function sha1Hash(str: string): string;
|
|
6
|
+
export declare function sha256Hash(str: string): string;
|
|
7
|
+
export declare function encodeBase64(str: string): string;
|
|
8
|
+
export declare function decodeBase64(encodedStr: string): string;
|
|
9
|
+
export declare function strToNumberHash(str: string): number;
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{randomBytes as t,createCipheriv as e,createDecipheriv as r,createHash as n}from"node:crypto";export function encrypt(r,n,o="aes-256-cbc",f=16,u=!1){if(!r||r.length<1)return"";const a=t(f),c=e(o,n,a);let i=c.update(r);i=Buffer.concat([i,c.final()]);const s=u?"base64":"hex";return`${a.toString(s)}:${i.toString(s)}`}export function decrypt(t,e,n="aes-256-cbc",o=!1){if(!t||t.length<1)return"";const f=o?"base64":"hex",u=t.split(":"),a=r(n,e,Buffer.from(u.shift(),f));let c=a.update(Buffer.from(u.join(":"),f));return c=Buffer.concat([c,a.final()]),c.toString()}export function objectId(){return Math.floor(Date.now()/1e3).toString(16)+"x".repeat(16).replace(/x/g,(()=>Math.floor(16*Math.random()).toString(16)))}export function md5Hash(t){return n("md5").update(t).digest("hex")}export function sha1Hash(t){return n("sha1").update(t).digest("hex")}export function sha256Hash(t){return n("sha256").update(t).digest("hex")}export function encodeBase64(t){return Buffer.from(t,"utf8").toString("base64")}export function decodeBase64(t){return Buffer.from(t,"base64").toString("utf8")}export function 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}
|
package/dist/date.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export declare function dayDiff(date1: Date, date2?: Date): number;
|
|
2
|
+
export declare function today(separator?: string, yearFirst?: boolean): string;
|
|
3
|
+
export declare function isValidDate(dateYYYYMMDD: string): boolean;
|
|
4
|
+
export declare function dateToYYYYMMDD(date: Date, separator?: string): string;
|
|
5
|
+
export declare function createDateListFromRange(startDate: Date, endDate: Date): string[];
|
package/dist/date.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function dayDiff(t,e){const r=e||new Date;return Math.ceil(Math.abs(r.getTime()-t.getTime())/864e5)}export function today(t="-",e=!0){const r=new Date,a=r.getMonth()+1,n=r.getDate(),o=[`${a<10?"0":""}${a}`,`${n<10?"0":""}${n}`];return e?o.unshift(r.getFullYear().toString()):o.push(r.getFullYear().toString()),o.join(t)}export function 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)}`)}export function 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}`}export function createDateListFromRange(t,e){if(!isValidDate(dateToYYYYMMDD(t))||!isValidDate(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=dateToYYYYMMDD(e),a=[];let n=t.getFullYear(),o=t.getMonth()+1,i=t.getDate(),s="";const d=(t,e,r)=>`${t}-${e<10?"0":""}${e}-${r<10?"0":""}${r}`;for(;r!==s;){/[0-9]{4}-12-31/g.test(s)&&(n+=1,o=1,i=1);const t=d(n,o,i);isValidDate(t)?(i+=1,a.push(t),s=t):(o+=1,i=1,s=d(n,o,i))}return a}
|
package/dist/format.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { AnyValueObject, DurationOptions, PositiveNumber } from './types/global';
|
|
2
|
+
export declare function numberFormat(number: number | string): string;
|
|
3
|
+
export declare function fileName(filePath: string, withExtension?: boolean): string;
|
|
4
|
+
export declare function fileSize<N extends number>(bytes: PositiveNumber<N>, decimals?: number): string;
|
|
5
|
+
export declare function fileExt(filePath: string): string;
|
|
6
|
+
export declare function duration(milliseconds: number, options?: DurationOptions): string;
|
|
7
|
+
export declare function safeJSONParse(jsonString: any, fallback?: {}): AnyValueObject;
|
|
8
|
+
export declare function safeParseInt(value: any, fallback?: number, radix?: number): number;
|
package/dist/format.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{basename as t,extname as r,win32 as e}from"path";export function numberFormat(t){return(new Intl.NumberFormat).format(t)}export function fileName(n,o=!1){return n?-1===n.indexOf("/")?o?e.basename(n):e.basename(n,r(n)):o?t(n):t(n,r(n)):""}export function fileSize(t,r=2){if(!t||0===t||t<0)return"0 Bytes";const e=Math.floor(Math.log(t)/Math.log(1024));return`${parseFloat((t/1024**e).toFixed(r<0?0:r))} ${["Bytes","KB","MB","GB","TB","PB","EB","ZB","YB"][e]}`}export function fileExt(t){if(-1===t.indexOf("."))return"Unknown";const r=t.trim().toLowerCase().split(".");return r.length>0?r[r.length-1]:"Unknown"}export function duration(t,r){const{useShortString:e=!1,useSpace:n=!0,withZeroValue:o=!1,separator:i=" "}={...r},a=[{name:"Millisecond",short:"ms",divider:1e3},{name:"Second",short:"S",divider:60},{name:"Minute",short:"M",divider:60},{name:"Hour",short:"H",divider:24},{name:"Day",short:"D",divider:31}],s=[];let u=t;for(let t=0;t<a.length;t+=1){const r=a[t];let i=u%r.divider;t===a.length-1?i=Math.trunc(u):u=(u-i)/r.divider,(o||!o&&0!==i)&&s.push(`${i}${n?" ":""}${e?r.short:`${r.name}${i<2?"":"s"}`}`)}return s.reverse().join(i)}export function safeJSONParse(t,r={}){if(!t)return r;if(Array.isArray(t)||"object"==typeof t)try{return JSON.parse(JSON.stringify(t))}catch(t){return r}try{return JSON.parse(t)}catch(t){return r}}export function safeParseInt(t,r=0,e=10){if(!t||t.toString().length<1)return r;try{return parseInt(t.toString().split(".")[0],e)}catch(t){return r}}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,98 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
useSpace?: boolean;
|
|
11
|
-
withZeroValue?: boolean;
|
|
12
|
-
separator?: string;
|
|
13
|
-
};
|
|
14
|
-
export default class Qsu {
|
|
15
|
-
static sleep<N extends number>(delay: PositiveNumber<N>): Promise<void>;
|
|
16
|
-
static funcTimes<N extends number>(times: PositiveNumber<N>, iteratee: any): Array<any>;
|
|
17
|
-
static debounce<N extends number>(func: (...args: any[]) => void, timeout: PositiveNumber<N>): (...args: any[]) => void;
|
|
18
|
-
static numRandom(min: number, max: number): number;
|
|
19
|
-
static sum(...args: any[]): number;
|
|
20
|
-
static sum(...args: Array<number>): number;
|
|
21
|
-
static mul(...args: any[]): number;
|
|
22
|
-
static mul(...args: Array<number>): number;
|
|
23
|
-
static sub(...args: any[]): number;
|
|
24
|
-
static sub(...args: Array<number>): number;
|
|
25
|
-
static div(...args: any[]): number;
|
|
26
|
-
static div(...args: Array<number>): number;
|
|
27
|
-
static dayDiff(date1: Date, date2?: Date): number;
|
|
28
|
-
static today(separator?: string, yearFirst?: boolean): string;
|
|
29
|
-
static isValidDate(dateYYYYMMDD: string): boolean;
|
|
30
|
-
static dateToYYYYMMDD(date: Date, separator?: string): string;
|
|
31
|
-
static createDateListFromRange(startDate: Date, endDate: Date): string[];
|
|
32
|
-
static arrShuffle(array: any[]): any[];
|
|
33
|
-
static arrWithDefault(defaultValue: any, length?: number): any[];
|
|
34
|
-
static arrUnique(array: any[]): any[];
|
|
35
|
-
static arrWithNumber(start: number, end: number): number[];
|
|
36
|
-
static average(array: number[]): number;
|
|
37
|
-
static arrMove<N extends number>(array: any[], from: PositiveNumber<N>, to: PositiveNumber<N>): any[];
|
|
38
|
-
static arrTo1dArray(array: any[]): any[];
|
|
39
|
-
static arrRepeat<N extends number>(array: any, count: PositiveNumber<N>): any[];
|
|
40
|
-
static arrCount(array: string[] | number[]): NumberValueObject;
|
|
41
|
-
static sortByObjectKey(array: any[], key: string, descending?: boolean, numerically?: boolean): any[];
|
|
42
|
-
static sortNumeric(array: string[], descending?: boolean): string[];
|
|
43
|
-
static arrGroupByMaxCount(array: any[], maxLengthPerGroup?: number): any[];
|
|
44
|
-
static objToQueryString(obj: AnyValueObject): string;
|
|
45
|
-
static objFindItemRecursiveByKey(obj: AnyValueObject | any[], searchKey: string, searchValue: any, childKey: string): AnyValueObject | null;
|
|
46
|
-
static objToPrettyStr(obj: AnyValueObject): string;
|
|
47
|
-
static objToArray(obj: AnyValueObject, recursive?: boolean): any[];
|
|
48
|
-
static objectTo1d(obj: AnyValueObject, separator?: string): AnyValueObject;
|
|
49
|
-
static objDeleteKeyByValue(obj: AnyValueObject, searchValue: string | number | null | undefined, recursive?: boolean): AnyValueObject | null;
|
|
50
|
-
static objUpdate(obj: AnyValueObject, key: string, value: any, recursive?: boolean, upsert?: boolean): AnyValueObject | null;
|
|
51
|
-
static trim(str?: string | null): string | null;
|
|
52
|
-
static removeSpecialChar(str: string, exceptionCharacters?: string): string;
|
|
53
|
-
static replaceBetween(str: string, startChar: string, endChar: string, replaceWith?: string): string;
|
|
54
|
-
static removeNewLine(str: string, replaceTo?: string): string;
|
|
55
|
-
static capitalizeFirst(str: string): string;
|
|
56
|
-
static capitalizeEverySentence(str: string, splitChar?: string): string;
|
|
57
|
-
static capitalizeEachWords(str: string, natural?: boolean): string;
|
|
58
|
-
static strCount(str: string, search: string): number;
|
|
59
|
-
static strShuffle(str: string): string;
|
|
60
|
-
static strRandom<N extends number>(length: PositiveNumber<N>, additionalCharacters?: string): string;
|
|
61
|
-
static strBlindRandom<N extends number>(str: string, blindLength: PositiveNumber<N>, blindStr?: string): string;
|
|
62
|
-
static truncate<N extends number>(str: string, length: PositiveNumber<N>, ellipsis?: string): string;
|
|
63
|
-
static truncateExpect<N extends number>(str: string, expectLength: PositiveNumber<N>, endStringChar?: string): string;
|
|
64
|
-
static split(str: string, ...splitter: any[]): string[];
|
|
65
|
-
static split(str: string, ...splitter: Array<string>): string[];
|
|
66
|
-
static strToAscii(str: string): number[];
|
|
67
|
-
static strUnique(str: string): string;
|
|
68
|
-
static urlJoin(...args: any[]): string;
|
|
69
|
-
static isObject(data: any): boolean;
|
|
70
|
-
static isEqual(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
71
|
-
static isEqualStrict(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
72
|
-
static isEmpty(data?: any): boolean;
|
|
73
|
-
static isUrl(url: string, withProtocol?: boolean, strict?: boolean): boolean;
|
|
74
|
-
static contains(str: any[] | string, search: any[] | string, exact?: boolean): boolean;
|
|
75
|
-
static is2dArray(array: any[]): boolean;
|
|
76
|
-
static between(range: [number, number], number: number, inclusive?: boolean): boolean;
|
|
77
|
-
static len(data: any): number;
|
|
78
|
-
static isEmail(email: string): boolean;
|
|
79
|
-
static isTrueMinimumNumberOfTimes(conditions: boolean[], minimumCount?: number): boolean;
|
|
80
|
-
static numberFormat(number: number | string): string;
|
|
81
|
-
static fileName(filePath: string, withExtension?: boolean): string;
|
|
82
|
-
static fileSize<N extends number>(bytes: PositiveNumber<N>, decimals?: number): string;
|
|
83
|
-
static fileExt(filePath: string): string;
|
|
84
|
-
static duration(milliseconds: number, options?: DurationOptions): string;
|
|
85
|
-
static safeJSONParse(jsonString: any, fallback?: {}): AnyValueObject;
|
|
86
|
-
static safeParseInt(value: any, fallback?: number, radix?: number): number;
|
|
87
|
-
static encrypt(str: string, secret: string, algorithm?: string, ivSize?: number, toBase64?: boolean): string;
|
|
88
|
-
static decrypt(str: string, secret: string, algorithm?: string, toBase64?: boolean): string;
|
|
89
|
-
static objectId(): string;
|
|
90
|
-
static md5(str: string): string;
|
|
91
|
-
static sha1(str: string): string;
|
|
92
|
-
static sha256(str: string): string;
|
|
93
|
-
static encodeBase64(str: string): string;
|
|
94
|
-
static decodeBase64(encodedStr: string): string;
|
|
95
|
-
static strToNumberHash(str: string): number;
|
|
96
|
-
}
|
|
97
|
-
export { Qsu };
|
|
98
|
-
export declare const sleep: typeof Qsu.sleep, funcTimes: typeof Qsu.funcTimes, debounce: typeof Qsu.debounce, 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, arrGroupByMaxCount: typeof Qsu.arrGroupByMaxCount, objToQueryString: typeof Qsu.objToQueryString, objToPrettyStr: typeof Qsu.objToPrettyStr, objFindItemRecursiveByKey: typeof Qsu.objFindItemRecursiveByKey, objToArray: typeof Qsu.objToArray, objectTo1d: typeof Qsu.objectTo1d, objDeleteKeyByValue: typeof Qsu.objDeleteKeyByValue, objUpdate: typeof Qsu.objUpdate, 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, isTrueMinimumNumberOfTimes: typeof Qsu.isTrueMinimumNumberOfTimes, numberFormat: typeof Qsu.numberFormat, fileName: typeof Qsu.fileName, fileSize: typeof Qsu.fileSize, fileExt: typeof Qsu.fileExt, safeJSONParse: typeof Qsu.safeJSONParse, safeParseInt: typeof Qsu.safeParseInt, duration: typeof Qsu.duration;
|
|
1
|
+
export * from './array';
|
|
2
|
+
export * from './crypto';
|
|
3
|
+
export * from './date';
|
|
4
|
+
export * from './format';
|
|
5
|
+
export * from './math';
|
|
6
|
+
export * from './misc';
|
|
7
|
+
export * from './object';
|
|
8
|
+
export * from './string';
|
|
9
|
+
export * from './verify';
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
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 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 arrGroupByMaxCount(t,e=1){const r=[],n=t.length;let a=[];for(let s=0;s<n;s+=1)a.length===e&&(r.push(a),a=[]),a.push(t[s]);return a.length>0&&r.push(a),r}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&&o.isObject(t[a])?n.push([a,r(t[a])]):n.push([a,t[a]])}return n};return r(t)}static objectTo1d(t,e="."){if(!e||e.length<1)throw new Error("`separator` must have value at least 1 character.");const r=(t,n="")=>{let a={};const s=Object.keys(t).length,i=n.length<1;for(let c=0;c<s;c+=1){const s=Object.keys(t)[c],l=t[s],u=`${n}${i?"":e}${s}`;o.isObject(l)?(a=Object.assign(a,r(l,u)),delete a[s]):a[u]=l}return a};return r(t)}static objDeleteKeyByValue(t,e,r=!1){if(!t||"object"!=typeof t)return null;const n=Object.assign(t,{});for(let t=Object.keys(n).length;t>=0;t-=1){const a=Object.keys(n)[t];r&&n[a]&&o.isObject(n[a])?o.objDeleteKeyByValue(n[a],e,r):n[a]===e&&delete n[a]}return n}static objUpdate(t,e,r,n=!1,a=!1){if(!t||"object"!=typeof t)return null;const s=Object.assign(t,{});let i=!1;const c=t=>{for(let a=0;a<Object.keys(t).length;a+=1){const s=Object.keys(t)[a];n&&t[s]&&o.isObject(t[s])&&c(t[s]),Object.hasOwn(t,e)&&(t[e]=r,i=!0)}};return c(s),!i&&a&&(s[e]=r),s}static trim(t){return"string"==typeof t||t?t.trim().replace(/\s{2,}/g," "):null}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 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 null!=t&&Object.getPrototypeOf(t)===Object.prototype}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 isTrueMinimumNumberOfTimes(t,e=1){const r=t.length;let n=0;for(let e=0;e<r;e+=1)"boolean"==typeof t[e]&&t[e]&&(n+=1);return n>=e}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 duration(t,e){const{useShortString:r=!1,useSpace:n=!0,withZeroValue:a=!1,separator:s=" "}={...e},i=[{name:"Millisecond",short:"ms",divider:1e3},{name:"Second",short:"S",divider:60},{name:"Minute",short:"M",divider:60},{name:"Hour",short:"H",divider:24},{name:"Day",short:"D",divider:31}],o=[];let c=t;for(let t=0;t<i.length;t+=1){const e=i[t];let s=c%e.divider;t===i.length-1?s=Math.trunc(c):c=(c-s)/e.divider,(a||!a&&0!==s)&&o.push(`${s}${n?" ":""}${r?e.short:`${e.name}${s<2?"":"s"}`}`)}return o.reverse().join(s)}static safeJSONParse(t,e={}){if(!t)return e;if(Array.isArray(t)||"object"==typeof t)try{return JSON.parse(JSON.stringify(t))}catch(t){return e}try{return JSON.parse(t)}catch(t){return e}}static safeParseInt(t,e=0,r=10){if(!t||t.toString().length<1)return e;try{return parseInt(t.toString().split(".")[0],r)}catch(t){return e}}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 objectId(){return Math.floor(Date.now()/1e3).toString(16)+"x".repeat(16).replace(/x/g,(()=>Math.floor(16*Math.random()).toString(16)))}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}}export{o as Qsu};export const{sleep:sleep,funcTimes:funcTimes,debounce:debounce,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,arrGroupByMaxCount:arrGroupByMaxCount,objToQueryString:objToQueryString,objToPrettyStr:objToPrettyStr,objFindItemRecursiveByKey:objFindItemRecursiveByKey,objToArray:objToArray,objectTo1d:objectTo1d,objDeleteKeyByValue:objDeleteKeyByValue,objUpdate:objUpdate,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,isTrueMinimumNumberOfTimes:isTrueMinimumNumberOfTimes,numberFormat:numberFormat,fileName:fileName,fileSize:fileSize,fileExt:fileExt,safeJSONParse:safeJSONParse,safeParseInt:safeParseInt,duration:duration}=o;
|
|
1
|
+
export*from"./array";export*from"./crypto";export*from"./date";export*from"./format";export*from"./math";export*from"./misc";export*from"./object";export*from"./string";export*from"./verify";
|
package/dist/math.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export declare function numRandom(min: number, max: number): number;
|
|
2
|
+
export declare function sum(...args: any[]): number;
|
|
3
|
+
export declare function sum(...args: Array<number>): number;
|
|
4
|
+
export declare function mul(...args: any[]): number;
|
|
5
|
+
export declare function mul(...args: Array<number>): number;
|
|
6
|
+
export declare function sub(...args: any[]): number;
|
|
7
|
+
export declare function sub(...args: Array<number>): number;
|
|
8
|
+
export declare function div(...args: any[]): number;
|
|
9
|
+
export declare function div(...args: Array<number>): number;
|
package/dist/math.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function numRandom(t,e){if(!t&&!e)return Math.random()>.5?1:0;const n=e||t,o=!e||t>=e?null:t;return Math.floor(Math.random()*(o?n-o+1:n+1))+(o||0)}export function sum(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let n=0;for(let t=0,o=e.length;t<o;t+=1)"number"==typeof e[t]&&(n+=e[t]);return n}export function mul(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let n=e[0];for(let t=1,o=e.length;t<o;t+=1)"number"==typeof e[t]&&(n*=e[t]);return n}export function sub(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let n=e[0];for(let t=1,o=e.length;t<o;t+=1)"number"==typeof e[t]&&(n-=e[t]);return n}export function div(...t){const e=t.length>0&&"object"==typeof t[0]?t[0]:t;let n=e[0];for(let t=1,o=e.length;t<o;t+=1)"number"==typeof e[t]&&(n/=e[t]);return n}
|
package/dist/misc.d.ts
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { PositiveNumber } from './types/global';
|
|
2
|
+
export declare function sleep<N extends number>(delay: PositiveNumber<N>): Promise<void>;
|
|
3
|
+
export declare function funcTimes<N extends number>(times: PositiveNumber<N>, iteratee: any): Array<any>;
|
|
4
|
+
export declare function debounce<N extends number>(func: (...args: any[]) => void, timeout: PositiveNumber<N>): (...args: any[]) => void;
|
package/dist/misc.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function sleep(e){return new Promise((t=>{setTimeout(t,e)}))}export function funcTimes(e,t){const n=[];for(let o=0;o<e;o+=1)n[o]="function"==typeof t?t.call():t;return n}export function debounce(e,t){let n;return(...o)=>{clearTimeout(n),n=setTimeout((()=>{e.apply(o)}),t)}}
|
package/dist/object.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { AnyValueObject } from './types/global';
|
|
2
|
+
export declare function objToQueryString(obj: AnyValueObject): string;
|
|
3
|
+
export declare function objFindItemRecursiveByKey(obj: AnyValueObject | any[], searchKey: string, searchValue: any, childKey: string): AnyValueObject | null;
|
|
4
|
+
export declare function objToPrettyStr(obj: AnyValueObject): string;
|
|
5
|
+
export declare function objToArray(obj: AnyValueObject, recursive?: boolean): any[];
|
|
6
|
+
export declare function objTo1d(obj: AnyValueObject, separator?: string): AnyValueObject;
|
|
7
|
+
export declare function objDeleteKeyByValue(obj: AnyValueObject, searchValue: string | number | null | undefined, recursive?: boolean): AnyValueObject | null;
|
|
8
|
+
export declare function objUpdate(obj: AnyValueObject, key: string, value: any, recursive?: boolean, upsert?: boolean): AnyValueObject | null;
|
|
9
|
+
export declare function objMergeNewKey(obj: AnyValueObject, obj2: AnyValueObject): AnyValueObject | null;
|
package/dist/object.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{isEmpty as e,isObject as t}from"./verify";export function objToQueryString(e){return Object.keys(e).map((t=>{let r=e[t];return"object"==typeof r&&(r=JSON.stringify(r)),`${encodeURIComponent(t)}=${encodeURIComponent(r)}`})).join("&")}export function objFindItemRecursiveByKey(t,r,n,o){const c=t=>{let s;s="object"==typeof t&&Array.isArray(t)?t:[t];for(let t=0,l=s.length;t<l;t+=1){if(s[t][r]===n)return s[t];if(!e(s[t][o])){const e=c(s[t][o]);if(e)return e}}return null};return c(t)}export function objToPrettyStr(e){return JSON.stringify(e,null,"\t")}export function objToArray(e,r=!1){const n=e=>{const o=[],c=Object.keys(e).length;for(let s=0;s<c;s+=1){const c=Object.keys(e)[s];r&&t(e[c])?o.push([c,n(e[c])]):o.push([c,e[c]])}return o};return n(e)}export function objTo1d(e,r="."){if(!r||r.length<1)throw new Error("`separator` must have value at least 1 character.");const n=(e,o="")=>{let c={};const s=Object.keys(e).length,l=o.length<1;for(let u=0;u<s;u+=1){const s=Object.keys(e)[u],y=e[s],f=`${o}${l?"":r}${s}`;t(y)?(c=Object.assign(c,n(y,f)),delete c[s]):c[f]=y}return c};return n(e)}export function objDeleteKeyByValue(e,r,n=!1){if(!e||"object"!=typeof e)return null;const o=Object.assign(e,{});for(let e=Object.keys(o).length;e>=0;e-=1){const c=Object.keys(o)[e];n&&o[c]&&t(o[c])?objDeleteKeyByValue(o[c],r,n):o[c]===r&&delete o[c]}return o}export function objUpdate(e,r,n,o=!1,c=!1){if(!e||"object"!=typeof e)return null;const s=Object.assign(e,{});let l=!1;const u=e=>{for(let c=0;c<Object.keys(e).length;c+=1){const s=Object.keys(e)[c];o&&e[s]&&t(e[s])&&u(e[s]),Object.hasOwn(e,r)&&(e[r]=n,l=!0)}};return u(s),!l&&c&&(s[r]=n),s}export function objMergeNewKey(e,r){if(!e||"object"!=typeof e||!r||"object"!=typeof r)return null;const n={...e};return Object.keys(r).forEach((e=>{const o=r[e];if(Object.hasOwn(n,e))if(Array.isArray(n[e])&&Array.isArray(o)){if(n[e].length===o.length)for(let r=0;r<n[e].length;r+=1){const c=o[r];t(c)&&(n[e][r]=objMergeNewKey(n[e][r],c))}}else t(n[e])&&t(o)?n[e]=objMergeNewKey(n[e],o):n[e]=o;else n[e]=o})),n}
|
package/dist/string.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { PositiveNumber } from './types/global';
|
|
2
|
+
export declare function trim(str?: string | null): string | null;
|
|
3
|
+
export declare function removeSpecialChar(str: string, exceptionCharacters?: string): string;
|
|
4
|
+
export declare function replaceBetween(str: string, startChar: string, endChar: string, replaceWith?: string): string;
|
|
5
|
+
export declare function removeNewLine(str: string, replaceTo?: string): string;
|
|
6
|
+
export declare function capitalizeFirst(str: string): string;
|
|
7
|
+
export declare function capitalizeEverySentence(str: string, splitChar?: string): string;
|
|
8
|
+
export declare function capitalizeEachWords(str: string, natural?: boolean): string;
|
|
9
|
+
export declare function strCount(str: string, search: string): number;
|
|
10
|
+
export declare function strShuffle(str: string): string;
|
|
11
|
+
export declare function strRandom<N extends number>(length: PositiveNumber<N>, additionalCharacters?: string): string;
|
|
12
|
+
export declare function strBlindRandom<N extends number>(str: string, blindLength: PositiveNumber<N>, blindStr?: string): string;
|
|
13
|
+
export declare function truncate<N extends number>(str: string, length: PositiveNumber<N>, ellipsis?: string): string;
|
|
14
|
+
export declare function truncateExpect<N extends number>(str: string, expectLength: PositiveNumber<N>, endStringChar?: string): string;
|
|
15
|
+
export declare function split(str: string, ...splitter: any[]): string[];
|
|
16
|
+
export declare function split(str: string, ...splitter: Array<string>): string[];
|
|
17
|
+
export declare function strToAscii(str: string): number[];
|
|
18
|
+
export declare function strUnique(str: string): string;
|
|
19
|
+
export declare function urlJoin(...args: any[]): string;
|
package/dist/string.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import{contains as t}from"./verify";import{numRandom as e}from"./math";export function trim(t){return"string"==typeof t||t?t.trim().replace(/\s{2,}/g," "):null}export function removeSpecialChar(t,e){return t?t.replace(new RegExp(`[^a-zA-Z가-힣ㄱ-ㅎㅏ-ㅣ0-9-ヿ㐀-䶿一-鿿豈-ヲ-゚${e??""}]`,"gi"),""):""}export function replaceBetween(t,e,r,n=""){if(!t)return"";const o=/[.*+?^${}()|[\]\\]/g,i=o.test(e)?`\\${e}`:e,l=o.test(r)?`\\${r}`:r;return t.replace(new RegExp(`${i}.*?${l}`,"g"),n)}export function removeNewLine(t,e=""){return t?t.replace(/(\r\n|\n|\r)/gm,e).trim():""}export function capitalizeFirst(t){return t?t.charAt(0).toUpperCase()+t.slice(1):""}export function capitalizeEverySentence(t,e){if(!t)return"";const r=e||".",n=t.split(r);let o,i="";for(let t=0,e=n.length;t<e;t+=1){o=[...n[t]];for(let e=0,r=o.length;e<r;e+=1)if(/[a-zA-Z]/.test(n[t][e])){o[e]=n[t][e].toUpperCase();break}i+=`${o.join("")}${t<e-1?r:""}`}return i}export function capitalizeEachWords(e,r){if(!e)return"";const n=e.trim().toLowerCase().split(" ");for(let e=0,o=n.length;e<o;e+=1)r&&t(n[e],["in","on","the","at","and","or","of","for","to","that","a","by","it","is","as","are","were","was","nor","an"],!0)||(n[e]=capitalizeFirst(n[e]));return capitalizeFirst(n.join(" "))}export function 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}export function strShuffle(t){return t?[...t].sort((()=>Math.random()-.5)).join(""):""}export function strRandom(t,e){const r=`abcdefghijklmnopqrstuvwxyz0123456789${e}`,n=r.length;let o,i="";for(let e=0;e<t;e+=1)o=r.charAt(Math.floor(Math.random()*n)),o=Math.random()<.5?o.toUpperCase():o,i+=o;return i}export function strBlindRandom(t,r,n="*"){if(!t)return"";let o=t,i=0,l=0,a=0;const c=o.length;for(;i<r&&a<c;)l=e(0,c),/[a-zA-Z가-힣]/.test(o.substring(l,l+1))&&(o=`${o.substring(0,l+1)}${n}${o.substring(l+2)}`,i+=1),a+=1;return o}export function truncate(t,e,r=""){if(!t)return"";let n=t;return t.length>e&&(n=t.substring(0,e)+r),n}export function truncateExpect(t,e,r="."){if(!t)return"";const n=t.slice(-1)===r,o=t.split(r),i=o.length;let l="",a=0;for(let t=0;t<i&&a<e;t+=1)l+=`${o[t]}${t!==i-1||n?r:""}`,a+=o[t].length+r.length;return l}export function split(t,...e){if(!t)return[];const r=e.length>0&&"object"==typeof e[0]?e[0]:e,n=r.length;let o="",i="";for(let t=0;t<n;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,"\\+")}`:o+="-"===e||"["===e||"]"===e?`\\${e}`:e}return o.length<1&&i.length<1?[t]:(o.length>0&&(o=`[${o}]`,i.length>0&&(i=`|${i}`)),t.split(new RegExp(`${o}${i}+`,"gi")))}export function strToAscii(t){const e=[];for(let r=0;r<t.length;r+=1)e.push(t.charCodeAt(r));return e}export function strUnique(t){return t?[...new Set(t)].join(""):""}export function urlJoin(...t){if(!t)return"";const e=t.length;let r="",n=0;for(let o=0;o<e;o+=1)null!==t[o]&&void 0!==t[o]&&(0===n||t[o].startsWith("/")||t[o].startsWith("?")||t[o].startsWith("&")?r+=t[o]:r+=`/${t[o]}`,n+=1);return r.replace(/\/$/g,"")}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type PositiveNumber<N extends number> = number extends N ? N : `${N}` extends `-${string}` ? never : N;
|
|
2
|
+
export type NumberValueObject = {
|
|
3
|
+
[key: string]: number;
|
|
4
|
+
};
|
|
5
|
+
export type AnyValueObject = {
|
|
6
|
+
[key: string]: any;
|
|
7
|
+
};
|
|
8
|
+
export type DurationOptions = {
|
|
9
|
+
useShortString?: boolean;
|
|
10
|
+
useSpace?: boolean;
|
|
11
|
+
withZeroValue?: boolean;
|
|
12
|
+
separator?: string;
|
|
13
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export{};
|
package/dist/verify.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare function isObject(data: any): boolean;
|
|
2
|
+
export declare function isEqual(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
3
|
+
export declare function isEqualStrict(leftOperand: any, ...rightOperand: Array<any>): boolean;
|
|
4
|
+
export declare function isEmpty(data?: any): boolean;
|
|
5
|
+
export declare function isUrl(url: string, withProtocol?: boolean, strict?: boolean): boolean;
|
|
6
|
+
export declare function contains(str: any[] | string, search: any[] | string, exact?: boolean): boolean;
|
|
7
|
+
export declare function is2dArray(array: any[]): boolean;
|
|
8
|
+
export declare function between(range: [number, number], number: number, inclusive?: boolean): boolean;
|
|
9
|
+
export declare function len(data: any): number;
|
|
10
|
+
export declare function isEmail(email: string): boolean;
|
|
11
|
+
export declare function isTrueMinimumNumberOfTimes(conditions: boolean[], minimumCount?: number): boolean;
|
package/dist/verify.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export function isObject(t){return null!=t&&Object.getPrototypeOf(t)===Object.prototype}export function 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}export function 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}export function 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}}export function 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}export function contains(t,e,r=!1){if("string"==typeof e)return!(t.length<1)&&-1!==t.indexOf(e);for(let n=0,i=e.length;n<i;n+=1)if(r){if(t===e[n])return!0}else if(-1!==t.indexOf(e[n]))return!0;return!1}export function is2dArray(t){return t.filter(Array.isArray).length>0}export function between(t,e,r=!1){const n=Math.min.apply(Math,[t[0],t[1]]),i=Math.max.apply(Math,[t[0],t[1]]);return r?e>=n&&e<=i:e>n&&e<i}export function 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}}export function 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)}export function isTrueMinimumNumberOfTimes(t,e=1){const r=t.length;let n=0;for(let e=0;e<r;e+=1)"boolean"==typeof t[e]&&t[e]&&(n+=1);return n>=e}
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "qsu",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.6.0",
|
|
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",
|
|
7
|
-
"homepage": "https://qsu.
|
|
7
|
+
"homepage": "https://qsu.cdget.com",
|
|
8
8
|
"repository": {
|
|
9
9
|
"type": "git",
|
|
10
10
|
"url": "https://github.com/jooy2/qsu.git"
|
|
@@ -16,10 +16,10 @@
|
|
|
16
16
|
"types": "dist/index.d.ts",
|
|
17
17
|
"scripts": {
|
|
18
18
|
"build": "npm run format:fix && tsc --project tsconfig.prod.json && npm run minify",
|
|
19
|
-
"test": "npm run build &&
|
|
19
|
+
"test": "npm run build && glob -c \"tsx --test\" \"./test/**/*.test.ts\"",
|
|
20
20
|
"lint": "eslint .",
|
|
21
21
|
"lint:fix": "eslint --fix .",
|
|
22
|
-
"minify": "terser dist
|
|
22
|
+
"minify": "terser-glob 'dist/**/*.js' --config-file terser.config.json",
|
|
23
23
|
"prepare": "npm run build",
|
|
24
24
|
"format": "prettier .",
|
|
25
25
|
"format:fix": "prettier . --write"
|
|
@@ -56,19 +56,18 @@
|
|
|
56
56
|
"file"
|
|
57
57
|
],
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@types/
|
|
60
|
-
"@
|
|
61
|
-
"@typescript-eslint/
|
|
62
|
-
"
|
|
63
|
-
"
|
|
64
|
-
"eslint": "
|
|
65
|
-
"eslint-config-
|
|
66
|
-
"eslint-
|
|
67
|
-
"
|
|
68
|
-
"
|
|
69
|
-
"
|
|
70
|
-
"
|
|
71
|
-
"
|
|
72
|
-
"typescript": "^5.5.2"
|
|
59
|
+
"@types/node": "22.10.1",
|
|
60
|
+
"@typescript-eslint/eslint-plugin": "7.14.1",
|
|
61
|
+
"@typescript-eslint/parser": "7.14.1",
|
|
62
|
+
"dayjs": "^1.11.13",
|
|
63
|
+
"eslint": "8.57.0",
|
|
64
|
+
"eslint-config-airbnb": "19.0.4",
|
|
65
|
+
"eslint-config-prettier": "9.1.0",
|
|
66
|
+
"eslint-plugin-import": "2.29.1",
|
|
67
|
+
"glob": "^11.0.0",
|
|
68
|
+
"prettier": "^3.4.2",
|
|
69
|
+
"terser-glob": "^1.1.0",
|
|
70
|
+
"tsx": "^4.19.2",
|
|
71
|
+
"typescript": "^5.7.2"
|
|
73
72
|
}
|
|
74
73
|
}
|
package/logo.webp
DELETED
|
Binary file
|