1 |
|
2 |
|
3 |
|
4 |
|
5 | var B = {};
|
6 |
|
7 | B.chunk = function() {}
|
8 | B.curry = function(f,o) {
|
9 | return function() {
|
10 | var args = Array.prototype.slice.call(arguments);
|
11 | return f.apply(null, [o].concat(args));
|
12 | }
|
13 | }
|
14 |
|
15 | B.curryThis = function(f) {
|
16 | return function() {
|
17 | var args = Array.prototype.slice.call(arguments);
|
18 | return f.apply(null, [this].concat(args));
|
19 | }
|
20 | }
|
21 |
|
22 | B.steps = function(from, to, step) {
|
23 | step=step || 1;
|
24 | var a=[];
|
25 | for (var t=from; t<=to; t+=step)
|
26 | a.push(t);
|
27 | return a;
|
28 | }
|
29 |
|
30 | B.calls = function() {
|
31 | var args = Array.prototype.slice.call(arguments);
|
32 | var n = args[0];
|
33 | var f = args[1];
|
34 | var params = args.slice(2);
|
35 | var a=[];
|
36 | for (var i=0; i<n; i++)
|
37 | a.push(f.apply(null, params));
|
38 | return a;
|
39 | }
|
40 |
|
41 |
|
42 | B.mix=function(o, map, options) {
|
43 | options = options || {};
|
44 | for (var name in map) {
|
45 | f = map[name];
|
46 | if (options.thisify) {
|
47 | f = B.curryThis(f);
|
48 | }
|
49 | if (typeof o[name] === 'undefined' || options.override) {
|
50 | if (options.override) {
|
51 | console.log("B.mix:", name, " overrided!");
|
52 | }
|
53 | Object.defineProperty(o, name, {
|
54 | enumerable: options.enumerable || false,
|
55 | writable: true,
|
56 | value: f,
|
57 | });
|
58 | } else if (typeof o[name] !== 'undefined') {
|
59 | console.log("B.mix:", name, " skipped! f=", f);
|
60 | }
|
61 | }
|
62 | }
|
63 |
|
64 | B.fmix=function(o, map) { B.mix(o, map, {thisify:true}); }
|
65 |
|
66 | B.ctrim=function(s, set, side) {
|
67 | side = side||"both";
|
68 | for (var b=0; b<s.length; b++)
|
69 | if (set.indexOf(s[b])<0) break;
|
70 | for (var e=s.length-1; e>=0; e--)
|
71 | if (set.indexOf(s[e])<0) break;
|
72 | if (side === "right") b=0;
|
73 | if (side === "left") e=s.length-1;
|
74 | return s.substring(b, e+1);
|
75 | }
|
76 |
|
77 | B.lpad=function(s, width, ch) {
|
78 | return s.length >= width ? s : new Array(width - s.length + 1).join(ch) + s;
|
79 | }
|
80 |
|
81 | module.exports = B;
|
82 |
|