Skip to content

Commit 265a0ed

Browse files
committed
Merge branch 'HEAD' of git@github.com:kvz/phpjs.git
2 parents e1f10b6 + 1cef931 commit 265a0ed

File tree

11 files changed

+92
-85
lines changed

11 files changed

+92
-85
lines changed
Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,39 @@
11
<?php
22
Class PHPJS_Library_Tester_Shell extends PHPJS_Library_Tester {
33
protected $_fleRealRhino = "/usr/bin/rhino";
4-
4+
55
public function getFleRealRhino() {
6+
if (file_exists($r = $this->_fleRealRhino)) {
7+
return $r;
8+
}
9+
if (file_exists($r = '/usr/local/bin/rhino')) {
10+
return $r;
11+
}
12+
613
return $this->_fleRealRhino;
714
}
815

916
public function getFleRealJsLint() {
1017
return $this->_dirRealTool.'/jslint.js';
1118
}
12-
19+
1320
public function getAllowedCmdArgs() {
1421
$posArgs = array();
1522
$posArgs["func"] = "--func";
1623
$posArgs["from"] = "--from";
1724
$posArgs["output"] = "--output";
1825
$posArgs["testcode"] = "--testcode";
1926
$posArgs["verbose"] = "-v";
20-
return $posArgs;
27+
return $posArgs;
2128
}
22-
29+
2330
public function parseCmdArgs($argv) {
2431
$posArgs = $this->getAllowedCmdArgs();
25-
32+
2633
if (isset($argv[1]) && isset($this->Functions[$argv[1]])) {
2734
$options["func"] = $argv[1];
2835
}
29-
36+
3037
$options = array();
3138
foreach($argv as $i=>$arg) {
3239
if (($option = array_search($arg, $posArgs)) !== false) {
@@ -37,10 +44,10 @@ public function parseCmdArgs($argv) {
3744
}
3845
}
3946
}
40-
41-
47+
48+
4249
return $options;
4350
}
4451

4552
}
46-
?>
53+
?>

_tools/phpjstest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@
3333
// Convert a path to plain function name
3434
$funcName = basename($funcName, ".js");
3535
}
36-
36+
3737
if (!$PHPJS_Tester_Shell->functionExists($funcName)) {
3838
die("Function '".$funcName."' does not exist\n");
3939
} else {
4040
$Function = $PHPJS_Tester_Shell->getFunction($funcName);
41-
}
41+
}
4242
} else {
4343
$mode = $arg1;
4444
$funcName = $arg2;
@@ -57,12 +57,12 @@
5757
echo "source file: ". $Function->getRealPath()."\n";
5858
echo "tester file: ". $Function->getTesterPath()."\n";
5959
echo "\n\n";
60-
60+
6161
echo "Examples\n";
6262
echo $hr;
6363
print_r($Function->DocBlock->getExamples());
6464
echo "\n\n";
65-
65+
6666
echo "Dependencies\n";
6767
echo $hr;
6868
print_r($Function->getDependencies());
@@ -72,7 +72,7 @@
7272
echo $hr;
7373
$Function->testFunction(true);
7474
echo "\n\n";
75-
75+
7676
echo "Test result\n";
7777
echo $hr;
7878
$Function->testFunction(false);
@@ -112,4 +112,4 @@
112112
die("Unknown command: ".$mode);
113113
break;
114114
}
115-
?>
115+
?>

