@ocap/util 1.17.2 → 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,10 +1,11 @@
1
1
  /// <reference types="node" />
2
2
  /// <reference types="jest" />
3
+ import { LiteralUnion } from 'type-fest';
3
4
  import rightPad from 'lodash/padEnd';
4
5
  import leftPad from 'lodash/padStart';
5
6
  import BN from 'bn.js';
6
7
  export declare type BytesType = string | Buffer | Uint8Array;
7
- export declare type EncodingType = 'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer';
8
+ export declare type EncodingType = LiteralUnion<'hex' | 'base16' | 'base58' | 'base64' | 'Uint8Array' | 'buffer', string>;
8
9
  export declare type KeyPairType = {
9
10
  publicKey: BytesType;
10
11
  secretKey: BytesType;
@@ -12,8 +13,8 @@ export declare type KeyPairType = {
12
13
  export { BN, leftPad, rightPad };
13
14
  /**
14
15
  * Returns a BN object, converts a number value to a BN
15
- * @param {String|Number|Object} `arg` input a string number, hex string number, number, BigNumber or BN object
16
- * @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
17
18
  * @throws if the argument is not an array, object that isn't a bignumber, not a string number or number
18
19
  */
19
20
  export declare const numberToBN: (arg: string | number | BN) => BN;
@@ -32,10 +33,8 @@ export declare const isHexPrefixed: (str: string) => boolean;
32
33
  *
33
34
  * @public
34
35
  * @static
35
- * @param {String} str the string value
36
- * @return {String|Optional} a string by pass if necessary
37
36
  */
38
- export declare const stripHexPrefix: (str: $TSFixMe) => any;
37
+ export declare const stripHexPrefix: (str: string | any) => any;
39
38
  /**
40
39
  * Returns true if object is BN, otherwise false
41
40
  *
@@ -115,7 +114,7 @@ export declare const hexToUtf8: (hex: string) => string;
115
114
  * @param {String|Number|BN} value
116
115
  * @returns {Number}
117
116
  */
118
- export declare const hexToNumber: (value: string | number | BN) => string | number | BN;
117
+ export declare const hexToNumber: (value: string | number | BN) => number;
119
118
  /**
120
119
  * Converts value to hex representation
121
120
  *
@@ -147,7 +146,7 @@ export declare const bytesToHex: (bytes: $TSFixMe) => string;
147
146
  * @param {String} hex
148
147
  * @returns {Array} the byte array
149
148
  */
150
- export declare const hexToBytes: (hex: $TSFixMe) => number[];
149
+ export declare const hexToBytes: (hex: $TSFixMe) => Array<any>;
151
150
  /**
152
151
  * Auto converts any given value into it's hex representation.
153
152
  * And even stringify objects before.
@@ -167,127 +166,54 @@ export declare const toHex: (value: string | number | boolean | BN | Uint8Array
167
166
  export declare const numberToString: (arg: $TSFixMe) => any;
168
167
  /**
169
168
  * Format a big number to human readable number, such as 1_0000_0000_0000_000 => 1 Token
170
- *
171
- * @public
172
- * @static
173
- * @method fromUnitToToken
174
- * @param {string|number} input
175
- * @param {number} [decimal=18]
176
- * @param {object} [optionsInput=undefined]
177
- * @returns {string}
178
169
  */
179
170
  export declare const fromUnitToToken: (input: string | number | BN, decimal?: number, optionsInput?: $TSFixMe) => string;
180
171
  /**
181
172
  * Convert human readable token number to big number instance
182
- *
183
- * @public
184
- * @static
185
- * @param {string} input
186
- * @param {number} [decimal=18]
187
- * @returns {BN}
188
173
  */
189
174
  export declare const fromTokenToUnit: (input: string | number, decimal?: number) => BN;
190
175
  /**
191
176
  * Validates if a value is an Uint8Array.
192
- *
193
- * @public
194
- * @static
195
- * @param {*} value - value to validate
196
- * @returns {Boolean} boolean indicating if a value is an Uint8Array
197
177
  */
198
178
  export declare function isUint8Array(value: $TSFixMe): boolean;
199
179
  /**
200
180
  * Generate a random UUID
201
- *
202
- * @public
203
- * @static
204
- * @returns {string} Generated uuid
205
181
  */
206
182
  export declare function UUID(): string;
207
183
  /**
208
184
  * Check if a string is valid UUID
209
- *
210
- * @public
211
- * @static
212
- * @param {string} str
213
- * @returns {boolean}
214
185
  */
215
186
  export declare function isUUID(str: string): boolean;
216
187
  /**
217
188
  * Convert input to Uint8Array on best effort, base64 node supported
218
- *
219
- * @public
220
- * @static
221
- * @param {buffer|base58|hex|Uint8Array|string} v
222
- * @returns {Uint8Array}
223
- * @throws {Error}
224
189
  */
225
- export declare function toUint8Array(v: $TSFixMe): Uint8Array;
190
+ export declare function toUint8Array(v: any): Uint8Array;
226
191
  /**
227
192
  * Convert input to Buffer on best effort, base64 not supported
228
- *
229
- * @public
230
- * @static
231
- * @param {buffer|base58|hex|Uint8Array} v
232
- * @returns {buffer}
233
- * @throws {Error}
234
193
  */
235
- export declare function toBuffer(v: $TSFixMe): Buffer;
194
+ export declare function toBuffer(v: any): Buffer;
236
195
  /**
237
196
  * Convert input to base58btc format on best effort
238
- *
239
- * @public
240
- * @static
241
- * @param {buffer|base58|hex|Uint8Array} v
242
- * @returns {string}
243
- * @throws {Error}
244
197
  */
245
- export declare function toBase58(v: $TSFixMe): string;
198
+ export declare function toBase58(v: any): string;
246
199
  /**
247
200
  * Decode base58 string
248
- *
249
- * @public
250
- * @static
251
- * @param {string} v
252
- * @returns {buffer}
253
201
  */
254
202
  export declare function fromBase58(v: string): Buffer;
255
203
  /**
256
204
  * Convert input to base64 format
257
- *
258
- * @public
259
- * @static
260
- * @param {buffer|base58|hex|Uint8Array} v
261
- * @param {escape} [escape=true]
262
- * @returns {string}
263
- * @throws {Error}
264
205
  */
265
- export declare function toBase64(v: $TSFixMe, escape?: boolean): string;
206
+ export declare function toBase64(v: any, escape?: boolean): string;
266
207
  /**
267
208
  * Decode base64(base64_url) string to buffer
268
- *
269
- * @public
270
- * @static
271
- * @param {string} v
272
- * @returns {buffer}
273
209
  */
274
210
  export declare function fromBase64(v: string): Buffer;
275
211
  /**
276
212
  * Convert did to address: remove `did:abt:` prefix
277
- *
278
- * @public
279
- * @static
280
- * @param {string} did - address string
281
- * @returns {string}
282
213
  */
283
214
  export declare function toAddress(did: string): string;
284
215
  /**
285
216
  * Convert address to did: prepend `did:abt:` prefix
286
- *
287
- * @public
288
- * @static
289
- * @param {string} did - address string
290
- * @returns {string}
291
217
  */
292
218
  export declare function toDid(address: string): string;
293
- 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.2",
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": "ddb64b9727c9d07bd3df4665444545e43b24a071"
69
+ "gitHead": "7a74e4e2b362a6e6ea8d14617f0480966c3363d5"
69
70
  }