flow2schema/src/utils.js

34 lines
681 B
JavaScript
Raw Normal View History

2018-07-25 08:38:10 +03:00
// @flow
2017-11-18 12:38:34 +03:00
import * as assert from 'assert';
2017-11-21 15:28:59 +03:00
// I so much dream about the user guards...
// @see flow#112.
export const invariant = assert.ok;
2017-11-28 17:42:19 +03:00
export function last<T>(list: T[]): T {
invariant(list.length > 0);
return list[list.length - 1];
2017-11-21 15:28:59 +03:00
}
2017-12-16 17:03:08 +03:00
export function collect<T>(iter: Iterable<[string, T]>): {[string]: T} {
const result = {};
for (const [key, value] of iter) {
result[key] = value;
}
return result;
}
export function partition<T>(iter: Iterable<T>, pred: T => boolean): [T[], T[]] {
const [left, right] = [[], []];
for (const item of iter) {
(pred(item) ? left : right).push(item);
}
return [left, right];
}