Fixing approvalFlagsToBools. (#1250)

This commit is contained in:
Jake Naviasky
2019-08-01 17:18:18 +02:00
committed by Jaco Greeff
parent b885b8681e
commit 58993042c1
3 changed files with 6 additions and 24 deletions
@@ -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) : [];
}
+1 -1
View File
@@ -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';