dolphindb 3.1.19 → 3.1.21

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/browser.d.ts CHANGED
@@ -180,11 +180,11 @@ export interface InspectOptions {
180
180
  /** timestamp 类型转换为字符串表示时显示到秒还是毫秒 */
181
181
  timestamp?: 's' | 'ms';
182
182
  }
183
- /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
183
+ /** 根据 DdbType 格式化单个元素 (value) 为字符串,空值根据 nullstr 选项返回 'null' / '' 字符串 */
184
184
  export declare function format(type: DdbType, value: DdbValue, le: boolean, options?: InspectOptions): string;
185
185
  /** 格式化向量、集合中的第 index 项为字符串,空值返回 'null' 字符串 formatted vector, the index-th item in the collection is a string, a null value returns a 'null' string */
186
186
  export declare function formati(obj: DdbVectorObj, index: number, options?: InspectOptions): string;
187
- export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob, char, timestamp }?: ConvertOptions): string | number | bigint | boolean | string[] | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbSymbolExtendedValue | DdbExtObjValue | DdbTensorValue | Uint8Array<ArrayBufferLike> | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | BigInt64Array<ArrayBufferLike> | BigInt128Array | Uint8Array<ArrayBufferLike>[] | DdbObj<DdbValue>[] | IotVectorItemValue | DdbArrayVectorValue | DdbDecimal128VectorValue | DdbDurationVectorValue | DdbMatrixValue | DdbChartValue;
187
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob, char, timestamp }?: ConvertOptions): string | number | bigint | boolean | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbSymbolExtendedValue | string[] | DdbExtObjValue | DdbTensorValue | Uint8Array<ArrayBufferLike> | DdbDecimal128VectorValue | BigInt128Array | Int8Array<ArrayBufferLike> | Int16Array<ArrayBufferLike> | Int32Array<ArrayBufferLike> | Float32Array<ArrayBufferLike> | Float64Array<ArrayBufferLike> | BigInt64Array<ArrayBufferLike> | IotVectorItemValue | DdbArrayVectorValue | DdbDurationVectorValue | Uint8Array<ArrayBufferLike>[] | DdbObj<DdbValue>[] | DdbMatrixValue | DdbChartValue;
188
188
  /** 转换一个向量到 js 原生数组 */
189
189
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[] | Uint8Array;
190
190
  /** 构造 void 类型,默认为 `DdbVoidType.undefined` */
package/browser.js CHANGED
@@ -3,7 +3,7 @@ import { blue, cyan, green, grey, magenta } from 'xshell/chalk.browser.js';
3
3
  import { concat, assert, Lock, genid, seq, zip_object, decode, delay, check } from 'xshell/utils.browser.js';
4
4
  import { connect_websocket } from 'xshell/net.browser.js';
5
5
  import { t } from "./i18n/index.js";
6
- import { date2str, datehour2str, datetime2str, ddb_tensor_bytes, DdbChartType, DdbDurationUnit, DdbForm, DdbFunctionType, DdbType, DdbVoidType, dictables, function_definition_patterns, generate_array_type, get_big_int_128, get_time_ddbobj, get_duration_unit, get_type_name, int1282str, ipaddr2str, is_decimal_null_value, is_decimal_type, minute2str, month2str, nanotime2str, nanotimestamp2str, nulls, second2str, set_big_int_128, time2str, timestamp2str, uuid2str, get_times_ddbobj, funcdefs, get_number_formatter } from "./common.js";
6
+ import { ddb_tensor_bytes, DdbChartType, DdbDurationUnit, DdbForm, generate_array_type, DdbFunctionType, DdbType, DdbVoidType, dictables, function_definition_patterns, get_big_int_128, get_time_ddbobj, get_duration_unit, get_type_name, int1282str, ipaddr2str, is_decimal_null_value, is_decimal_type, time_formatters, number_nulls, nulls, set_big_int_128, uuid2str, get_times_ddbobj, funcdefs, get_number_formatter } from "./common.js";
7
7
  export * from "./common.js";
8
8
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
9
9
  export class DdbObj {
@@ -1566,46 +1566,64 @@ export class DdbObj {
1566
1566
  return obj;
1567
1567
  }
1568
1568
  }
