Skip to content

Commit a196bfe

Browse files
committed
Merge branch 'master' of github.com:kvz/phpjs
2 parents 0023116 + 4973a62 commit a196bfe

40 files changed

+329
-254
lines changed
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
function call_user_method(method, obj) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// + input by: dnukem
5+
// + improved by: Brett Zamir (http://brett-zamir.me)
46
// - depends on: Exception
57
// % note 1: Deprecated in PHP
6-
// * example 1: call_user_method('alert', 'this.window', 'Hello!');
8+
// * example 1: call_user_method('alert', this.window, 'Hello!');
79
// * returns 1: 'Hello!'
10+
// * example 2: call_user_method([this.window, 'alert'], 'Hello!');
11+
// * returns 2: 'Hello!'
812

9-
var func;
10-
func = eval(obj+"['"+method+"']");
13+
var func, object = obj, methodName = method, paramBegin = 2;
14+
if (method && typeof method === 'object' && method.length) {
15+
paramBegin = 1;
16+
object = method[0];
17+
object = typeof object === 'string' ? this.window[object] : object;
18+
methodName = method[1];
19+
}
20+
func = object[methodName];
1121

1222
if (typeof func !== 'function') {
1323
throw new this.Exception(func + ' is not a valid method');
1424
}
1525

16-
return func.apply(null, Array.prototype.slice.call(arguments, 2));
26+
return func.apply(object, Array.prototype.slice.call(arguments, paramBegin));
1727
}
Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,27 @@
11
function call_user_method_array(method, obj, params) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Brett Zamir (http://brett-zamir.me)
4+
// + input by: dnukem
5+
// + improved by: Brett Zamir (http://brett-zamir.me)
46
// - depends on: Exception
57
// % note 1: Deprecated in PHP
6-
// * example 1: call_user_method_array('alert', 'this.window', ['Hello!']);
8+
// * example 1: call_user_method_array('alert', this.window, ['Hello!']);
9+
// * returns 1: 'Hello!'
10+
// * example 1: call_user_method_array([this.window, 'alert'], ['Hello!']);
711
// * returns 1: 'Hello!'
812

9-
var func;
10-
func = eval(obj+"['"+method+"']");
13+
var func, object = obj, methodName = method;
14+
if (method && typeof method === 'object' && method.length) {
15+
params = obj;
16+
object = method[0];
17+
object = typeof object === 'string' ? this.window[object] : object;
18+
methodName = method[1];
19+
}
20+
func = object[methodName];
1121

1222
if (typeof func !== 'function') {
1323
throw new this.Exception(func + ' is not a valid method');
1424
}
1525

16-
return func.apply(null, params);
26+
return func.apply(object, params);
1727
}

