Skip to content

Commit 4324f6e

Browse files
committed
Merge branch 'master' of github.com:kvz/phpjs
Conflicts: functions/datetime/strtotime.js
2 parents 00fd55b + 3a9f259 commit 4324f6e

34 files changed

+110
-80
lines changed

functions/array/shuffle.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ function shuffle(inputArr) {
1313
// % note 1: property deletion is supported. Note that we intend to implement the PHP
1414
// % note 1: behavior by default if IE ever does allow it; only gives shallow copy since
1515
// % note 1: is by reference in PHP anyways
16+
// * test: skip
1617
// * example 1: ini_set('phpjs.strictForIn', true);
1718
// * example 2: var data = {5:'a', 2:'3', 3:'c', 4:5, 'q':5};
1819
// * example 1: shuffle(data);

functions/ctype/ctype_cntrl.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ function ctype_cntrl(text) {
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// - depends on: setlocale
55
// * example 1: ctype_cntrl('\u0020');
6-
// * returns 1: true
6+
// * returns 1: false
7+
// * example 2: ctype_cntrl('\u001F');
8+
// * returns 2: true
79
if (typeof text !== 'string') {
810
return false;
911
}

functions/datetime/date_parse.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@ function date_parse(date) {
99
this.php_js = this.php_js || {};
1010
// END REDUNDANT
1111

12-
var warningsOffset = this.php_js.warnings ? this.php_js.warnings.length : null;
13-
var errorsOffset = this.php_js.errors ? this.php_js.errors.length : null;
12+
var ts,
13+
warningsOffset = this.php_js.warnings ? this.php_js.warnings.length : null,
14+
errorsOffset = this.php_js.errors ? this.php_js.errors.length : null;
1415

1516
try {
16-
var ts = this.strtotime(date);
17+
this.php_js.date_parse_state = true; // Allow strtotime to return a decimal (which it normally does not)
18+
ts = this.strtotime(date);
19+
this.php_js.date_parse_state = false;
1720
} finally {
1821
if (!ts) {
1922
return false;
@@ -35,7 +38,7 @@ function date_parse(date) {
3538
retObj.minute = dt.getMinutes();
3639
retObj.second = dt.getSeconds();
3740
retObj.fraction = parseFloat('0.' + dt.getMilliseconds());
38-
retObj.is_localtime = dt.getTimezoneOffset !== 0;
41+
retObj.is_localtime = dt.getTimezoneOffset() !== 0;
3942

4043
return retObj;
4144
}

functions/datetime/gettimeofday.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ function gettimeofday(return_float) {
66
// + revised by: Theriault
77
// * example 1: gettimeofday();
88
// * returns 1: {sec: 12, usec: 153000, minuteswest: -480, dsttime: 0}
9-
// * example 1: gettimeofday(true);
10-
// * returns 1: 1238748978.49
9+
// * example 2: gettimeofday(true);
10+
// * returns 2: 1238748978.49
1111
var t = new Date(),
1212
y = 0;
1313

functions/datetime/idate.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function idate(format, timestamp) {
66
// + improved by: Theriault
77
// + derived from: date
88
// + derived from: gettimeofday
9-
// * example 1: idate('y');
9+
// * example 1: idate('y', 1255633200);
1010
// * returns 1: 9
1111
if (format === undefined) {
1212
throw 'idate() expects at least 1 parameter, 0 given';

functions/datetime/strptime.js

Lines changed: 42 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ function strptime(dateStr, format) {
44
// + based on: strftime
55
// - depends on: setlocale
66
// - depends on: array_map
7-
// * test: skip
7+
// * test: skip
88
// * example 1: strptime('20091112222135', '%Y%m%d%H%M%S'); // Return value will depend on date and locale
99
// * returns 1: {tm_sec: 35, tm_min: 21, tm_hour: 22, tm_mday: 12, tm_mon: 10, tm_year: 109, tm_wday: 4, tm_yday: 315, unparsed: ''}
1010
// * example 1: strptime('2009extra', '%Y');
@@ -15,39 +15,41 @@ function strptime(dateStr, format) {
1515
// Needs more thorough testing and examples
1616

1717
var retObj = {
18-
tm_sec: 0,
19-
tm_min: 0,
20-
tm_hour: 0,
21-
tm_mday: 0,
22-
tm_mon: 0,
23-
tm_year: 0,
24-
tm_wday: 0,
25-
tm_yday: 0,
26-
unparsed: ''
27-
},
28-
that = this,
29-
amPmOffset = 0,
30-
prevHour = false,
31-
_date = function() {
32-
var o = retObj;
33-
// We set date to at least 1 to ensure year or month doesn't go backwards
34-
return _reset(new Date(Date.UTC(o.tm_year + 1900, o.tm_mon, o.tm_mday || 1, o.tm_hour, o.tm_min, o.tm_sec)), o.tm_mday);
35-
};
36-
_reset = function(dateObj, realMday) {
37-
// realMday is to allow for a value of 0 in return results (but without
38-
// messing up the Date() object)
39-
var o = retObj;
40-
var d = dateObj;
41-
o.tm_sec = d.getUTCSeconds();
42-
o.tm_min = d.getUTCMinutes();
43-
o.tm_hour = d.getUTCHours();
44-
o.tm_mday = realMday === 0 ? realMday : d.getUTCDate();
45-
o.tm_mon = d.getUTCMonth();
46-
o.tm_year = d.getUTCFullYear() - 1900;
47-
o.tm_wday = realMday === 0 ? (d.getUTCDay() > 0 ? d.getUTCDay() - 1 : 6) : d.getUTCDay();
48-
var jan1 = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
49-
o.tm_yday = Math.ceil((d - jan1) / (1000 * 60 * 60 * 24));
50-
};
18+
tm_sec: 0,
19+
tm_min: 0,
20+
tm_hour: 0,
21+
tm_mday: 0,
22+
tm_mon: 0,
23+
tm_year: 0,
24+
tm_wday: 0,
25+
tm_yday: 0,
26+
unparsed: ''
27+
},
28+
i = 0,
29+
that = this,
30+
amPmOffset = 0,
31+
prevHour = false,
32+
_reset = function(dateObj, realMday) {
33+
// realMday is to allow for a value of 0 in return results (but without
34+
// messing up the Date() object)
35+
var jan1,
36+
o = retObj,
37+
d = dateObj;
38+
o.tm_sec = d.getUTCSeconds();
39+
o.tm_min = d.getUTCMinutes();
40+
o.tm_hour = d.getUTCHours();
41+
o.tm_mday = realMday === 0 ? realMday : d.getUTCDate();
42+
o.tm_mon = d.getUTCMonth();
43+
o.tm_year = d.getUTCFullYear() - 1900;
44+
o.tm_wday = realMday === 0 ? (d.getUTCDay() > 0 ? d.getUTCDay() - 1 : 6) : d.getUTCDay();
45+
jan1 = new Date(Date.UTC(d.getUTCFullYear(), 0, 1));
46+
o.tm_yday = Math.ceil((d - jan1) / (1000 * 60 * 60 * 24));
47+
},
48+
_date = function() {
49+
var o = retObj;
50+
// We set date to at least 1 to ensure year or month doesn't go backwards
51+
return _reset(new Date(Date.UTC(o.tm_year + 1900, o.tm_mon, o.tm_mday || 1, o.tm_hour, o.tm_min, o.tm_sec)), o.tm_mday);
52+
};
5153

5254
// BEGIN STATIC
5355
var _NWS = /\S/,
@@ -132,12 +134,13 @@ Oy
132134
};
133135

134136
// BEGIN PROCESSING CHARACTERS
135-
for (var i = 0, j = 0; i < format.length; i++) {
137+
for (i = 0, j = 0; i < format.length; i++) {
136138
if (format.charAt(i) === '%') {
137139
var literalPos = ['%', 'n', 't'].indexOf(format.charAt(i + 1));
138140
if (literalPos !== -1) {
139141
if (['%', '\n', '\t'].indexOf(dateStr.charAt(j)) === literalPos) { // a matched literal
140-
++i, ++j; // skip beyond
142+
++i;
143+
++j; // skip beyond
141144
continue;
142145
}
143146
// Format indicated a percent literal, but not actually present
@@ -323,7 +326,6 @@ Oy
323326
break;
324327
default:
325328
throw 'Unrecognized formatting character in strptime()';
326-
break;
327329
}
328330
} catch (e) {
329331
if (e === 'No match in string') { // Allow us to exit
@@ -341,10 +343,10 @@ Oy
341343
} else if (format.charAt(i).search(_NWS) !== -1) { // Any extra formatting characters besides white-space causes
342344
// problems (do check after WS though, as may just be WS in string before next character)
343345
return false;
344-
} else { // Extra WS in format
345-
// Adjust strings when encounter non-matching whitespace, so they align in future checks above
346-
// Will check on next iteration (against same (non-WS) string character)
347346
}
347+
// Extra WS in format
348+
// Adjust strings when encounter non-matching whitespace, so they align in future checks above
349+
// Will check on next iteration (against same (non-WS) string character)
348350
} else {
349351
j++;
350352
}

functions/exec/escapeshellarg.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ function escapeshellarg(arg) {
33
// + original by: Felix Geisendoerfer (http://www.debuggable.com/felix)
44
// + improved by: Brett Zamir (http://brett-zamir.me)
55
// * example 1: escapeshellarg("kevin's birthday");
6-
// * returns 1: "'kevin\'s birthday'"
6+
// * returns 1: "'kevin\\'s birthday'"
77
var ret = '';
88

99
ret = arg.replace(/[^\\]'/g, function(m, i, s) {

functions/filesystem/basename.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,23 @@ function basename(path, suffix) {
44
// + improved by: Ash Searle (http://hexmen.com/blog/)
55
// + improved by: Lincoln Ramsay
66
// + improved by: djmix
7+
// + improved by: Dmitry Gorelenkov
78
// * example 1: basename('/www/site/home.htm', '.htm');
89
// * returns 1: 'home'
910
// * example 2: basename('ecra.php?p=1');
1011
// * returns 2: 'ecra.php?p=1'
11-
var b = path.replace(/^.*[\/\\]/g, '');
12+
// * example 3: basename('/some/path/');
13+
// * returns 3: 'path'
14+
// * example 4: basename('/some/path_ext.ext/','.ext');
15+
// * returns 4: 'path_ext'
16+
var b = path;
17+
var lastChar = b.charAt(b.length-1);
18+
19+
if(lastChar === "/" || lastChar === "\\") {
20+
b = b.slice(0, -1);
21+
}
22+
23+
b = b.replace(/^.*[\/\\]/g, '');
1224

1325
if (typeof suffix === 'string' && b.substr(b.length - suffix.length) == suffix) {
1426
b = b.substr(0, b.length - suffix.length);

functions/filesystem/pathinfo.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ function pathinfo(path, options) {
1313
// % note 2: But by way we implemented this function, if you want you can still declare the PATHINFO_*
1414
// % note 2: yourself, and then you can use: pathinfo('/www/index.html', PATHINFO_BASENAME | PATHINFO_EXTENSION);
1515
// % note 2: which makes it fully compliant with PHP syntax.
16-
// - depends on: dirname
1716
// - depends on: basename
18-
// * test: skip
1917
// * example 1: pathinfo('/www/htdocs/index.html', 1);
2018
// * returns 1: '/www/htdocs'
2119
// * example 2: pathinfo('/www/htdocs/index.html', 'PATHINFO_BASENAME');
@@ -83,8 +81,8 @@ function pathinfo(path, options) {
8381

8482
// Gather path infos
8583
if (options & OPTS.PATHINFO_DIRNAME) {
86-
var dirname = this.dirname(path);
87-
tmp_arr.dirname = dirname === path ? '.' : dirname;
84+
var dirName = path.replace(/\\/g, '/').replace(/\/[^\/]*\/?$/, ''); // dirname
85+
tmp_arr.dirname = dirName === path ? '.' : dirName;
8886
}
8987

9088
if (options & OPTS.PATHINFO_BASENAME) {

functions/i18n/i18n_loc_get_default.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ function i18n_loc_get_default() {
55
// % note 2: List of locales at http://demo.icu-project.org/icu-bin/locexp
66
// % note 3: To be usable with sort() if it is passed the SORT_LOCALE_STRING sorting flag: http://php.net/manual/en/function.sort.php
77
// - depends on: i18n_loc_set_default
8+
// * example 1: i18n_loc_set_default('pt_PT');
89
// * example 1: i18n_loc_get_default();
9-
// * returns 1: 'en_US_POSIX'
10+
// * returns 1: 'pt_PT'
1011

1112
// BEGIN REDUNDANT
1213
this.php_js = this.php_js || {};

functions/info/getlastmod.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function getlastmod() {
22
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// % note 1: Will not work on browsers which don't support document.lastModified
5+
// * test: skip
56
// * example 1: getlastmod();
67
// * returns 1: 1237610043
78
return new Date(this.window.document.lastModified).getTime() / 1000;

functions/info/ini_get.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function ini_get(varname) {
22
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// % note 1: The ini values must be set by ini_set or manually within an ini file
5+
// * example 1: ini_set('date.timezone', 'Asia/Hong_Kong');
56
// * example 1: ini_get('date.timezone');
67
// * returns 1: 'Asia/Hong_Kong'
78

functions/info/ini_set.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ function ini_set(varname, newvalue) {
22
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// % note 1: This will not set a global_value or access level for the ini item
5-
// * test: skip
5+
// * example 1: ini_set('date.timezone', 'Asia/Hong_Kong');
66
// * example 1: ini_set('date.timezone', 'America/Chicago');
77
// * returns 1: 'Asia/Hong_Kong'
88

functions/json/json_decode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ function json_decode(str_json) {
55
// + improved by: T.J. Leahy
66
// + improved by: Michael White
77
// * example 1: json_decode('[ 1 ]');
8-
// * returns 1: true
8+
// * returns 1: [1]
99

1010
/*
1111
http://www.JSON.org/json2.js

functions/json/json_encode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function json_encode(mixed_val) {
66
// + input by: felix
77
// + bugfixed by: Brett Zamir (http://brett-zamir.me)
88
// * example 1: json_encode('Kevin');
9-
// * returns 1: 'Kevin'
9+
// * returns 1: '"Kevin"'
1010

1111
/*
1212
http://www.JSON.org/json2.js

functions/math/cos.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ function cos(arg) {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
44
// * example 1: cos(8723321.4);
5-
// * returns 1: -0.18127180117607017
5+
// * returns 1: -0.18127180117605912
66
return Math.cos(arg);
77
}

functions/math/lcg_value.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
function lcg_value() {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
4+
// * test: skip
45
// * example 1: lcg_value()
56
// * returns 1: 1
67

functions/math/pow.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ function pow(base, exp) {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
44
// * example 1: pow(8723321.4, 7);
5-
// * returns 1: 3.843909168077899e+48
5+
// * returns 1: 3.8439091680778995e+48
66
return Math.pow(base, exp);
77
}

functions/math/sin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ function sin(arg) {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
44
// * example 1: sin(8723321.4);
5-
// * returns 1: -0.9834330348825909
5+
// * returns 1: -0.9834330348825929
66
return Math.sin(arg);
77
}

functions/math/tan.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@ function tan(arg) {
22
// From: http://phpjs.org/functions
33
// + original by: Onno Marsman
44
// * example 1: tan(8723321.4);
5-
// * returns 1: 5.4251848798444815
5+
// * returns 1: 5.4251848798448234
66
return Math.tan(arg);
77
}

functions/misc/uniqid.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ function uniqid(prefix, more_entropy) {
22
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
33
// + revised by: Kankrelune (http://www.webfaktory.info/)
44
// % note 1: Uses an internal counter (in php_js global) to avoid collision
5+
// * test: skip
56
// * example 1: uniqid();
67
// * returns 1: 'a30285b160c14'
78
// * example 2: uniqid('foo');

functions/pcre/preg_quote.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ function preg_quote(str, delimiter) {
66
// + bugfixed by: Onno Marsman
77
// + improved by: Brett Zamir (http://brett-zamir.me)
88
// * example 1: preg_quote("$40");
9-
// * returns 1: '\$40'
9+
// * returns 1: '\\$40'
1010
// * example 2: preg_quote("*RRRING* Hello?");
11-
// * returns 2: '\*RRRING\* Hello\?'
11+
// * returns 2: '\\*RRRING\\* Hello\\?'
1212
// * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:");
13-
// * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:'
13+
// * returns 3: '\\\\\\.\\+\\*\\?\\[\\^\\]\\$\\(\\)\\{\\}\\=\\!\\<\\>\\|\\:'
1414
return (str + '').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\' + (delimiter || '') + '-]', 'g'), '\\$&');
1515
}

functions/strings/addslashes.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ function addslashes(str) {
99
// + improved by: Brett Zamir (http://brett-zamir.me)
1010
// + improved by: Oskar Larsson Högfeldt (http://oskar-lh.name/)
1111
// * example 1: addslashes("kevin's birthday");
12-
// * returns 1: 'kevin\'s birthday'
12+
// * returns 1: "kevin\\'s birthday"
1313
return (str + '').replace(/[\\"']/g, '\\$&').replace(/\u0000/g, '\\0');
1414
}

functions/strings/chr.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ function chr(codePt) {
22
// From: http://phpjs.org/functions
33
// + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
44
// + improved by: Brett Zamir (http://brett-zamir.me)
5-
// * example 1: chr(75);
6-
// * returns 1: 'K'
5+
// * example 1: chr(75) === 'K';
6+
// * returns 1: true
77
// * example 1: chr(65536) === '\uD800\uDC00';
88
// * returns 1: true
99
if (codePt > 0xFFFF) { // Create a four-byte string (length 2) since this code point is high

functions/strings/count_chars.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ function count_chars(str, mode) {
77
// + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
88
// + revised by: Theriault
99
// * example 1: count_chars("Hello World!", 3);
10-
// * returns 1: "!HWdelor"
10+
// * returns 1: " !HWdelor"
1111
// * example 2: count_chars("Hello World!", 1);
1212
// * returns 2: {32:1,33:1,72:1,87:1,100:1,101:1,108:3,111:2,114:1}
1313
var result = {},

functions/strings/localeconv.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ function localeconv() {
22
// From: http://phpjs.org/functions
33
// + original by: Brett Zamir (http://brett-zamir.me)
44
// - depends on: setlocale
5+
// * example 1: setlocale('LC_ALL', 'en_US');
56
// * example 1: localeconv();
6-
// * returns 1: {decimal_point: '.', thousands_sep: ',', positive_sign: '', negative_sign: '-', int_frac_digits: 2, frac_digits: 2, p_cs_precedes: 1, p_sep_by_space: 0, n_cs_precedes: 1, n_sep_by_space: 0, p_sign_posn: 3, n_sign_posn: 0, grouping: 3, int_curr_symbol: 'USD', currency_symbol: '$', mon_decimal_point: '.', mon_thousands_sep: ',', mon_grouping: 3}
7+
// * returns 1: {decimal_point: '.', thousands_sep: '', positive_sign: '', negative_sign: '-', int_frac_digits: 2, frac_digits: 2, p_cs_precedes: 1, p_sep_by_space: 0, n_cs_precedes: 1, n_sep_by_space: 0, p_sign_posn: 1, n_sign_posn: 1, grouping: [], int_curr_symbol: 'USD ', currency_symbol: '$', mon_decimal_point: '.', mon_thousands_sep: ',', mon_grouping: [3, 3]}
78
var arr = {},
89
prop = '';
910

0 commit comments

Comments
 (0)