fabric-samples/full-stack-asset-transfer-guide/applications/conga-cards/src/utils.ts
jkneubuh a299e18e26
Moves the Full Stack Asset Transfer Development Guide to fabric-samples (#852)
* Import Full Stack Asset Transfer Guide at commit fb554befdbbeff9e69159b54fce0b811603f29c7

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Update the workshop with a new WORKSHOP_PATH under fabric-samples

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* missed a .git ignored directory on add

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Updates to run the workshop on the Apple M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Workaround for https://github.com/eslint/eslint/issues/15299 in the contract tslinter

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* Build an arch-specific CC images on M1

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* empty commit - force a build

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

* revert an accidental commit that was building the top-level asset-transfer as arm64

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>

Signed-off-by: Josh Kneubuhl <jkneubuh@us.ibm.com>
2022-11-10 10:40:27 -05:00

75 lines
2.3 KiB
TypeScript

/*
* Copyright contributors to the Hyperledgendary Full Stack Asset Transfer Guide project
*
* SPDX-License-Identifier: Apache-2.0
*/
import { inspect, TextDecoder } from 'util';
const utf8Decoder = new TextDecoder();
/**
* Pick a random element from an array.
* @param values Candidate elements.
*/
export function randomElement<T>(values: T[]): T {
return values[randomInt(values.length)];
}
/**
* Generate a random integer in the range 0 to max - 1.
* @param max Maximum value (exclusive).
*/
export function randomInt(max: number): number {
return Math.floor(Math.random() * max);
}
/**
* Pick a random element from an array, excluding the current value.
* @param values Candidate elements.
* @param currentValue Value to avoid.
*/
export function differentElement<T>(values: T[], currentValue: T): T {
const candidateValues = values.filter(value => value !== currentValue);
return randomElement(candidateValues);
}
/**
* Wait for all promises to complete, then throw an Error only if any of the promises were rejected.
* @param promises Promises to be awaited.
*/
export async function allFulfilled(promises: Promise<unknown>[]): Promise<void> {
const results = await Promise.allSettled(promises);
const failures = results
.map(result => result.status === 'rejected' && result.reason as unknown)
.filter(reason => !!reason)
.map(reason => inspect(reason));
if (failures.length > 0) {
const failMessages = '- ' + failures.join('\n- ');
throw new Error(`${failures.length} failures:\n${failMessages}\n`);
}
}
export type PrintView<T> = {
[K in keyof T]: T[K] extends Uint8Array ? string : T[K];
};
export function printable<T extends object>(event: T): PrintView<T> {
return Object.fromEntries(
Object.entries(event).map(([k, v]) => [k, v instanceof Uint8Array ? utf8Decoder.decode(v) : v])
) as PrintView<T>;
}
export function assertAllDefined<T>(values: (T | undefined)[], message: string | (() => string)): T[] {
values.forEach(value => assertDefined(value, message));
return values as T[];
}
export function assertDefined<T>(value: T | undefined, message: string | (() => string)): T {
if (value == undefined) {
throw new Error(typeof message === 'string' ? message : message());
}
return value;
}