_octopress/source/about/index.markdown

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ php.js is a resource that offers community-built JavaScript alternatives to PHP
1616
- to see if we can
1717
- to learn JavaScript
1818
- to help others learn JavaScript
19-
- to see how a php program performs with V8 strapped on it's back
20-
- to run & debug php code in the browser
19+
- to see how a php scripts perform with V8 strapped on their backs
2120
- to profit from helpful functions like:
2221
[strip_tags](http://phpjs.org/functions/strip_tags/),
2322
[strtotime](http://phpjs.org/functions/strtotime/),
23+
[md5](http://phpjs.org/functions/md5/),
2424
[strftime](http://phpjs.org/functions/strftime/),
2525
[number_format](http://phpjs.org/functions/number_format/),
2626
[wordwrap](http://phpjs.org/functions/wordwrap/),
@@ -39,7 +39,7 @@ gigantic php.js packages or compilers.
3939
That said, we do think it's a challenge to port everything and decided to also port
4040
low-level PHP functions like
4141
[strpos](http://phpjs.org/functions/strpos/)
42-
that have a perfectly fine (and more performant!) JavaScript
42+
that have perfectly fine (and more performant!) JavaScript
4343
counterparts ([String.indexOf](https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/indexOf) in this case).
4444

4545
Besides the geeky-challenge of seeing how far we could come, porting ALL THE THINGS also
@@ -55,8 +55,8 @@ to decide what makes sense to take from it.
5555
And what not.
5656

5757
Going by [The McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d)
58-
we have plently of functions here online that aren't perfect just yet.
59-
If you know how to improve, just send us a pull request on GitHub!
58+
we have plently of functions online that aren't perfect just yet.
59+
If you know how better ways, just send us a pull request on GitHub!
6060

6161
## Contributing
6262

functions/_phpjs_shared/_phpjs_shared_bc.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -931,9 +931,9 @@ function _phpjs_shared_bc () {
931931
borrow = 1;
932932
} else {
933933
borrow = 0;
934-
diff.n_value[diffptr--] = val;
935-
//*diffptr-- = val;
936934
}
935+
diff.n_value[diffptr--] = val;
936+
//*diffptr-- = val;
937937
}
938938
}
939939

@@ -1118,7 +1118,7 @@ function _phpjs_shared_bc () {
11181118
},
11191119

11201120
cint: function (v) {
1121-
if (typeof(v) == 'undefined') {
1121+
if (typeof v === 'undefined') {
11221122
v = 0;
11231123
}
11241124
var x = parseInt(v, 10);

functions/array/array_count_values.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function array_count_values (array) {
2727
};
2828

2929
var __countValue = function (value) {
30-
switch (typeof(value)) {
30+
switch (typeof value) {
3131
case "number":
3232
if (Math.floor(value) !== value) {
3333
return;

functions/array/array_map.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ function array_map (callback) {
22
// http://kevin.vanzonneveld.net
33
// + original by: Andrea Giammarchi (http://webreflection.blogspot.com)
44
// + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
5+
// + input by: thekid
56
// + improved by: Brett Zamir (http://brett-zamir.me)
6-
// % note 1: Takes a function as an argument, not a function's name
7-
// % note 2: If the callback is a string, it can only work if the function name is in the global context
7+
// % note 1: If the callback is a string (or object, if an array is supplied), it can only work if the function name is in the global context
88
// * example 1: array_map( function (a){return (a * a * a)}, [1, 2, 3, 4, 5] );
99
// * returns 1: [ 1, 8, 27, 64, 125 ]
1010
var argc = arguments.length,
11-
argv = arguments;
12-
var j = argv[1].length,
11+
argv = arguments,
12+
glbl = this.window,
13+
obj = null,
14+
cb = callback,
15+
j = argv[1].length,
1316
i = 0,
1417
k = 1,
15-
m = 0;
16-
var tmp = [],
18+
m = 0,
19+
tmp = [],
1720
tmp_ar = [];
1821

1922
while (i < j) {
@@ -26,9 +29,16 @@ function array_map (callback) {
2629

2730
if (callback) {
2831
if (typeof callback === 'string') {
29-
callback = this.window[callback];
32+
cb = glbl[callback];
33+
}
34+
else if (typeof callback === 'object' && callback.length) {
35+
obj = typeof callback[0] === 'string' ? glbl[callback[0]] : callback[0];
36+
if (typeof obj === 'undefined') {
37+
throw 'Object not found: ' + callback[0];
38+
}
39+
cb = typeof callback[1] === 'string' ? obj[callback[1]] : callback[1];
3040
}
31-
tmp_ar[i++] = callback.apply(null, tmp);
41+
tmp_ar[i++] = cb.apply(obj, tmp);
3242
} else {
3343
tmp_ar[i++] = tmp;
3444
}

functions/array/array_merge_recursive.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ function array_merge_recursive (arr1, arr2) {
1818
} else if ((arr1 && (arr1 instanceof Object)) && (arr2 && (arr2 instanceof Object))) {
1919
for (idx in arr2) {
2020
if (idx in arr1) {
21-
if (typeof arr1[idx] == 'object' && typeof arr2 == 'object') {
21+
if (typeof arr1[idx] === 'object' && typeof arr2 === 'object') {
2222
arr1[idx] = this.array_merge(arr1[idx], arr2[idx]);
2323
} else {
2424
arr1[idx] = arr2[idx];

functions/array/array_walk_recursive.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ function array_walk_recursive (array, funcname, userdata) {
1212
}
1313

1414
for (key in array) {
15-
if (typeof array[key] == 'object') {
15+
if (typeof array[key] === 'object') {
1616
return this.array_walk_recursive(array[key], funcname, userdata);
1717
}
1818

19-
if (typeof(userdata) != 'undefined') {
19+
if (typeof userdata !== 'undefined') {
2020
eval(funcname + '( array [key] , key , userdata )');
2121
} else {
2222
eval(funcname + '( userdata ) ');

functions/bc/bcadd.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ function bcadd (left_operand, right_operand, scale) {
1515

1616
var first, second, result;
1717

18-
if (typeof(scale) == 'undefined') {
18+
if (typeof scale === 'undefined') {
1919
scale = libbcmath.scale;
2020
}
2121
scale = ((scale < 0) ? 0 : scale);

functions/bc/bccomp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function bccomp (left_operand, right_operand, scale) {
1414
var libbcmath = this._phpjs_shared_bc();
1515

1616
var first, second; //bc_num
17-
if (typeof(scale) == 'undefined') {
17+
if (typeof scale === 'undefined') {
1818
scale = libbcmath.scale;
1919
}
2020
scale = ((scale < 0) ? 0 : scale);

functions/bc/bcdiv.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ function bcdiv (left_operand, right_operand, scale) {
2020

2121
var first, second, result;
2222

23-
if (typeof(scale) == 'undefined') {
23+
if (typeof scale === 'undefined') {
2424
scale = libbcmath.scale;
2525
}
2626
scale = ((scale < 0) ? 0 : scale);

functions/bc/bcmul.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function bcmul (left_operand, right_operand, scale) {
1616

1717
var first, second, result;
1818

19-
if (typeof(scale) == 'undefined') {
19+
if (typeof scale === 'undefined') {
2020
scale = libbcmath.scale;
2121
}
2222
scale = ((scale < 0) ? 0 : scale);

functions/bc/bcsub.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function bcsub (left_operand, right_operand, scale) {
1717

1818
var first, second, result;
1919

20-
if (typeof(scale) == 'undefined') {
20+
if (typeof scale === 'undefined') {
2121
scale = libbcmath.scale;
2222
}
2323
scale = ((scale < 0) ? 0 : scale);

functions/datetime/date.js

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,9 @@ function date (format, timestamp) {
77
// + improved by: Brad Touesnard
88
// + improved by: Tim Wiel
99
// + improved by: Bryan Elliott
10-
//
11-
// + improved by: Brett Zamir (http://brett-zamir.me)
1210
// + improved by: David Randall
1311
// + input by: Brett Zamir (http://brett-zamir.me)
1412
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
15-
// + improved by: Brett Zamir (http://brett-zamir.me)
16-
// + improved by: Brett Zamir (http://brett-zamir.me)
1713
// + improved by: Theriault
1814
// + derived from: gettimeofday
1915
// + input by: majak
@@ -31,6 +27,8 @@ function date (format, timestamp) {
3127
// + bugfixed by: omid (http://phpjs.org/functions/380:380#comment_137122)
3228
// + input by: Martin
3329
// + input by: Alex Wilson
30+
// + input by: Haravikk
31+
// + improved by: Theriault
3432
// + bugfixed by: Chris (http://www.devotis.nl/)
3533
// % note 1: Uses global: php_js to store the default timezone
3634
// % note 2: Although the function potentially allows timezone info (see notes), it currently does not set
@@ -59,19 +57,24 @@ function date (format, timestamp) {
5957
var that = this,
6058
jsdate,
6159
f,
62-
formatChr = /\\?([a-z])/gi,
63-
formatChrCb,
6460
// Keep this here (works, but for code commented-out
6561
// below for file size reasons)
6662
//, tal= [],
67-
_pad = function (n, c) {
68-
n = n.toString();
69-
return n.length < c ? _pad('0' + n, c, '0') : n;
63+
txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"],
64+
// trailing backslash -> (dropped)
65+
// a backslash followed by any character (including backslash) -> the character
66+
// empty string -> empty string
67+
formatChr = /\\?(.?)/gi,
68+
formatChrCb = function (t, s) {
69+
return f[t] ? f[t]() : s;
7070
},
71-
txt_words = ["Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
72-
formatChrCb = function (t, s) {
73-
return f[t] ? f[t]() : s;
74-
};
71+
_pad = function (n, c) {
72+
n = String(n);
73+
while (n.length < c) {
74+
n = '0' + n;
75+
}
76+
return n;
77+
};
7578
f = {
7679
// Day
7780
d: function () { // Day of month w/leading 0; 01..31
@@ -90,9 +93,11 @@ function date (format, timestamp) {
9093
return f.w() || 7;
9194
},
9295
S: function(){ // Ordinal suffix for day of month; st, nd, rd, th
93-
var j = f.j()
94-
i = j%10;
95-
if (i <= 3 && parseInt((j%100)/10) == 1) i = 0;
96+
var j = f.j(),
97+
i = j%10;
98+
if (i <= 3 && parseInt((j%100)/10, 10) == 1) {
99+
i = 0;
100+
}
96101
return ['st', 'nd', 'rd'][i - 1] || 'th';
97102
},
98103
w: function () { // Day of week; 0[Sun]..6[Sat]

functions/datetime/getdate.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function getdate (timestamp) {
77
// * returns 1: {'seconds': 40, 'minutes': 58, 'hours': 21, 'mday': 17, 'wday': 2, 'mon': 6, 'year': 2003, 'yday': 167, 'weekday': 'Tuesday', 'month': 'June', '0': 1055901520}
88
var _w = ['Sun', 'Mon', 'Tues', 'Wednes', 'Thurs', 'Fri', 'Satur'];
99
var _m = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
10-
var d = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
11-
(typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
10+
var d = ((typeof timestamp === 'undefined') ? new Date() : // Not provided
11+
(typeof timestamp === 'object') ? new Date(timestamp) : // Javascript Date()
1212
new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
1313
);
1414
var w = d.getDay();

functions/datetime/gmstrftime.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function gmstrftime (format, timestamp) {
66
// - depends on: strftime
77
// * example 1: gmstrftime("%A", 1062462400);
88
// * returns 1: 'Tuesday'
9-
var dt = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
10-
(typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
9+
var dt = ((typeof timestamp === 'undefined') ? new Date() : // Not provided
10+
(typeof timestamp === 'object') ? new Date(timestamp) : // Javascript Date()
1111
new Date(timestamp * 1000) // UNIX timestamp (auto-convert to int)
1212
);
1313
timestamp = Date.parse(dt.toUTCString().slice(0, -4)) / 1000;

functions/datetime/strftime.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,8 @@ OW
174174
Oy
175175
*/
176176

177-
var _date = ((typeof(timestamp) == 'undefined') ? new Date() : // Not provided
178-
(typeof(timestamp) == 'object') ? new Date(timestamp) : // Javascript Date()
177+
var _date = ((typeof timestamp === 'undefined') ? new Date() : // Not provided
178+
(typeof timestamp === 'object') ? new Date(timestamp) : // Javascript Date()
179179
new Date(timestamp * 1000) // PHP API expects UNIX timestamp (auto-convert to int)
180180
);
181181

@@ -209,7 +209,7 @@ Oy
209209
return _date[f]();
210210
} else if (typeof f === 'function') {
211211
return f(_date);
212-
} else if (typeof f === 'object' && typeof(f[0]) === 'string') {
212+
} else if (typeof f === 'object' && typeof f[0] === 'string') {
213213
return _xPad(_date[f[0]](), f[1]);
214214
} else { // Shouldn't reach here
215215
return m1;

0 commit comments

Comments
 (0)