1569
- /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
1570
- export function format(type, value, le, options = {}) {
1571
- const { nullstr = false, colors = false, quote = false, timestamp = 'ms' } = options;
1572
- const number_formatter = get_number_formatter(options.decimals, options.grouping);
1573
- function get_nullstr() {
1574
- const str = value === DdbVoidType.default ? 'default' : 'null';
1575
- return nullstr ?
1576
- colors ? grey(str) : str
1569
+ const grey_nullstr = grey('null');
1570
+ const nullstr = 'null';
1571
+ function get_nullstr(options) {
1572
+ return options.nullstr ?
1573
+ options.colors ? grey_nullstr : nullstr
1574
+ :
1575
+ '';
1576
+ }
1577
+ function format_time(type, value, options) {
1578
+ if (value === null ||
1579
+ value === ((type === DdbType.timestamp ||
1580
+ type === DdbType.nanotime ||
1581
+ type === DdbType.nanotimestamp) ? nulls.int64 : nulls.int32))
1582
+ return get_nullstr(options);
1583
+ let str;
1584
+ // formatter 可能会在 value 不属于 new Date() 有效值时,调用 抛出错误,这里统一处理
1585
+ try {
1586
+ str = time_formatters.get(type)(value, (type === DdbType.timestamp && options.timestamp === 's') ?
1587
+ 'YYYY.MM.DD HH:mm:ss'
1577
1588
  :
1578
- '';
1589
+ undefined);
1579
1590
  }
1580
- function format_time(formatter, _null) {
1581
- if (value === null || value === _null)
1582
- return get_nullstr();
1583
- let str;
1584
- // formatter 可能会在 value 不属于 new Date() 有效值时,调用 抛出错误,这里统一处理
1585
- try {
1586
- str = formatter(value, (type === DdbType.timestamp && timestamp === 's') ? 'YYYY.MM.DD HH:mm:ss' : undefined);
1587
- }
1588
- catch (error) {
1589
- if (error instanceof RangeError)
1590
- str = 'Invalid Date';
1591
- else
1592
- throw error;
1593
- }
1594
- return colors ? magenta(str) : str;
1591
+ catch (error) {
1592
+ if (error instanceof RangeError)
1593
+ str = 'Invalid Date';
1594
+ else
1595
+ throw error;
1595
1596
  }
1597
+ return options.colors ? magenta(str) : str;
1598
+ }
1599
+ function format_number(type, value, options) {
1600
+ if (value === null || value === number_nulls.get(type))
1601
+ return options.colors ? grey_nullstr : nullstr;
1602
+ const str = get_number_formatter(type === DdbType.int || type === DdbType.long || type === DdbType.short, options.decimals, options.grouping).format(value);
1603
+ return options.colors ? green(str) : str;
1604
+ }
1605
+ /** 根据 DdbType 格式化单个元素 (value) 为字符串,空值根据 nullstr 选项返回 'null' / '' 字符串 */
1606
+ export function format(type, value, le, options = {}) {
1596
1607
  switch (type) {
1597
- case DdbType.void:
1598
- return get_nullstr();
1608
+ case DdbType.void: {
1609
+ return options.nullstr ?
1610
+ ''
1611
+ :
1612
+ value === DdbVoidType.default ?
1613
+ options.colors ? grey('default') : 'default'
1614
+ :
1615
+ options.colors ? grey_nullstr : nullstr;
1616
+ }
1599
1617
  case DdbType.bool:
1600
1618
  if (value === null || value === nulls.int8)
1601
- return get_nullstr();
1619
+ return get_nullstr(options);
1602
1620
  else {
1603
1621
  const str = String(Boolean(value));
1604
- return colors ? blue(str) : str;
1622
+ return options.colors ? blue(str) : str;
1605
1623
  }
1606
1624
  case DdbType.char:
1607
1625
  if (value === null || value === nulls.int8)
1608
- return get_nullstr();
1626
+ return get_nullstr(options);
1609
1627
  else {
1610
1628
  let str =
1611
1629
  // ascii printable
@@ -1614,101 +1632,63 @@ export function format(type, value, le, options = {}) {
1614
1632
  String.fromCharCode(value)
1615
1633
  :
1616
1634
  '\\' + String(value);
1617
- if (quote)
1635
+ if (options.quote)
1618
1636
  str = str.quote();
1619
- return colors ? green(str) : str;
1637
+ return options.colors ? green(str) : str;
1620
1638
  }
1621
1639
  case DdbType.short:
1622
- if (value === null || value === nulls.int16)
1623
- return get_nullstr();
1624
- else {
1625
- const str = number_formatter.format(value);
1626
- return colors ? green(str) : str;
1627
- }
1628
1640
  case DdbType.int:
1629
- if (value === null || value === nulls.int32)
1630
- return get_nullstr();
1631
- else {
1632
- const str = number_formatter.format(value);
1633
- return colors ? green(str) : str;
1634
- }
1635
1641
  case DdbType.long:
1636
- if (value === null || value === nulls.int64)
1637
- return get_nullstr();
1638
- else {
1639
- const str = number_formatter.format(value);
1640
- return colors ? green(str) : str;
1641
- }
1642
+ case DdbType.float:
1643
+ case DdbType.double:
1644
+ return format_number(type, value, options);
1642
1645
  case DdbType.date:
1643
- return format_time(date2str, nulls.int32);
1644
1646
  case DdbType.month:
1645
- return format_time(month2str, nulls.int32);
1646
1647
  case DdbType.time:
1647
- return format_time(time2str, nulls.int32);
1648
1648
  case DdbType.minute:
1649
- return format_time(minute2str, nulls.int32);
1650
1649
  case DdbType.second:
1651
- return format_time(second2str, nulls.int32);
1652
1650
  case DdbType.datetime:
1653
- return format_time(datetime2str, nulls.int32);
1654
1651
  case DdbType.timestamp:
1655
- return format_time(timestamp2str, nulls.int64);
1656
1652
  case DdbType.nanotime:
1657
- return format_time(nanotime2str, nulls.int64);
1658
1653
  case DdbType.nanotimestamp:
1659
- return format_time(nanotimestamp2str, nulls.int64);
1660
- case DdbType.float:
1661
- if (value === null || value === nulls.float32)
1662
- return get_nullstr();
1663
- else {
1664
- const str = number_formatter.format(value);
1665
- return colors ? green(str) : str;
1666
- }
1667
- case DdbType.double:
1668
- if (value === null || value === nulls.double)
1669
- return get_nullstr();
1670
- else {
1671
- const str = number_formatter.format(value);
1672
- return colors ? green(str) : str;
1673
- }
1654
+ case DdbType.datehour:
1655
+ return format_time(type, value, options);
1674
1656
  case DdbType.symbol:
1675
1657
  case DdbType.string: {
1676
1658
  let str = value;
1677
- if (quote)
1659
+ if (options.quote)
1678
1660
  str = str.quote('single');
1679
- return colors ? cyan(str) : str;
1661
+ return options.colors ? cyan(str) : str;
1680
1662
  }
1681
1663
  case DdbType.uuid: {
1682
1664
  const str = uuid2str(value, le);
1683
- return colors ? cyan(str) : str;
1665
+ return options.colors ? cyan(str) : str;
1684
1666
  }
1685
1667
  case DdbType.functiondef: {
1686
1668
  const str = value.name.quote('single');
1687
- return colors ? cyan(str) : str;
1669
+ return options.colors ? cyan(str) : str;
1688
1670
  }
1689
1671
  case DdbType.handle:
1690
1672
  case DdbType.code:
1691
1673
  case DdbType.datasource:
1692
1674
  case DdbType.resource: {
1693
1675
  const str = value.quote('single');
1694
- return colors ? cyan(str) : str;
1676
+ return options.colors ? cyan(str) : str;
1695
1677
  }
1696
- case DdbType.datehour:
1697
- return format_time(datehour2str, nulls.int32);
1698
1678
  case DdbType.ipaddr: {
1699
1679
  const str = ipaddr2str(value, le);
1700
- return colors ? cyan(str) : str;
1680
+ return options.colors ? cyan(str) : str;
1701
1681
  }
1702
1682
  case DdbType.int128: {
1703
1683
  const str = int1282str(value, le);
1704
- return colors ? cyan(str) : str;
1684
+ return options.colors ? cyan(str) : str;
1705
1685
  }
1706
1686
  case DdbType.blob: {
1707
1687
  const str = value.length > 100 ?
1708
1688
  DdbObj.dec.decode(value.subarray(0, 98)) + '…'
1709
1689
  :
1710
1690
  DdbObj.dec.decode(value);
1711
- return colors ? cyan(str) : str;
1691
+ return options.colors ? cyan(str) : str;
1712
1692
  }
1713
1693
  case DdbType.point: {
1714
1694
  const [x, y] = value;
@@ -1721,7 +1701,7 @@ export function format(type, value, le, options = {}) {
1721
1701
  case DdbType.duration: {
1722
1702
  const { data, unit } = value;
1723
1703
  const str = `${data}${DdbDurationUnit[unit] ?? get_duration_unit(unit)}`;
1724
- return colors ? magenta(str) : str;
1704
+ return options.colors ? magenta(str) : str;
1725
1705
  }
1726
1706
  case DdbType.decimal32:
1727
1707
  case DdbType.decimal64:
@@ -1729,18 +1709,18 @@ export function format(type, value, le, options = {}) {
1729
1709
  const { scale, data } = value;
1730
1710
  if (data === null ||
1731
1711
  is_decimal_null_value(type, data))
1732
- return get_nullstr();
1712
+ return get_nullstr(options);
1733
1713
  const s = String(data < 0 ? -data : data).padStart(scale, '0');
1734
1714
  const str = (data < 0 ? '-' : '') + (scale ? `${s.slice(0, -scale) || '0'}.${s.slice(-scale)}` : s);
1735
- return colors ? green(str) : str;
1715
+ return options.colors ? green(str) : str;
1736
1716
  }
1737
1717
  default:
1738
- return value === null ? get_nullstr() : String(value);
1718
+ return value === null ? get_nullstr(options) : String(value);
1739
1719
  }
1740
1720
  }
1741
1721
  /** 格式化向量、集合中的第 index 项为字符串,空值返回 'null' 字符串 formatted vector, the index-th item in the collection is a string, a null value returns a 'null' string */
1742
1722
  export function formati(obj, index, options = {}) {
1743
- check(index < obj.rows, 'index < obj.rows');
1723
+ check(index < obj.rows, 'index 应该 < obj.rows');
1744
1724
  if (obj.type < 64 || obj.type >= 128) // 普通数组
1745
1725
  switch (obj.type) {
1746
1726
  case DdbType.symbol_extended: {