@ocap/util 1.17.0 → 1.17.3

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.
@@ -1 +1 @@
1
- export declare const createSortedList: (list: $TSFixMe) => unknown[];
1
+ export declare const createSortedList: (list: $TSFixMe) => any;
package/lib/index.d.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  /// <reference types="node" />
2
+ /// <reference types="jest" />
3
+ import { LiteralUnion } from 'type-fest';
2
4
  import rightPad from 'lodash/padEnd';
3
5
  import leftPad from 'lodash/padStart';
4
6
  import BN from 'bn.js';
5
7
  export declare type BytesType = string | Buffer | Uint8Array;
6
- export declare type EncodingType = 'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer';
8
+ export declare type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>;
7
9
  export declare type KeyPairType = {
8
10
  publicKey: BytesType;
9
11
  secretKey: BytesType;
@@ -11,8 +13,8 @@ export declare type KeyPairType = {
11
13
  export { BN, leftPad, rightPad };
12
14
  /**
13
15
  * Returns a BN object, converts a number value to a BN
14
- * @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
15
- * @return {Object} `output` BN object of the number
16
+ * @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object
17
+ * @return {BN} `output` BN object of the number
16
18
  * @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
17
19
  */
18
20
  export declare const numberToBN: (arg: string | number | BN) => BN;
@@ -31,10 +33,8 @@ export declare const isHexPrefixed: (str: string) => boolean;
31
33
  *
32
34
  * @public
33
35
  * @static
34
- * @param {String} str the string value
35
- * @return {String|Optional} a string by pass if necessary
36
36
  */
37
- export declare const stripHexPrefix: (str: $TSFixMe) => any;
37
+ export declare const stripHexPrefix: (str: string | any) => any;
38
38
  /**
39
39
  * Returns true if object is BN, otherwise false
40
40
  *
@@ -114,7 +114,7 @@ export declare const hexToUtf8: (hex: string) => string;
114
114
  * @param {String|Number|BN} value
115
115
  * @returns {Number}
116
116
  */
117
- export declare const hexToNumber: (value: string | number | BN) => string | number | BN;
117
+ export declare const hexToNumber: (value: string | number | BN) => number;
118
118
  /**
119
119
  * Converts value to hex representation
120
120
  *
@@ -146,7 +146,7 @@ export declare const bytesToHex: (bytes: $TSFixMe) => string;
146
146
  * @param {String} hex
147
147
  * @returns {Array} the byte array
148
148
  */
149
- export declare const hexToBytes: (hex: $TSFixMe) => number[];
149
+ export declare const hexToBytes: (hex: $TSFixMe) => Array<any>;
150
150
  /**
151
151
  * Auto converts any given value into it's hex representation.
152
152
  * And even stringify objects before.
@@ -166,127 +166,54 @@ export declare const toHex: (value: string | number | boolean | BN | Uint8Array
166
166
  export declare const numberToString: (arg: $TSFixMe) => any;
167
167
  /**
168
168
  * Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
169
- *
170
- * @public
171
- * @static
172
- * @method fromUnitToToken
173
- * @param {string|number} input
174
- * @param {number} [decimal=18]
175
- * @param {object} [optionsInput=undefined]
176
- * @returns {string}
177
169
  */
178
170
  export declare const fromUnitToToken: (input: string | number | BN, decimal?: number, optionsInput?: $TSFixMe) => string;
179
171
  /**
180
172
  * Convert human readable token number to big number instance
181
- *
182
- * @public
183
- * @static
184
- * @param {string} input
185
- * @param {number} [decimal=18]
186
- * @returns {BN}
187
173
  */
188
174
  export declare const fromTokenToUnit: (input: string | number, decimal?: number) => BN;
189
175
  /**
190
176
  * Validates if a value is an Uint8Array.
191
- *
192
- * @public
193
- * @static
194
- * @param {*} value - value to validate
195
- * @returns {Boolean} boolean indicating if a value is an Uint8Array
196
177
  */
197
178
  export declare function isUint8Array(value: $TSFixMe): boolean;
198
179
  /**
199
180
  * Generate a random UUID
200
- *
201
- * @public
202
- * @static
203
- * @returns {string} Generated uuid
204
181
  */
205
182
  export declare function UUID(): string;
206
183
  /**
207
184
  * Check if a string is valid UUID
208
- *
209
- * @public
210
- * @static
211
- * @param {string} str
212
- * @returns {boolean}
213
185
  */
214
186
  export declare function isUUID(str: string): boolean;
215
187
  /**
216
188
  * Convert input to Uint8Array on best effort, base64 node supported
217
- *
218
- * @public
219
- * @static
220
- * @param {buffer|base58|hex|Uint8Array|string} v
221
- * @returns {Uint8Array}
222
- * @throws {Error}
223
189
  */
224
- export declare function toUint8Array(v: $TSFixMe): Uint8Array;
190
+ export declare function toUint8Array(v: any): Uint8Array;
225
191
  /**
226
192
  * Convert input to Buffer on best effort, base64 not supported
227
- *
228
- * @public
229
- * @static
230
- * @param {buffer|base58|hex|Uint8Array} v
231
- * @returns {buffer}
232
- * @throws {Error}
233
193
  */
234
- export declare function toBuffer(v: $TSFixMe): Buffer;
194
+ export declare function toBuffer(v: any): Buffer;
235
195
  /**
236
196
  * Convert input to base58btc format on best effort
237
- *
238
- * @public
239
- * @static
240
- * @param {buffer|base58|hex|Uint8Array} v
241
- * @returns {string}
242
- * @throws {Error}
243
197
  */
244
- export declare function toBase58(v: $TSFixMe): string;
198
+ export declare function toBase58(v: any): string;
245
199
  /**
246
200
  * Decode base58 string
247
- *
248
- * @public
249
- * @static
250
- * @param {string} v
251
- * @returns {buffer}
252
201
  */
253
202
  export declare function fromBase58(v: string): Buffer;
254
203
  /**
255
204
  * Convert input to base64 format
256
- *
257
- * @public
258
- * @static
259
- * @param {buffer|base58|hex|Uint8Array} v
260
- * @param {escape} [escape=true]
261
- * @returns {string}
262
- * @throws {Error}
263
205
  */
264
- export declare function toBase64(v: $TSFixMe, escape?: boolean): string;
206
+ export declare function toBase64(v: any, escape?: boolean): string;
265
207
  /**
266
208
  * Decode base64(base64_url) string to buffer
267
- *
268
- * @public
269
- * @static
270
- * @param {string} v
271
- * @returns {buffer}
272
209
  */
273
210
  export declare function fromBase64(v: string): Buffer;
274
211
  /**
275
212
  * Convert did to address: remove `did:abt:` prefix
276
- *
277
- * @public
278
- * @static
279
- * @param {string} did - address string
280
- * @returns {string}
281
213
  */
282
214
  export declare function toAddress(did: string): string;
283
215
  /**
284
216
  * Convert address to did: prepend `did:abt:` prefix
285
- *
286
- * @public
287
- * @static
288
- * @param {string} did - address string
289
- * @returns {string}
290
217
  */
291
218
  export declare function toDid(address: string): string;
292
- export declare const formatTxType: (type: string) => string;
219
+ export declare function formatTxType(type: string): any;
package/lib/index.js CHANGED
@@ -27,9 +27,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
29
  exports.formatTxType = exports.toDid = exports.toAddress = exports.fromBase64 = exports.toBase64 = exports.fromBase58 = exports.toBase58 = exports.toBuffer = exports.toUint8Array = exports.isUUID = exports.UUID = exports.isUint8Array = exports.fromTokenToUnit = exports.fromUnitToToken = exports.numberToString = exports.toHex = exports.hexToBytes = exports.bytesToHex = exports.numberToHex = exports.hexToNumber = exports.hexToUtf8 = exports.utf8ToHex = exports.toBN = exports.isHex = exports.isHexStrict = exports.isBigNumber = exports.isBN = exports.stripHexPrefix = exports.isHexPrefixed = exports.numberToBN = exports.rightPad = exports.leftPad = exports.BN = void 0;
30
- /* eslint-disable @typescript-eslint/no-use-before-define */
31
- /* eslint-disable no-param-reassign */
32
- /* eslint-disable no-bitwise */
33
30
  const isBoolean_1 = __importDefault(require("lodash/isBoolean"));
34
31
  const isString_1 = __importDefault(require("lodash/isString"));
35
32
  const isNumber_1 = __importDefault(require("lodash/isNumber"));
@@ -62,8 +59,8 @@ const isBase58btc = (data) => {
62
59
  };
63
60
  /**
64
61
  * Returns a BN object, converts a number value to a BN
65
- * @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
66
- * @return {Object} `output` BN object of the number
62
+ * @param {string|number|BN} `arg` input a string number, hex string number, number, BigNumber or BN object
63
+ * @return {BN} `output` BN object of the number
67
64
  * @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
68
65
  */
69
66
  const numberToBN = (arg) => {
@@ -113,14 +110,12 @@ exports.isHexPrefixed = isHexPrefixed;
113
110
  *
114
111
  * @public
115
112
  * @static
116
- * @param {String} str the string value
117
- * @return {String|Optional} a string by pass if necessary
118
113
  */
119
114
  const stripHexPrefix = (str) => {
120
- if (typeof str !== 'string') {
121
- return str;
115
+ if (typeof str === 'string') {
116
+ return (0, exports.isHexPrefixed)(str) ? str.slice(2) : str;
122
117
  }
123
- return (0, exports.isHexPrefixed)(str) ? str.slice(2) : str;
118
+ return str;
124
119
  };
125
120
  exports.stripHexPrefix = stripHexPrefix;
126
121
  /**
@@ -257,7 +252,7 @@ exports.hexToUtf8 = hexToUtf8;
257
252
  */
258
253
  const hexToNumber = (value) => {
259
254
  if (!value) {
260
- return value;
255
+ return 0;
261
256
  }
262
257
  return (0, exports.toBN)(value).toNumber();
263
258
  };
@@ -353,7 +348,7 @@ const toHex = (value, returnType = false) => {
353
348
  return returnType ? 'string' : (0, exports.utf8ToHex)(JSON.stringify(value));
354
349
  }
355
350
  // if its a negative number, pass it through numberToHex
356
- if ((0, isString_1.default)(value)) {
351
+ if (typeof value === 'string') {
357
352
  if (value.indexOf('-0x') === 0 || value.indexOf('-0X') === 0) {
358
353
  return returnType ? 'int256' : (0, exports.numberToHex)(value);
359
354
  }
@@ -391,14 +386,6 @@ const numberToString = (arg) => {
391
386
  exports.numberToString = numberToString;
392
387
  /**
393
388
  * Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
394
- *
395
- * @public
396
- * @static
397
- * @method fromUnitToToken
398
- * @param {string|number} input
399
- * @param {number} [decimal=18]
400
- * @param {object} [optionsInput=undefined]
401
- * @returns {string}
402
389
  */
403
390
  const fromUnitToToken = (input, decimal = 18, optionsInput = {}) => {
404
391
  let unit = (0, exports.toBN)(input);
@@ -430,12 +417,6 @@ const fromUnitToToken = (input, decimal = 18, optionsInput = {}) => {
430
417
  exports.fromUnitToToken = fromUnitToToken;
431
418
  /**
432
419
  * Convert human readable token number to big number instance
433
- *
434
- * @public
435
- * @static
436
- * @param {string} input
437
- * @param {number} [decimal=18]
438
- * @returns {BN}
439
420
  */
440
421
  const fromTokenToUnit = (input, decimal = 18) => {
441
422
  let token = (0, exports.numberToString)(input);
@@ -479,11 +460,6 @@ const fromTokenToUnit = (input, decimal = 18) => {
479
460
  exports.fromTokenToUnit = fromTokenToUnit;
480
461
  /**
481
462
  * Validates if a value is an Uint8Array.
482
- *
483
- * @public
484
- * @static
485
- * @param {*} value - value to validate
486
- * @returns {Boolean} boolean indicating if a value is an Uint8Array
487
463
  */
488
464
  function isUint8Array(value) {
489
465
  return Object.prototype.toString.call(value) === '[object Uint8Array]';
@@ -491,10 +467,6 @@ function isUint8Array(value) {
491
467
  exports.isUint8Array = isUint8Array;
492
468
  /**
493
469
  * Generate a random UUID
494
- *
495
- * @public
496
- * @static
497
- * @returns {string} Generated uuid
498
470
  */
499
471
  function UUID() {
500
472
  return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
@@ -506,11 +478,6 @@ function UUID() {
506
478
  exports.UUID = UUID;
507
479
  /**
508
480
  * Check if a string is valid UUID
509
- *
510
- * @public
511
- * @static
512
- * @param {string} str
513
- * @returns {boolean}
514
481
  */
515
482
  function isUUID(str) {
516
483
  return /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/.test(str);
@@ -518,12 +485,6 @@ function isUUID(str) {
518
485
  exports.isUUID = isUUID;
519
486
  /**
520
487
  * Convert input to Uint8Array on best effort, base64 node supported
521
- *
522
- * @public
523
- * @static
524
- * @param {buffer|base58|hex|Uint8Array|string} v
525
- * @returns {Uint8Array}
526
- * @throws {Error}
527
488
  */
528
489
  function toUint8Array(v) {
529
490
  let vb = null;
@@ -553,12 +514,6 @@ function toUint8Array(v) {
553
514
  exports.toUint8Array = toUint8Array;
554
515
  /**
555
516
  * Convert input to Buffer on best effort, base64 not supported
556
- *
557
- * @public
558
- * @static
559
- * @param {buffer|base58|hex|Uint8Array} v
560
- * @returns {buffer}
561
- * @throws {Error}
562
517
  */
563
518
  function toBuffer(v) {
564
519
  return Buffer.from(toUint8Array(v));
@@ -566,12 +521,6 @@ function toBuffer(v) {
566
521
  exports.toBuffer = toBuffer;
567
522
  /**
568
523
  * Convert input to base58btc format on best effort
569
- *
570
- * @public
571
- * @static
572
- * @param {buffer|base58|hex|Uint8Array} v
573
- * @returns {string}
574
- * @throws {Error}
575
524
  */
576
525
  function toBase58(v) {
577
526
  const buf = base58btc.encode(toUint8Array(v));
@@ -580,11 +529,6 @@ function toBase58(v) {
580
529
  exports.toBase58 = toBase58;
581
530
  /**
582
531
  * Decode base58 string
583
- *
584
- * @public
585
- * @static
586
- * @param {string} v
587
- * @returns {buffer}
588
532
  */
589
533
  function fromBase58(v) {
590
534
  if (isBase58btc(v) === false) {
@@ -595,27 +539,14 @@ function fromBase58(v) {
595
539
  exports.fromBase58 = fromBase58;
596
540
  /**
597
541
  * Convert input to base64 format
598
- *
599
- * @public
600
- * @static
601
- * @param {buffer|base58|hex|Uint8Array} v
602
- * @param {escape} [escape=true]
603
- * @returns {string}
604
- * @throws {Error}
605
542
  */
606
543
  function toBase64(v, escape = true) {
607
- // FIXME: 这个是正确的?
608
544
  const encoded = base64_url_1.default.encode(toBuffer(v));
609
545
  return escape ? base64_url_1.default.escape(encoded) : encoded;
610
546
  }
611
547
  exports.toBase64 = toBase64;
612
548
  /**
613
549
  * Decode base64(base64_url) string to buffer
614
- *
615
- * @public
616
- * @static
617
- * @param {string} v
618
- * @returns {buffer}
619
550
  */
620
551
  function fromBase64(v) {
621
552
  if (typeof v !== 'string') {
@@ -626,11 +557,6 @@ function fromBase64(v) {
626
557
  exports.fromBase64 = fromBase64;
627
558
  /**
628
559
  * Convert did to address: remove `did:abt:` prefix
629
- *
630
- * @public
631
- * @static
632
- * @param {string} did - address string
633
- * @returns {string}
634
560
  */
635
561
  function toAddress(did) {
636
562
  return did.replace(DID_PREFIX, '');
@@ -638,15 +564,12 @@ function toAddress(did) {
638
564
  exports.toAddress = toAddress;
639
565
  /**
640
566
  * Convert address to did: prepend `did:abt:` prefix
641
- *
642
- * @public
643
- * @static
644
- * @param {string} did - address string
645
- * @returns {string}
646
567
  */
647
568
  function toDid(address) {
648
569
  return `${DID_PREFIX}${toAddress(address)}`;
649
570
  }
650
571
  exports.toDid = toDid;
651
- const formatTxType = (type) => (0, upperFirst_1.default)((0, camelCase_1.default)(type));
572
+ function formatTxType(type) {
573
+ return (0, upperFirst_1.default)((0, camelCase_1.default)(type));
574
+ }
652
575
  exports.formatTxType = formatTxType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ocap/util",
3
- "version": "1.17.0",
3
+ "version": "1.17.3",
4
4
  "description": "utils shared across multiple forge js libs, works in both node.js and browser",
5
5
  "keywords": [
6
6
  "arcblock",
@@ -15,6 +15,7 @@
15
15
  "bn.js": "5.2.1",
16
16
  "bs58": "^5.0.0",
17
17
  "lodash": "^4.17.21",
18
+ "type-fest": "^2.13.1",
18
19
  "utf8": "^3.0.0"
19
20
  },
20
21
  "resolutions": {
@@ -65,5 +66,5 @@
65
66
  "bugs": {
66
67
  "url": "https://github.com/ArcBlock/asset-chain/issues"
67
68
  },
68
- "gitHead": "f0944b60a5fb4115e1a3727bb07f68174bc56c5c"
69
+ "gitHead": "7a74e4e2b362a6e6ea8d14617f0480966c3363d5"
69
70
  }