-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
86 lines (80 loc) · 2.12 KB
/
Copy pathtools.js
File metadata and controls
86 lines (80 loc) · 2.12 KB
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
(function(exports)
{
exports.rangeOfPart = function(rangeToSplit, partsCount, idx)
{
// here is a bug somewhere!
var begin = rangeToSplit.begin
var end = rangeToSplit.end
var rangeLen = end-begin
var partLen = rangeLen / partsCount
return {
begin: Math.floor(begin + idx * partLen),
end: Math.floor(begin + (idx+1) * partLen) - 1
}
}
Object.defineProperty(Object.prototype, 'forEach',
{
value: function(visit)
{
var idx = 0
for (var key in this)
visit(idx++, key, this[key])
},
writable: true,
configurable: true,
enumerable: false
})
Object.defineProperty(Array.prototype, 'without',
{
value: function(toRemove)
{
if (!toRemove)
return this
return this.filter(function(i) { return toRemove.indexOf(i) === -1 })
},
writable: true,
configurable: true,
enumerable: false
})
Object.defineProperty(Object.prototype, 'merge',
{
value: function(other)
{
var dest = this
other.forEach(function(idx, id, v)
{
dest[id] = v
})
return this
},
writable: true,
configurable: true,
enumerable: false
})
Object.defineProperty(Number.prototype, 'toSubscript',
{
value: function()
{
var result = ''
var str = this.toString()
for (var i = 0; i < str.length; i++)
result += String.fromCharCode(8320 + Number(str.charAt(i)))
return result
},
writable: true,
configurable: true,
enumerable: false
})
exports.findOrCreateHtmlElement = function(id, parent, factory)
{
var elem = document.getElementById(id)
if (!elem)
{
elem = factory()
elem.id = id
$(parent).append(elem)
}
return elem
}
})
(typeof exports === 'undefined' ? this['tools']={} : exports)