dolphindb 2.0.512 → 2.0.513

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
@@ -182,6 +182,7 @@ export declare class DdbObj<T extends DdbValue = DdbValue> {
182
182
  to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
183
183
  to_dict<T = Record<string, any>>(): T;
184
184
  }
185
+ export declare function format(type: DdbType, value: DdbValue, le: boolean): string;
185
186
  export declare class DdbVoid extends DdbObj<undefined> {
186
187
  constructor();
187
188
  }
@@ -246,6 +247,10 @@ export declare class DdbTable extends DdbObj<DdbObj[]> {
246
247
  constructor(columns: DdbObj[], name?: string);
247
248
  }
248
249
  export declare function date2str(date: number, format?: string): string;
250
+ export declare function month2str(month: number | null): string;
251
+ export declare function time2str(time: number, format?: string): string;
252
+ export declare function minute2str(minute: number | null, format?: string): string;
253
+ export declare function second2str(second: number | null, format?: string): string;
249
254
  export declare function datetime2str(datetime: number, format?: string): string;
250
255
  /** format timestamp (bigint) to string
251
256
  - timestamp: bigint value
@@ -254,6 +259,7 @@ export declare function datetime2str(datetime: number, format?: string): string;
254
259
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
255
260
  */
256
261
  export declare function timestamp2str(timestamp: bigint, format?: string): string;
262
+ export declare function datehour2str(datehour: number, format?: string): string;
257
263
  /** parse timestamp string to bigint value
258
264
  - str: timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
259
265
  - format?:
@@ -261,6 +267,7 @@ export declare function timestamp2str(timestamp: bigint, format?: string): strin
261
267
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
262
268
  */
263
269
  export declare function str2timestamp(str: string, format?: string): bigint;
270
+ export declare function nanotime2str(nanotime: bigint, format?: string): string;
264
271
  /** format nanotimestamp value (bigint) to string
265
272
  - nanotimestamp: bigint value
266
273
  - format?:
@@ -277,6 +284,9 @@ export declare function nanotimestamp2str(nanotimestamp: bigint, format?: string
277
284
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
278
285
  */
279
286
  export declare function str2nanotimestamp(str: string, format?: string): bigint;
287
+ export declare function ipaddr2str(buffer: Uint8Array, le?: boolean): string;
288
+ export declare function uuid2str(buffer: Uint8Array, le?: boolean): string;
289
+ export declare function int1282str(buffer: Uint8Array, le?: boolean): string;
280
290
  export declare class DDB {
281
291
  /** 当前的 session id (http 或 tcp) */
282
292
  sid: string;
package/browser.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ddb = exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.str2timestamp = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbTable = exports.DdbSetString = exports.DdbSetDouble = exports.DdbSetInt = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorString = exports.DdbVectorDouble = exports.DdbVectorInt = exports.DdbFunction = exports.DdbPair = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbDurationUnit = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
3
+ exports.ddb = exports.DDB = exports.int1282str = exports.uuid2str = exports.ipaddr2str = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.nanotime2str = exports.str2timestamp = exports.datehour2str = exports.timestamp2str = exports.datetime2str = exports.second2str = exports.minute2str = exports.time2str = exports.month2str = exports.date2str = exports.DdbTable = exports.DdbSetString = exports.DdbSetDouble = exports.DdbSetInt = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorString = exports.DdbVectorDouble = exports.DdbVectorInt = exports.DdbFunction = exports.DdbPair = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.format = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbDurationUnit = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
6
6
  const customParseFormat_js_1 = tslib_1.__importDefault(require("dayjs/plugin/customParseFormat.js"));
@@ -905,33 +905,71 @@ class DdbObj {
905
905
  case DdbForm.set:
906
906
  return `set<${tname}>[${this.rows}]`;
907
907
  case DdbForm.table:
908
- return `table[${this.rows} rows][${this.cols} cols]`;
908
+ return `table[${this.rows}r][${this.cols}c]`;
909
909
  case DdbForm.dict:
910
910
  return `dict<${DdbType[this.value[0].type]}, ${DdbType[this.value[1].type]}>`;
911
911
  case DdbForm.matrix:
912
- return `matrix<${DdbType[this.type]}>[${this.rows} rows][${this.cols} cols]`;
912
+ return `matrix<${DdbType[this.type]}>[${this.rows}r][${this.cols}c]`;
913
913
  default:
914
914
  return `${DdbForm[this.form]} ${tname}`;
915
915
  }
916
916
  })();
