feat(utils): add helpers

This commit is contained in:
George Lima 2019-03-17 00:18:40 -03:00
parent 7409264b15
commit cb8749d6be
3 changed files with 36 additions and 0 deletions

14
app/utils/async-filter.js Normal file
View File

@ -0,0 +1,14 @@
// @flow
export const asyncFilter = async (array: any[], fn: any => Promise<boolean>): Promise<any[]> => {
const result = [];
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
for (const cur of array) {
if (await fn(cur)) result.push(cur);
}
return result;
};

14
app/utils/async-map.js Normal file
View File

@ -0,0 +1,14 @@
// @flow
export const asyncMap = async (array: any[], fn: any => Promise<any>): Promise<any[]> => {
const result = [];
/* eslint-disable no-restricted-syntax */
/* eslint-disable no-await-in-loop */
for (const cur of array) {
result.push(await fn(cur));
}
return result;
};

8
app/utils/is-hex.js Normal file
View File

@ -0,0 +1,8 @@
// @flow
const HEX_REGEX = /([0-9]|[a-f])/gim;
export const isHex = (input: ?string) => {
if (!input) return false;
return (input.match(HEX_REGEX) || []).length === input.length;
};