Skip to content

Commit

Permalink
feat(core): new utils.promise.array_filter function
Browse files Browse the repository at this point in the history
  • Loading branch information
wdavidw committed Mar 1, 2021
1 parent e89ada5 commit 8793cb3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
13 changes: 13 additions & 0 deletions packages/core/lib/utils/promise.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions packages/core/src/utils/promise.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@

module.exports =
array_filter: (arr, handler) ->
fail = Symbol()
(
await Promise.all arr.map (item) ->
if await handler(item) then item else fail
).filter (i) -> i isnt fail
is: (obj) ->
# promise && !!promise.then
return !!obj and (typeof obj is 'object' || typeof obj is 'function') and typeof obj.then is 'function'
18 changes: 14 additions & 4 deletions packages/core/test/utils/promise.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,19 @@ promise = require '../../src/utils/promise'

describe 'utils.promise', ->
return unless tags.api

describe 'array_filter', ->

it 'filter', ->
result = await promise.array_filter [1,2,3,4], (el) ->
new Promise (resolve) ->
setImmediate -> resolve el % 2 is 0
result.should.eql [2, 4]

describe 'is', ->

it 'true', ->
promise.is(new Promise (->)).should.be.true()
it 'true', ->
promise.is(new Promise (->)).should.be.true()

it 'false', ->
promise.is({}).should.be.false()
it 'false', ->
promise.is({}).should.be.false()

0 comments on commit 8793cb3

Please sign in to comment.