917
917
  const data = (() => {
918
918
  switch (this.form) {
919
- case DdbForm.pair:
920
- return `${this.value[0]}, ${this.value[1]}`;
921
919
  case DdbForm.scalar:
920
+ return format(this.type, this.value, this.le);
921
+ case DdbForm.vector:
922
+ case DdbForm.pair:
923
+ case DdbForm.set: {
924
+ const form = this.form;
925
+ function format_array(items, ellipsis) {
926
+ const str_items = items.join(', ') + (ellipsis ? ', ...' : '');
927
+ return form === DdbForm.pair ?
928
+ str_items
929
+ :
930
+ str_items.bracket('square');
931
+ }
922
932
  switch (this.type) {
923
- case DdbType.functiondef:
924
- return `'${this.value.name}'`;
925
- case DdbType.duration: {
926
- const { data, unit } = this.value;
927
- return `${data}${DdbDurationUnit[unit]}`;
933
+ case DdbType.symbol_extended: {
934
+ const limit = 50;
935
+ const { base, data } = this.value;
936
+ let items = new Array(Math.min(limit, data.length));
937
+ for (let i = 0; i < items.length; i++)
938
+ items[i] = base[data[i]].quote('single');
939
+ return format_array(items, data.length > limit);
940
+ }
941
+ case DdbType.uuid:
942
+ case DdbType.int128:
943
+ case DdbType.ipaddr: {
944
+ const limit = 10;
945
+ const value = this.value;
946
+ const len_data = value.length / 16;
947
+ let items = new Array(Math.min(limit, len_data));
948
+ for (let i = 0; i < items.length; i++)
949
+ items[i] = format(this.type, value.subarray(16 * i, 16 * (i + 1)), this.le);
950
+ return format_array(items, len_data > limit);
951
+ }
952
+ case DdbType.complex:
953
+ case DdbType.point: {
954
+ const limit = 20;
955
+ const value = this.value;
956
+ const len_data = value.length / 2;
957
+ let items = new Array(Math.min(limit, len_data));
958
+ for (let i = 0; i < items.length; i++)
959
+ items[i] = format(this.type, value.subarray(2 * i, 2 * (i + 1)), this.le);
960
+ return format_array(items, len_data > limit);
961
+ }
962
+ default: {
963
+ const limit = 50;
964
+ let items = new Array(Math.min(limit, this.value.length));
965
+ for (let i = 0; i < items.length; i++)
966
+ items[i] = format(this.type, this.value[i], this.le);
967
+ return format_array(items, this.value.length > limit);
928
968
  }
929
- case DdbType.string:
930
- return `'${this.value}'`;
931
969
  }
932
- break;
970
+ }
933
971
  }
934
- return this.value;
972
+ return String(this.value);
935
973
  })();
936
974
  return `${type}(${this.name ? `'${this.name}', ` : ''}${data})\n`;
937
975
  }
@@ -943,13 +981,13 @@ class DdbObj {
943
981
  };
944
982
  switch (col.type) {
945
983
  case DdbType.timestamp:
946
- col_.render = timestamp2str;
984
+ col_.render = value => timestamp2str(value);
947
985
  break;
948
986
  case DdbType.date:
949
- col_.render = date2str;
987
+ col_.render = value => date2str(value);
950
988
  break;
951
989
  case DdbType.ipaddr:
952
- col_.render = value => value.slice().reverse().join('.').replace(/^(0\.)+/, '');
990
+ col_.render = value => ipaddr2str(value, this.le);
953
991
  break;
954
992
  }
955
993
  return col_;
@@ -1007,6 +1045,127 @@ DdbObj.enc = new TextEncoder();
1007
1045
  DdbObj.symbol_bases = {};
