xjs-common 10.0.0 → 10.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/func/u-string.d.ts +4 -1
- package/dist/func/u-string.js +40 -6
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -8,11 +8,13 @@ This module is very simple, therefore it has no dependencies.
|
|
|
8
8
|
```
|
|
9
9
|
npm i xjs-common
|
|
10
10
|
```
|
|
11
|
+
**NOTE**: features related nodejs was moved to [xjs-node](https://github.com/begyyal/xjs_node) at v10.0.0.
|
|
12
|
+
if you has been used the features (e.g. `HttpResolver`, `UFile`), please use the new package instead of this.
|
|
11
13
|
|
|
12
14
|
# Code example (only part)
|
|
13
15
|
### Miscellaneous utilities.
|
|
14
16
|
```ts
|
|
15
|
-
import {
|
|
17
|
+
import { delay, int2array, UHttp, retry, MaybeArray, Loggable } from "xjs-common";
|
|
16
18
|
|
|
17
19
|
(async () => {
|
|
18
20
|
// await 3 seconds.
|
package/dist/func/u-string.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { TimeUnit } from "../const/time-unit";
|
|
2
2
|
export declare namespace UString {
|
|
3
3
|
function eq(s1: string, s2: string): boolean;
|
|
4
|
+
function repeat(token: string, mlt: number): string;
|
|
4
5
|
/**
|
|
5
6
|
* generate date time number as fixed length (depends on `unit`) string without separator charactor.
|
|
6
7
|
* For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
|
|
@@ -11,10 +12,12 @@ export declare namespace UString {
|
|
|
11
12
|
date?: Date;
|
|
12
13
|
unit?: TimeUnit;
|
|
13
14
|
}): string;
|
|
14
|
-
function trimProps(obj: any): void;
|
|
15
15
|
function generateRandomString(len: number): string;
|
|
16
16
|
function idx2az(idx: number): string;
|
|
17
17
|
function az2idx(az: string): number;
|
|
18
|
+
function asJpy(amount: number): string;
|
|
19
|
+
function asUsd(amount: number): string;
|
|
20
|
+
function asPercentage(amount: number): string;
|
|
18
21
|
function is_yyyy(v: string): boolean;
|
|
19
22
|
function is_yyyyMM(v: string): boolean;
|
|
20
23
|
function is_yyyyMMdd(v: string): boolean;
|
package/dist/func/u-string.js
CHANGED
|
@@ -12,6 +12,10 @@ var UString;
|
|
|
12
12
|
return !u_type_1.UType.isString(s1) || !u_type_1.UType.isString(s2) ? s1 === s2 : s1.trim() === s2.trim();
|
|
13
13
|
}
|
|
14
14
|
UString.eq = eq;
|
|
15
|
+
function repeat(token, mlt) {
|
|
16
|
+
return (0, u_1.int2array)(mlt).map(_ => token).join("");
|
|
17
|
+
}
|
|
18
|
+
UString.repeat = repeat;
|
|
15
19
|
/**
|
|
16
20
|
* generate date time number as fixed length (depends on `unit`) string without separator charactor.
|
|
17
21
|
* For example, `2025-06-08T10:15:06.366Z` is to be `20250608101506366`.
|
|
@@ -25,12 +29,6 @@ var UString;
|
|
|
25
29
|
return t.substring(0, 14 - (6 - (op?.unit ?? time_unit_1.TimeUnit.Sec)) * 2);
|
|
26
30
|
}
|
|
27
31
|
UString.simpleTime = simpleTime;
|
|
28
|
-
function trimProps(obj) {
|
|
29
|
-
Object.keys(obj)
|
|
30
|
-
.filter(k => typeof obj[k] === "string")
|
|
31
|
-
.forEach(k => obj[k] = obj[k]?.trim());
|
|
32
|
-
}
|
|
33
|
-
UString.trimProps = trimProps;
|
|
34
32
|
function generateRandomString(len) {
|
|
35
33
|
return (0, u_1.int2array)(len).map(_ => {
|
|
36
34
|
let rnd = Math.floor(62 * Math.random());
|
|
@@ -59,6 +57,42 @@ var UString;
|
|
|
59
57
|
.map((idx, i) => (idx + 1) * (26 ** i)).reduce((v1, v2) => v1 + v2) - 1;
|
|
60
58
|
}
|
|
61
59
|
UString.az2idx = az2idx;
|
|
60
|
+
function asAmount(amount, unit) {
|
|
61
|
+
const int2dec = amount.toString().split(".");
|
|
62
|
+
const etni = int2dec[0].split("").reverse().join("");
|
|
63
|
+
let fetni = "";
|
|
64
|
+
const max = Math.ceil(etni.length / 3);
|
|
65
|
+
for (let i = 0; i < max; i++) {
|
|
66
|
+
if (i === max - 1)
|
|
67
|
+
fetni += etni.substring(i * 3);
|
|
68
|
+
else
|
|
69
|
+
fetni += (etni.substring(i * 3, (i + 1) * 3) + ",");
|
|
70
|
+
}
|
|
71
|
+
const finte = unit + fetni.split("").reverse().join("");
|
|
72
|
+
if (int2dec.length === 1)
|
|
73
|
+
return finte;
|
|
74
|
+
else
|
|
75
|
+
return finte + "." + int2dec[1];
|
|
76
|
+
}
|
|
77
|
+
function asJpy(amount) {
|
|
78
|
+
return u_type_1.UType.isEmpty(amount) ? "" : asAmount(Math.floor(amount), "¥");
|
|
79
|
+
}
|
|
80
|
+
UString.asJpy = asJpy;
|
|
81
|
+
function asUsd(amount) {
|
|
82
|
+
return u_type_1.UType.isEmpty(amount) ? "" : asAmount(Number(amount.toFixed(2)), "$");
|
|
83
|
+
}
|
|
84
|
+
UString.asUsd = asUsd;
|
|
85
|
+
function asPercentage(amount) {
|
|
86
|
+
if (u_type_1.UType.isEmpty(amount))
|
|
87
|
+
return "";
|
|
88
|
+
let percent = (amount * 100).toFixed(2);
|
|
89
|
+
while (percent.endsWith("0"))
|
|
90
|
+
percent = percent.substring(0, percent.length - 1);
|
|
91
|
+
if (percent.endsWith("."))
|
|
92
|
+
percent = percent.substring(0, percent.length - 1);
|
|
93
|
+
return percent + "%";
|
|
94
|
+
}
|
|
95
|
+
UString.asPercentage = asPercentage;
|
|
62
96
|
function is_yyyy(v) {
|
|
63
97
|
return !!v?.match(/^[1-9]\d{3}$/);
|
|
64
98
|
}
|