@polkadot/types-codec 8.8.3-12 → 8.8.3-13

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/base/Enum.js CHANGED
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { mapToTypeMap, typesToMap } from "../utils/index.js";
5
5
  import { Null } from "./Null.js"; // export interface, this is used in Enum.with, so required as public by TS
6
6
 
@@ -73,16 +73,36 @@ function extractDef(registry, _def) {
73
73
  };
74
74
  }
75
75
 
76
- function createFromValue(registry, def, index = 0, value) {
77
- const entry = Object.values(def).find(e => e.index === index);
76
+ function getEntryType(def, checkIdx) {
77
+ const values = Object.values(def);
78
+
79
+ for (let i = 0; i < values.length; i++) {
80
+ const {
81
+ Type,
82
+ index
83
+ } = values[i];
78
84
 
79
- if (isUndefined(entry)) {
80
- throw new Error(`Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
85
+ if (index === checkIdx) {
86
+ return Type;
87
+ }
81
88
  }
82
89
 
90
+ throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
91
+ }
92
+
93
+ function createFromU8a(registry, def, index, value) {
94
+ const Type = getEntryType(def, index);
95
+ return {
96
+ index,
97
+ value: new Type(registry, value)
98
+ };
99
+ }
100
+
101
+ function createFromValue(registry, def, index = 0, value) {
102
+ const Type = getEntryType(def, index);
83
103
  return {
84
104
  index,
85
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
105
+ value: value instanceof Type ? value : new Type(registry, value)
86
106
  };
87
107
  }
88
108
 
@@ -113,7 +133,7 @@ function decodeEnum(registry, def, value, index) {
113
133
  const u8a = u8aToU8a(value); // nested, we don't want to match isObject below
114
134
 
115
135
  if (u8a.length) {
116
- return createFromValue(registry, def, u8a[0], u8a.subarray(1));
136
+ return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
117
137
  }
118
138
  } else if (value instanceof Enum) {
119
139
  return createFromValue(registry, def, value.index, value.value);
@@ -155,7 +175,7 @@ export class Enum {
155
175
  isIndexed
156
176
  } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
157
177
 
158
- const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
178
+ const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromU8a(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
159
179
  this.registry = registry;
160
180
  this.#def = def;
161
181
  this.#isBasic = isBasic;
package/cjs/base/Enum.js CHANGED
@@ -85,18 +85,38 @@ function extractDef(registry, _def) {
85
85
  };
86
86
  }
87
87
 
88
- function createFromValue(registry, def) {
89
- let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
90
- let value = arguments.length > 3 ? arguments[3] : undefined;
91
- const entry = Object.values(def).find(e => e.index === index);
88
+ function getEntryType(def, checkIdx) {
89
+ const values = Object.values(def);
90
+
91
+ for (let i = 0; i < values.length; i++) {
92
+ const {
93
+ Type,
94
+ index
95
+ } = values[i];
92
96
 
93
- if ((0, _util.isUndefined)(entry)) {
94
- throw new Error(`Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
97
+ if (index === checkIdx) {
98
+ return Type;
99
+ }
95
100
  }
96
101
 
102
+ throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
103
+ }
104
+
105
+ function createFromU8a(registry, def, index, value) {
106
+ const Type = getEntryType(def, index);
107
+ return {
108
+ index,
109
+ value: new Type(registry, value)
110
+ };
111
+ }
112
+
113
+ function createFromValue(registry, def) {
114
+ let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
115
+ let value = arguments.length > 3 ? arguments[3] : undefined;
116
+ const Type = getEntryType(def, index);
97
117
  return {
98
118
  index,
99
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
119
+ value: value instanceof Type ? value : new Type(registry, value)
100
120
  };
101
121
  }
102
122
 
@@ -127,7 +147,7 @@ function decodeEnum(registry, def, value, index) {
127
147
  const u8a = (0, _util.u8aToU8a)(value); // nested, we don't want to match isObject below
128
148
 
129
149
  if (u8a.length) {
130
- return createFromValue(registry, def, u8a[0], u8a.subarray(1));
150
+ return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
131
151
  }
132
152
  } else if (value instanceof Enum) {
133
153
  return createFromValue(registry, def, value.index, value.value);
@@ -170,7 +190,7 @@ class Enum {
170
190
  isIndexed
171
191
  } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
172
192
 
173
- const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
193
+ const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromU8a(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
174
194
  this.registry = registry;
175
195
  this.#def = def;
176
196
  this.#isBasic = isBasic;
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.8.3-12'
14
+ version: '8.8.3-13'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.8.3-12",
23
+ "version": "8.8.3-13",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -415,6 +415,6 @@
415
415
  },
416
416
  "dependencies": {
417
417
  "@babel/runtime": "^7.18.3",
418
- "@polkadot/util": "^9.4.2-22"
418
+ "@polkadot/util": "^9.5.1"
419
419
  }
420
420
  }
package/packageInfo.js CHANGED
@@ -5,5 +5,5 @@ export const packageInfo = {
5
5
  name: '@polkadot/types-codec',
6
6
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
7
  type: 'esm',
8
- version: '8.8.3-12'
8
+ version: '8.8.3-13'
9
9
  };