master-utils-lib 1.0.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/DEVELOPMENT.md +274 -0
- package/PUBLISH.md +248 -0
- package/QUICK_START.md +174 -0
- package/README.md +125 -0
- package/USAGE_EXAMPLES.md +549 -0
- package/babel.config.js +16 -0
- package/dist/index.esm.js +111 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +124 -0
- package/dist/index.js.map +1 -0
- package/dist/index.min.js +130 -0
- package/dist/index.min.js.map +1 -0
- package/dist/types/device/index.d.ts +3 -0
- package/dist/types/format/index.d.ts +2 -0
- package/dist/types/index.d.ts +5 -0
- package/dist/types/storage/index.d.ts +3 -0
- package/dist/types/url/index.d.ts +2 -0
- package/dist/types/validate/index.d.ts +2 -0
- package/jest.config.js +14 -0
- package/package.json +68 -0
- package/rollup.config.js +39 -0
- package/src/device/index.ts +14 -0
- package/src/format/index.ts +31 -0
- package/src/index.ts +5 -0
- package/src/storage/index.ts +35 -0
- package/src/url/index.ts +21 -0
- package/src/validate/index.ts +7 -0
- package/tests/device.test.ts +28 -0
- package/tests/format.test.ts +15 -0
- package/tsconfig.json +26 -0
- package/tsconfig.test.json +19 -0
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
interface StorageData<T> {
|
|
2
|
+
value: T;
|
|
3
|
+
expire: number | null;
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
export const setStorage = <T>(key: string, value: T, expire: number | null = null): void => {
|
|
7
|
+
if (typeof window === 'undefined') return;
|
|
8
|
+
const data: StorageData<T> = {
|
|
9
|
+
value,
|
|
10
|
+
expire: expire !== null ? new Date().getTime() + expire : null,
|
|
11
|
+
};
|
|
12
|
+
localStorage.setItem(key, JSON.stringify(data));
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export const getStorage = <T>(key: string): T | null => {
|
|
16
|
+
if (typeof window === 'undefined') return null;
|
|
17
|
+
const item = localStorage.getItem(key);
|
|
18
|
+
if (!item) return null;
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const data: StorageData<T> = JSON.parse(item);
|
|
22
|
+
if (data.expire && data.expire < new Date().getTime()) {
|
|
23
|
+
localStorage.removeItem(key);
|
|
24
|
+
return null;
|
|
25
|
+
}
|
|
26
|
+
return data.value;
|
|
27
|
+
} catch (e) {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export const removeStorage = (key: string): void => {
|
|
33
|
+
if (typeof window === 'undefined') return;
|
|
34
|
+
localStorage.removeItem(key);
|
|
35
|
+
};
|
package/src/url/index.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
export const getQueryString = (name: string): string | null => {
|
|
2
|
+
if (typeof window === 'undefined') return null;
|
|
3
|
+
const reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
|
|
4
|
+
const r = window.location.search.substr(1).match(reg);
|
|
5
|
+
if (r != null) return decodeURIComponent(r[2]);
|
|
6
|
+
return null;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const stringifyQuery = (obj: Record<string, any>): string => {
|
|
10
|
+
if (!obj) return '';
|
|
11
|
+
const pairs: string[] = [];
|
|
12
|
+
for (const key in obj) {
|
|
13
|
+
if (Object.prototype.hasOwnProperty.call(obj, key)) {
|
|
14
|
+
const value = obj[key];
|
|
15
|
+
if (value !== undefined && value !== null) {
|
|
16
|
+
pairs.push(`${encodeURIComponent(key)}=${encodeURIComponent(String(value))}`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
return pairs.join('&');
|
|
21
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { isIOS, isAndroid, isWeChat } from 'h5-utils-lib';
|
|
2
|
+
|
|
3
|
+
describe('Device Utils', () => {
|
|
4
|
+
let userAgentGetter: any;
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
userAgentGetter = jest.spyOn(window.navigator, 'userAgent', 'get');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
afterEach(() => {
|
|
11
|
+
jest.clearAllMocks();
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
test('isIOS should return true for iPhone user agent', () => {
|
|
15
|
+
userAgentGetter.mockReturnValue('Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X)');
|
|
16
|
+
expect(isIOS()).toBe(true);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
test('isAndroid should return true for Android user agent', () => {
|
|
20
|
+
userAgentGetter.mockReturnValue('Mozilla/5.0 (Linux; Android 10; SM-G981B)');
|
|
21
|
+
expect(isAndroid()).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
test('isWeChat should return true for WeChat user agent', () => {
|
|
25
|
+
userAgentGetter.mockReturnValue('Mozilla/5.0 (iPhone; CPU iPhone OS 14_0 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Mobile/15E148 MicroMessenger/7.0.18(0x17001228) NetType/WIFI Language/zh_CN');
|
|
26
|
+
expect(isWeChat()).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { formatMoney, formatDate } from 'h5-utils-lib';
|
|
2
|
+
|
|
3
|
+
describe('Format Utils', () => {
|
|
4
|
+
test('formatMoney should format currency correctly', () => {
|
|
5
|
+
expect(formatMoney(1234.56)).toBe('¥1,234.56');
|
|
6
|
+
expect(formatMoney(1234.56, 1, '$')).toBe('$1,234.6');
|
|
7
|
+
expect(formatMoney('abc')).toBe('¥0.00');
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
test('formatDate should format date correctly', () => {
|
|
11
|
+
const date = new Date('2023-01-01T12:30:45');
|
|
12
|
+
expect(formatDate(date, 'YYYY-MM-DD')).toBe('2023-01-01');
|
|
13
|
+
expect(formatDate(date, 'YYYY-MM-DD hh:mm:ss')).toBe('2023-01-01 12:30:45');
|
|
14
|
+
});
|
|
15
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"module": "esnext",
|
|
5
|
+
"moduleResolution": "node",
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"declarationDir": "./dist/types",
|
|
8
|
+
"strict": true,
|
|
9
|
+
"esModuleInterop": true,
|
|
10
|
+
"skipLibCheck": true,
|
|
11
|
+
"forceConsistentCasingInFileNames": true,
|
|
12
|
+
"outDir": "./dist",
|
|
13
|
+
"lib": [
|
|
14
|
+
"es6",
|
|
15
|
+
"dom"
|
|
16
|
+
]
|
|
17
|
+
},
|
|
18
|
+
"include": [
|
|
19
|
+
"src/**/*"
|
|
20
|
+
],
|
|
21
|
+
"exclude": [
|
|
22
|
+
"node_modules",
|
|
23
|
+
"dist",
|
|
24
|
+
"**/*.test.ts"
|
|
25
|
+
]
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"extends": "./tsconfig.json",
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
"baseUrl": ".",
|
|
5
|
+
"paths": {
|
|
6
|
+
"h5-utils-lib": [
|
|
7
|
+
"src/index.ts"
|
|
8
|
+
]
|
|
9
|
+
}
|
|
10
|
+
},
|
|
11
|
+
"include": [
|
|
12
|
+
"src/**/*",
|
|
13
|
+
"tests/**/*"
|
|
14
|
+
],
|
|
15
|
+
"exclude": [
|
|
16
|
+
"node_modules",
|
|
17
|
+
"dist"
|
|
18
|
+
]
|
|
19
|
+
}
|