-
Notifications
You must be signed in to change notification settings - Fork 1
/
lang.js
181 lines (163 loc) · 4.5 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
define([
], function(){
// summary:
// The export of this module contains some common language methods.
// A much more concise module compared to dojo.lang.
//
// returns: Object
//
// 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];
},
bind = function(ctx, func, dbg){
if(typeof(func) == "string"){
if(!func){ func = ctx; ctx = window; }
return function(){
ctx[func].apply(ctx, arguments); }
}else{
var method = !!func ? ctx.func || func : ctx;
var scope = !!func ? ctx : window;
return function(){ method.apply(scope, arguments); }
}
}
return {
uid:
// summary:
// Returns a unique ID. Accepts an optional string for a
// prefix.
uid,
bind:
// summary:
// Fixes the context of a function so "this" is correct.
bind,
hitch:
// summary:
// For those used to dojo.hitch.
bind,
last: function(/*Array*/array){
// summary:
// Returns the last element of an array.
return array[array.length -1];
},
copy: function(o){
// summary:
// Makes a shallow copy of an object
var o1 = {};
for(var nm in o){
if(typeof(o[nm]) == "object"){
o1[nm] = this.copy(o[nm])
}else{
o1[nm] = o[nm]
}
}
return o1;
},
isEmpty: function(o){
// summary:
// Check if an object has any properties,
// or if it's even there
if(!o) return true;
if(typeof o != 'object') return true;
if(o.length) return !!obj.length;
for(var nm in o) return false;
return true;
},
mix: function(/*Object*/source1, /*Object|Array*/source2, /*Object?*/opt){
// summary:
// Mixes two objects together.
// Warning: not deep!
// source1: Object
// Object to add properties and methods
// source2: Object|Array
// Object to get properties and methods from. If an array
// it is looped and mixed in order.
// opt: Object
// copy
// Make a copy of source1 before mixing
// notUndefined
// Don't mix properties if undefined in source1
opt = opt || {};
if(opt.copy) source1 = this.copy(source1);
var notUndefined = opt.notUndefined;
if(Array.isArray(source2)){
delete opt.copy; // already copied
source2.forEach(function(o){
this.mix(source1, o, opt);
}, this);
return source1;
}
for(var nm in source2){
if(notUndefined && source1[nm] === undefined) continue;
source1[nm] = source2[nm];
}
return source1;
},
clamp: 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));
},
timeCode: function(/*Number*/pos, /*String?*/format){
// summary:
// Converts a number into timecode, formatted as per the format
// option.
// NOTE:
// This may get moved to a date module (if more date methods
// are needed)
//
if (!isNaN(pos)){
var tc, hh, mm, ss, mmss = ".0";
if (Math.floor(pos) != pos){
mmss = (((pos - Math.floor(pos)).toString()).substring(1, 3));
pos = Math.floor(pos);
}
hh = Math.floor(pos / 3600);
mm = Math.floor((pos - hh * 3600) / 60);
ss = Math.floor(pos - (hh * 3600) - (mm * 60));
if (hh < 10) hh = "0" + hh;
if (mm < 10 && mm > 0) mm = "0" + mm;
if (ss < 10) ss = "0" + ss;
if(format == "h:m:s"){ // Silverlight madness
return hh + ":" + mm + ":" + ss;
}else if(format == "h:m:s.m"){
return hh + ":" + mm + ":" + ss + mmss;
}
// always return HH:MM:SS if greater than 1 hour.
if (pos > 3600){
tc = hh + ":" + mm + ":" + ss;// + mmss;
}else if (format == 'ss_ms'){
tc = mm + ":" + ss + mmss;
}else if (format == "mm_ss"){
tc = mm + ":" + ss;
}else{
tc = mm + ":" + ss + mmss;
}
}else{
//console.warn("toTimeCode failed: "+pos+" ::"+Number(pos))
return "0:00";
}
return tc;
}
};
});