-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang.js
78 lines (67 loc) · 1.62 KB
/
lang.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
define([
'dojo/_base/lang'
], function(lang){
// summary:
// The export of this module contains some common Dojo lang methods,
// generally using shorter names. It also contains some unique methods.
// returns: Object
//
// description:
// The methods provided and their maps to the Dojo equivalents are:
// lang.bind: lang.hitch
// lang.mix: lang.mixin
//
// TODO: http://millermedeiros.github.com/amd-utils/math.html
//
var
_uidMap = {},
uid = function(str){
str = str || "id";
if(!_uidMap[str]) _uidMap[str] = 0;
_uidMap[str]++;
return str+"_"+_uidMap[str];
};
return {
uid:
// summary:
// Returns a unique ID. Accepts and optional string for a
// prefix.
uid,
bind:
// summary:
// Renames dojo.hitch.
lang.hitch,
hitch:
// summary:
// Shortcut to dojo.hitch.
lang.hitch,
mix:
// summary:
// Shortened name for dojo.mixin.
lang.mixin,
last: function(/*Array*/array){
// summary:
// Returns the last element of an array.
return array[array.length -1];
},
minMax: function(/*Number*/num, /*Number*/n1, /*Number*/n2){
// summary:
// Returns the number if it is inbetween the min and the max.
// If it is over it returns the max, if under returns the min.
// Returns Number
//
// TODO: May rename to clamp()
//
var min, max;
if(n1 > n2){
min = n2; max = n1;
}else{
min = n1; max = n2;
}
if(num < min) return min;
if(num > max) return max;
return num;
return Math.max(num, Math.min(num, max));
}
};
});