1008
1046
  /** little endian (client) */
1009
1047
  DdbObj.le_client = Boolean(new Uint8Array(Uint32Array.of(1).buffer)[0]);
1048
+ function format(type, value, le) {
1049
+ switch (type) {
1050
+ case DdbType.bool:
1051
+ return String((value === null || value === exports.nulls.int8) ?
1052
+ null
1053
+ :
1054
+ Boolean(value));
1055
+ case DdbType.char:
1056
+ return String((value === null || value === exports.nulls.int8) ?
1057
+ null
1058
+ :
1059
+ value);
1060
+ case DdbType.short:
1061
+ return String((value === null || value === exports.nulls.int16) ?
1062
+ null
1063
+ :
1064
+ value);
1065
+ case DdbType.int:
1066
+ return String((value === null || value === exports.nulls.int32) ?
1067
+ null
1068
+ :
1069
+ value);
1070
+ case DdbType.long:
1071
+ return (value === null || value === exports.nulls.int64) ?
1072
+ 'null'
1073
+ :
1074
+ String(value);
1075
+ case DdbType.date:
1076
+ return (value === null || value === exports.nulls.int32) ?
1077
+ 'null'
1078
+ :
1079
+ date2str(value);
1080
+ case DdbType.month:
1081
+ return (value === null || value === exports.nulls.int32) ?
1082
+ 'null'
1083
+ :
1084
+ month2str(value);
1085
+ case DdbType.time:
1086
+ return (value === null || value === exports.nulls.int32) ?
1087
+ 'null'
1088
+ :
1089
+ time2str(value);
1090
+ case DdbType.minute:
1091
+ return (value === null || value === exports.nulls.int32) ?
1092
+ 'null'
1093
+ :
1094
+ minute2str(value);
1095
+ case DdbType.second:
1096
+ return (value === null || value === exports.nulls.int32) ?
1097
+ 'null'
1098
+ :
1099
+ second2str(value);
1100
+ case DdbType.datetime:
1101
+ return (value === null || value === exports.nulls.int32) ?
1102
+ 'null'
1103
+ :
1104
+ datetime2str(value);
1105
+ case DdbType.timestamp:
1106
+ return (value === null || value === exports.nulls.int64) ?
1107
+ 'null'
1108
+ :
1109
+ timestamp2str(value);
1110
+ case DdbType.nanotime:
1111
+ return (value === null || value === exports.nulls.int64) ?
1112
+ 'null'
1113
+ :
1114
+ nanotime2str(value);
1115
+ case DdbType.nanotimestamp:
1116
+ return (value === null || value === exports.nulls.int64) ?
1117
+ 'null'
1118
+ :
1119
+ nanotimestamp2str(value);
1120
+ case DdbType.float:
1121
+ return (value === null || value === exports.nulls.float32) ?
1122
+ 'null'
1123
+ :
1124
+ String(value);
1125
+ case DdbType.double:
1126
+ return (value === null || value === exports.nulls.double) ?
1127
+ 'null'
1128
+ :
1129
+ String(value);
1130
+ case DdbType.symbol:
1131
+ case DdbType.string:
1132
+ return value.quote('single');
1133
+ case DdbType.uuid:
1134
+ return uuid2str(value, le);
1135
+ case DdbType.functiondef:
1136
+ return value.quote('single');
1137
+ case DdbType.handle:
1138
+ case DdbType.code:
1139
+ return value.quote('single');
1140
+ case DdbType.datehour:
1141
+ return (value === null || value === exports.nulls.int32) ?
1142
+ 'null'
1143
+ :
1144
+ datehour2str(value);
1145
+ case DdbType.ipaddr:
1146
+ return ipaddr2str(value, le);
1147
+ case DdbType.int128:
1148
+ return int1282str(value, le);
1149
+ case DdbType.blob:
1150
+ return DdbObj.dec.decode(value.subarray(0, 1000)).quote('single');
1151
+ case DdbType.complex:
1152
+ case DdbType.point: {
1153
+ let [x, y] = value;
1154
+ if (x === exports.nulls.double)
1155
+ x = null;
1156
+ if (y === exports.nulls.double)
1157
+ y = null;
1158
+ return `(${String(x)}, ${String(y)})`;
1159
+ }
1160
+ case DdbType.duration: {
1161
+ const { data, unit } = value;
1162
+ return `${data}${DdbDurationUnit[unit]}`;
1163
+ }
1164
+ default:
1165
+ return String(value);
1166
+ }
1167
+ }
1168
+ exports.format = format;
1010
1169
  class DdbVoid extends DdbObj {
1011
1170
  constructor() {
1012
1171
  super({
@@ -1317,14 +1476,48 @@ class DdbTable extends DdbObj {
1317
1476
  }
1318
1477
  exports.DdbTable = DdbTable;
1319
1478
  function date2str(date, format = 'YYYY.MM.DD') {
1320
- return date === exports.nulls.int32 ?
1479
+ return (date === null || date === exports.nulls.int32) ?
1321
1480
  ''
1322
1481
  :
1323
1482
  (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * 24 * date).format(format);
1324
1483
  }
1325
1484
  exports.date2str = date2str;
1485
+ function month2str(month) {
1486
+ if (month === null || month === exports.nulls.int32)
1487
+ return '';
1488
+ if (month < 0)
1489
+ return String(month);
1490
+ let _month = month % 12;
1491
+ let year = Math.floor(month / 12);
1492
+ return `${String(year).padStart(4, '0')}.${String(_month + 1).padStart(2, '0')}`;
1493
+ }
1494
+ exports.month2str = month2str;
1495
+ function time2str(time, format = 'HH:mm:ss.SSS') {
1496
+ return (time === null || time === exports.nulls.int32) ?
1497
+ ''
1498
+ :
1499
+ (0, dayjs_1.default)(exports.timezone_offset + time)
1500
+ .format(format);
1501
+ }
1502
+ exports.time2str = time2str;
1503
+ function minute2str(minute, format = 'HH:mm') {
1504
+ return (minute === null || minute === exports.nulls.int32) ?
1505
+ ''
1506
+ :
1507
+ (0, dayjs_1.default)(exports.timezone_offset + 60 * 1000 * minute)
1508
+ .format(format);
1509
+ }
1510
+ exports.minute2str = minute2str;
1511
+ function second2str(second, format = 'HH:mm:ss') {
1512
+ return (second === null || second === exports.nulls.int32) ?
1513
+ ''
1514
+ :
1515
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * second)
1516
+ .format(format);
1517
+ }
1518
+ exports.second2str = second2str;
1326
1519
  function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
1327
- return datetime === exports.nulls.int32 ?
1520
+ return (datetime === null || datetime === exports.nulls.int32) ?
1328
1521
  ''
1329
1522
  :
1330
1523
  (0, dayjs_1.default)(exports.timezone_offset + 1000 * datetime).format(format);
@@ -1337,12 +1530,19 @@ exports.datetime2str = datetime2str;
1337
1530
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1338
1531
  */
1339
1532
  function timestamp2str(timestamp, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
1340
- return timestamp === exports.nulls.int64 ?
1533
+ return (timestamp === null || timestamp === exports.nulls.int64) ?
1341
1534
  ''
1342
1535
  :
1343
1536
  (0, dayjs_1.default)(exports.timezone_offset + Number(timestamp)).format(format);
1344
1537
  }
1345
1538
  exports.timestamp2str = timestamp2str;
1539
+ function datehour2str(datehour, format = 'YYYY.MM.DD.HH') {
1540
+ return (datehour === null || datehour === exports.nulls.int32) ?
1541
+ ''
1542
+ :
1543
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * datehour).format(format);
1544
+ }
1545
+ exports.datehour2str = datehour2str;
1346
1546
  /** parse timestamp string to bigint value
1347
1547
  - str: timestamp string, If it is an empty string, it will return the corresponding empty value (nulls.int64)
1348
1548
  - format?:
@@ -1358,6 +1558,19 @@ function str2timestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSS') {
1358
1558
  (0, dayjs_1.default)(str, format).valueOf());
1359
1559
  }
1360
1560
  exports.str2timestamp = str2timestamp;
1561
+ function nanotime2str(nanotime, format = 'HH:mm:ss.SSSSSSSSS') {
1562
+ const i_second_start = format.indexOf('ss');
1563
+ if (i_second_start === -1)
1564
+ throw new Error('格式串必须包含秒的格式 (ss)');
1565
+ const i_second_end = i_second_start + 2;
1566
+ const i_nanosecond_start = format.indexOf('SSSSSSSSS', i_second_end);
1567
+ if (i_nanosecond_start === -1)
1568
+ throw new Error('格式串必须包含纳秒的格式 (SSSSSSSSS)');
1569
+ return ((0, dayjs_1.default)(exports.timezone_offset + (Number(nanotime) / 1000000)).format(format.slice(0, i_second_end)) +
1570
+ format.slice(i_second_end, i_nanosecond_start) +
1571
+ String(nanotime % 1000000000n).padStart(9, '0'));
1572
+ }
1573
+ exports.nanotime2str = nanotime2str;
1361
1574
  /** format nanotimestamp value (bigint) to string
1362
1575
  - nanotimestamp: bigint value
1363
1576
  - format?:
@@ -1366,7 +1579,7 @@ exports.str2timestamp = str2timestamp;
1366
1579
  https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1367
1580
  */
1368
1581
  function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
1369
- if (nanotimestamp === exports.nulls.int64)
1582
+ if (nanotimestamp === null || nanotimestamp === exports.nulls.int64)
1370
1583
  return '';
1371
1584
  const i_second_start = format.indexOf('ss');
1372
1585
  if (i_second_start === -1)
@@ -1377,7 +1590,7 @@ function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSS
1377
1590
  throw new Error('The format string must contain the format for nanoseconds (SSSSSSSSS)');
1378
1591
  return ((0, dayjs_1.default)(exports.timezone_offset + (Number(nanotimestamp) / 1000000)).format(format.slice(0, i_second_end)) +
1379
1592
  format.slice(i_second_end, i_nanosecond_start) +
1380
- String(nanotimestamp % 1000000000n));
1593
+ String(nanotimestamp % 1000000000n).padStart(9, '0'));
1381
1594
  }
