forked from octokit/octokit.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.js
140 lines (129 loc) · 4.67 KB
/
util.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
/** section: github
* class Util
*
* Copyright 2012 Cloud9 IDE, Inc.
*
* This product includes software developed by
* Cloud9 IDE, Inc (http://c9.io).
*
* Author: Mike de Boer <[email protected]>
**/
var Util = require("util");
/**
* Util#extend(dest, src, noOverwrite) -> Object
* - dest (Object): destination object
* - src (Object): source object
* - noOverwrite (Boolean): set to `true` to overwrite values in `src`
*
* Shallow copy of properties from the `src` object to the `dest` object. If the
* `noOverwrite` argument is set to to `true`, the value of a property in `src`
* will not be overwritten if it already exists.
**/
exports.extend = function(dest, src, noOverwrite) {
for (var prop in src) {
if (!noOverwrite || typeof dest[prop] == "undefined")
dest[prop] = src[prop];
}
return dest;
};
/**
* Util#escapeRegExp(str) -> String
* - str (String): string to escape
*
* Escapes characters inside a string that will an error when it is used as part
* of a regex upon instantiation like in `new RegExp("[0-9" + str + "]")`
**/
exports.escapeRegExp = function(str) {
return str.replace(/([.*+?^${}()|[\]\/\\])/g, '\\$1');
};
/**
* Util#toCamelCase(str, [upper]) -> String
* - str (String): string to transform
* - upper (Boolean): set to `true` to transform to CamelCase
*
* Transform a string that contains spaces or dashes to camelCase. If `upper` is
* set to `true`, the string will be transformed to CamelCase.
*
* Example:
*
* Util.toCamelCase("why U no-work"); // returns 'whyUNoWork'
* Util.toCamelCase("I U no-work", true); // returns 'WhyUNoWork'
**/
exports.toCamelCase = function(str, upper) {
str = str.toLowerCase().replace(/(?:(^.)|(\s+.)|(-.))/g, function(match) {
return match.charAt(match.length - 1).toUpperCase();
});
if (upper)
return str;
return str.charAt(0).toLowerCase() + str.substr(1);
};
/**
* Util#isTrue(c) -> Boolean
* - c (mixed): value the variable to check. Possible values:
* true The function returns true.
* 'true' The function returns true.
* 'on' The function returns true.
* 1 The function returns true.
* '1' The function returns true.
*
* Determines whether a string is true in the html attribute sense.
**/
exports.isTrue = function(c){
return (c === true || c === "true" || c === "on" || typeof c == "number" && c > 0 || c === "1");
};
/**
* Util#isFalse(c) -> Boolean
* - c (mixed): value the variable to check. Possible values:
* false The function returns true.
* 'false' The function returns true.
* 'off' The function returns true.
* 0 The function returns true.
* '0' The function returns true.
*
* Determines whether a string is false in the html attribute sense.
**/
exports.isFalse = function(c){
return (c === false || c === "false" || c === "off" || c === 0 || c === "0");
};
var levels = {
"info": ["\033[90m", "\033[39m"], // grey
"error": ["\033[31m", "\033[39m"], // red
"fatal": ["\033[35m", "\033[39m"], // magenta
"exit": ["\033[36m", "\033[39m"] // cyan
};
var _slice = Array.prototype.slice;
/**
* Util#log(arg1, [arg2], [type]) -> null
* - arg1 (mixed): messages to be printed to the standard output
* - type (String): type denotation of the message. Possible values:
* 'info', 'error', 'fatal', 'exit'. Optional, defaults to 'info'.
*
* Unified logging to the console; arguments passed to this function will put logged
* to the standard output of the current process and properly formatted.
* Any non-String object will be inspected by the NodeJS util#inspect utility
* function.
* Messages will be prefixed with its type (with corresponding font color), like so:
*
* [info] informational message
* [error] error message
* [fatal] fatal error message
* [exit] program exit message (not an error)
*
* The type of message can be defined by passing it to this function as the last/
* final argument. If the type can not be found, this last/ final argument will be
* regarded as yet another message.
**/
exports.log = function() {
var args = _slice.call(arguments);
var lastArg = args[args.length - 1];
var level = levels[lastArg] ? args.pop() : "info";
if (!args.length)
return;
var msg = args.map(function(arg) {
return typeof arg != "string" ? Util.inspect(arg) : arg;
}).join(" ");
var pfx = levels[level][0] + "[" + level + "]" + levels[level][1];
msg.split("\n").forEach(function(line) {
console.log(pfx + " " + line);
});
};