函数公用
一套每个人都需要的功能功能.
安装
npm install @applitools/functional-commons
使用量
使用示例 range数字 :
const {range} = require('@applitools/functional-commons')
for (const i of range(0, 3)) {
console.log(i)
}
// ==> outputs 0, 1, 2
API 密码
range(from, to, [step])
返回数组 from.to - 1 (跳过) step 数字)
from (数目)
数组中的第一个数字
to (数目)
数组中最后一个数字将小于to
step (数目)
创建数组中的数字时要跳过的三角形( 默认为 1)
示例
range(4, 7) // ==> [4, 5, 6]
range(4, 11, 2) // ==> [4, 6, 8, 10]
cacheFunctionSync(f)
返回一个签名与 f,但根据通过的论点,哪个可以记住返回值
f
任何职能。 提交该职能的论据是: JSON.string- 装作可以缓冲.
返回函数
该函数的签名与 f,它会返回相同的返回值,但用同样的参数反复调用此函数将只生成一个调用到 f。 。 。 .
示例
function square(x) {
console.log('called')
return x ** 2
}
const memoizedSquare = cacheFunctionSync(square)
console.log(memoizedSquare(4)) // prints "called" and "16"
console.log(memoizedSquare(4)) // prints only "16"
console.log(memoizedSquare(5)) // prints "called" and "25"
cacheFunctionAsync(f, [options])
返回一个签名与 f,但根据通过的论点,哪个可以记住返回值. f 是一个async函数(返回 a) Promise(中文(简体) )
f
任何同步函数 。 提交该职能的论据是: JSON.string- 装作可以缓冲.
options
expireAfterMs
缓存值将在 expireAfterMs 毫秒长. 默认不会过期( N)udefined) (中文(简体) ).
nowService
(用于测试目的) 带有 now 自 Epoch 时间返回毫秒的函数。 默认只是 Date,使其以毫秒返回当前时间.
返回时
该函数的签名与 f,它会返回相同的返回值,但用同样的参数反复调用此函数将只生成一个调用到 f。 。 。 .
示例
async function square(x) {
console.log('called')
return x ** 2
}
const memoizedSquare = cacheFunctionAsync(square)
console.log(await memoizedSquare(4)) // prints "called" and "16"
console.log(await memoizedSquare(4)) // prints only "16"
console.log(await memoizedSquare(5)) // prints "called" and "25"
throws_(err)
丢出错误的函数 。 不错的地方,你想要一个箭头功能 只是扔.
err
任何类型。 通常继承自 Error虽然.
返回时
此函数永远不会返回 。 它总是扔.
objectFromEntries(entries)
转动数组 [key, value] 组合成对象.
entries
由2个值阵列组成的阵列(也叫"内入"),其中一对中的第一个值为键,二是值.
返回时
属性与给定的条目相对应的对象
示例
console.log(objectFromEntries([['a', 4, 'b' , 5]])) // prints {a: 4, b: 5}
mapObject(object, mapFunc)
返回新对象,输入对象中的所有条目都使用 mapFunc。 。 。 .
object
任何物体.
mapFunc
一个函数,将条目"pair"(第一个元素为属性键而第二个为值的二元素数组)映射到另一个条目.
返回时
对象(原型来自 Object)带有映射属性.
示例
console.log(mapObject({a: 4, b: 5}, ([key, value]) => [key.toUpperCase(), value * 2])) // prints {A: 8, B: 10}
mapValues(object, mapFunc)
返回新对象,其中输入对象中的所有值都使用 mapFunc。 。 。 .
object
任何物体.
mapFunc
将值映射到另一个值的函数
返回时
对象(原型来自 Object),有映射的地产价值.
示例
console.log(mapObject({a: 4, b: 5}, (value) => value * 2)) // prints {a: 8, b: 10}
filterKeys(object, filterFunc)
使用获取密钥的过滤函数过滤对象。 不可思议.
object
任何需要过滤属性的JavaScript对象.
filterFunc
接受属性名称并返回是否保留该属性的布尔函数( R)true 意味着保留财产)
返回时
一个新的对象(其原型是 Object属性与 object,减去过滤的.
示例
console.log(filterKeys({a: 4, b: 5}, key => key === 'b')) // prints {a: 4}
filterValues(object, filterFunc)
使用获得值的过滤函数过滤对象。 不可思议.
object
任何需要过滤属性的JavaScript对象.
filterFunc
接受属性值并返回是否保留属性的布尔函数( )true 意味着保留财产)
返回时
一个新的对象(其原型是 Object属性与 object,减去过滤的.
示例
console.log(filterKeys({a: 4, b: 5}, key => value === '5')) // prints {a: 4}
filterEntries(object, filterFunc)
使用获取条目的过滤函数过滤对象。 不可思议.
object
任何需要过滤属性的JavaScript对象.
filterFunc
接受[关键值,值]的 2 元素数组并返回是否保留该属性的布尔函数( R)true 意味着保留财产)
返回时
一个新的对象(其原型是 Object属性与 object,减去过滤的.
示例
console.log(filterKeys({a: 4, b: 5}, ([key, value]) => key === 'b' && value === 5)) // prints {a: 4}
failAfter(ms, errFactory)
掷出 errFactory() 之后 ms 毫秒
ms
抛出前等待的毫秒数
errFactory
一个函数,当调用时返回将抛出值
返回时
没什么 它总是扔
示例
try {
await failAfter(1000, () => new Error('oops'))
} catch(err) {
console.log(err.message)
}
// prints 'oops' after a second
presult(promise)
将诺言从已决或已决的约言变为已决的约言 返回错误和结果为 tuple, Scala/Go/Rust 样式.
promise
承诺要么解决 要么拒绝
返回时
承诺总是解决了两个问题:
- 如果口头承诺以
value,将决心[undefined, value] - 如果Ooginal承诺被拒绝
err,将决心[err, undefined]
示例
const [err, value] = await presult(Promise.resolve(4))
console.log(value) // prints '4'
const [err2, value2] = await presult(Promise.reject(new Error('oops')))
console.log(err2.message) // prints 'oops'
delay
延迟特定时间的同步函数
ms
延迟时间, 以毫秒计
返回时
返回时 undefined
示例
await delay(100) // waits for 100ms
ptimeoutWithValue(promiseOrPromiseFunc, timeout, value)
函数在给定的许诺和超时后返回的值之间相竞 .
promiseOrPromiseFunc
许诺,或有能回取许诺之能取之能取之能取也.
如果值是一个函数,它将被调用并传递一个函数(通常称为 hasAborted如果被调用,
如果超时已经发生,则返回正则,您可以中止此功能.
timeout
毫秒至超时数.
value
如果在承诺解决之前超时, 从此函数返回的值 .
示例
console.log(await ptimeoutWithValue(delay(10).then(_ => 43), 100, 42)) // prints "43"
console.log(await ptimeoutWithValue(delay(100).then(_ => 43), 10, 42)) // prints "42"
ptimeoutWithError(promiseOrPromiseFunc, timeout, error)
一个在给定承诺之间进行比赛的函数,并在超时后丢出例外.
promiseOrPromiseFunc
许诺,或有能回取许诺之能取之能取之能取也.
如果值是一个函数,它将被调用并传递一个函数(通常称为 hasAborted如果被调用,
如果超时已经发生,则返回正则,您可以中止此功能.
timeout
毫秒至超时数.
error
如果在承诺解决之前出现超时, 此函数将出现错误 .
示例
console.log(await ptimeoutWithError(delay(10).then(_ => 43), 100, new Error('oops'))) // prints "43"
console.log(await ptimeoutWithError(delay(100).then(_ => 43), 10, new Error('oops'))) // throws 'oops' error
ptimeoutWithFunction(promiseOrPromiseFunc, timeout, func)
函数在给定的许诺和超时后返回的值( 从函数创建) 之间进行比赛.
promiseOrPromiseFunc
许诺,或有能回取许诺之能取之能取之能取也.
如果值是一个函数,它将被调用并传递一个函数(通常称为 hasAborted如果被调用,
如果超时已经发生,则返回正则,您可以中止此功能.
timeout
毫秒至超时数.
func
如果超时, 要调用以获取返回值的async 函数
示例
console.log(await ptimeoutWithValue(delay(10).then(_ => 43), 100, 42)) // prints "43"
console.log(await ptimeoutWithValue(delay(100).then(_ => 43), 10, 42)) // prints "42"
makeError(error, properties)
将属性添加到错误中的函数
error
要修改的错误 。 注意此功能 哑弹 更改此错误.
若为 error 是一个字符串,然后是一个 Error 以此字符串作为消息创建.
properties
以可变方式添加到错误对象中的其他属性
返回时
这个 error 对象通过,或者如果 error 是一个字符串, 新生成的错误 .
示例
console.log(Object.entries(makeError('oops', {code: 'OOPS', status: 500}))) // prints {code: 'OOPS', status: 500}
zip(...arrays)
常规功能拉链数组.
arrays
要zip的数组列表
返回时
数组,即得到的数组的拉链.
示例
console.log(zip([1, 2, 3], [4, 5, 6])) // prints [[1, 4], [2, 5], [3, 6]]
平面图(阵列, [最大功能])
平整数组( 一级), 并绘制值
array
数组、数组或数组.
mapperFunction
将一个值映射到另一个值的函数 。 可选的.
返回时
阵列被平整( 只有一个关卡), 如果有一个 mapperFunction。 。 。 .
示例
console.log(flatMap([4, [5, 6], 7])) // prints [4, 5, 6, 7]
console.log(flatMap([4, [5, 6], 7], x => x * 2)) // prints [8, 10, 12, 14]
makeThrottledFunction(func, [options])
返回代理函数到 func除了叫它会把电话打到 func。 。 。 .
func
最初的功能.
`备选办法'
maxCallsPerTimeSpan
在时间段允许的最大通话次数 timeSpan。默认为120.
timeSpan
这个 timeSpan 我们的油门.
nowService
(用于测试目的) 带有 now 自 Epoch 时间返回毫秒的函数.
默认只是 Date,使其以毫秒返回当前时间.