diff --git a/packages/api-derive/src/util/approvalFlagToBool.ts b/packages/api-derive/src/util/approvalFlagToBool.ts deleted file mode 100644 index dd25be906d..0000000000 --- a/packages/api-derive/src/util/approvalFlagToBool.ts +++ /dev/null @@ -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): 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) : []; -} diff --git a/packages/api-derive/src/util/approvalFlagsToBools.ts b/packages/api-derive/src/util/approvalFlagsToBools.ts index 8cc9aca248..c40feea667 100644 --- a/packages/api-derive/src/util/approvalFlagsToBools.ts +++ b/packages/api-derive/src/util/approvalFlagsToBools.ts @@ -12,10 +12,13 @@ export function approvalFlagsToBools (flags: Vec): 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) : []; } diff --git a/packages/api-derive/src/util/index.ts b/packages/api-derive/src/util/index.ts index b26e793299..28af21e7b0 100644 --- a/packages/api-derive/src/util/index.ts +++ b/packages/api-derive/src/util/index.ts @@ -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';