Merge pull request #8662 from allenve/master

feat: 公式支持uuid生成
This commit is contained in:
hsm-lv 2023-11-08 19:12:01 +08:00 committed by GitHub
commit e14c9a5281
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 69 additions and 0 deletions

View File

@ -563,6 +563,11 @@ test('evalute:Math', () => {
expect(evaluate('${POW(2, infinity)}', data)).toBe(data.infinity);
});
test('evalute:UUID', () => {
expect(evaluate('${UUID()}', {}).length).toBe(32);
expect(evaluate('${UUID(8)}', {}).length).toBe(8);
});
test('evalute:namespace', () => {
localStorage.setItem('a', '1');
localStorage.setItem('b', '2');

View File

@ -591,6 +591,16 @@
返回a.json`。
### UUID
用法:`UUID(8)`
* `length:number` 生成的UUID字符串长度默认为32位
返回:`string` 生成的UUID字符串
生成UUID字符串
## 日期函数
### DATE

View File

@ -1022,6 +1022,23 @@ export const doc: {
},
namespace: '文本函数'
},
{
name: 'UUID',
description: '生成UUID字符串',
example: 'UUID(8)',
params: [
{
type: 'number',
name: 'length',
description: '生成的UUID字符串长度默认为32位'
}
],
returns: {
type: 'string',
description: '生成的UUID字符串'
},
namespace: '文本函数'
},
{
name: 'DATE',
description:

View File

@ -1514,6 +1514,21 @@ export class Evaluator {
return text.split(/[\\/]/).pop();
}
/**
* UUID字符串
*
* @param {number} length - UUID字符串长度32
* @example UUID()
* @example UUID(8)
* @namespace
*
* @returns {string} UUID字符串
*/
fnUUID(length: number = 32) {
const len = Math.min(Math.max(length, 0), 32);
return uuidv4().slice(0, len);
}
// 日期函数
/**
@ -2414,3 +2429,25 @@ export function createObject(
return obj;
}
export function createStr() {
return (
'00000000000000000' + (Math.random() * 0xffffffffffffffff).toString(16)
).slice(-16);
}
export function uuidv4() {
const a = createStr();
const b = createStr();
return (
a.slice(0, 8) +
'-' +
a.slice(8, 12) +
'-4' +
a.slice(13) +
'-a' +
b.slice(1, 4) +
'-' +
b.slice(4)
);
}