1 | {curry} = require "fairmont-core"
|
2 | {Method} = require "fairmont-multimethods"
|
3 | {isFunction, isGenerator} = require "./type"
|
4 | {promise, async} = require "./promise"
|
5 | {isArray, isString, isObject} = require "./type"
|
6 | {blank} = require "./string"
|
7 |
|
8 |
|
9 | memoize = do (_hash = undefined) ->
|
10 | _hash = (x) -> x.toString()
|
11 | (fn, hash = _hash, memo = {}) ->
|
12 | (x) -> memo[hash x] ?= fn x
|
13 |
|
14 | timer = (wait, action) ->
|
15 | id = setTimeout(action, wait)
|
16 | -> clearTimeout( id )
|
17 |
|
18 | sleep = (interval) ->
|
19 | promise (resolve, reject) ->
|
20 | timer interval, -> resolve()
|
21 |
|
22 | times = curry (fn, n) -> fn() until n-- == 0
|
23 |
|
24 | benchmark = Method.create()
|
25 |
|
26 | Method.define benchmark, isFunction, (fn) ->
|
27 | start = Date.now()
|
28 | fn()
|
29 | Date.now() - start
|
30 |
|
31 | Method.define benchmark, isGenerator, (fn) ->
|
32 | start = Date.now()
|
33 | yield fn()
|
34 | Date.now() - start
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 | empty = (x) -> x.length == 0
|
45 | length = (x) -> x.length
|
46 |
|
47 | assert = require "assert"
|
48 | deepEqual = (a, b) ->
|
49 | try
|
50 | assert.deepEqual a, b
|
51 | true
|
52 | catch
|
53 | false
|
54 |
|
55 | module.exports = {times, sleep, timer, memoize,
|
56 | times, benchmark, empty, length, deepEqual}
|