1382
1595
  exports.nanotimestamp2str = nanotimestamp2str;
1383
1596
  /** parse nano timestamp string to bigint value
@@ -1405,6 +1618,31 @@ function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss.SSSSSSSSS') {
1405
1618
  BigInt(str.slice(i_nanosecond_start, i_nanosecond_start + 9)));
1406
1619
  }
1407
1620
  exports.str2nanotimestamp = str2nanotimestamp;
1621
+ function ipaddr2str(buffer, le = true) {
1622
+ let buf = buffer;
1623
+ if (le)
1624
+ buf = buffer.slice().reverse();
1625
+ const i_non_zero = buf.findIndex(x => x);
1626
+ if (i_non_zero !== -1 && i_non_zero < 12) // ipv6 (i_non_zero === -1 或 0 < i_non_zero < 12)
1627
+ return [...buf].map(x => x.toString(16)
1628
+ .padStart(2, '0')).join(':');
1629
+ else // ipv4
1630
+ return buf.subarray(12).join('.');
1631
+ }
1632
+ exports.ipaddr2str = ipaddr2str;
1633
+ function uuid2str(buffer, le = true) {
1634
+ const str = int1282str(buffer, le);
1635
+ return `${str.slice(0, 8)}-${str.slice(8, 12)}-${str.slice(12, 16)}-${str.slice(16, 20)}-${str.slice(20)}`;
1636
+ }
1637
+ exports.uuid2str = uuid2str;
1638
+ function int1282str(buffer, le = true) {
1639
+ let buf = buffer;
1640
+ if (le)
1641
+ buf = buffer.slice().reverse();
1642
+ return [...buf].map(x => x.toString(16)
1643
+ .padStart(2, '0')).join('');
1644
+ }
1645
+ exports.int1282str = int1282str;
1408
1646
  class DDB {
1409
1647
  /**
1410
1648
  Initialize an instance of DolphinDB Client using the WebSocket URL