functions/classobj/get_class.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function get_class (obj) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Ates Goral (http://magnetiq.com)
44
// + improved by: David James
5+
// + improved by: David Neilsen
56
// * example 1: get_class(new (function MyClass() {}));
67
// * returns 1: "MyClass"
78
// * example 2: get_class({});
@@ -15,10 +16,11 @@ function get_class (obj) {
1516
// * example 6: get_class(function MyFunction() {});
1617
// * returns 6: false
1718
if (obj && typeof obj === 'object' &&
18-
Object.prototype.toString.call(obj) !== '[object Array]' && obj.constructor && obj != this.window) {
19+
Object.prototype.toString.call(obj) !== '[object Array]' &&
20+
obj.constructor && obj !== this.window) {
1921
var arr = obj.constructor.toString().match(/function\s*(\w+)/);
2022

21-
if (arr && arr.length == 2) {
23+
if (arr && arr.length === 2) {
2224
return arr[1];
2325
}
2426
}

functions/datetime/date.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function date (format, timestamp) {
9090
},
9191
S: function () { // Ordinal suffix for day of month; st, nd, rd, th
9292
var j = f.j();
93-
return j > 4 && j < 21 ? 'th' : {1: 'st', 2: 'nd', 3: 'rd'}[j % 10] || 'th';
93+
return j < 4 | j > 20 && ['st', 'nd', 'rd'][j%10 - 1] || 'th';
9494
},
9595
w: function () { // Day of week; 0[Sun]..6[Sat]
9696
return jsdate.getDay();
@@ -127,7 +127,8 @@ function date (format, timestamp) {
127127

128128
// Year
129129
L: function () { // Is leap year?; 0 or 1
130-
return new Date(f.Y(), 1, 29).getMonth() === 1 | 0;
130+
var j = f.Y();
131+
return j%4==0 & j%100!=0 | j%400==0;
131132
},
132133
o: function () { // ISO-8601 year
133134
var n = f.n(),
@@ -248,12 +249,12 @@ function date (format, timestamp) {
248249
return 'D, d M Y H:i:s O'.replace(formatChr, formatChrCb);
249250
},
250251
U: function () { // Seconds since UNIX epoch
251-
return jsdate.getTime() / 1000 | 0;
252+
return jsdate / 1000 | 0;
252253
}
253254
};
254255
this.date = function (format, timestamp) {
255256
that = this;
256-
jsdate = ((typeof timestamp === 'undefined') ? new Date() : // Not provided
257+
jsdate = (timestamp == null ? new Date() : // Not provided
257258
(timestamp instanceof Date) ? new Date(timestamp) : // JS Date()
258259
new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
259260
);

functions/datetime/strtotime.js

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@ function strtotime (str, now) {
1616
// * returns 3: 1127041200
1717
// * example 4: strtotime('2009-05-04 08:30:00');
1818
// * returns 4: 1241418600
19-
var i, match, s, strTmp = '',
20-
parse = '';
19+
var i, l, match, s, parse = '';
2120

22-
strTmp = str;
23-
strTmp = strTmp.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
24-
strTmp = strTmp.replace(/[\t\r\n]/g, ''); // unecessary chars
25-
if (strTmp == 'now') {
26-
return (new Date()).getTime() / 1000; // Return seconds, not milli-seconds
27-
} else if (!isNaN(parse = Date.parse(strTmp))) {
28-
return (parse / 1000);
21+
str = str.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
22+
str = str.replace(/[\t\r\n]/g, ''); // unecessary chars
23+
if (str === 'now') {
24+
return now === null || isNaN(now) ? new Date().getTime() / 1000 | 0 : now | 0;
25+
} else if (!isNaN(parse = Date.parse(str))) {
26+
return parse / 1000 | 0;
2927
} else if (now) {
3028
now = new Date(now * 1000); // Accept PHP-style seconds
3129
} else {
3230
now = new Date();
3331
}
3432

35-
strTmp = strTmp.toLowerCase();
33+
str = str.toLowerCase();
3634

3735
var __is = {
3836
day: {
@@ -44,36 +42,33 @@ function strtotime (str, now) {
4442
'fri': 5,
4543
'sat': 6
4644
},
47-
mon: {
48-
'jan': 0,
49-
'feb': 1,
50-
'mar': 2,
51-
'apr': 3,
52-
'may': 4,
53-
'jun': 5,
54-
'jul': 6,
55-
'aug': 7,
56-
'sep': 8,
57-
'oct': 9,
58-
'nov': 10,
59-
'dec': 11
60-
}
45+
mon: [
46+
'jan',
47+
'feb',
48+
'mar',
49+
'apr',
50+
'may',
51+
'jun',
52+
'jul',
53+
'aug',
54+
'sep',
55+
'oct',
56+
'nov',
57+
'dec'
58+
]
6159
};
6260

6361
var process = function (m) {
64-
var ago = (m[2] && m[2] == 'ago');
65-
var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);
66-
62+
var ago = (m[2] && m[2] === 'ago');
63+
var num = (num = m[0] === 'last' ? -1 : 1) * (ago ? -1 : 1);
64+
6765
switch (m[0]) {
6866
case 'last':
6967
case 'next':
7068
switch (m[1].substring(0, 3)) {
7169
case 'yea':
7270
now.setFullYear(now.getFullYear() + num);
7371
break;
74-
case 'mon':
75-
now.setMonth(now.getMonth() + num);
76-
break;
7772
case 'wee':
7873
now.setDate(now.getDate() + (num * 7));
7974
break;
@@ -89,22 +84,29 @@ function strtotime (str, now) {
8984
case 'sec':
9085
now.setSeconds(now.getSeconds() + num);
9186
break;
87+
case 'mon':
88+
if (m[1] === "month") {
89+
now.setMonth(now.getMonth() + num);
90+
break;
91+
}
92+
// fall through
9293
default:
93-
var day;
94-
if (typeof(day = __is.day[m[1].substring(0, 3)]) != 'undefined') {
94+
var day = __is.day[m[1].substring(0, 3)];
95+
if (typeof day !== 'undefined') {
9596
var diff = day - now.getDay();
96-
if (diff == 0) {
97+
if (diff === 0) {
9798
diff = 7 * num;
9899
} else if (diff > 0) {
99-
if (m[0] == 'last') {
100+
if (m[0] === 'last') {
100101
diff -= 7;
101102
}
102103
} else {
103-
if (m[0] == 'next') {
104+
if (m[0] === 'next') {
104105
diff += 7;
105106
}
106107
}
107108
now.setDate(now.getDate() + diff);
109+
now.setHours(0, 0, 0, 0); // when jumping to a specific last/previous day of week, PHP sets the time to 00:00:00
108110
}
109111
}
110112
break;
@@ -144,8 +146,8 @@ function strtotime (str, now) {
144146
return true;
145147
};
146148

147-
match = strTmp.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);
148-
if (match != null) {
149+
match = str.match(/^(\d{2,4}-\d{2}-\d{2})(?:\s(\d{1,2}:\d{2}(:\d{2})?)?(?:\.(\d+))?)?$/);
150+
if (match !== null) {
149151
if (!match[2]) {
150152
match[2] = '00:00:00';
151153
} else if (!match[3]) {
@@ -154,29 +156,25 @@ function strtotime (str, now) {
154156

155157
s = match[1].split(/-/g);
156158

157-
for (i in __is.mon) {
158-
if (__is.mon[i] == s[1] - 1) {
159-
s[1] = i;
160-
}
161-
}
162-
s[0] = parseInt(s[0], 10);
159+
s[1] = __is.mon[s[1] - 1] || s[1];
160+
s[0] = +s[0];
163161

164162
s[0] = (s[0] >= 0 && s[0] <= 69) ? '20' + (s[0] < 10 ? '0' + s[0] : s[0] + '') : (s[0] >= 70 && s[0] <= 99) ? '19' + s[0] : s[0] + '';
165163
return parseInt(this.strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2]) + (match[4] ? match[4] / 1000 : ''), 10);
166164
}
167165

168166
var regex = '([+-]?\\d+\\s' + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?' + '|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday' + '|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday)' + '|(last|next)\\s' + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?' + '|sun\\.?|sunday|mon\\.?|monday|tue\\.?|tuesday|wed\\.?|wednesday' + '|thu\\.?|thursday|fri\\.?|friday|sat\\.?|saturday))' + '(\\sago)?';
169167

170-
match = strTmp.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
171-
if (match == null) {
168+
match = str.match(new RegExp(regex, 'gi')); // Brett: seems should be case insensitive per docs, so added 'i'
169+
if (match === null) {
172170
return false;
173171
}
174172

175-
for (i = 0; i < match.length; i++) {
173+
for (i = 0, l = match.length; i < l; i++) {
176174
if (!process(match[i].split(' '))) {
177175
return false;
178176
}
179177
}
180178

181-
return (now.getTime() / 1000);
182-
}
179+
return now.getTime() / 1000 | 0;
180+
}

functions/strings/money_format.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ function money_format (format, number) {
8686
if (mon_grouping[0] < integer.length) {
8787
for (var i = 0, idx = integer.length; i < mon_grouping.length; i++) {
8888
idx -= mon_grouping[i]; // e.g., 3
89-
if (idx < 0) {
89+
if (idx <= 0) {
9090
break;
9191
}
9292
if (filler && idx < fillnum) {

functions/strings/str_shuffle.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,23 @@ function str_shuffle (str) {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// * example 1: shuffled = str_shuffle("abcdef");
55
// * results 1: shuffled.length == 6
6-
if (str == undefined) {
6+
if (arguments.length === 0) {
77
throw 'Wrong parameter count for str_shuffle()';
88
}
9+
10+
if (str == null) {
11+
return '';
12+
}
13+
14+
str += '';
915

10-
var getRandomInt = function (max) {
11-
return Math.floor(Math.random() * (max + 1));
12-
};
13-
var newStr = '',
14-
rand = 0;
16+
var newStr = '', rand, i = str.length;
1517

16-
while (str.length) {
17-
rand = getRandomInt(str.length - 1);
18+
while (i) {
19+
rand = Math.floor(Math.random() * i);
1820
newStr += str.charAt(rand);
1921
str = str.substring(0, rand) + str.substr(rand + 1);
22+
i--;
2023
}
2124

2225
return newStr;

functions/url/base64_decode.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ function base64_decode (data) {
99
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
1010
// + input by: Brett Zamir (http://brett-zamir.me)
1111
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
12-
// - depends on: utf8_decode
1312
// * example 1: base64_decode('S2V2aW4gdmFuIFpvbm5ldmVsZA==');
1413
// * returns 1: 'Kevin van Zonneveld'
1514
// mozilla has this native
@@ -51,7 +50,6 @@ function base64_decode (data) {
5150
} while (i < data.length);
5251

5352
dec = tmp_arr.join('');
54-
dec = this.utf8_decode(dec);
5553

5654
return dec;
5755
}

0 commit comments

Comments
 (0)