Fixing approvalFlagsToBools. (#1250)
This commit is contained in:
committed by
Jaco Greeff
parent
b885b8681e
commit
58993042c1
@@ -1,21 +0,0 @@
|
||||
// Copyright 2017-2019 @polkadot/api-derive authors & contributors
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
import { ApprovalFlag } from '@polkadot/types/interfaces/elections';
|
||||
|
||||
import BN from 'bn.js';
|
||||
import { Vec } from '@polkadot/types';
|
||||
|
||||
export function approvalFlagToBool (flags: Vec<ApprovalFlag>): boolean[] {
|
||||
const bools: boolean[] = [];
|
||||
for (const flag of flags) {
|
||||
for (const bit of [...Array(flag.bitLength())].map((_, i): number => i)) {
|
||||
bools.push(!flag.toBn().uand((new BN(1)).shln(bit)).isZero());
|
||||
}
|
||||
}
|
||||
|
||||
const lastApproval: number = bools.lastIndexOf(true);
|
||||
|
||||
return lastApproval >= 0 ? bools.slice(0, lastApproval + 1) : [];
|
||||
}
|
||||
@@ -12,10 +12,13 @@ export function approvalFlagsToBools (flags: Vec<ApprovalFlag>): boolean[] {
|
||||
flags.forEach((flag: ApprovalFlag): void => {
|
||||
const str = flag.toString(2);
|
||||
|
||||
for (const bit of str) {
|
||||
// read from lowest bit to highest
|
||||
for (const bit of str.split('').reverse()) {
|
||||
bools.push(!!parseInt(bit, 10));
|
||||
}
|
||||
});
|
||||
|
||||
return bools;
|
||||
// slice off trailing "false" values, as in substrate
|
||||
const lastApproval: number = bools.lastIndexOf(true);
|
||||
return lastApproval >= 0 ? bools.slice(0, lastApproval + 1) : [];
|
||||
}
|
||||
|
||||
@@ -2,4 +2,4 @@
|
||||
// This software may be modified and distributed under the terms
|
||||
// of the Apache-2.0 license. See the LICENSE file for details.
|
||||
|
||||
export * from './approvalFlagToBool';
|
||||
export * from './approvalFlagsToBools';
|
||||
|
||||
Reference in New Issue
Block a user