1 |
|
2 | (function() {
|
3 | var Method, assert, async, benchmark, blank, curry, deepEqual, empty, isArray, isFunction, isGenerator, isObject, isString, length, memoize, promise, ref, ref1, ref2, sleep, timer, times;
|
4 |
|
5 | curry = require("fairmont-core").curry;
|
6 |
|
7 | Method = require("fairmont-multimethods").Method;
|
8 |
|
9 | ref = require("./type"), isFunction = ref.isFunction, isGenerator = ref.isGenerator;
|
10 |
|
11 | ref1 = require("./promise"), promise = ref1.promise, async = ref1.async;
|
12 |
|
13 | ref2 = require("./type"), isArray = ref2.isArray, isString = ref2.isString, isObject = ref2.isObject;
|
14 |
|
15 | blank = require("./string").blank;
|
16 |
|
17 | memoize = (function(_hash) {
|
18 | _hash = function(x) {
|
19 | return x.toString();
|
20 | };
|
21 | return function(fn, hash, memo) {
|
22 | if (hash == null) {
|
23 | hash = _hash;
|
24 | }
|
25 | if (memo == null) {
|
26 | memo = {};
|
27 | }
|
28 | return function(x) {
|
29 | var name;
|
30 | return memo[name = hash(x)] != null ? memo[name] : memo[name] = fn(x);
|
31 | };
|
32 | };
|
33 | })(void 0);
|
34 |
|
35 | timer = function(wait, action) {
|
36 | var id;
|
37 | id = setTimeout(action, wait);
|
38 | return function() {
|
39 | return clearTimeout(id);
|
40 | };
|
41 | };
|
42 |
|
43 | sleep = function(interval) {
|
44 | return promise(function(resolve, reject) {
|
45 | return timer(interval, function() {
|
46 | return resolve();
|
47 | });
|
48 | });
|
49 | };
|
50 |
|
51 | times = curry(function(fn, n) {
|
52 | var results;
|
53 | results = [];
|
54 | while (n-- !== 0) {
|
55 | results.push(fn());
|
56 | }
|
57 | return results;
|
58 | });
|
59 |
|
60 | benchmark = Method.create();
|
61 |
|
62 | Method.define(benchmark, isFunction, function(fn) {
|
63 | var start;
|
64 | start = Date.now();
|
65 | fn();
|
66 | return Date.now() - start;
|
67 | });
|
68 |
|
69 | Method.define(benchmark, isGenerator, function*(fn) {
|
70 | var start;
|
71 | start = Date.now();
|
72 | (yield fn());
|
73 | return Date.now() - start;
|
74 | });
|
75 |
|
76 | empty = function(x) {
|
77 | return x.length === 0;
|
78 | };
|
79 |
|
80 | length = function(x) {
|
81 | return x.length;
|
82 | };
|
83 |
|
84 | assert = require("assert");
|
85 |
|
86 | deepEqual = function(a, b) {
|
87 | var error;
|
88 | try {
|
89 | assert.deepEqual(a, b);
|
90 | return true;
|
91 | } catch (error) {
|
92 | return false;
|
93 | }
|
94 | };
|
95 |
|
96 | module.exports = {
|
97 | times: times,
|
98 | sleep: sleep,
|
99 | timer: timer,
|
100 | memoize: memoize,
|
101 | times: times,
|
102 | benchmark: benchmark,
|
103 | empty: empty,
|
104 | length: length,
|
105 | deepEqual: deepEqual
|
106 | };
|
107 |
|
108 | }).call(this);
|