@ faybl/ utils (英语)
Faybl服务和网络应用通用JavaScript.
npm install @faybl/utils
历史
fetchHistory 通过 faybl 客户端读取一个频道的历史为数组
上升时间,每点 ts 财务报告和审定财务报表 pl,这是图画.
import { fetchHistory, fetchHistoryLast } from '@faybl/utils';
const { data } = await fetchHistory({ client, rid, from, to });
const newest = await fetchHistoryLast({ client, rid });
决定 ascending 财务报告和审定财务报表 reverse 数据历史:
| 请求 | 回返 |
|---|---|
from,有或无 to | 范围,从开始 |
limit 财务报告和审定财务报表 from | 首个 limit 从点 from |
limit 无 from | 最新的 limit 点前 to 当给定时 |
| 无 | 从最古老的地方 |
samplingInterval, (中文(简体) ), downsamplingMethod 财务报告和审定财务报表 timeZone 被传递。 空
数值被忽略,没有其他数据历史.
fetchHistoryLast 是最新的一点,之前 to 提供时,或 undefined。 。 。 .
等待时
waitFor 调用一个功能,直到它的承诺解决
数值。 这是服务如何在开始时等待 一个依赖它的
自己,记录每个尝试而不是退出.
import { waitFor } from '@faybl/utils';
const storage = await waitFor(connectToDatabase, {
onRetry: (error, attempt) => logger.warn(`Database not reachable, attempt ${attempt}:`, error),
});
| 选项 | 默认 | 它会做什么 |
|---|---|---|
interval | 5000块 | 尝试之间的毫秒 |
retryIf | 永远是真的 | 被拒绝; 假传去而不是再试一次 |
onRetry | 无所作为 | 由于拒绝和尝试的号码, 在等待之前 |
对尝试没有限制.
delay(milliseconds, signal?) 在时间之后解决。 中止 signal
清除计时器并拒绝信号的理由.
await delay(1000);
await delay(1000, AbortSignal.timeout(500)); // rejects after 500 ms
路径和选择
pathStr('pl.temp')(data) 读取一个点分隔的路径,例如一个用户类型;
数组索引是路径中的数字('list.0') (中文(简体) ). 一条缺失的路径和一条 null
(或 NaN() 两者的数值 undefined。 。 。 .
pickRandom(items) 是随机项目,或 undefined 没有。 这需要它
随机性作为测试的第二个参数: pickRandom(items, () => 0)。 。 。 .
剪贴板
copyText(text) 写入系统剪贴板并解析为结果 : Ok, (中文(简体) ),
或者说 Err({ kind: 'ClipboardWriteFailed', cause }) 当没有剪贴板时,
权限被拒绝或页面没有焦点。 从点击处理器调用
在任何时间之前 await,或浏览器拒绝写入.
match(({ cause }) => console.warn('Copy by hand:', cause))(() => setCopied(true))(
await copyText(url),
);
结果
预期失败作为值: 简单、可序列的数据, Ok(value) =:
{ ok: true, value } 或者说 Err(error) =: { ok: false, error }组合
map 编号 : chain 编号 : mapErr 从此软件包( 从不) R.map 编号 : R.chain: 拉姆达
地图 a 结果的字段为平面对象, 收集列表 all (第一届)
Err 和在边缘解开 match(onErr)(onOk)。 。 。 .
import * as R from 'ramda';
import { Err, Ok, chain, fromNil, match, tryCatchAsync } from '@faybl/utils';
const fetchJson = tryCatchAsync(async (url) => Ok(await (await fetch(url)).json()))((cause, url) =>
Err({ kind: 'FetchFailed', url, cause }),
);
const getPort = R.pipe(R.path(['server', 'port']), fromNil({ kind: 'MissingPort' }));
const port = chain(getPort)(await fetchJson(url));
match(({ kind }) => console.error(kind))(console.log)(port);
在边界上环绕外方抛出者 R.tryCatch (同步)或
tryCatchAsync (同声. 当打包的电话也可以发出错误时
非预期失败, 使用 catchWhen(isExpected)(toErr) 作为捕捉者:
将错误匹配到一个 Err 并重新扔任何东西 没有改变,所以
缺陷仍在崩溃.
const readText = tryCatchAsync(async (path) => Ok(await readFile(path, 'utf8')))(
catchWhen((e) => e?.code === 'ENOENT')((cause, path) =>
Err({ kind: 'FileNotFound', path, cause }),
),
);
频道
channelsOfParent(parent)(records) 保存频道记录资产或东西
清单 channels (名称) 清除,每个与 nameFromParent; 缺少
ưμ㼯a. recordsToSelectOptions 将频道记录转换为
{ id: rid, label } 选项.
名称和模拟数据
haiku() 是一个随机的双字标题, 如 Misty creek。 。 。 . mockHistory() 实值
过去10秒的10分 形状像数据历史反应
没有后端的应用程序运行; mockHistoryEmpty() 没有。 都带上
随机性(和 mockHistory 联合国 now作为测试的论据.