-
Notifications
You must be signed in to change notification settings - Fork 26
/
tools.js
42 lines (35 loc) · 1.11 KB
/
tools.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
/*
---
name: tools.js
description: Augments prototypes with some useful functions
author: [Guillermo Rauch](http://devthought.com)
...
*/
// from jQuery (Dual licensed under the MIT and GPL licenses.)
// Copyright (c) 2009 John Resig
this.isArray = function(obj) {
return Object.prototype.toString.call(obj) === "[object Array]";
}
// from MooTools (MIT-style license)
// Copyright (c) 2009 Valerio Proietti
this.substitute = function(str, object, regexp){
return str.replace(regexp || (/\\?\{([^{}]+)\}/g), function(match, name){
if (match.charAt(0) == '\\') return match.slice(1);
return (object[name] != undefined) ? object[name] : '';
});
};
// dummy merge
this.merge = function(obj, newobj){
if (!newobj) return obj;
for (var i in newobj) obj[i] = newobj[i];
return obj;
};
// reads argv, parses --option=value into {option: value}
this.argvToObject = function(argv){
var obj = {}, regex = /\-\-(\w+)(\=(.+))?/;
for (var i = 0, l = argv.length, match; i < l; i++){
match = argv[i].match(regex);
if (match) obj[match[1]] = match[3] !== undefined ? eval("'"+match[3]+"'") : null;
}
return obj;
};