toFixed(str: string | number, count: number)

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

获取指定长度字符串,不足时前补零,超出时从右侧截取

参数

参数类型必填说明
strstring | number是需要填充的字符串或数字
countnumber是返回字符串的固定长度

返回值

类型:string
填充或截取后的定长字符串

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { toFixed } = utils;

toFixed('111', 5);    // '00111'
toFixed('3456111', 5); // '56111'
toFixed(42, 4);        // '0042'

toFilled(str: string, count: number)

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

将字符串用前导零填充到指定最小长度

参数

参数类型必填说明
strstring是需要填充的字符串
countnumber是返回字符串的最小长度

返回值

类型:string
填充后的字符串;若原始长度已 >= count,则原样返回

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { toFilled } = utils;

toFilled('111', 5);     // '00111'
toFilled('3456111', 5); // '3456111'

partition(str: string, chunk: number)

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

将字符串按指定长度分割为子字符串数组

参数

参数类型必填说明
strstring是需要分割的原始字符串
chunknumber是每个子字符串的长度

返回值

类型:string[]
分割后的字符串数组,最后一个元素长度可能小于 chunk

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { partition } = utils;

partition('1234567', 3); // ['123', '456', '7']
partition('AABB', 2);    // ['AA', 'BB']

get(object: Record<string, any>, pathString: string, defaultValue?: string | number | false | true | Record<string, any>)

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

根据路径字符串安全地获取嵌套对象的属性值(lodash.get 的轻量替代)

参数

参数类型必填说明
是要查询的源对象
pathStringstring是属性路径,使用点号分隔,如 'a.b.c'
否当路径解析结果为 undefined 时返回的默认值

返回值

类型:string | number | false | true | Record<string, any>
路径对应的属性值,若未找到则返回 defaultValue

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { get } = utils;

const obj = { a: { b: { c: 42 } } };
get(obj, 'a.b.c');          // 42
get(obj, 'a.b.x', 'N/A');  // 'N/A'
get(obj, 'a.b.c.d');       // undefined

pick(object: Record<string, unknown>, keys: string[])

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

从对象中选取指定键的属性,返回新对象(lodash.pick 的轻量替代)

参数

参数类型必填说明
是源对象
keysstring[]是需要选取的属性键名数组

返回值

类型:Record<string, unknown>
仅包含指定键的新对象

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { pick } = utils;

const obj = { a: 1, b: 2, c: 3 };
pick(obj, ['a', 'c']); // { a: 1, c: 3 }
pick(obj, ['d']);       // {}

omit(object: Record<string, unknown>, keys: string[])

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

从对象中排除指定键的属性,返回新对象(lodash.omit 的轻量替代)

参数

参数类型必填说明
是源对象
keysstring[]是需要排除的属性键名数组

返回值

类型:Record<string, unknown>
排除指定键后的新对象(浅拷贝)

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { omit } = utils;

const obj = { a: 1, b: 2, c: 3 };
omit(obj, ['b']);      // { a: 1, c: 3 }
omit(obj, ['a', 'c']); // { b: 2 }

chunk(arr: any[], chunkSize?: number, cache?: any[])

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

将数组按指定大小分割为多个子数组(lodash.chunk 的轻量替代)

参数

参数类型必填说明
arrany[]是需要分割的原始数组
chunkSizenumber否每个子数组的大小,默认为 1
cacheany[]否内部递归用缓存数组,通常无需传入

返回值

类型:any[]
分割后的二维数组

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { chunk } = utils;

chunk([1, 2, 3, 4, 5], 2); // [[1, 2], [3, 4], [5]]
chunk(['a', 'b', 'c'], 1); // [['a'], ['b'], ['c']]

compareVersion(v1: string, v2: string)

版本要求:
@ray-js/panel-sdk≥1.0.0

描述

比较两个语义化版本号字符串的大小

参数

参数类型必填说明
v1string是第一个版本号字符串,如 '1.2.3'
v2string是第二个版本号字符串,如 '1.2.4'

返回值

类型:false | 0 | 1 | -1
1 表示 v1 大于 v2,-1 表示 v1 小于 v2,0 表示相等,false 表示参数无效

合法值

return
值说明最低版本
false--
0--
1--
-1--

示例代码

基础用法

import { utils } from '@ray-js/panel-sdk';

const { compareVersion } = utils;

compareVersion('1.2.3', '1.2.4'); // -1
compareVersion('2.0.0', '1.9.9'); // 1
compareVersion('1.0.0', '1.0.0'); // 0
compareVersion(null, '1.0.0');    // false